|
| 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