sanders: KeyHandler: Vibration improvements
This commit is contained in:
@@ -30,7 +30,6 @@ import android.content.pm.ActivityInfo;
|
|||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
import android.content.pm.ResolveInfo;
|
import android.content.pm.ResolveInfo;
|
||||||
import android.content.res.Resources;
|
import android.content.res.Resources;
|
||||||
import android.database.ContentObserver;
|
|
||||||
import android.hardware.Sensor;
|
import android.hardware.Sensor;
|
||||||
import android.hardware.SensorEvent;
|
import android.hardware.SensorEvent;
|
||||||
import android.hardware.SensorEventListener;
|
import android.hardware.SensorEventListener;
|
||||||
@@ -88,10 +87,10 @@ public class KeyHandler implements DeviceKeyHandler {
|
|||||||
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
|
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
|
||||||
.setUsage(AudioAttributes.USAGE_ASSISTANCE_SONIFICATION)
|
.setUsage(AudioAttributes.USAGE_ASSISTANCE_SONIFICATION)
|
||||||
.build();
|
.build();
|
||||||
|
private static final long[] LONG_PRESS_VIBRATION_PATTERN = new long[] {0, 25, 10};
|
||||||
private final Context mContext;
|
private final Context mContext;
|
||||||
private final PowerManager mPowerManager;
|
private final PowerManager mPowerManager;
|
||||||
private final NotificationManager mNotificationManager;
|
private final NotificationManager mNotificationManager;
|
||||||
public boolean mIsHapticFeedbackEnabledOnSystem;
|
|
||||||
WakeLock mProximityWakeLock;
|
WakeLock mProximityWakeLock;
|
||||||
WakeLock mGestureWakeLock;
|
WakeLock mGestureWakeLock;
|
||||||
private KeyguardManager mKeyguardManager;
|
private KeyguardManager mKeyguardManager;
|
||||||
@@ -109,7 +108,6 @@ public class KeyHandler implements DeviceKeyHandler {
|
|||||||
private int fpTapCounts = 0;
|
private int fpTapCounts = 0;
|
||||||
private boolean fpTapPending = false;
|
private boolean fpTapPending = false;
|
||||||
private boolean fpGesturePending = false;
|
private boolean fpGesturePending = false;
|
||||||
private SettingsObserver mSettingsObserver;
|
|
||||||
private Runnable doubleTapRunnable = new Runnable() {
|
private Runnable doubleTapRunnable = new Runnable() {
|
||||||
public void run() {
|
public void run() {
|
||||||
int action = 0;
|
int action = 0;
|
||||||
@@ -124,7 +122,7 @@ public class KeyHandler implements DeviceKeyHandler {
|
|||||||
if (action != 0) {
|
if (action != 0) {
|
||||||
boolean isActionSupported = ArrayUtils.contains(mPowerManager.isScreenOn() ? sFPSupportedActions : sFPSupportedActionsScreenOff, action);
|
boolean isActionSupported = ArrayUtils.contains(mPowerManager.isScreenOn() ? sFPSupportedActions : sFPSupportedActionsScreenOff, action);
|
||||||
if (isActionSupported) {
|
if (isActionSupported) {
|
||||||
fireFPAction(action, false);
|
fireFPAction(action, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
resetDoubleTapOnFP();
|
resetDoubleTapOnFP();
|
||||||
@@ -139,9 +137,6 @@ public class KeyHandler implements DeviceKeyHandler {
|
|||||||
public KeyHandler(Context context) {
|
public KeyHandler(Context context) {
|
||||||
mContext = context;
|
mContext = context;
|
||||||
|
|
||||||
mSettingsObserver = new SettingsObserver(new Handler());
|
|
||||||
mSettingsObserver.observe();
|
|
||||||
|
|
||||||
mPowerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
|
mPowerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
|
||||||
mNotificationManager
|
mNotificationManager
|
||||||
= (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
|
= (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
|
||||||
@@ -248,11 +243,6 @@ public class KeyHandler implements DeviceKeyHandler {
|
|||||||
return launcherInfo.activityInfo.packageName;
|
return launcherInfo.activityInfo.packageName;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setHapticFeedbackEnabledOnSystem(boolean enabled) {
|
|
||||||
Settings.System.putIntForUser(mContext.getContentResolver(),
|
|
||||||
Settings.System.HAPTIC_FEEDBACK_ENABLED, enabled ? 1 : 0, UserHandle.USER_CURRENT);
|
|
||||||
}
|
|
||||||
|
|
||||||
private String getRearCameraId() {
|
private String getRearCameraId() {
|
||||||
if (mRearCameraId == null) {
|
if (mRearCameraId == null) {
|
||||||
try {
|
try {
|
||||||
@@ -536,14 +526,14 @@ public class KeyHandler implements DeviceKeyHandler {
|
|||||||
}
|
}
|
||||||
boolean isActionSupported = ArrayUtils.contains(isScreenOn ? sFPSupportedActions : sFPSupportedActionsScreenOff, action);
|
boolean isActionSupported = ArrayUtils.contains(isScreenOn ? sFPSupportedActions : sFPSupportedActionsScreenOff, action);
|
||||||
if (isActionSupported) {
|
if (isActionSupported) {
|
||||||
fireFPAction(action, true);
|
fireFPAction(action, false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void fireFPAction(int action, boolean haptic) {
|
private void fireFPAction(int action, boolean isDoubleTap) {
|
||||||
ensureKeyguardManager();
|
ensureKeyguardManager();
|
||||||
boolean isHapticFeedbackEnabledOnFP = isHapticFeedbackEnabledOnFP();
|
boolean isHapticFeedbackEnabledOnFP = isHapticFeedbackEnabledOnFP();
|
||||||
if (!haptic) {
|
if (isDoubleTap && action != ACTION_CAMERA && action != ACTION_FLASHLIGHT) {
|
||||||
isHapticFeedbackEnabledOnFP = false;
|
isHapticFeedbackEnabledOnFP = false;
|
||||||
}
|
}
|
||||||
if (isHapticFeedbackEnabledOnFP && (action == ACTION_CAMERA || action == ACTION_FLASHLIGHT)) {
|
if (isHapticFeedbackEnabledOnFP && (action == ACTION_CAMERA || action == ACTION_FLASHLIGHT)) {
|
||||||
@@ -650,16 +640,12 @@ public class KeyHandler implements DeviceKeyHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void triggerVirtualKeypress(final Handler handler, final int keyCode) {
|
private void triggerVirtualKeypress(final Handler handler, final int keyCode) {
|
||||||
final boolean mIsHapticFeedbackEnabledOnSystem_ = mIsHapticFeedbackEnabledOnSystem;
|
|
||||||
if (mIsHapticFeedbackEnabledOnSystem_) {
|
|
||||||
setHapticFeedbackEnabledOnSystem(false);
|
|
||||||
}
|
|
||||||
final InputManager im = InputManager.getInstance();
|
final InputManager im = InputManager.getInstance();
|
||||||
long now = SystemClock.uptimeMillis();
|
long now = SystemClock.uptimeMillis();
|
||||||
|
|
||||||
final KeyEvent downEvent = new KeyEvent(now, now, KeyEvent.ACTION_DOWN,
|
final KeyEvent downEvent = new KeyEvent(now, now, KeyEvent.ACTION_DOWN,
|
||||||
keyCode, 0, 0, KeyCharacterMap.VIRTUAL_KEYBOARD, 0,
|
keyCode, 0, 0, KeyCharacterMap.VIRTUAL_KEYBOARD, 0,
|
||||||
KeyEvent.FLAG_FROM_SYSTEM | KeyEvent.FLAG_VIRTUAL_HARD_KEY, InputDevice.SOURCE_CLASS_BUTTON);
|
KeyEvent.FLAG_FROM_SYSTEM, InputDevice.SOURCE_CLASS_BUTTON);
|
||||||
final KeyEvent upEvent = KeyEvent.changeAction(downEvent,
|
final KeyEvent upEvent = KeyEvent.changeAction(downEvent,
|
||||||
KeyEvent.ACTION_UP);
|
KeyEvent.ACTION_UP);
|
||||||
|
|
||||||
@@ -675,9 +661,6 @@ public class KeyHandler implements DeviceKeyHandler {
|
|||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
im.injectInputEvent(upEvent, InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
|
im.injectInputEvent(upEvent, InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
|
||||||
if (mIsHapticFeedbackEnabledOnSystem_) {
|
|
||||||
setHapticFeedbackEnabledOnSystem(true);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}, 20);
|
}, 20);
|
||||||
}
|
}
|
||||||
@@ -829,53 +812,15 @@ public class KeyHandler implements DeviceKeyHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (isHapticFeedbackEnabledOnFP()) {
|
if (isHapticFeedbackEnabledOnFP()) {
|
||||||
long[] pattern = null;
|
|
||||||
int owningUid;
|
int owningUid;
|
||||||
String owningPackage;
|
String owningPackage;
|
||||||
owningUid = android.os.Process.myUid();
|
owningUid = android.os.Process.myUid();
|
||||||
owningPackage = mContext.getOpPackageName();
|
owningPackage = mContext.getOpPackageName();
|
||||||
VibrationEffect effect;
|
VibrationEffect effect = longpress ? VibrationEffect.createWaveform(LONG_PRESS_VIBRATION_PATTERN, -1) : VibrationEffect.get(VibrationEffect.EFFECT_CLICK);
|
||||||
if (longpress) {
|
|
||||||
pattern = getLongIntArray(mContext.getResources(), com.android.internal.R.array.config_longPressVibePattern);
|
|
||||||
} else {
|
|
||||||
pattern = getLongIntArray(mContext.getResources(), com.android.internal.R.array.config_virtualKeyVibePattern);
|
|
||||||
}
|
|
||||||
if (pattern.length == 0) {
|
|
||||||
// No vibration
|
|
||||||
return;
|
|
||||||
} else if (pattern.length == 1) {
|
|
||||||
// One-shot vibration
|
|
||||||
effect = VibrationEffect.createOneShot(pattern[0], VibrationEffect.DEFAULT_AMPLITUDE);
|
|
||||||
} else {
|
|
||||||
// Pattern vibration
|
|
||||||
effect = VibrationEffect.createWaveform(pattern, -1);
|
|
||||||
}
|
|
||||||
mVibrator.vibrate(owningUid, owningPackage, effect, VIBRATION_ATTRIBUTES);
|
mVibrator.vibrate(owningUid, owningPackage, effect, VIBRATION_ATTRIBUTES);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private class SettingsObserver extends ContentObserver {
|
|
||||||
SettingsObserver(Handler handler) {
|
|
||||||
super(handler);
|
|
||||||
}
|
|
||||||
|
|
||||||
void observe() {
|
|
||||||
mContext.getContentResolver().registerContentObserver(Settings.System.getUriFor(
|
|
||||||
Settings.System.HAPTIC_FEEDBACK_ENABLED), false, this, UserHandle.USER_ALL);
|
|
||||||
update();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onChange(boolean selfChange) {
|
|
||||||
update();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void update() {
|
|
||||||
mIsHapticFeedbackEnabledOnSystem = Settings.System.getIntForUser(mContext.getContentResolver(),
|
|
||||||
Settings.System.HAPTIC_FEEDBACK_ENABLED, 0, UserHandle.USER_CURRENT) == 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private class FPScreenOffGesturesHandler extends Handler {
|
private class FPScreenOffGesturesHandler extends Handler {
|
||||||
@Override
|
@Override
|
||||||
public void handleMessage(Message msg) {
|
public void handleMessage(Message msg) {
|
||||||
|
|||||||
Reference in New Issue
Block a user