From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: anupritaisno1 Date: Mon, 7 Jun 2021 22:04:53 +0100 Subject: [PATCH] automatically reboot device after timeout if set Change-Id: Idf78e9f3512ad8d407623a7c7c822b4712f8201c --- core/java/android/provider/Settings.java | 7 ++++ data/etc/privapp-permissions-platform.xml | 1 + packages/SystemUI/AndroidManifest.xml | 3 ++ .../keyguard/KeyguardViewMediator.java | 35 +++++++++++++++++++ 4 files changed, 46 insertions(+) diff --git a/core/java/android/provider/Settings.java b/core/java/android/provider/Settings.java index 0c73965d13c2..c0260a239ceb 100644 --- a/core/java/android/provider/Settings.java +++ b/core/java/android/provider/Settings.java @@ -13047,6 +13047,13 @@ public final class Settings { */ public static final String GNSS_HAL_LOCATION_REQUEST_DURATION_MILLIS = "gnss_hal_location_request_duration_millis"; + + /** + * Whether to automatically reboot the device after a user defined timeout + * + * @hide + */ + public static final String SETTINGS_REBOOT_AFTER_TIMEOUT = "settings_reboot_after_timeout"; } /** diff --git a/data/etc/privapp-permissions-platform.xml b/data/etc/privapp-permissions-platform.xml index 82b6a22d2740..686edd41af13 100644 --- a/data/etc/privapp-permissions-platform.xml +++ b/data/etc/privapp-permissions-platform.xml @@ -375,6 +375,7 @@ applications that come with the platform + diff --git a/packages/SystemUI/AndroidManifest.xml b/packages/SystemUI/AndroidManifest.xml index c924effe6933..18559af7ae5f 100644 --- a/packages/SystemUI/AndroidManifest.xml +++ b/packages/SystemUI/AndroidManifest.xml @@ -216,6 +216,9 @@ + + + diff --git a/packages/SystemUI/src/com/android/systemui/keyguard/KeyguardViewMediator.java b/packages/SystemUI/src/com/android/systemui/keyguard/KeyguardViewMediator.java index bac481c8e478..8a7be570ee76 100644 --- a/packages/SystemUI/src/com/android/systemui/keyguard/KeyguardViewMediator.java +++ b/packages/SystemUI/src/com/android/systemui/keyguard/KeyguardViewMediator.java @@ -148,6 +148,8 @@ public class KeyguardViewMediator extends SystemUI { private final static String TAG = "KeyguardViewMediator"; + private static final String DELAYED_REBOOT_ACTION = + "com.android.internal.policy.impl.PhoneWindowManager.DELAYED_REBOOT"; private static final String DELAYED_KEYGUARD_ACTION = "com.android.internal.policy.impl.PhoneWindowManager.DELAYED_KEYGUARD"; private static final String DELAYED_LOCK_PROFILE_ACTION = @@ -269,6 +271,11 @@ public class KeyguardViewMediator extends SystemUI { */ private int mDelayedProfileShowingSequence; + /** + * Same as {@link #mDelayedProfileShowingSequence}, but used for our reboot implementation + */ + private int mDelayedRebootSequence; + /** * If the user has disabled the keyguard, then requests to exit, this is * how we'll ultimately let them know whether it was successful. We use this @@ -735,6 +742,7 @@ public class KeyguardViewMediator extends SystemUI { final IntentFilter delayedActionFilter = new IntentFilter(); delayedActionFilter.addAction(DELAYED_KEYGUARD_ACTION); delayedActionFilter.addAction(DELAYED_LOCK_PROFILE_ACTION); + delayedActionFilter.addAction(DELAYED_REBOOT_ACTION); mContext.registerReceiver(mDelayedLockBroadcastReceiver, delayedActionFilter, SYSTEMUI_PERMISSION, null /* scheduler */); @@ -998,6 +1006,18 @@ public class KeyguardViewMediator extends SystemUI { } } + private void doRebootForOwnerAfterTimeoutIfEnabled(long rebootAfterTimeout) { + long when = SystemClock.elapsedRealtime() + rebootAfterTimeout; + Intent rebootIntent = new Intent(DELAYED_REBOOT_ACTION); + rebootIntent.putExtra("seq", mDelayedRebootSequence); + rebootIntent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); + PendingIntent sender = PendingIntent.getBroadcast(mContext, + 0, rebootIntent, PendingIntent.FLAG_CANCEL_CURRENT); + mAlarmManager.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, when, sender); + if (DEBUG) Log.d(TAG, "setting alarm to reboot device, timeout = " + + String.valueOf(rebootAfterTimeout)); + } + private void doKeyguardForChildProfilesLocked() { UserManager um = UserManager.get(mContext); for (int profileId : um.getEnabledProfileIds(UserHandle.myUserId())) { @@ -1015,6 +1035,10 @@ public class KeyguardViewMediator extends SystemUI { mDelayedProfileShowingSequence++; } + private void cancelDoRebootForOwnerAfterTimeoutIfEnabled() { + mDelayedRebootSequence++; + } + /** * Let's us know when the device is waking up. */ @@ -1402,6 +1426,10 @@ public class KeyguardViewMediator extends SystemUI { if (DEBUG) Log.d(TAG, "doKeyguard: showing the lock screen"); showLocked(options); + final long rebootAfterTimeout = Settings.Global.getLong(mContext.getContentResolver(), Settings.Global.SETTINGS_REBOOT_AFTER_TIMEOUT, 0); + if (rebootAfterTimeout >= 1) { + doRebootForOwnerAfterTimeoutIfEnabled(rebootAfterTimeout); + } } private void lockProfile(int userId) { @@ -1565,6 +1593,12 @@ public class KeyguardViewMediator extends SystemUI { } } } + } else if (DELAYED_REBOOT_ACTION.equals(intent.getAction())) { + final int sequence = intent.getIntExtra("seq", 0); + if (sequence == mDelayedRebootSequence) { + PowerManager pm = mContext.getSystemService(PowerManager.class); + pm.reboot(null); + } } } }; @@ -1961,6 +1995,7 @@ public class KeyguardViewMediator extends SystemUI { mHideAnimationRun = false; adjustStatusBarLocked(); sendUserPresentBroadcast(); + cancelDoRebootForOwnerAfterTimeoutIfEnabled(); mUpdateMonitor.setKeyguardGoingAway(false /* goingAway */); } Trace.endSection();