Skip to content

Commit df09ae9

Browse files
committed
UNC: improved user present detection logic
More info GravityBox#1667 (comment)
1 parent c44bb16 commit df09ae9

File tree

1 file changed

+33
-19
lines changed

1 file changed

+33
-19
lines changed

src/com/ceco/oreo/gravitybox/ModLedControl.java

+33-19
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ public class ModLedControl {
9494
private static Sensor mProxSensor;
9595
private static QuietHours mQuietHours;
9696
private static Map<String, Long> mNotifTimestamps = new HashMap<String, Long>();
97-
private static boolean mUserPresent;
9897
private static Object mNotifManagerService;
9998
private static boolean mProximityWakeUpEnabled;
10099
private static boolean mScreenOnDueToActiveScreen;
@@ -170,10 +169,8 @@ public void onReceive(Context context, Intent intent) {
170169
mQuietHours = new QuietHours(intent.getExtras());
171170
} else if (action.equals(Intent.ACTION_USER_PRESENT)) {
172171
if (DEBUG) log("User present");
173-
mUserPresent = true;
174172
mScreenOnDueToActiveScreen = false;
175173
} else if (action.equals(Intent.ACTION_SCREEN_OFF)) {
176-
mUserPresent = false;
177174
mScreenOnDueToActiveScreen = false;
178175
} else if (action.equals(ACTION_CLEAR_NOTIFICATIONS)) {
179176
clearNotifications();
@@ -422,7 +419,7 @@ protected void afterHookedMethod(final MethodHookParam param) throws Throwable {
422419
ls.getVisibilityLs() != VisibilityLs.CLEARABLE &&
423420
ls.getVisibilityLs() != VisibilityLs.ALL &&
424421
!qhActiveIncludingActiveScreen && !isOngoing &&
425-
mPm != null && mKm.isKeyguardLocked()) {
422+
!userPresent) {
426423
n.extras.putBoolean(NOTIF_EXTRA_ACTIVE_SCREEN, true);
427424
n.extras.putString(NOTIF_EXTRA_ACTIVE_SCREEN_MODE,
428425
ls.getActiveScreenMode().toString());
@@ -444,18 +441,40 @@ protected void afterHookedMethod(final MethodHookParam param) throws Throwable {
444441
}
445442
};
446443

444+
private static PowerManager getPowerManager() {
445+
if (mPm == null) {
446+
mPm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
447+
}
448+
return mPm;
449+
}
450+
451+
private static KeyguardManager getKeyguardManager() {
452+
if (mKm == null) {
453+
mKm = (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
454+
}
455+
return mKm;
456+
}
457+
458+
private static TelephonyManager getTelephonyManager() {
459+
if (mTelephonyManager == null) {
460+
mTelephonyManager = (TelephonyManager)
461+
mContext.getSystemService(Context.TELEPHONY_SERVICE);
462+
}
463+
return mTelephonyManager;
464+
}
465+
447466
private static boolean isUserPresent() {
448467
try {
449-
if (mTelephonyManager == null) {
450-
mTelephonyManager = (TelephonyManager)
451-
mContext.getSystemService(Context.TELEPHONY_SERVICE);
452-
}
453-
final int callState = mTelephonyManager.getCallState();
454-
if (DEBUG) log("isUserPresent: call state: " + callState);
455-
return (mUserPresent || callState == TelephonyManager.CALL_STATE_OFFHOOK);
468+
final boolean interactive =
469+
getPowerManager().isInteractive() &&
470+
!getKeyguardManager().isKeyguardLocked();
471+
final int callState = getTelephonyManager().getCallState();
472+
if (DEBUG) log("isUserPresent: interactive=" + interactive +
473+
"; call state=" + callState);
474+
return (interactive || callState == TelephonyManager.CALL_STATE_OFFHOOK);
456475
} catch (Throwable t) {
457476
GravityBox.log(TAG, t);
458-
return mUserPresent;
477+
return false;
459478
}
460479
}
461480

@@ -574,14 +593,13 @@ private static boolean shouldIgnoreUpdatedNotificationLight(Object record, boole
574593
}
575594

576595
private static XC_MethodHook applyZenModeHook = new XC_MethodHook() {
577-
@SuppressWarnings("deprecation")
578596
@Override
579597
protected void afterHookedMethod(final MethodHookParam param) throws Throwable {
580598
try {
581599
Notification n = (Notification) XposedHelpers.callMethod(param.args[0], "getNotification");
582600
if (!n.extras.containsKey(NOTIF_EXTRA_ACTIVE_SCREEN) ||
583601
!n.extras.containsKey(NOTIF_EXTRA_ACTIVE_SCREEN_MODE) ||
584-
!(mPm != null && !mPm.isScreenOn() && mKm.isKeyguardLocked())) {
602+
isUserPresent()) {
585603
n.extras.remove(NOTIF_EXTRA_ACTIVE_SCREEN);
586604
return;
587605
}
@@ -689,15 +707,11 @@ private static void updateActiveScreenFeature() {
689707
try {
690708
final boolean enable = !mUncLocked && mUncActiveScreenEnabled;
691709
if (enable && mSm == null) {
692-
mPm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
693-
mKm = (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
694710
mSm = (SensorManager) mContext.getSystemService(Context.SENSOR_SERVICE);
695711
mProxSensor = mSm.getDefaultSensor(Sensor.TYPE_PROXIMITY);
696712
} else if (!enable) {
697713
mProxSensor = null;
698714
mSm = null;
699-
mPm = null;
700-
mKm = null;
701715
}
702716
if (DEBUG) log("Active screen feature: " + enable);
703717
} catch (Throwable t) {
@@ -711,7 +725,7 @@ private static void performActiveScreen() {
711725
public void run() {
712726
long ident = Binder.clearCallingIdentity();
713727
try {
714-
XposedHelpers.callMethod(mPm, "wakeUp", SystemClock.uptimeMillis());
728+
XposedHelpers.callMethod(getPowerManager(), "wakeUp", SystemClock.uptimeMillis());
715729
mScreenOnDueToActiveScreen = true;
716730
} finally {
717731
Binder.restoreCallingIdentity(ident);

0 commit comments

Comments
 (0)