From e65c0c61b771f05e9712cbd546f675aa7c380265 Mon Sep 17 00:00:00 2001 From: Mark Salyzyn Date: Tue, 15 Aug 2017 07:53:47 -0700 Subject: [PATCH] Switch /data/misc/reboot/last_reboot_reason to persistent property Switch from /data/misc/reboot/last_reboot_reason to persistent Android property persist.sys.boot.reason for indicating why the device is rebooted or shutdown. persist.sys.boot.reason has a standard as outlined in b/63736262 and the associated investigation. Made adjustments to the values so that we did not create a problem even before we started. Compliance is part of the tests in boot_reason_test.sh. Test: system/core/bootstat/boot_reason_test.sh Bug: 64687998 Change-Id: Iba69acf2105f4446411d86cdb8097a1755a20f15 --- .../server/power/PowerManagerService.java | 20 ++++++++----------- .../server/power/PowerManagerServiceTest.java | 19 +++++------------- 2 files changed, 13 insertions(+), 26 deletions(-) diff --git a/services/core/java/com/android/server/power/PowerManagerService.java b/services/core/java/com/android/server/power/PowerManagerService.java index 3b5db29de9e0a..1f4517e48c096 100644 --- a/services/core/java/com/android/server/power/PowerManagerService.java +++ b/services/core/java/com/android/server/power/PowerManagerService.java @@ -200,8 +200,8 @@ public final class PowerManagerService extends SystemService // Possible reasons for shutting down for use in data/misc/reboot/last_shutdown_reason private static final String REASON_SHUTDOWN = "shutdown"; private static final String REASON_REBOOT = "reboot"; - private static final String REASON_USERREQUESTED = "userrequested"; - private static final String REASON_THERMAL_SHUTDOWN = "thermal-shutdown"; + private static final String REASON_USERREQUESTED = "shutdown,userrequested"; + private static final String REASON_THERMAL_SHUTDOWN = "shutdown,thermal"; private static final String TRACE_SCREEN_ON = "Screen turning on"; @@ -216,8 +216,8 @@ public final class PowerManagerService extends SystemService private static final int HALT_MODE_REBOOT = 1; private static final int HALT_MODE_REBOOT_SAFE_MODE = 2; - // File location for last reboot reason - private static final String LAST_REBOOT_LOCATION = "/data/misc/reboot/last_reboot_reason"; + // Persistent property for last reboot reason + private static final String LAST_REBOOT_PROPERTY = "persist.sys.boot.reason"; private final Context mContext; private final ServiceThread mHandlerThread; @@ -4373,7 +4373,7 @@ public final class PowerManagerService extends SystemService final long ident = Binder.clearCallingIdentity(); try { - return getLastShutdownReasonInternal(new File(LAST_REBOOT_LOCATION)); + return getLastShutdownReasonInternal(LAST_REBOOT_PROPERTY); } finally { Binder.restoreCallingIdentity(ident); } @@ -4607,13 +4607,9 @@ public final class PowerManagerService extends SystemService } @VisibleForTesting - int getLastShutdownReasonInternal(File lastRebootReason) { - String line = ""; - try (BufferedReader bufferedReader = new BufferedReader(new FileReader(lastRebootReason))){ - line = bufferedReader.readLine(); - } catch (IOException e) { - Slog.e(TAG, "Failed to read last_reboot_reason file", e); - } + // lastRebootReasonProperty argument to permit testing + int getLastShutdownReasonInternal(String lastRebootReasonProperty) { + String line = SystemProperties.get(lastRebootReasonProperty); if (line == null) { return PowerManager.SHUTDOWN_REASON_UNKNOWN; } diff --git a/services/tests/servicestests/src/com/android/server/power/PowerManagerServiceTest.java b/services/tests/servicestests/src/com/android/server/power/PowerManagerServiceTest.java index d12c07a84004b..14b1ce1662c7c 100644 --- a/services/tests/servicestests/src/com/android/server/power/PowerManagerServiceTest.java +++ b/services/tests/servicestests/src/com/android/server/power/PowerManagerServiceTest.java @@ -20,6 +20,7 @@ import android.content.Context; import android.hardware.display.DisplayManagerInternal.DisplayPowerRequest; import android.os.PowerManager; import android.os.PowerSaveState; +import android.os.SystemProperties; import android.test.AndroidTestCase; import android.test.suitebuilder.annotation.SmallTest; import android.text.TextUtils; @@ -44,17 +45,14 @@ public class PowerManagerServiceTest extends AndroidTestCase { private static final float PRECISION = 0.001f; private static final float BRIGHTNESS_FACTOR = 0.7f; private static final boolean BATTERY_SAVER_ENABLED = true; - private static final String LAST_REBOOT_REASON = "last_reboot_reason"; + private static final String TEST_LAST_REBOOT_PROPERTY = "test.sys.boot.reason"; private @Mock BatterySaverPolicy mBatterySaverPolicy; private PowerManagerService mService; private PowerSaveState mPowerSaveState; private DisplayPowerRequest mDisplayPowerRequest; - private File mTempReason; @Rule - public TemporaryFolder temporaryFolder = new TemporaryFolder(); - public void setUp() throws Exception { super.setUp(); MockitoAnnotations.initMocks(this); @@ -68,8 +66,6 @@ public class PowerManagerServiceTest extends AndroidTestCase { .thenReturn(mPowerSaveState); mDisplayPowerRequest = new DisplayPowerRequest(); mService = new PowerManagerService(getContext(), mBatterySaverPolicy); - temporaryFolder.create(); - mTempReason = temporaryFolder.newFile(LAST_REBOOT_REASON); } @SmallTest @@ -82,14 +78,9 @@ public class PowerManagerServiceTest extends AndroidTestCase { @SmallTest public void testGetLastShutdownReasonInternal() { - try { - FileWriter writer = new FileWriter(mTempReason); - writer.append("thermal-shutdown\n"); - writer.close(); - } catch (IOException e) { - e.printStackTrace(); - } - int reason = mService.getLastShutdownReasonInternal(mTempReason); + SystemProperties.set(TEST_LAST_REBOOT_PROPERTY, "shutdown,thermal"); + int reason = mService.getLastShutdownReasonInternal(TEST_LAST_REBOOT_PROPERTY); + SystemProperties.set(TEST_LAST_REBOOT_PROPERTY, ""); assertThat(reason).isEqualTo(PowerManager.SHUTDOWN_REASON_THERMAL_SHUTDOWN); } }