summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Hui Yu <huiyu@google.com> 2019-06-05 11:44:40 -0700
committer Hui Yu <huiyu@google.com> 2019-06-05 21:38:06 +0000
commit4303dc06ce0ed5013fc831c6656ddc89ae689d4b (patch)
tree876ddfb11b864fd3934e380f319878e988a9b664
parentcf2ff61cceb9a22d6813cdf78da6b41d2fd724a1 (diff)
Check for file existence before reading it.
To avoid FileNotFoundException. Fix: 134585043 Test: Factory wipe the device, first time bootup, observe no FileNotFoundException exception message from BatteryStatsImpl. Change-Id: If7830d8fefe07c57b0b2c86ab6cd603913c23e34
-rw-r--r--core/java/com/android/internal/os/BatteryStatsImpl.java45
1 files changed, 25 insertions, 20 deletions
diff --git a/core/java/com/android/internal/os/BatteryStatsImpl.java b/core/java/com/android/internal/os/BatteryStatsImpl.java
index 1df9533a07cc..9fd92719882b 100644
--- a/core/java/com/android/internal/os/BatteryStatsImpl.java
+++ b/core/java/com/android/internal/os/BatteryStatsImpl.java
@@ -13322,7 +13322,8 @@ public class BatteryStatsImpl extends BatteryStats {
return;
}
- if (mBatteryStatsHistory.getActiveFile() == null) {
+ final AtomicFile activeHistoryFile = mBatteryStatsHistory.getActiveFile();
+ if (activeHistoryFile == null) {
Slog.w(TAG,
"readLocked: no history file associated with this instance");
return;
@@ -13333,14 +13334,16 @@ public class BatteryStatsImpl extends BatteryStats {
Parcel stats = Parcel.obtain();
try {
final long start = SystemClock.uptimeMillis();
- byte[] raw = mStatsFile.readFully();
- stats.unmarshall(raw, 0, raw.length);
- stats.setDataPosition(0);
- readSummaryFromParcel(stats);
- if (DEBUG) {
- Slog.d(TAG, "readLocked stats file:" + mStatsFile.getBaseFile().getPath()
- + " bytes:" + raw.length + " takes ms:" + (SystemClock.uptimeMillis()
- - start));
+ if (mStatsFile.exists()) {
+ byte[] raw = mStatsFile.readFully();
+ stats.unmarshall(raw, 0, raw.length);
+ stats.setDataPosition(0);
+ readSummaryFromParcel(stats);
+ if (DEBUG) {
+ Slog.d(TAG, "readLocked stats file:" + mStatsFile.getBaseFile().getPath()
+ + " bytes:" + raw.length + " takes ms:" + (SystemClock.uptimeMillis()
+ - start));
+ }
}
} catch (Exception e) {
Slog.e(TAG, "Error reading battery statistics", e);
@@ -13352,17 +13355,19 @@ public class BatteryStatsImpl extends BatteryStats {
Parcel history = Parcel.obtain();
try {
final long start = SystemClock.uptimeMillis();
- byte[] raw = mBatteryStatsHistory.getActiveFile().readFully();
- if (raw.length > 0) {
- history.unmarshall(raw, 0, raw.length);
- history.setDataPosition(0);
- readHistoryBuffer(history, true);
- }
- if (DEBUG) {
- Slog.d(TAG, "readLocked history file::"
- + mBatteryStatsHistory.getActiveFile().getBaseFile().getPath()
- + " bytes:" + raw.length + " takes ms:" + (SystemClock.uptimeMillis()
- - start));
+ if (activeHistoryFile.exists()) {
+ byte[] raw = activeHistoryFile.readFully();
+ if (raw.length > 0) {
+ history.unmarshall(raw, 0, raw.length);
+ history.setDataPosition(0);
+ readHistoryBuffer(history, true);
+ }
+ if (DEBUG) {
+ Slog.d(TAG, "readLocked history file::"
+ + activeHistoryFile.getBaseFile().getPath()
+ + " bytes:" + raw.length + " takes ms:" + (SystemClock.uptimeMillis()
+ - start));
+ }
}
} catch (Exception e) {
Slog.e(TAG, "Error reading battery history", e);