diff options
author | Veena Arvind <aveena@google.com> | 2023-10-26 19:00:39 +0000 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2023-10-26 19:00:39 +0000 |
commit | d1e06ec38fb18f36fdea5e4217f52abda93df260 (patch) | |
tree | 3260ad1442fa8b1682024dbec4a4fa7f1fadf8ff /service/java/com/android/server | |
parent | 395ce9c4c625b0af07b04913f30cfc941da6f209 (diff) | |
parent | 4810d926625e92dd0846f10debe1fb6164f0f7e2 (diff) | |
download | ConfigInfrastructure-d1e06ec38fb18f36fdea5e4217f52abda93df260.tar.gz |
Merge "Check for network connectivity before rebooting device, rescheduling if is none." into main
Diffstat (limited to 'service/java/com/android/server')
-rw-r--r-- | service/java/com/android/server/deviceconfig/UnattendedRebootManager.java | 105 | ||||
-rw-r--r-- | service/java/com/android/server/deviceconfig/UnattendedRebootManagerInjector.java | 5 |
2 files changed, 91 insertions, 19 deletions
diff --git a/service/java/com/android/server/deviceconfig/UnattendedRebootManager.java b/service/java/com/android/server/deviceconfig/UnattendedRebootManager.java index 36e6b06..e6ccf9f 100644 --- a/service/java/com/android/server/deviceconfig/UnattendedRebootManager.java +++ b/service/java/com/android/server/deviceconfig/UnattendedRebootManager.java @@ -10,6 +10,10 @@ import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.IntentSender; +import android.net.ConnectivityManager; +import android.net.Network; +import android.net.NetworkCapabilities; +import android.net.NetworkRequest; import android.os.PowerManager; import android.os.RecoverySystem; import android.util.Log; @@ -29,7 +33,8 @@ import java.time.ZoneId; * @hide */ final class UnattendedRebootManager { - private static final int DEFAULT_REBOOT_WINDOW_START_TIME_HOUR = 2; + private static final int DEFAULT_REBOOT_WINDOW_START_TIME_HOUR = 1; + private static final int DEFAULT_REBOOT_WINDOW_END_TIME_HOUR = 5; private static final int DEFAULT_REBOOT_FREQUENCY_DAYS = 2; @@ -67,20 +72,28 @@ final class UnattendedRebootManager { return DEFAULT_REBOOT_WINDOW_START_TIME_HOUR; } + public int getRebootEndTime() { + return DEFAULT_REBOOT_WINDOW_END_TIME_HOUR; + } + public int getRebootFrequency() { return DEFAULT_REBOOT_FREQUENCY_DAYS; } public void setRebootAlarm(Context context, long rebootTimeMillis) { AlarmManager alarmManager = context.getSystemService(AlarmManager.class); - PendingIntent pendingIntent = - PendingIntent.getBroadcast( - context, - /* requestCode= */ 0, - new Intent(ACTION_TRIGGER_REBOOT), - PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_IMMUTABLE); + alarmManager.setExact( + AlarmManager.RTC_WAKEUP, rebootTimeMillis, createTriggerRebootPendingIntent(context)); + } - alarmManager.setExact(AlarmManager.RTC_WAKEUP, rebootTimeMillis, pendingIntent); + public void triggerRebootOnNetworkAvailable(Context context) { + final ConnectivityManager connectivityManager = + context.getSystemService(ConnectivityManager.class); + NetworkRequest request = + new NetworkRequest.Builder() + .addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) + .build(); + connectivityManager.requestNetwork(request, createTriggerRebootPendingIntent(context)); } public int rebootAndApply(@NonNull Context context, @NonNull String reason, boolean slotSwitch) @@ -102,6 +115,14 @@ final class UnattendedRebootManager { PowerManager powerManager = context.getSystemService(PowerManager.class); powerManager.reboot(REBOOT_REASON); } + + private static PendingIntent createTriggerRebootPendingIntent(Context context) { + return PendingIntent.getBroadcast( + context, + /* requestCode= */ 0, + new Intent(ACTION_TRIGGER_REBOOT), + PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_IMMUTABLE); + } } @VisibleForTesting @@ -178,22 +199,53 @@ final class UnattendedRebootManager { @VisibleForTesting void tryRebootOrSchedule() { - // TODO(b/305259443): check network is connected - // Check if RoR is supported. + Log.v(TAG, "Attempting unattended reboot"); + + // Is RoR is supported? if (!isDeviceSecure(mContext)) { Log.v(TAG, "Device is not secure. Proceed with regular reboot"); mInjector.regularReboot(mContext); - } else if (isPreparedForUnattendedReboot()) { - try { - mInjector.rebootAndApply(mContext, REBOOT_REASON, /* slotSwitch= */ false); - } catch (IOException e) { - Log.e(TAG, e.getLocalizedMessage()); - } - // If reboot is successful, should not reach this. - } else { - // Lskf is not captured, try again the following day + return; + } + // Is RoR prepared? + if (!isPreparedForUnattendedReboot()) { + Log.v(TAG, "Lskf is not captured, reschedule reboot."); prepareUnattendedReboot(); scheduleReboot(); + return; + } + // Is network connected? + // TODO(b/305259443): Use after-boot network connectivity projection + if (!isNetworkConnected(mContext)) { + Log.i(TAG, "Network is not connected, schedule reboot for another time."); + mInjector.triggerRebootOnNetworkAvailable(mContext); + return; + } + // Is current time between reboot window? + int currentHour = + Instant.ofEpochMilli(mInjector.now()) + .atZone(mInjector.zoneId()) + .toLocalDateTime() + .getHour(); + if (currentHour < mInjector.getRebootStartTime() + && currentHour >= mInjector.getRebootEndTime()) { + Log.v(TAG, "Reboot requested outside of reboot window, reschedule."); + prepareUnattendedReboot(); + scheduleReboot(); + return; + } + + // Proceed with RoR. + try { + int success = mInjector.rebootAndApply(mContext, REBOOT_REASON, /* slotSwitch= */ false); + if (success != 0) { + // If reboot is not successful, reschedule. + Log.w(TAG, "Unattended reboot failed, reschedule."); + scheduleReboot(); + } + } catch (IOException e) { + Log.e(TAG, e.getLocalizedMessage()); + scheduleReboot(); } } @@ -220,4 +272,19 @@ final class UnattendedRebootManager { } return keyguardManager.isDeviceSecure(); } + + private static boolean isNetworkConnected(Context context) { + final ConnectivityManager connectivityManager = + context.getSystemService(ConnectivityManager.class); + if (connectivityManager == null) { + Log.w(TAG, "ConnectivityManager is null"); + return false; + } + Network activeNetwork = connectivityManager.getActiveNetwork(); + NetworkCapabilities networkCapabilities = + connectivityManager.getNetworkCapabilities(activeNetwork); + return networkCapabilities != null + && networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) + && networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED); + } } diff --git a/service/java/com/android/server/deviceconfig/UnattendedRebootManagerInjector.java b/service/java/com/android/server/deviceconfig/UnattendedRebootManagerInjector.java index 4e883d3..e1e0909 100644 --- a/service/java/com/android/server/deviceconfig/UnattendedRebootManagerInjector.java +++ b/service/java/com/android/server/deviceconfig/UnattendedRebootManagerInjector.java @@ -23,11 +23,16 @@ interface UnattendedRebootManagerInjector { /** Reboot time injectors. */ int getRebootStartTime(); + int getRebootEndTime(); + int getRebootFrequency(); /** Reboot Alarm injector. */ void setRebootAlarm(Context context, long rebootTimeMillis); + /** Connectivity injector. */ + void triggerRebootOnNetworkAvailable(Context context); + /** {@link RecoverySystem} methods injectors. */ int rebootAndApply(@NonNull Context context, @NonNull String reason, boolean slotSwitch) throws IOException; |