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

Commit 55717a61 authored by Amith Yamasani's avatar Amith Yamasani
Browse files

Mark apps as not-idle at least once

On a fresh boot or update to M, mark existing apps
as being used if there is no existing entry in the
usage stats. On subsequent OTAs, make sure that at
least the new system apps are marked as used.

Reduce idle threshold to 1 day.

Bug: 20066058
Change-Id: I9a273c051d04432877bacd381c85bf6e721c1a85
parent 092f038d
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -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();
+43 −3
Original line number Diff line number Diff line
@@ -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;

@@ -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;
@@ -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[] {
@@ -73,7 +76,7 @@ class UsageStatsDatabase {
                }
            }

            checkVersionLocked();
            checkVersionAndBuildLocked();
            indexFilesLocked();

            // Delete files that are in the future.
@@ -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;
        }
@@ -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);
@@ -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,
+1 −1
Original line number Diff line number Diff line
@@ -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>
+34 −0
Original line number Diff line number Diff line
@@ -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;
@@ -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();
@@ -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) {
@@ -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) {