Loading services/core/java/com/android/server/job/controllers/AppIdleController.java +1 −1 Original line number Diff line number Diff line Loading @@ -42,7 +42,7 @@ public class AppIdleController extends StateController implements UsageStatsManagerInternal.AppIdleStateChangeListener { private static final String LOG_TAG = "AppIdleController"; private static final boolean DEBUG = true; private static final boolean DEBUG = false; // Singleton factory private static Object sCreationLock = new Object(); Loading services/usage/java/com/android/server/usage/UsageStatsDatabase.java +43 −3 Original line number Diff line number Diff line Loading @@ -18,6 +18,7 @@ package com.android.server.usage; import android.app.usage.TimeSparseArray; import android.app.usage.UsageStatsManager; import android.os.Build; import android.util.AtomicFile; import android.util.Slog; Loading @@ -35,7 +36,7 @@ import java.util.List; * Provides an interface to query for UsageStat data from an XML database. */ class UsageStatsDatabase { private static final int CURRENT_VERSION = 2; private static final int CURRENT_VERSION = 3; private static final String TAG = "UsageStatsDatabase"; private static final boolean DEBUG = UsageStatsService.DEBUG; Loading @@ -47,6 +48,8 @@ class UsageStatsDatabase { private final TimeSparseArray<AtomicFile>[] mSortedStatFiles; private final UnixCalendar mCal; private final File mVersionFile; private boolean mFirstUpdate; private boolean mNewUpdate; public UsageStatsDatabase(File dir) { mIntervalDirs = new File[] { Loading @@ -73,7 +76,7 @@ class UsageStatsDatabase { } } checkVersionLocked(); checkVersionAndBuildLocked(); indexFilesLocked(); // Delete files that are in the future. Loading Loading @@ -194,10 +197,35 @@ class UsageStatsDatabase { } } private void checkVersionLocked() { /** * Is this the first update to the system from L to M? */ boolean isFirstUpdate() { return mFirstUpdate; } /** * Is this a system update since we started tracking build fingerprint in the version file? */ boolean isNewUpdate() { return mNewUpdate; } private void checkVersionAndBuildLocked() { int version; String buildFingerprint; String currentFingerprint = getBuildFingerprint(); mFirstUpdate = true; mNewUpdate = true; try (BufferedReader reader = new BufferedReader(new FileReader(mVersionFile))) { version = Integer.parseInt(reader.readLine()); buildFingerprint = reader.readLine(); if (buildFingerprint != null) { mFirstUpdate = false; } if (currentFingerprint.equals(buildFingerprint)) { mNewUpdate = false; } } catch (NumberFormatException | IOException e) { version = 0; } Loading @@ -205,9 +233,15 @@ class UsageStatsDatabase { if (version != CURRENT_VERSION) { Slog.i(TAG, "Upgrading from version " + version + " to " + CURRENT_VERSION); doUpgradeLocked(version); } if (version != CURRENT_VERSION || mNewUpdate) { try (BufferedWriter writer = new BufferedWriter(new FileWriter(mVersionFile))) { writer.write(Integer.toString(CURRENT_VERSION)); writer.write("\n"); writer.write(currentFingerprint); writer.write("\n"); writer.flush(); } catch (IOException e) { Slog.e(TAG, "Failed to write new version"); throw new RuntimeException(e); Loading @@ -215,6 +249,12 @@ class UsageStatsDatabase { } } private String getBuildFingerprint() { return Build.VERSION.RELEASE + ";" + Build.VERSION.CODENAME + ";" + Build.VERSION.INCREMENTAL; } private void doUpgradeLocked(int thisVersion) { if (thisVersion < 2) { // Delete all files if we are version 0. This is a pre-release version, Loading services/usage/java/com/android/server/usage/UsageStatsService.java +1 −1 Original line number Diff line number Diff line Loading @@ -92,7 +92,7 @@ public class UsageStatsService extends SystemService implements long mRealTimeSnapshot; long mSystemTimeSnapshot; private static final long DEFAULT_APP_IDLE_THRESHOLD_MILLIS = 3L * 24 * 60 * 60 * 1000; //3 days private static final long DEFAULT_APP_IDLE_THRESHOLD_MILLIS = 1L * 24 * 60 * 60 * 1000; // 1 day private long mAppIdleDurationMillis; private ArrayList<UsageStatsManagerInternal.AppIdleStateChangeListener> Loading services/usage/java/com/android/server/usage/UserUsageStatsService.java +34 −0 Original line number Diff line number Diff line Loading @@ -19,8 +19,11 @@ package com.android.server.usage; import android.app.usage.ConfigurationStats; import android.app.usage.TimeSparseArray; import android.app.usage.UsageEvents; import android.app.usage.UsageEvents.Event; import android.app.usage.UsageStats; import android.app.usage.UsageStatsManager; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.res.Configuration; import android.os.SystemClock; import android.content.Context; Loading Loading @@ -60,6 +63,7 @@ class UserUsageStatsService { private final UnixCalendar mDailyExpiryDate; private final StatsUpdatedListener mListener; private final String mLogPrefix; private final int mUserId; interface StatsUpdatedListener { void onStatsUpdated(); Loading @@ -73,6 +77,7 @@ class UserUsageStatsService { mCurrentStats = new IntervalStats[UsageStatsManager.INTERVAL_COUNT]; mListener = listener; mLogPrefix = "User[" + Integer.toString(userId) + "] "; mUserId = userId; } void init(final long currentTimeMillis) { Loading Loading @@ -128,6 +133,35 @@ class UserUsageStatsService { stat.updateConfigurationStats(null, stat.lastTimeSaved); } if (mDatabase.isNewUpdate()) { initializeDefaultsForApps(currentTimeMillis, mDatabase.isFirstUpdate()); } } /** * If any of the apps don't have a last-used entry, add one now. * @param currentTimeMillis the current time * @param firstUpdate if it is the first update, touch all installed apps, otherwise only * touch the system apps */ private void initializeDefaultsForApps(long currentTimeMillis, boolean firstUpdate) { PackageManager pm = mContext.getPackageManager(); List<PackageInfo> packages = pm.getInstalledPackages(0, mUserId); final int packageCount = packages.size(); for (int i = 0; i < packageCount; i++) { final PackageInfo pi = packages.get(i); String packageName = pi.packageName; if (pi.applicationInfo != null && (firstUpdate || pi.applicationInfo.isSystemApp()) && getLastPackageAccessTime(packageName) == -1) { for (IntervalStats stats : mCurrentStats) { stats.update(packageName, currentTimeMillis, Event.INTERACTION); mStatsChanged = true; } } } // Persist the new OTA-related access stats. persistActiveStats(); } void onTimeChanged(long oldTime, long newTime) { Loading Loading
services/core/java/com/android/server/job/controllers/AppIdleController.java +1 −1 Original line number Diff line number Diff line Loading @@ -42,7 +42,7 @@ public class AppIdleController extends StateController implements UsageStatsManagerInternal.AppIdleStateChangeListener { private static final String LOG_TAG = "AppIdleController"; private static final boolean DEBUG = true; private static final boolean DEBUG = false; // Singleton factory private static Object sCreationLock = new Object(); Loading
services/usage/java/com/android/server/usage/UsageStatsDatabase.java +43 −3 Original line number Diff line number Diff line Loading @@ -18,6 +18,7 @@ package com.android.server.usage; import android.app.usage.TimeSparseArray; import android.app.usage.UsageStatsManager; import android.os.Build; import android.util.AtomicFile; import android.util.Slog; Loading @@ -35,7 +36,7 @@ import java.util.List; * Provides an interface to query for UsageStat data from an XML database. */ class UsageStatsDatabase { private static final int CURRENT_VERSION = 2; private static final int CURRENT_VERSION = 3; private static final String TAG = "UsageStatsDatabase"; private static final boolean DEBUG = UsageStatsService.DEBUG; Loading @@ -47,6 +48,8 @@ class UsageStatsDatabase { private final TimeSparseArray<AtomicFile>[] mSortedStatFiles; private final UnixCalendar mCal; private final File mVersionFile; private boolean mFirstUpdate; private boolean mNewUpdate; public UsageStatsDatabase(File dir) { mIntervalDirs = new File[] { Loading @@ -73,7 +76,7 @@ class UsageStatsDatabase { } } checkVersionLocked(); checkVersionAndBuildLocked(); indexFilesLocked(); // Delete files that are in the future. Loading Loading @@ -194,10 +197,35 @@ class UsageStatsDatabase { } } private void checkVersionLocked() { /** * Is this the first update to the system from L to M? */ boolean isFirstUpdate() { return mFirstUpdate; } /** * Is this a system update since we started tracking build fingerprint in the version file? */ boolean isNewUpdate() { return mNewUpdate; } private void checkVersionAndBuildLocked() { int version; String buildFingerprint; String currentFingerprint = getBuildFingerprint(); mFirstUpdate = true; mNewUpdate = true; try (BufferedReader reader = new BufferedReader(new FileReader(mVersionFile))) { version = Integer.parseInt(reader.readLine()); buildFingerprint = reader.readLine(); if (buildFingerprint != null) { mFirstUpdate = false; } if (currentFingerprint.equals(buildFingerprint)) { mNewUpdate = false; } } catch (NumberFormatException | IOException e) { version = 0; } Loading @@ -205,9 +233,15 @@ class UsageStatsDatabase { if (version != CURRENT_VERSION) { Slog.i(TAG, "Upgrading from version " + version + " to " + CURRENT_VERSION); doUpgradeLocked(version); } if (version != CURRENT_VERSION || mNewUpdate) { try (BufferedWriter writer = new BufferedWriter(new FileWriter(mVersionFile))) { writer.write(Integer.toString(CURRENT_VERSION)); writer.write("\n"); writer.write(currentFingerprint); writer.write("\n"); writer.flush(); } catch (IOException e) { Slog.e(TAG, "Failed to write new version"); throw new RuntimeException(e); Loading @@ -215,6 +249,12 @@ class UsageStatsDatabase { } } private String getBuildFingerprint() { return Build.VERSION.RELEASE + ";" + Build.VERSION.CODENAME + ";" + Build.VERSION.INCREMENTAL; } private void doUpgradeLocked(int thisVersion) { if (thisVersion < 2) { // Delete all files if we are version 0. This is a pre-release version, Loading
services/usage/java/com/android/server/usage/UsageStatsService.java +1 −1 Original line number Diff line number Diff line Loading @@ -92,7 +92,7 @@ public class UsageStatsService extends SystemService implements long mRealTimeSnapshot; long mSystemTimeSnapshot; private static final long DEFAULT_APP_IDLE_THRESHOLD_MILLIS = 3L * 24 * 60 * 60 * 1000; //3 days private static final long DEFAULT_APP_IDLE_THRESHOLD_MILLIS = 1L * 24 * 60 * 60 * 1000; // 1 day private long mAppIdleDurationMillis; private ArrayList<UsageStatsManagerInternal.AppIdleStateChangeListener> Loading
services/usage/java/com/android/server/usage/UserUsageStatsService.java +34 −0 Original line number Diff line number Diff line Loading @@ -19,8 +19,11 @@ package com.android.server.usage; import android.app.usage.ConfigurationStats; import android.app.usage.TimeSparseArray; import android.app.usage.UsageEvents; import android.app.usage.UsageEvents.Event; import android.app.usage.UsageStats; import android.app.usage.UsageStatsManager; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.res.Configuration; import android.os.SystemClock; import android.content.Context; Loading Loading @@ -60,6 +63,7 @@ class UserUsageStatsService { private final UnixCalendar mDailyExpiryDate; private final StatsUpdatedListener mListener; private final String mLogPrefix; private final int mUserId; interface StatsUpdatedListener { void onStatsUpdated(); Loading @@ -73,6 +77,7 @@ class UserUsageStatsService { mCurrentStats = new IntervalStats[UsageStatsManager.INTERVAL_COUNT]; mListener = listener; mLogPrefix = "User[" + Integer.toString(userId) + "] "; mUserId = userId; } void init(final long currentTimeMillis) { Loading Loading @@ -128,6 +133,35 @@ class UserUsageStatsService { stat.updateConfigurationStats(null, stat.lastTimeSaved); } if (mDatabase.isNewUpdate()) { initializeDefaultsForApps(currentTimeMillis, mDatabase.isFirstUpdate()); } } /** * If any of the apps don't have a last-used entry, add one now. * @param currentTimeMillis the current time * @param firstUpdate if it is the first update, touch all installed apps, otherwise only * touch the system apps */ private void initializeDefaultsForApps(long currentTimeMillis, boolean firstUpdate) { PackageManager pm = mContext.getPackageManager(); List<PackageInfo> packages = pm.getInstalledPackages(0, mUserId); final int packageCount = packages.size(); for (int i = 0; i < packageCount; i++) { final PackageInfo pi = packages.get(i); String packageName = pi.packageName; if (pi.applicationInfo != null && (firstUpdate || pi.applicationInfo.isSystemApp()) && getLastPackageAccessTime(packageName) == -1) { for (IntervalStats stats : mCurrentStats) { stats.update(packageName, currentTimeMillis, Event.INTERACTION); mStatsChanged = true; } } } // Persist the new OTA-related access stats. persistActiveStats(); } void onTimeChanged(long oldTime, long newTime) { Loading