Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 4303dc06 authored by Hui Yu's avatar Hui Yu
Browse files

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
parent cf2ff61c
Loading
Loading
Loading
Loading
+25 −20
Original line number Diff line number Diff line
@@ -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,6 +13334,7 @@ public class BatteryStatsImpl extends BatteryStats {
        Parcel stats = Parcel.obtain();
        try {
            final long start = SystemClock.uptimeMillis();
            if (mStatsFile.exists()) {
                byte[] raw = mStatsFile.readFully();
                stats.unmarshall(raw, 0, raw.length);
                stats.setDataPosition(0);
@@ -13342,6 +13344,7 @@ public class BatteryStatsImpl extends BatteryStats {
                            + " bytes:" + raw.length + " takes ms:" + (SystemClock.uptimeMillis()
                            - start));
                }
            }
        } catch (Exception e) {
            Slog.e(TAG, "Error reading battery statistics", e);
            resetAllStatsLocked();
@@ -13352,7 +13355,8 @@ public class BatteryStatsImpl extends BatteryStats {
        Parcel history = Parcel.obtain();
        try {
            final long start = SystemClock.uptimeMillis();
            byte[] raw = mBatteryStatsHistory.getActiveFile().readFully();
            if (activeHistoryFile.exists()) {
                byte[] raw = activeHistoryFile.readFully();
                if (raw.length > 0) {
                    history.unmarshall(raw, 0, raw.length);
                    history.setDataPosition(0);
@@ -13360,10 +13364,11 @@ public class BatteryStatsImpl extends BatteryStats {
                }
                if (DEBUG) {
                    Slog.d(TAG, "readLocked history file::"
                        + mBatteryStatsHistory.getActiveFile().getBaseFile().getPath()
                            + activeHistoryFile.getBaseFile().getPath()
                            + " bytes:" + raw.length + " takes ms:" + (SystemClock.uptimeMillis()
                            - start));
                }
            }
        } catch (Exception e) {
            Slog.e(TAG, "Error reading battery history", e);
            clearHistoryLocked();