Skip to content

Commit

Permalink
UNC: improved user present detection logic
Browse files Browse the repository at this point in the history
  • Loading branch information
C3C0 committed Sep 5, 2018
1 parent c44bb16 commit df09ae9
Showing 1 changed file with 33 additions and 19 deletions.
52 changes: 33 additions & 19 deletions src/com/ceco/oreo/gravitybox/ModLedControl.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ public class ModLedControl {
private static Sensor mProxSensor;
private static QuietHours mQuietHours;
private static Map<String, Long> mNotifTimestamps = new HashMap<String, Long>();
private static boolean mUserPresent;
private static Object mNotifManagerService;
private static boolean mProximityWakeUpEnabled;
private static boolean mScreenOnDueToActiveScreen;
Expand Down Expand Up @@ -170,10 +169,8 @@ public void onReceive(Context context, Intent intent) {
mQuietHours = new QuietHours(intent.getExtras());
} else if (action.equals(Intent.ACTION_USER_PRESENT)) {
if (DEBUG) log("User present");
mUserPresent = true;
mScreenOnDueToActiveScreen = false;
} else if (action.equals(Intent.ACTION_SCREEN_OFF)) {
mUserPresent = false;
mScreenOnDueToActiveScreen = false;
} else if (action.equals(ACTION_CLEAR_NOTIFICATIONS)) {
clearNotifications();
Expand Down Expand Up @@ -422,7 +419,7 @@ protected void afterHookedMethod(final MethodHookParam param) throws Throwable {
ls.getVisibilityLs() != VisibilityLs.CLEARABLE &&
ls.getVisibilityLs() != VisibilityLs.ALL &&
!qhActiveIncludingActiveScreen && !isOngoing &&
mPm != null && mKm.isKeyguardLocked()) {
!userPresent) {
n.extras.putBoolean(NOTIF_EXTRA_ACTIVE_SCREEN, true);
n.extras.putString(NOTIF_EXTRA_ACTIVE_SCREEN_MODE,
ls.getActiveScreenMode().toString());
Expand All @@ -444,18 +441,40 @@ protected void afterHookedMethod(final MethodHookParam param) throws Throwable {
}
};

private static PowerManager getPowerManager() {
if (mPm == null) {
mPm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
}
return mPm;
}

private static KeyguardManager getKeyguardManager() {
if (mKm == null) {
mKm = (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
}
return mKm;
}

private static TelephonyManager getTelephonyManager() {
if (mTelephonyManager == null) {
mTelephonyManager = (TelephonyManager)
mContext.getSystemService(Context.TELEPHONY_SERVICE);
}
return mTelephonyManager;
}

private static boolean isUserPresent() {
try {
if (mTelephonyManager == null) {
mTelephonyManager = (TelephonyManager)
mContext.getSystemService(Context.TELEPHONY_SERVICE);
}
final int callState = mTelephonyManager.getCallState();
if (DEBUG) log("isUserPresent: call state: " + callState);
return (mUserPresent || callState == TelephonyManager.CALL_STATE_OFFHOOK);
final boolean interactive =
getPowerManager().isInteractive() &&
!getKeyguardManager().isKeyguardLocked();
final int callState = getTelephonyManager().getCallState();
if (DEBUG) log("isUserPresent: interactive=" + interactive +
"; call state=" + callState);
return (interactive || callState == TelephonyManager.CALL_STATE_OFFHOOK);
} catch (Throwable t) {
GravityBox.log(TAG, t);
return mUserPresent;
return false;
}
}

Expand Down Expand Up @@ -574,14 +593,13 @@ private static boolean shouldIgnoreUpdatedNotificationLight(Object record, boole
}

private static XC_MethodHook applyZenModeHook = new XC_MethodHook() {
@SuppressWarnings("deprecation")
@Override
protected void afterHookedMethod(final MethodHookParam param) throws Throwable {
try {
Notification n = (Notification) XposedHelpers.callMethod(param.args[0], "getNotification");
if (!n.extras.containsKey(NOTIF_EXTRA_ACTIVE_SCREEN) ||
!n.extras.containsKey(NOTIF_EXTRA_ACTIVE_SCREEN_MODE) ||
!(mPm != null && !mPm.isScreenOn() && mKm.isKeyguardLocked())) {
isUserPresent()) {
n.extras.remove(NOTIF_EXTRA_ACTIVE_SCREEN);
return;
}
Expand Down Expand Up @@ -689,15 +707,11 @@ private static void updateActiveScreenFeature() {
try {
final boolean enable = !mUncLocked && mUncActiveScreenEnabled;
if (enable && mSm == null) {
mPm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
mKm = (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
mSm = (SensorManager) mContext.getSystemService(Context.SENSOR_SERVICE);
mProxSensor = mSm.getDefaultSensor(Sensor.TYPE_PROXIMITY);
} else if (!enable) {
mProxSensor = null;
mSm = null;
mPm = null;
mKm = null;
}
if (DEBUG) log("Active screen feature: " + enable);
} catch (Throwable t) {
Expand All @@ -711,7 +725,7 @@ private static void performActiveScreen() {
public void run() {
long ident = Binder.clearCallingIdentity();
try {
XposedHelpers.callMethod(mPm, "wakeUp", SystemClock.uptimeMillis());
XposedHelpers.callMethod(getPowerManager(), "wakeUp", SystemClock.uptimeMillis());
mScreenOnDueToActiveScreen = true;
} finally {
Binder.restoreCallingIdentity(ident);
Expand Down

0 comments on commit df09ae9

Please sign in to comment.