Merge "Switch /data/misc/reboot/last_reboot_reason to persistent property" am: 0172ce8dbf

am: cf5fa80fbf

Change-Id: Ica26cd9240a977d9140c72f5606c8b9d874001d9
This commit is contained in:
Mark Salyzyn
2017-08-25 14:37:35 +00:00
committed by android-build-merger
2 changed files with 13 additions and 26 deletions

View File

@@ -204,8 +204,8 @@ public final class PowerManagerService extends SystemService
// Possible reasons for shutting down for use in data/misc/reboot/last_shutdown_reason // 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_SHUTDOWN = "shutdown";
private static final String REASON_REBOOT = "reboot"; private static final String REASON_REBOOT = "reboot";
private static final String REASON_USERREQUESTED = "userrequested"; private static final String REASON_USERREQUESTED = "shutdown,userrequested";
private static final String REASON_THERMAL_SHUTDOWN = "thermal-shutdown"; private static final String REASON_THERMAL_SHUTDOWN = "shutdown,thermal";
private static final String TRACE_SCREEN_ON = "Screen turning on"; private static final String TRACE_SCREEN_ON = "Screen turning on";
@@ -220,8 +220,8 @@ public final class PowerManagerService extends SystemService
private static final int HALT_MODE_REBOOT = 1; private static final int HALT_MODE_REBOOT = 1;
private static final int HALT_MODE_REBOOT_SAFE_MODE = 2; private static final int HALT_MODE_REBOOT_SAFE_MODE = 2;
// File location for last reboot reason // Persistent property for last reboot reason
private static final String LAST_REBOOT_LOCATION = "/data/misc/reboot/last_reboot_reason"; private static final String LAST_REBOOT_PROPERTY = "persist.sys.boot.reason";
private final Context mContext; private final Context mContext;
private final ServiceThread mHandlerThread; private final ServiceThread mHandlerThread;
@@ -4388,7 +4388,7 @@ public final class PowerManagerService extends SystemService
final long ident = Binder.clearCallingIdentity(); final long ident = Binder.clearCallingIdentity();
try { try {
return getLastShutdownReasonInternal(new File(LAST_REBOOT_LOCATION)); return getLastShutdownReasonInternal(LAST_REBOOT_PROPERTY);
} finally { } finally {
Binder.restoreCallingIdentity(ident); Binder.restoreCallingIdentity(ident);
} }
@@ -4622,13 +4622,9 @@ public final class PowerManagerService extends SystemService
} }
@VisibleForTesting @VisibleForTesting
int getLastShutdownReasonInternal(File lastRebootReason) { // lastRebootReasonProperty argument to permit testing
String line = ""; int getLastShutdownReasonInternal(String lastRebootReasonProperty) {
try (BufferedReader bufferedReader = new BufferedReader(new FileReader(lastRebootReason))){ String line = SystemProperties.get(lastRebootReasonProperty);
line = bufferedReader.readLine();
} catch (IOException e) {
Slog.e(TAG, "Failed to read last_reboot_reason file", e);
}
if (line == null) { if (line == null) {
return PowerManager.SHUTDOWN_REASON_UNKNOWN; return PowerManager.SHUTDOWN_REASON_UNKNOWN;
} }

View File

@@ -20,6 +20,7 @@ import android.content.Context;
import android.hardware.display.DisplayManagerInternal.DisplayPowerRequest; import android.hardware.display.DisplayManagerInternal.DisplayPowerRequest;
import android.os.PowerManager; import android.os.PowerManager;
import android.os.PowerSaveState; import android.os.PowerSaveState;
import android.os.SystemProperties;
import android.test.AndroidTestCase; import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.SmallTest; import android.test.suitebuilder.annotation.SmallTest;
import android.text.TextUtils; import android.text.TextUtils;
@@ -44,17 +45,14 @@ public class PowerManagerServiceTest extends AndroidTestCase {
private static final float PRECISION = 0.001f; private static final float PRECISION = 0.001f;
private static final float BRIGHTNESS_FACTOR = 0.7f; private static final float BRIGHTNESS_FACTOR = 0.7f;
private static final boolean BATTERY_SAVER_ENABLED = true; 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 @Mock BatterySaverPolicy mBatterySaverPolicy;
private PowerManagerService mService; private PowerManagerService mService;
private PowerSaveState mPowerSaveState; private PowerSaveState mPowerSaveState;
private DisplayPowerRequest mDisplayPowerRequest; private DisplayPowerRequest mDisplayPowerRequest;
private File mTempReason;
@Rule @Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();
public void setUp() throws Exception { public void setUp() throws Exception {
super.setUp(); super.setUp();
MockitoAnnotations.initMocks(this); MockitoAnnotations.initMocks(this);
@@ -68,8 +66,6 @@ public class PowerManagerServiceTest extends AndroidTestCase {
.thenReturn(mPowerSaveState); .thenReturn(mPowerSaveState);
mDisplayPowerRequest = new DisplayPowerRequest(); mDisplayPowerRequest = new DisplayPowerRequest();
mService = new PowerManagerService(getContext(), mBatterySaverPolicy); mService = new PowerManagerService(getContext(), mBatterySaverPolicy);
temporaryFolder.create();
mTempReason = temporaryFolder.newFile(LAST_REBOOT_REASON);
} }
@SmallTest @SmallTest
@@ -82,14 +78,9 @@ public class PowerManagerServiceTest extends AndroidTestCase {
@SmallTest @SmallTest
public void testGetLastShutdownReasonInternal() { public void testGetLastShutdownReasonInternal() {
try { SystemProperties.set(TEST_LAST_REBOOT_PROPERTY, "shutdown,thermal");
FileWriter writer = new FileWriter(mTempReason); int reason = mService.getLastShutdownReasonInternal(TEST_LAST_REBOOT_PROPERTY);
writer.append("thermal-shutdown\n"); SystemProperties.set(TEST_LAST_REBOOT_PROPERTY, "");
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
int reason = mService.getLastShutdownReasonInternal(mTempReason);
assertThat(reason).isEqualTo(PowerManager.SHUTDOWN_REASON_THERMAL_SHUTDOWN); assertThat(reason).isEqualTo(PowerManager.SHUTDOWN_REASON_THERMAL_SHUTDOWN);
} }
} }