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

Commit 746b82aa authored by Hui Yu's avatar Hui Yu
Browse files

Catch IOException when failed to parse new fields.

When parsing from a XML file written by previous code, the new fields
will get an IOException, catch the IOException and let the new fields
to have default value.

Change-Id: I2b2a494bf17aaf1383a6cc469d9c2addd6cac9c3
Fix: 119822293
Test: test com.google.android.gts.backup.UsageStatsRestoreHostSideTest#testUsageStatsRestore
parent 9d7964b9
Loading
Loading
Loading
Loading
+27 −5
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ import android.app.usage.UsageEvents;
import android.app.usage.UsageStats;
import android.content.res.Configuration;
import android.util.ArrayMap;
import android.util.Log;

import com.android.internal.util.XmlUtils;

@@ -89,11 +90,23 @@ final class UsageStatsXmlV1 {
        // Apply the offset to the beginTime to find the absolute time.
        stats.mLastTimeUsed = statsOut.beginTime + XmlUtils.readLongAttribute(
                parser, LAST_TIME_ACTIVE_ATTR);

        try {
            stats.mLastTimeForegroundServiceUsed = statsOut.beginTime + XmlUtils.readLongAttribute(
                    parser, LAST_TIME_SERVICE_USED_ATTR);
        } catch (IOException e) {
            Log.e(TAG, "Failed to parse mLastTimeForegroundServiceUsed", e);
        }

        stats.mTotalTimeInForeground = XmlUtils.readLongAttribute(parser, TOTAL_TIME_ACTIVE_ATTR);

        try {
            stats.mTotalTimeForegroundServiceUsed = XmlUtils.readLongAttribute(parser,
                TOTAL_TIME_SERVICE_USED_ATTR);
        } catch (IOException e) {
            Log.e(TAG, "Failed to parse mTotalTimeForegroundServiceUsed", e);
        }

        stats.mLastEvent = XmlUtils.readIntAttribute(parser, LAST_EVENT_ATTR);
        stats.mAppLaunchCount = XmlUtils.readIntAttribute(parser, APP_LAUNCH_COUNT_ATTR,
                0);
@@ -350,8 +363,17 @@ final class UsageStatsXmlV1 {
        }

        statsOut.endTime = statsOut.beginTime + XmlUtils.readLongAttribute(parser, END_TIME_ATTR);
        try {
            statsOut.majorVersion = XmlUtils.readIntAttribute(parser, MAJOR_VERSION_ATTR);
        } catch (IOException e) {
            Log.e(TAG, "Failed to parse majorVersion", e);
        }

        try {
            statsOut.minorVersion = XmlUtils.readIntAttribute(parser, MINOR_VERSION_ATTR);
        } catch (IOException e) {
            Log.e(TAG, "Failed to parse minorVersion", e);
        }

        int eventCode;
        int outerDepth = parser.getDepth();