summaryrefslogtreecommitdiff
path: root/src/com/android/apppredictionservice/PredictionService.java
blob: b25edebe1064485ee019a33a858b7a294c3d1cc9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.apppredictionservice;

import static android.os.Process.myUserHandle;
import static android.text.TextUtils.isEmpty;

import static java.util.Collections.emptyList;

import android.app.prediction.AppPredictionContext;
import android.app.prediction.AppPredictionSessionId;
import android.app.prediction.AppTarget;
import android.app.prediction.AppTargetEvent;
import android.app.prediction.AppTargetId;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.pm.LauncherActivityInfo;
import android.content.pm.LauncherApps;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.CancellationSignal;
import android.service.appprediction.AppPredictionService;
import android.util.Log;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Consumer;

/*
 * New plugin that replaces prediction driven Aiai APK in P
 * PredictionService simply populates the top row of the app
 * drawer with the 5 most recently used apps. Each time a new
 * app is launched, it is added to the left of the top row.
 * Duplicates are not added.
 */
public class PredictionService extends AppPredictionService {

    private static final String TAG = PredictionService.class.getSimpleName();

    private final Set<AppPredictionSessionId> activeLauncherSessions = new HashSet<>();

    private boolean mAppSuggestionsEnabled = true;

    public static final String MY_PREF = "mypref";

    private final List<AppTarget> predictionList = new ArrayList<>(5);

    private final List<String> appNames = new ArrayList<>(5);
    private final String[] appNameKeys = new String[] {
            "first", "second", "third", "fourth", "fifth" };

    SharedPreferences sharedPreferences;
    SharedPreferences.Editor editor;

    @Override
    public void onCreate() {
        super.onCreate();

        Intent calendarIntent = new Intent(Intent.ACTION_MAIN);
        calendarIntent.addCategory(Intent.CATEGORY_APP_CALENDAR);

        Intent galleryIntent = new Intent(Intent.ACTION_MAIN);
        galleryIntent.addCategory(Intent.CATEGORY_APP_GALLERY);

        Intent mapsIntent = new Intent(Intent.ACTION_MAIN);
        mapsIntent.addCategory(Intent.CATEGORY_APP_MAPS);

        Intent emailIntent = new Intent(Intent.ACTION_MAIN);
        emailIntent.addCategory(Intent.CATEGORY_APP_EMAIL);

        Intent browserIntent = new Intent(Intent.ACTION_MAIN);
        browserIntent.addCategory(Intent.CATEGORY_APP_BROWSER);

        String[] DEFAULT_PACKAGES = new String[] {
              getDefaultSystemHandlerActivityPackageName(calendarIntent),
              getDefaultSystemHandlerActivityPackageName(galleryIntent),
              getDefaultSystemHandlerActivityPackageName(mapsIntent),
              getDefaultSystemHandlerActivityPackageName(emailIntent),
              getDefaultSystemHandlerActivityPackageName(browserIntent),
        };

        Log.d(TAG, "AppPredictionService onCreate");
        this.sharedPreferences = getSharedPreferences(MY_PREF, Context.MODE_PRIVATE);
        this.editor = sharedPreferences.edit();

        if (sharedPreferences.getString(appNameKeys[0], "").isEmpty()) {
            // fill the list with defaults if first one is null when devices powers up for the first time
            for (int i = 0; i < appNameKeys.length; i++) {
                editor.putString(appNameKeys[i],
                        getLauncherComponent(DEFAULT_PACKAGES[i]).flattenToShortString());
            }
            this.editor.apply();
        }

        for (int i = 0; i < appNameKeys.length; i++) {
            String appName = sharedPreferences.getString(appNameKeys[i], "");
            ComponentName cn = ComponentName.unflattenFromString(appName);
            AppTarget target = new AppTarget.Builder(
                    new AppTargetId(Integer.toString(i + 1)), cn.getPackageName(), myUserHandle())
                    .setClassName(cn.getClassName())
                    .build();
            appNames.add(appName);
            predictionList.add(target);
        }
        postPredictionUpdateToAllClients();
    }

    private ComponentName getLauncherComponent(String packageName) {
        List<LauncherActivityInfo> infos = getSystemService(LauncherApps.class)
                .getActivityList(packageName, myUserHandle());
        if (infos.isEmpty()) {
            return new ComponentName(packageName, "#");
        } else {
            return infos.get(0).getComponentName();
        }
    }

    private void postPredictionUpdate(AppPredictionSessionId sessionId) {
        updatePredictions(sessionId, mAppSuggestionsEnabled ? predictionList : emptyList());
    }

    private void postPredictionUpdateToAllClients() {
        for (AppPredictionSessionId session : activeLauncherSessions) {
            postPredictionUpdate(session);
        }
    }

    @Override
    public void onCreatePredictionSession(
            AppPredictionContext context, AppPredictionSessionId sessionId) {
        Log.d(TAG, "onCreatePredictionSession");

        if (context.getUiSurface().equals("home") || context.getUiSurface().equals("overview")) {
            activeLauncherSessions.add(sessionId);
            postPredictionUpdate(sessionId);
        }
    }

    @Override
    public void onAppTargetEvent(AppPredictionSessionId sessionId, AppTargetEvent event) {

        if (!activeLauncherSessions.contains(sessionId)) {
            return;
        }

        boolean found = false;
        Log.d(TAG, "onAppTargetEvent");

        AppTarget target = event.getTarget();
        if (target == null || isEmpty(target.getPackageName()) || isEmpty(target.getClassName())) {
            return;
        }
        String mostRecentComponent = new ComponentName(
                target.getPackageName(), target.getClassName()).flattenToString();

        // Check if packageName already exists in existing list of appNames
        for (String packageName:appNames) {
            if (packageName.contains(target.getPackageName())) {
                found = true;
                break;
            }
        }

        if (!found) {
            appNames.remove(appNames.size() - 1);
            appNames.add(0, mostRecentComponent);

            for (int i = 0; i < appNameKeys.length; i++) {
                editor.putString(appNameKeys[i], appNames.get(i));
            }
            editor.apply();

            predictionList.remove(predictionList.size() - 1);
            predictionList.add(0, event.getTarget());

            Log.d(TAG, "onAppTargetEvent:: update predictions");
            postPredictionUpdateToAllClients();
        }
    }

    @Override
    public void onLaunchLocationShown(
            AppPredictionSessionId sessionId, String launchLocation, List<AppTargetId> targetIds) {
        Log.d(TAG, "onLaunchLocationShown");
    }

    @Override
    public void onSortAppTargets(
            AppPredictionSessionId sessionId,
            List<AppTarget> targets,
            CancellationSignal cancellationSignal,
            Consumer<List<AppTarget>> callback) {

        Log.d(TAG, "onSortAppTargets");
        if (!activeLauncherSessions.contains(sessionId)) {
            callback.accept(emptyList());
        } else {
            // No-op
            callback.accept(targets);
        }
    }

    @Override
    public void onRequestPredictionUpdate(AppPredictionSessionId sessionId) {
        Log.d(TAG, "onRequestPredictionUpdate");

        if (!activeLauncherSessions.contains(sessionId)) {
            updatePredictions(sessionId, emptyList());
        } else {
            postPredictionUpdate(sessionId);
            Log.d(TAG, "update predictions");
        }
    }

    @Override
    public void onDestroyPredictionSession(AppPredictionSessionId sessionId) {
        Log.d(TAG, "onDestroyPredictionSession");
        activeLauncherSessions.remove(sessionId);
    }

    @Override
    public void onStartPredictionUpdates() {
        Log.d(TAG, "onStartPredictionUpdates");
    }

    @Override
    public void onStopPredictionUpdates() {
        Log.d(TAG, "onStopPredictionUpdates");
    }

    public void setAppSuggestionsEnabled(boolean enabled) {
        mAppSuggestionsEnabled = enabled;
        postPredictionUpdateToAllClients();
    }

    private String getDefaultSystemHandlerActivityPackageName(Intent intent) {
        return getDefaultSystemHandlerActivityPackageName(intent, 0);
    }

    private String getDefaultSystemHandlerActivityPackageName(Intent intent, int flags) {
        ResolveInfo handler = getPackageManager().resolveActivity(intent, flags | PackageManager.MATCH_SYSTEM_ONLY);
        if (handler == null) {
            return null;
        }
        if ((handler.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
            return handler.activityInfo.packageName;
        }
        return null;
    }
}