Permalink
Browse files

Longpress power lockscreen torch [1/2]

Long pressing power while screen is off will launch torch

Credits:
Lion0738: the main hooks here:https://github.com/lion0738/android_frameworks_base/commit/9af2b7844a4d973c8b6c542d7937f56a24a7e5f1
Atlantis: the logic on where to hook into this for power button only
AlexCruz: for helping and giving me some pointers along the way
Change-Id: I1f9a249def4b32d133247c308e8b7e76e0f23e17
  • Loading branch information...
beanstown106 authored and xlxfoxxlx committed Jan 17, 2016
1 parent 835a89b commit 50d8a2a41307894ca213957309e024be0dc1b08c
@@ -3754,6 +3754,12 @@ public boolean validate(String value) {
public static final String VOLBTN_MUSIC_CONTROLS = "volbtn_music_controls";
/**
* whether to enable torch on lockscreen
* @hide
*/
public static final String KEYGUARD_TOGGLE_TORCH = "keyguard_toggle_torch";
/**
* Whether to show media art on lockscreen
* @hide
*/
@@ -69,6 +69,9 @@
import android.database.ContentObserver;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraCharacteristics;
import android.hardware.camera2.CameraManager;
import android.hardware.hdmi.HdmiControlManager;
import android.hardware.hdmi.HdmiPlaybackClient;
import android.hardware.hdmi.HdmiPlaybackClient.OneTouchPlayCallback;
@@ -815,6 +818,11 @@ public void onDrawn() {
private boolean mHasPermanentMenuKey;
private CameraManager mCameraManager;
private boolean mTorchEnabled;
private boolean mIsTorchActive;
private boolean mWasTorchActive;
private class PolicyHandler extends Handler {
@Override
public void handleMessage(Message msg) {
@@ -1011,6 +1019,9 @@ void observe() {
resolver.registerContentObserver(Settings.System.getUriFor(
Settings.System.THREE_FINGER_GESTURE), false, this,
UserHandle.USER_ALL);
resolver.registerContentObserver(Settings.System.getUriFor(
Settings.System.KEYGUARD_TOGGLE_TORCH), false, this,
UserHandle.USER_ALL);
updateSettings();
}
@@ -1318,9 +1329,18 @@ private void interceptPowerKeyDown(KeyEvent event, boolean interactive) {
ViewConfiguration.get(mContext).getDeviceGlobalActionKeyTimeout());
}
} else {
wakeUpFromPowerKey(event.getDownTime());
if (mSupportLongPressPowerWhenNonInteractive && hasLongPressOnPowerBehavior()) {
if (!mTorchEnabled) {
wakeUpFromPowerKey(event.getDownTime());
}
if (mTorchEnabled && mIsTorchActive) {
try {
mCameraManager.setTorchMode(getCameraId(), false);
} catch (Exception e) {
mWasTorchActive = false;
}
mIsTorchActive = false;
mWasTorchActive = true;
} else if (mTorchEnabled || (mSupportLongPressPowerWhenNonInteractive && hasLongPressOnPowerBehavior())) {
Message msg = mHandler.obtainMessage(MSG_POWER_LONG_PRESS);
msg.setAsynchronous(true);
mHandler.sendMessageDelayed(msg,
@@ -1339,6 +1359,22 @@ private void interceptPowerKeyDown(KeyEvent event, boolean interactive) {
}
}
private String getCameraId() throws CameraAccessException {
String[] ids = mCameraManager.getCameraIdList();
int length = ids.length;
for (int i = 0; i < length; i += 1) {
String id = ids[i];
CameraCharacteristics c = mCameraManager.getCameraCharacteristics(id);
Boolean flashAvailable = c.get(CameraCharacteristics.FLASH_INFO_AVAILABLE);
Integer lensFacing = c.get(CameraCharacteristics.LENS_FACING);
if (flashAvailable != null && flashAvailable
&& lensFacing != null && lensFacing == CameraCharacteristics.LENS_FACING_BACK) {
return id;
}
}
return null;
}
private void interceptPowerKeyUp(KeyEvent event, boolean interactive, boolean canceled) {
final boolean handled = canceled || mPowerKeyHandled;
mPowerKeyTriggered = false;
@@ -1385,6 +1421,13 @@ private void cancelPendingPowerKeyAction() {
if (!mPowerKeyHandled) {
mPowerKeyHandled = true;
mHandler.removeMessages(MSG_POWER_LONG_PRESS);
if (mTorchEnabled && !isScreenOn()) {
if (mWasTorchActive) {
mWasTorchActive = false;
} else {
wakeUpFromPowerKey(SystemClock.uptimeMillis());
}
}
}
}
@@ -1493,24 +1536,34 @@ private int getMaxMultiPressPowerCount() {
}
private void powerLongPress() {
final int behavior = getResolvedLongPressOnPowerBehavior();
switch (behavior) {
case LONG_PRESS_POWER_NOTHING:
break;
case LONG_PRESS_POWER_GLOBAL_ACTIONS:
mPowerKeyHandled = true;
if (!performHapticFeedbackLw(null, HapticFeedbackConstants.LONG_PRESS, false)) {
performAuditoryFeedbackForAccessibilityIfNeed();
if (!mTorchEnabled || isScreenOn()) {
int behavior = getResolvedLongPressOnPowerBehavior();
switch (behavior) {
case LONG_PRESS_POWER_NOTHING:
break;
case LONG_PRESS_POWER_GLOBAL_ACTIONS:
mPowerKeyHandled = true;
if (!performHapticFeedbackLw(null, HapticFeedbackConstants.LONG_PRESS, false)) {
performAuditoryFeedbackForAccessibilityIfNeed();
}
showGlobalActionsInternal();
break;
case LONG_PRESS_POWER_SHUT_OFF:
case LONG_PRESS_POWER_SHUT_OFF_NO_CONFIRM:
mPowerKeyHandled = true;
performHapticFeedbackLw(null, HapticFeedbackConstants.LONG_PRESS, false);
sendCloseSystemWindows(SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS);
mWindowManagerFuncs.shutdown(behavior == LONG_PRESS_POWER_SHUT_OFF);
break;
}
showGlobalActionsInternal();
break;
case LONG_PRESS_POWER_SHUT_OFF:
case LONG_PRESS_POWER_SHUT_OFF_NO_CONFIRM:
mPowerKeyHandled = true;
performHapticFeedbackLw(null, HapticFeedbackConstants.LONG_PRESS, false);
sendCloseSystemWindows(SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS);
mWindowManagerFuncs.shutdown(behavior == LONG_PRESS_POWER_SHUT_OFF);
break;
} else if (mTorchEnabled && !isScreenOn()) {
try {
mCameraManager.setTorchMode(getCameraId(), true);
} catch (Exception e) {
}
mWasTorchActive = false;
mIsTorchActive = true;
mPowerKeyHandled = true;
}
}
@@ -1813,6 +1866,7 @@ public void init(Context context, IWindowManager windowManager,
mPowerManagerInternal = LocalServices.getService(PowerManagerInternal.class);
mAppOpsManager = (AppOpsManager) mContext.getSystemService(Context.APP_OPS_SERVICE);
mHasFeatureWatch = mContext.getPackageManager().hasSystemFeature(FEATURE_WATCH);
mCameraManager = (CameraManager) mContext.getSystemService(Context.CAMERA_SERVICE);
// Init display burn-in protection
boolean burnInProtectionEnabled = context.getResources().getBoolean(
@@ -2369,9 +2423,10 @@ public void updateSettings() {
Settings.System.VOLUME_WAKE_SCREEN, 0, UserHandle.USER_CURRENT) == 1);
mScreenrecordChordType = (Settings.System.getIntForUser(resolver,
Settings.System.SCREENRECORD_CHORD_TYPE, 0, UserHandle.USER_CURRENT) == 1);
mAospRecentsGrid = (Settings.System.getIntForUser(resolver,
Settings.System.RECENTS_USE_GRID, 0, UserHandle.USER_CURRENT) == 1);
mTorchEnabled = (Settings.System.getIntForUser(resolver,
Settings.System.KEYGUARD_TOGGLE_TORCH, 0, UserHandle.USER_CURRENT) == 1);
}
synchronized (mWindowManagerFuncs.getWindowManagerLock()) {

0 comments on commit 50d8a2a

Please sign in to comment.