Loading apex/jobscheduler/service/java/com/android/server/usage/AppIdleHistory.java +7 −2 Original line number Diff line number Diff line Loading @@ -578,7 +578,7 @@ public class AppIdleHistory { } } } catch (IOException | XmlPullParserException e) { Slog.e(TAG, "Unable to read app idle file for user " + userId); Slog.e(TAG, "Unable to read app idle file for user " + userId, e); } finally { IoUtils.closeQuietly(fis); } Loading Loading @@ -608,6 +608,11 @@ public class AppIdleHistory { final int N = userHistory.size(); for (int i = 0; i < N; i++) { String packageName = userHistory.keyAt(i); // Skip any unexpected null package names if (packageName == null) { Slog.w(TAG, "Skipping App Idle write for unexpected null package"); continue; } AppUsageHistory history = userHistory.valueAt(i); xml.startTag(null, TAG_PACKAGE); xml.attribute(null, ATTR_NAME, packageName); Loading Loading @@ -641,7 +646,7 @@ public class AppIdleHistory { appIdleFile.finishWrite(fos); } catch (Exception e) { appIdleFile.failWrite(fos); Slog.e(TAG, "Error writing app idle file for user " + userId); Slog.e(TAG, "Error writing app idle file for user " + userId, e); } } Loading services/tests/servicestests/src/com/android/server/usage/AppIdleHistoryTests.java +17 −1 Original line number Diff line number Diff line Loading @@ -28,7 +28,6 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import android.app.usage.UsageStatsManager; import android.os.FileUtils; import android.test.AndroidTestCase; Loading Loading @@ -150,4 +149,21 @@ public class AppIdleHistoryTests extends AndroidTestCase { aih = new AppIdleHistory(mStorageDir, 5000); assertEquals(REASON_MAIN_TIMEOUT, aih.getAppStandbyReason(PACKAGE_1, USER_ID, 5000)); } public void testNullPackage() throws Exception { AppIdleHistory aih = new AppIdleHistory(mStorageDir, 1000); // Report usage of a package aih.reportUsage(PACKAGE_1, USER_ID, STANDBY_BUCKET_ACTIVE, REASON_SUB_USAGE_MOVE_TO_FOREGROUND, 2000, 0); // "Accidentally" report usage against a null named package aih.reportUsage(null, USER_ID, STANDBY_BUCKET_ACTIVE, REASON_SUB_USAGE_MOVE_TO_FOREGROUND, 2000, 0); // Persist data aih.writeAppIdleTimes(USER_ID); // Recover data from disk aih = new AppIdleHistory(mStorageDir, 5000); // Verify data is intact assertEquals(REASON_MAIN_USAGE | REASON_SUB_USAGE_MOVE_TO_FOREGROUND, aih.getAppStandbyReason(PACKAGE_1, USER_ID, 3000)); } } No newline at end of file Loading
apex/jobscheduler/service/java/com/android/server/usage/AppIdleHistory.java +7 −2 Original line number Diff line number Diff line Loading @@ -578,7 +578,7 @@ public class AppIdleHistory { } } } catch (IOException | XmlPullParserException e) { Slog.e(TAG, "Unable to read app idle file for user " + userId); Slog.e(TAG, "Unable to read app idle file for user " + userId, e); } finally { IoUtils.closeQuietly(fis); } Loading Loading @@ -608,6 +608,11 @@ public class AppIdleHistory { final int N = userHistory.size(); for (int i = 0; i < N; i++) { String packageName = userHistory.keyAt(i); // Skip any unexpected null package names if (packageName == null) { Slog.w(TAG, "Skipping App Idle write for unexpected null package"); continue; } AppUsageHistory history = userHistory.valueAt(i); xml.startTag(null, TAG_PACKAGE); xml.attribute(null, ATTR_NAME, packageName); Loading Loading @@ -641,7 +646,7 @@ public class AppIdleHistory { appIdleFile.finishWrite(fos); } catch (Exception e) { appIdleFile.failWrite(fos); Slog.e(TAG, "Error writing app idle file for user " + userId); Slog.e(TAG, "Error writing app idle file for user " + userId, e); } } Loading
services/tests/servicestests/src/com/android/server/usage/AppIdleHistoryTests.java +17 −1 Original line number Diff line number Diff line Loading @@ -28,7 +28,6 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import android.app.usage.UsageStatsManager; import android.os.FileUtils; import android.test.AndroidTestCase; Loading Loading @@ -150,4 +149,21 @@ public class AppIdleHistoryTests extends AndroidTestCase { aih = new AppIdleHistory(mStorageDir, 5000); assertEquals(REASON_MAIN_TIMEOUT, aih.getAppStandbyReason(PACKAGE_1, USER_ID, 5000)); } public void testNullPackage() throws Exception { AppIdleHistory aih = new AppIdleHistory(mStorageDir, 1000); // Report usage of a package aih.reportUsage(PACKAGE_1, USER_ID, STANDBY_BUCKET_ACTIVE, REASON_SUB_USAGE_MOVE_TO_FOREGROUND, 2000, 0); // "Accidentally" report usage against a null named package aih.reportUsage(null, USER_ID, STANDBY_BUCKET_ACTIVE, REASON_SUB_USAGE_MOVE_TO_FOREGROUND, 2000, 0); // Persist data aih.writeAppIdleTimes(USER_ID); // Recover data from disk aih = new AppIdleHistory(mStorageDir, 5000); // Verify data is intact assertEquals(REASON_MAIN_USAGE | REASON_SUB_USAGE_MOVE_TO_FOREGROUND, aih.getAppStandbyReason(PACKAGE_1, USER_ID, 3000)); } } No newline at end of file