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

Commit 09a65601 authored by Adam Powell's avatar Adam Powell
Browse files

Sort ResolverActivity items based on UsageStats

Sort targets by the total amount of time the user has spent with their
containing package in the foreground.

Bug 15694906

Change-Id: I63c956424f78eb22911517674dfefd96901d19f8
parent e9111d30
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -134,6 +134,7 @@ public final class UsageStats implements Parcelable {
        mLastTimeSaved = stats.mLastTimeSaved;

        final int pkgCount = stats.mPackageStats.size();
        mPackageStats.ensureCapacity(pkgCount);
        for (int i = 0; i < pkgCount; i++) {
            PackageUsageStats pkgStats = stats.mPackageStats.valueAt(i);
            mPackageStats.append(stats.mPackageStats.keyAt(i), new PackageUsageStats(pkgStats));
+68 −8
Original line number Diff line number Diff line
@@ -17,6 +17,9 @@
package com.android.internal.app;

import android.app.Activity;
import android.app.usage.PackageUsageStats;
import android.app.usage.UsageStats;
import android.app.usage.UsageStatsManager;
import android.os.AsyncTask;
import android.widget.AbsListView;
import android.widget.GridView;
@@ -54,12 +57,13 @@ import android.widget.ListView;
import android.widget.TextView;
import com.android.internal.widget.ResolverDrawerLayout;

import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
@@ -69,7 +73,7 @@ import java.util.Set;
 */
public class ResolverActivity extends Activity implements AdapterView.OnItemClickListener {
    private static final String TAG = "ResolverActivity";
    private static final boolean DEBUG = true;
    private static final boolean DEBUG = false;

    private int mLaunchedFromUid;
    private ResolveListAdapter mAdapter;
@@ -84,6 +88,10 @@ public class ResolverActivity extends Activity implements AdapterView.OnItemClic
    private int mMaxColumns;
    private int mLastSelected = ListView.INVALID_POSITION;

    private UsageStatsManager mUsm;
    private UsageStats mStats;
    private static final long USAGE_STATS_PERIOD = 1000 * 60 * 60 * 24 * 14;

    private boolean mRegistered;
    private final PackageMonitor mPackageMonitor = new PackageMonitor() {
        @Override public void onSomePackagesChanged() {
@@ -170,7 +178,7 @@ public class ResolverActivity extends Activity implements AdapterView.OnItemClic
    protected void onCreate(Bundle savedInstanceState, Intent intent,
            CharSequence title, Intent[] initialIntents,
            List<ResolveInfo> rList, boolean alwaysUseOption) {
        onCreate(savedInstanceState, intent, title, initialIntents, rList, alwaysUseOption);
        onCreate(savedInstanceState, intent, title, 0, initialIntents, rList, alwaysUseOption);
    }

    protected void onCreate(Bundle savedInstanceState, Intent intent,
@@ -185,6 +193,12 @@ public class ResolverActivity extends Activity implements AdapterView.OnItemClic
            mLaunchedFromUid = -1;
        }
        mPm = getPackageManager();
        mUsm = (UsageStatsManager) getSystemService(Context.USAGE_STATS_SERVICE);

        final long sinceTime = System.currentTimeMillis() - USAGE_STATS_PERIOD;
        mStats = mUsm.getRecentStatsSince(sinceTime);
        Log.d(TAG, "sinceTime=" + sinceTime);

        mMaxColumns = getResources().getInteger(R.integer.config_maxResolverActivityColumns);

        mPackageMonitor.register(this, getMainLooper(), false);
@@ -672,7 +686,7 @@ public class ResolverActivity extends Activity implements AdapterView.OnItemClic
                for (int i=1; i<N; i++) {
                    ResolveInfo ri = currentResolveList.get(i);
                    if (DEBUG) Log.v(
                        "ResolveListActivity",
                        TAG,
                        r0.activityInfo.name + "=" +
                        r0.priority + "/" + r0.isDefault + " vs " +
                        ri.activityInfo.name + "=" +
@@ -689,8 +703,8 @@ public class ResolverActivity extends Activity implements AdapterView.OnItemClic
                    }
                }
                if (N > 1) {
                    ResolveInfo.DisplayNameComparator rComparator =
                            new ResolveInfo.DisplayNameComparator(mPm);
                    Comparator<ResolveInfo> rComparator =
                            new ResolverComparator(ResolverActivity.this);
                    Collections.sort(currentResolveList, rComparator);
                }
                // First put the initial items at the top.
@@ -703,8 +717,7 @@ public class ResolverActivity extends Activity implements AdapterView.OnItemClic
                        ActivityInfo ai = ii.resolveActivityInfo(
                                getPackageManager(), 0);
                        if (ai == null) {
                            Log.w("ResolverActivity", "No activity found for "
                                    + ii);
                            Log.w(TAG, "No activity found for " + ii);
                            continue;
                        }
                        ResolveInfo ri = new ResolveInfo();
@@ -939,5 +952,52 @@ public class ResolverActivity extends Activity implements AdapterView.OnItemClic
            mTargetView.setImageDrawable(info.displayIcon);
        }
    }

    class ResolverComparator implements Comparator<ResolveInfo> {
        private final Collator mCollator;

        public ResolverComparator(Context context) {
            mCollator = Collator.getInstance(context.getResources().getConfiguration().locale);
        }

        @Override
        public int compare(ResolveInfo lhs, ResolveInfo rhs) {
            // We want to put the one targeted to another user at the end of the dialog.
            if (lhs.targetUserId != UserHandle.USER_CURRENT) {
                return 1;
            }
            if (lhs.targetUserId != UserHandle.USER_CURRENT) {
                return -1;
            }

            if (mStats != null) {
                final long timeDiff =
                        getPackageTimeSpent(rhs.activityInfo.packageName) -
                        getPackageTimeSpent(lhs.activityInfo.packageName);

                if (timeDiff != 0) {
                    return timeDiff > 0 ? 1 : -1;
                }
            }

            CharSequence  sa = lhs.loadLabel(mPm);
            if (sa == null) sa = lhs.activityInfo.name;
            CharSequence  sb = rhs.loadLabel(mPm);
            if (sb == null) sb = rhs.activityInfo.name;

            return mCollator.compare(sa.toString(), sb.toString());
        }

        private long getPackageTimeSpent(String packageName) {
            if (mStats != null) {
                final PackageUsageStats stats = mStats.getPackage(packageName);
                if (stats != null) {
                    return stats.getTotalTimeSpent();
                }

            }
            return 0;
        }
    }
}
+4 −3
Original line number Diff line number Diff line
@@ -2462,6 +2462,7 @@
        android:label="@string/permlab_pkgUsageStats"
        android:description="@string/permdesc_pkgUsageStats"
        android:protectionLevel="signature|system" />
    <uses-permission android:name="android.permission.PACKAGE_USAGE_STATS" />

    <!-- @SystemApi Allows an application to collect battery statistics -->
    <permission android:name="android.permission.BATTERY_STATS"
@@ -2796,12 +2797,12 @@
                 android:icon="@drawable/ic_launcher_android"
                 android:supportsRtl="true">
        <activity android:name="com.android.internal.app.ChooserActivity"
                android:theme="@style/Theme.Holo.Dialog.Alert"
                android:theme="@style/Theme.DeviceDefault.Resolver"
                android:finishOnCloseSystemDialogs="true"
                android:excludeFromRecents="true"
                android:multiprocess="true"
                android:documentLaunchMode="never"
                android:relinquishTaskIdentity="true">
                android:relinquishTaskIdentity="true"
                android:process=":ui">
            <intent-filter>
                <action android:name="android.intent.action.CHOOSER" />
                <category android:name="android.intent.category.DEFAULT" />
+2 −0
Original line number Diff line number Diff line
@@ -43,6 +43,7 @@
        <!-- Activity name -->
        <TextView android:id="@android:id/text1"
                  android:textAppearance="?android:attr/textAppearanceSmall"
                  android:fontFamily="sans-serif-condensed"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:gravity="center"
@@ -51,6 +52,7 @@
        <!-- Extended activity info to distinguish between duplicate activity names -->
        <TextView android:id="@android:id/text2"
                  android:textAppearance="?android:attr/textAppearanceSmall"
                  android:fontFamily="sans-serif-condensed"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:gravity="center"