sanders: KeyHandler: Cleanup code
This commit is contained in:
@@ -19,20 +19,18 @@ package org.lineageos.settings.device;
|
||||
|
||||
import android.app.ActivityManager;
|
||||
import android.app.ActivityManagerNative;
|
||||
import android.app.ISearchManager;
|
||||
import android.app.KeyguardManager;
|
||||
import android.app.NotificationManager;
|
||||
import android.app.ISearchManager;
|
||||
import android.widget.Toast;
|
||||
import android.content.ActivityNotFoundException;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.database.ContentObserver;
|
||||
import android.content.res.Resources;
|
||||
import android.content.pm.ActivityInfo;
|
||||
import android.content.ComponentName;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.content.pm.ResolveInfo;
|
||||
import android.provider.MediaStore;
|
||||
import android.content.res.Resources;
|
||||
import android.database.ContentObserver;
|
||||
import android.hardware.Sensor;
|
||||
import android.hardware.SensorEvent;
|
||||
import android.hardware.SensorEventListener;
|
||||
@@ -43,8 +41,8 @@ import android.hardware.camera2.CameraManager;
|
||||
import android.hardware.input.InputManager;
|
||||
import android.media.AudioAttributes;
|
||||
import android.media.session.MediaSessionLegacyHelper;
|
||||
import android.os.Bundle;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.os.Message;
|
||||
@@ -54,26 +52,24 @@ import android.os.RemoteException;
|
||||
import android.os.ServiceManager;
|
||||
import android.os.SystemClock;
|
||||
import android.os.UserHandle;
|
||||
import android.os.Vibrator;
|
||||
import android.os.VibrationEffect;
|
||||
import android.util.Log;
|
||||
import android.os.Vibrator;
|
||||
import android.provider.MediaStore;
|
||||
import android.provider.Settings;
|
||||
import android.util.Log;
|
||||
import android.view.InputDevice;
|
||||
import android.view.KeyCharacterMap;
|
||||
import android.view.KeyEvent;
|
||||
import android.view.ViewConfiguration;
|
||||
import android.view.IWindowManager;
|
||||
import android.view.WindowManagerGlobal;
|
||||
import android.text.TextUtils;
|
||||
import android.Manifest;
|
||||
|
||||
import java.util.List;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.android.internal.os.DeviceKeyHandler;
|
||||
import com.android.internal.util.ArrayUtils;
|
||||
|
||||
import org.lineageos.settings.device.util.FileUtils;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.lineageos.settings.device.actions.Constants.*;
|
||||
|
||||
public class KeyHandler implements DeviceKeyHandler {
|
||||
@@ -88,10 +84,14 @@ public class KeyHandler implements DeviceKeyHandler {
|
||||
|
||||
private static final String GESTURE_WAKEUP_REASON = "keyhandler-gesture-wakeup";
|
||||
private static final int GESTURE_WAKELOCK_DURATION = 3000;
|
||||
|
||||
private static final AudioAttributes VIBRATION_ATTRIBUTES = new AudioAttributes.Builder()
|
||||
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
|
||||
.setUsage(AudioAttributes.USAGE_ASSISTANCE_SONIFICATION)
|
||||
.build();
|
||||
private final Context mContext;
|
||||
private final PowerManager mPowerManager;
|
||||
private final NotificationManager mNotificationManager;
|
||||
public boolean mIsHapticFeedbackEnabledOnSystem;
|
||||
WakeLock mProximityWakeLock;
|
||||
WakeLock mGestureWakeLock;
|
||||
private KeyguardManager mKeyguardManager;
|
||||
@@ -105,49 +105,37 @@ public class KeyHandler implements DeviceKeyHandler {
|
||||
private Vibrator mVibrator;
|
||||
private int mProximityTimeOut;
|
||||
private boolean mProximityWakeSupported;
|
||||
|
||||
private ISearchManager mSearchManagerService;
|
||||
|
||||
private Handler mHandler;
|
||||
private int fpTapCounts = 0;
|
||||
private boolean fpTapPending = false;
|
||||
private boolean screenOffGesturePending = false;
|
||||
|
||||
public boolean mIsHapticFeedbackEnabledOnSystem;
|
||||
|
||||
private SettingsObserver mSettingsObserver;
|
||||
|
||||
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 Runnable doubleTapRunnable = new Runnable() {
|
||||
public void run() {
|
||||
int action = 0;
|
||||
if (fpTapCounts > 1) {
|
||||
action = str2int(FileUtils.readOneLine(getFPNodeBasedOnScreenState(FP_KEY_DBLTAP_NODE)));
|
||||
} else {
|
||||
if (isSingleTapEnabledOnFP()) {
|
||||
action = str2int(FileUtils.readOneLine(getFPNodeBasedOnScreenState(FP_KEYS_NODE)));
|
||||
}
|
||||
}
|
||||
|
||||
private void setHapticFeedbackEnabledOnSystem(boolean enabled){
|
||||
Settings.System.putIntForUser(mContext.getContentResolver(),
|
||||
Settings.System.HAPTIC_FEEDBACK_ENABLED, enabled ? 1 : 0, UserHandle.USER_CURRENT);
|
||||
if (action != 0) {
|
||||
boolean isActionSupported = ArrayUtils.contains(mPowerManager.isScreenOn() ? sFPSupportedActions : sFPSupportedActionsScreenOff, action);
|
||||
if (isActionSupported) {
|
||||
fireFPAction(action, false);
|
||||
}
|
||||
|
||||
private static final AudioAttributes VIBRATION_ATTRIBUTES = new AudioAttributes.Builder()
|
||||
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
|
||||
.setUsage(AudioAttributes.USAGE_ASSISTANCE_SONIFICATION)
|
||||
.build();
|
||||
}
|
||||
resetDoubleTapOnFP();
|
||||
}
|
||||
};
|
||||
private Runnable screenOffGestureRunnable = new Runnable() {
|
||||
public void run() {
|
||||
resetScreenOffGestureDelay();
|
||||
}
|
||||
};
|
||||
|
||||
public KeyHandler(Context context) {
|
||||
mContext = context;
|
||||
@@ -203,6 +191,37 @@ public class KeyHandler implements DeviceKeyHandler {
|
||||
return out;
|
||||
}
|
||||
|
||||
private static ActivityInfo getRunningActivityInfo(Context context) {
|
||||
final ActivityManager am = (ActivityManager) context
|
||||
.getSystemService(Context.ACTIVITY_SERVICE);
|
||||
final PackageManager pm = context.getPackageManager();
|
||||
|
||||
List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1);
|
||||
if (tasks != null && !tasks.isEmpty()) {
|
||||
ActivityManager.RunningTaskInfo top = tasks.get(0);
|
||||
try {
|
||||
return pm.getActivityInfo(top.topActivity, 0);
|
||||
} catch (PackageManager.NameNotFoundException e) {
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static void dispatchMediaKeyWithWakeLock(int keycode, Context context) {
|
||||
if (ActivityManagerNative.isSystemReady()) {
|
||||
KeyEvent event = new KeyEvent(SystemClock.uptimeMillis(),
|
||||
SystemClock.uptimeMillis(), KeyEvent.ACTION_DOWN, keycode, 0);
|
||||
MediaSessionLegacyHelper.getHelper(context).sendMediaButtonEvent(event, true);
|
||||
event = KeyEvent.changeAction(event, KeyEvent.ACTION_UP);
|
||||
MediaSessionLegacyHelper.getHelper(context).sendMediaButtonEvent(event, true);
|
||||
}
|
||||
}
|
||||
|
||||
private void setHapticFeedbackEnabledOnSystem(boolean enabled) {
|
||||
Settings.System.putIntForUser(mContext.getContentResolver(),
|
||||
Settings.System.HAPTIC_FEEDBACK_ENABLED, enabled ? 1 : 0, UserHandle.USER_CURRENT);
|
||||
}
|
||||
|
||||
private String getRearCameraId() {
|
||||
if (mRearCameraId == null) {
|
||||
try {
|
||||
@@ -362,27 +381,6 @@ public class KeyHandler implements DeviceKeyHandler {
|
||||
mHandler.removeCallbacks(doubleTapRunnable);
|
||||
}
|
||||
|
||||
private Runnable doubleTapRunnable = new Runnable() {
|
||||
public void run() {
|
||||
int action = 0;
|
||||
if (fpTapCounts > 1){
|
||||
action = str2int(FileUtils.readOneLine(getFPNodeBasedOnScreenState(FP_KEY_DBLTAP_NODE)));
|
||||
}else{
|
||||
if (isSingleTapEnabledOnFP()){
|
||||
action = str2int(FileUtils.readOneLine(getFPNodeBasedOnScreenState(FP_KEYS_NODE)));
|
||||
}
|
||||
}
|
||||
|
||||
if (action != 0){
|
||||
boolean isActionSupported = ArrayUtils.contains(mPowerManager.isScreenOn() ? sFPSupportedActions : sFPSupportedActionsScreenOff, action);
|
||||
if (isActionSupported){
|
||||
fireFPAction(action, false);
|
||||
}
|
||||
}
|
||||
resetDoubleTapOnFP();
|
||||
}
|
||||
};
|
||||
|
||||
private void detectDoubleTapOnFP() {
|
||||
fpTapCounts++;
|
||||
if (fpTapCounts == 1 || fpTapCounts == 2) {
|
||||
@@ -597,22 +595,6 @@ public class KeyHandler implements DeviceKeyHandler {
|
||||
triggerVirtualKeypress(mHandler, 171);
|
||||
}
|
||||
|
||||
private static ActivityInfo getRunningActivityInfo(Context context) {
|
||||
final ActivityManager am = (ActivityManager) context
|
||||
.getSystemService(Context.ACTIVITY_SERVICE);
|
||||
final PackageManager pm = context.getPackageManager();
|
||||
|
||||
List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1);
|
||||
if (tasks != null && !tasks.isEmpty()) {
|
||||
ActivityManager.RunningTaskInfo top = tasks.get(0);
|
||||
try {
|
||||
return pm.getActivityInfo(top.topActivity, 0);
|
||||
} catch (PackageManager.NameNotFoundException e) {
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private void toggleScreenState() {
|
||||
if (mPowerManager.isScreenOn()) {
|
||||
mPowerManager.goToSleep(SystemClock.uptimeMillis());
|
||||
@@ -621,16 +603,6 @@ public class KeyHandler implements DeviceKeyHandler {
|
||||
}
|
||||
}
|
||||
|
||||
private static void dispatchMediaKeyWithWakeLock(int keycode, Context context) {
|
||||
if (ActivityManagerNative.isSystemReady()) {
|
||||
KeyEvent event = new KeyEvent(SystemClock.uptimeMillis(),
|
||||
SystemClock.uptimeMillis(), KeyEvent.ACTION_DOWN, keycode, 0);
|
||||
MediaSessionLegacyHelper.getHelper(context).sendMediaButtonEvent(event, true);
|
||||
event = KeyEvent.changeAction(event, KeyEvent.ACTION_UP);
|
||||
MediaSessionLegacyHelper.getHelper(context).sendMediaButtonEvent(event, true);
|
||||
}
|
||||
}
|
||||
|
||||
private void triggerVirtualKeypress(final Handler handler, final int keyCode) {
|
||||
final boolean mIsHapticFeedbackEnabledOnSystem_ = mIsHapticFeedbackEnabledOnSystem;
|
||||
if (mIsHapticFeedbackEnabledOnSystem_) {
|
||||
@@ -650,7 +622,8 @@ public class KeyHandler implements DeviceKeyHandler {
|
||||
@Override
|
||||
public void run() {
|
||||
im.injectInputEvent(downEvent, InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
|
||||
}}, 10);
|
||||
}
|
||||
}, 10);
|
||||
|
||||
handler.postDelayed(new Runnable() {
|
||||
@Override
|
||||
@@ -659,16 +632,8 @@ public class KeyHandler implements DeviceKeyHandler {
|
||||
if (mIsHapticFeedbackEnabledOnSystem_) {
|
||||
setHapticFeedbackEnabledOnSystem(true);
|
||||
}
|
||||
}}, 20);
|
||||
}
|
||||
|
||||
private static void sendCloseSystemWindows(String reason) {
|
||||
if (ActivityManagerNative.isSystemReady()) {
|
||||
try {
|
||||
ActivityManagerNative.getDefault().closeSystemDialogs(reason);
|
||||
} catch (RemoteException e) {
|
||||
}
|
||||
}
|
||||
}, 20);
|
||||
}
|
||||
|
||||
private void fireGoogleNowOnTap() {
|
||||
@@ -744,12 +709,6 @@ public class KeyHandler implements DeviceKeyHandler {
|
||||
mHandler.removeCallbacks(screenOffGestureRunnable);
|
||||
}
|
||||
|
||||
private Runnable screenOffGestureRunnable = new Runnable() {
|
||||
public void run() {
|
||||
resetScreenOffGestureDelay();
|
||||
}
|
||||
};
|
||||
|
||||
private void processScreenOffScancode(int scanCode) {
|
||||
if (screenOffGesturePending) {
|
||||
return;
|
||||
@@ -802,38 +761,6 @@ public class KeyHandler implements DeviceKeyHandler {
|
||||
}, mProximitySensor, SensorManager.SENSOR_DELAY_FASTEST);
|
||||
}
|
||||
|
||||
private class ScreenOffGesturesHandler extends Handler {
|
||||
@Override
|
||||
public void handleMessage(Message msg) {
|
||||
int scanCode = msg.arg1;
|
||||
int action = 0;
|
||||
switch (scanCode) {
|
||||
case GESTURE_SWIPE_RIGHT_SCANCODE:
|
||||
action = str2int(FileUtils.readOneLine(GESTURE_SWIPE_RIGHT_NODE));
|
||||
break;
|
||||
case GESTURE_SWIPE_LEFT_SCANCODE:
|
||||
action = str2int(FileUtils.readOneLine(GESTURE_SWIPE_LEFT_NODE));
|
||||
break;
|
||||
case GESTURE_SWIPE_DOWN_SCANCODE:
|
||||
action = str2int(FileUtils.readOneLine(GESTURE_SWIPE_DOWN_NODE));
|
||||
break;
|
||||
case GESTURE_SWIPE_UP_SCANCODE:
|
||||
action = str2int(FileUtils.readOneLine(GESTURE_SWIPE_UP_NODE));
|
||||
break;
|
||||
case GESTURE_DOUBLE_TAP_SCANCODE:
|
||||
action = str2int(FileUtils.readOneLine(GESTURE_DOUBLE_TAP_NODE));
|
||||
if (action != 0){
|
||||
action = ACTION_POWER;
|
||||
}
|
||||
break;
|
||||
}
|
||||
boolean isActionSupported = ArrayUtils.contains(sScreenOffSupportedActions, action);
|
||||
if(isActionSupported){
|
||||
fireScreenOffAction(action);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void fireScreenOffAction(int action) {
|
||||
boolean haptic = Settings.System.getInt(mContext.getContentResolver(), KEY_GESTURE_ENABLE_HAPTIC_FEEDBACK, 1) != 0;
|
||||
if (haptic && (action == ACTION_CAMERA || action == ACTION_FLASHLIGHT)) {
|
||||
@@ -876,13 +803,6 @@ public class KeyHandler implements DeviceKeyHandler {
|
||||
}
|
||||
}
|
||||
|
||||
private class FPScreenOffGesturesHandler extends Handler {
|
||||
@Override
|
||||
public void handleMessage(Message msg) {
|
||||
processFPScancode(msg.arg1);
|
||||
}
|
||||
}
|
||||
|
||||
private void startActivitySafely(Intent intent) {
|
||||
intent.addFlags(
|
||||
Intent.FLAG_ACTIVITY_NEW_TASK
|
||||
@@ -937,6 +857,67 @@ public class KeyHandler implements DeviceKeyHandler {
|
||||
}
|
||||
}
|
||||
|
||||
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 ScreenOffGesturesHandler extends Handler {
|
||||
@Override
|
||||
public void handleMessage(Message msg) {
|
||||
int scanCode = msg.arg1;
|
||||
int action = 0;
|
||||
switch (scanCode) {
|
||||
case GESTURE_SWIPE_RIGHT_SCANCODE:
|
||||
action = str2int(FileUtils.readOneLine(GESTURE_SWIPE_RIGHT_NODE));
|
||||
break;
|
||||
case GESTURE_SWIPE_LEFT_SCANCODE:
|
||||
action = str2int(FileUtils.readOneLine(GESTURE_SWIPE_LEFT_NODE));
|
||||
break;
|
||||
case GESTURE_SWIPE_DOWN_SCANCODE:
|
||||
action = str2int(FileUtils.readOneLine(GESTURE_SWIPE_DOWN_NODE));
|
||||
break;
|
||||
case GESTURE_SWIPE_UP_SCANCODE:
|
||||
action = str2int(FileUtils.readOneLine(GESTURE_SWIPE_UP_NODE));
|
||||
break;
|
||||
case GESTURE_DOUBLE_TAP_SCANCODE:
|
||||
action = str2int(FileUtils.readOneLine(GESTURE_DOUBLE_TAP_NODE));
|
||||
if (action != 0) {
|
||||
action = ACTION_POWER;
|
||||
}
|
||||
break;
|
||||
}
|
||||
boolean isActionSupported = ArrayUtils.contains(sScreenOffSupportedActions, action);
|
||||
if (isActionSupported) {
|
||||
fireScreenOffAction(action);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private class FPScreenOffGesturesHandler extends Handler {
|
||||
@Override
|
||||
public void handleMessage(Message msg) {
|
||||
processFPScancode(msg.arg1);
|
||||
}
|
||||
}
|
||||
|
||||
private class MyTorchCallback extends CameraManager.TorchCallback {
|
||||
@Override
|
||||
public void onTorchModeChanged(String cameraId, boolean enabled) {
|
||||
|
||||
Reference in New Issue
Block a user