Skip to content
This repository was archived by the owner on Jan 1, 2019. It is now read-only.

Commit 305438d

Browse files
committed
create a predictive apps provider and apply it to the all apps drawer
1 parent 526d4ff commit 305438d

File tree

3 files changed

+143
-6
lines changed

3 files changed

+143
-6
lines changed

app/src/main/java/com/android/launcher3/Launcher.java

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,13 @@
9999
import com.android.launcher3.DropTarget.DragObject;
100100
import com.android.launcher3.PagedView.PageSwitchListener;
101101
import com.android.launcher3.allapps.AllAppsContainerView;
102+
import com.android.launcher3.allapps.PredictiveAppsProvider;
102103
import com.android.launcher3.compat.AppWidgetManagerCompat;
103104
import com.android.launcher3.compat.LauncherActivityInfoCompat;
104105
import com.android.launcher3.compat.LauncherAppsCompat;
105106
import com.android.launcher3.compat.UserHandleCompat;
106107
import com.android.launcher3.compat.UserManagerCompat;
107108
import com.android.launcher3.model.WidgetsModel;
108-
import com.android.launcher3.testing.LauncherExtension;
109109
import com.android.launcher3.util.ComponentKey;
110110
import com.android.launcher3.util.LongArrayMap;
111111
import com.android.launcher3.util.Thunk;
@@ -431,6 +431,8 @@ protected void onCreate(Bundle savedInstanceState) {
431431
.build());
432432
}
433433

434+
predictiveAppsProvider = new PredictiveAppsProvider(this);
435+
434436
if (mLauncherCallbacks != null) {
435437
mLauncherCallbacks.preOnCreate();
436438
}
@@ -1071,6 +1073,8 @@ protected void onResume() {
10711073
if (mLauncherCallbacks != null) {
10721074
mLauncherCallbacks.onResume();
10731075
}
1076+
1077+
tryAndUpdatePredictedApps();
10741078
}
10751079

10761080
@Override
@@ -2951,10 +2955,12 @@ private boolean startActivity(View v, Intent intent, Object tag) {
29512955
}
29522956
try {
29532957
success = startActivity(v, intent, tag);
2958+
predictiveAppsProvider.updateComponentCount(intent.getComponent());
29542959
} catch (ActivityNotFoundException e) {
29552960
Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
29562961
Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
29572962
}
2963+
29582964
return success;
29592965
}
29602966

@@ -3461,11 +3467,16 @@ void exitSpringLoadedDragMode() {
34613467
* resumed.
34623468
*/
34633469
private void tryAndUpdatePredictedApps() {
3470+
List<ComponentKey> apps;
34643471
if (mLauncherCallbacks != null) {
3465-
List<ComponentKey> apps = mLauncherCallbacks.getPredictedApps();
3466-
if (apps != null) {
3467-
mAppsView.setPredictedApps(apps);
3468-
}
3472+
apps = mLauncherCallbacks.getPredictedApps();
3473+
} else {
3474+
apps = predictiveAppsProvider.getPredictions();
3475+
predictiveAppsProvider.updateTopPredictedApps();
3476+
}
3477+
3478+
if (apps != null) {
3479+
mAppsView.setPredictedApps(apps);
34693480
}
34703481
}
34713482

@@ -4762,9 +4773,11 @@ public Void doInBackground(Void ... args) {
47624773
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
47634774
}
47644775
}
4776+
4777+
private PredictiveAppsProvider predictiveAppsProvider;
47654778
}
47664779

47674780
interface DebugIntents {
47684781
static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
47694782
static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
4770-
}
4783+
}

app/src/main/java/com/android/launcher3/allapps/AlphabeticalAppsList.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package com.android.launcher3.allapps;
1717

18+
import android.content.ComponentName;
1819
import android.content.Context;
1920
import android.support.v7.widget.RecyclerView;
2021
import android.util.Log;
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package com.android.launcher3.allapps;
2+
3+
4+
import android.content.ComponentName;
5+
import android.content.Context;
6+
import android.content.SharedPreferences;
7+
import android.preference.PreferenceManager;
8+
import android.util.Log;
9+
10+
import com.android.launcher3.compat.UserHandleCompat;
11+
import com.android.launcher3.util.ComponentKey;
12+
13+
import java.util.ArrayList;
14+
import java.util.Collections;
15+
import java.util.Comparator;
16+
import java.util.HashSet;
17+
import java.util.List;
18+
import java.util.Set;
19+
20+
public class PredictiveAppsProvider {
21+
private static final int NUM_PREDICTIVE_APPS_TO_HOLD = 9; // since we can't have more than 9 columns
22+
23+
private static final String PREDICTIVE_APPS_KEY = "predictive_apps";
24+
private static final String TOP_PREDICTIVE_APPS_KEY = "top_predictive_apps";
25+
26+
private SharedPreferences sharedPreferences;
27+
private Context context;
28+
29+
public PredictiveAppsProvider(Context context) {
30+
this.context = context;
31+
this.sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
32+
}
33+
34+
public void updateComponentCount(ComponentName component) {
35+
String key = buildComponentString(component);
36+
long current = sharedPreferences.getLong(key, 0);
37+
38+
sharedPreferences.edit().putLong(key, current + 1).commit();
39+
40+
// ensure that the set of predictive apps contains this one
41+
Set<String> predictiveApps =
42+
sharedPreferences.getStringSet(PREDICTIVE_APPS_KEY, new HashSet<String>());
43+
if (!predictiveApps.contains(key)) {
44+
predictiveApps.add(key);
45+
sharedPreferences.edit().putStringSet(PREDICTIVE_APPS_KEY, predictiveApps).commit();
46+
}
47+
}
48+
49+
public void updateTopPredictedApps() {
50+
new Thread(new Runnable() {
51+
@Override
52+
public void run() {
53+
List< PredictedApp > allPredictions = new ArrayList<>();
54+
Set<String> predictiveAppsSet =
55+
sharedPreferences.getStringSet(PREDICTIVE_APPS_KEY, new HashSet<String>());
56+
57+
for (String s : predictiveAppsSet) {
58+
allPredictions.add(new PredictedApp(buildComponentFromString(s),
59+
sharedPreferences.getLong(s, 0)));
60+
}
61+
62+
Collections.sort(allPredictions, new Comparator<PredictedApp>() {
63+
public int compare(PredictedApp result1, PredictedApp result2) {
64+
return Long.valueOf(result2.count).compareTo(Long.valueOf(result1.count));
65+
}
66+
});
67+
68+
if (allPredictions.size() > NUM_PREDICTIVE_APPS_TO_HOLD) {
69+
allPredictions = allPredictions.subList(0, NUM_PREDICTIVE_APPS_TO_HOLD);
70+
}
71+
72+
sharedPreferences.edit().putString(TOP_PREDICTIVE_APPS_KEY, buildStringFromAppList(allPredictions)).commit();
73+
}
74+
}).start();
75+
}
76+
77+
public List<ComponentKey> getPredictions() {
78+
String[] topPredictions = sharedPreferences.getString(TOP_PREDICTIVE_APPS_KEY, "").split(" ");
79+
List<ComponentKey> keys = new ArrayList<>();
80+
81+
for (int i = 0; i < topPredictions.length - 1; i++) {
82+
keys.add(buildComponentKey(topPredictions[i] + " " + topPredictions[i + 1]));
83+
}
84+
85+
return keys;
86+
}
87+
88+
private String buildStringFromAppList(List<PredictedApp> apps) {
89+
String string = "";
90+
for (PredictedApp app : apps) {
91+
string += buildComponentString(app.component) + " ";
92+
}
93+
94+
return string.substring(0, string.length() - 1);
95+
}
96+
97+
private String buildComponentString(ComponentName component) {
98+
return component.getPackageName() + " " + component.getClassName();
99+
}
100+
101+
private ComponentName buildComponentFromString(String key) {
102+
String[] arr = key.split(" ");
103+
return new ComponentName(arr[0], arr[1]);
104+
}
105+
106+
private ComponentKey buildComponentKey(String key) {
107+
return buildComponentKey(buildComponentFromString(key));
108+
}
109+
110+
private ComponentKey buildComponentKey(ComponentName component) {
111+
return new ComponentKey(component, UserHandleCompat.myUserHandle());
112+
}
113+
114+
private class PredictedApp {
115+
public ComponentName component;
116+
public long count;
117+
118+
public PredictedApp(ComponentName component, long count) {
119+
this.component = component;
120+
this.count = count;
121+
}
122+
}
123+
}

0 commit comments

Comments
 (0)