• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.launcher3.compat;
18 
19 import android.content.Context;
20 import android.content.pm.PackageInstaller;
21 import android.content.pm.PackageInstaller.SessionCallback;
22 import android.content.pm.PackageInstaller.SessionInfo;
23 import android.os.Handler;
24 import android.os.Process;
25 import android.os.UserHandle;
26 import android.util.SparseArray;
27 
28 import com.android.launcher3.IconCache;
29 import com.android.launcher3.LauncherAppState;
30 import com.android.launcher3.LauncherModel;
31 import com.android.launcher3.util.Thunk;
32 
33 import java.util.HashMap;
34 
35 public class PackageInstallerCompatVL extends PackageInstallerCompat {
36 
37     @Thunk final SparseArray<String> mActiveSessions = new SparseArray<>();
38 
39     @Thunk final PackageInstaller mInstaller;
40     private final IconCache mCache;
41     private final Handler mWorker;
42 
PackageInstallerCompatVL(Context context)43     PackageInstallerCompatVL(Context context) {
44         mInstaller = context.getPackageManager().getPackageInstaller();
45         mCache = LauncherAppState.getInstance(context).getIconCache();
46         mWorker = new Handler(LauncherModel.getWorkerLooper());
47 
48         mInstaller.registerSessionCallback(mCallback, mWorker);
49     }
50 
51     @Override
updateAndGetActiveSessionCache()52     public HashMap<String, Integer> updateAndGetActiveSessionCache() {
53         HashMap<String, Integer> activePackages = new HashMap<>();
54         UserHandle user = Process.myUserHandle();
55         for (SessionInfo info : mInstaller.getAllSessions()) {
56             addSessionInfoToCache(info, user);
57             if (info.getAppPackageName() != null) {
58                 activePackages.put(info.getAppPackageName(), (int) (info.getProgress() * 100));
59                 mActiveSessions.put(info.getSessionId(), info.getAppPackageName());
60             }
61         }
62         return activePackages;
63     }
64 
addSessionInfoToCache(SessionInfo info, UserHandle user)65     @Thunk void addSessionInfoToCache(SessionInfo info, UserHandle user) {
66         String packageName = info.getAppPackageName();
67         if (packageName != null) {
68             mCache.cachePackageInstallInfo(packageName, user, info.getAppIcon(),
69                     info.getAppLabel());
70         }
71     }
72 
73     @Override
onStop()74     public void onStop() {
75         mInstaller.unregisterSessionCallback(mCallback);
76     }
77 
sendUpdate(PackageInstallInfo info)78     @Thunk void sendUpdate(PackageInstallInfo info) {
79         LauncherAppState app = LauncherAppState.getInstanceNoCreate();
80         if (app != null) {
81             app.getModel().setPackageState(info);
82         }
83     }
84 
85     private final SessionCallback mCallback = new SessionCallback() {
86 
87         @Override
88         public void onCreated(int sessionId) {
89             pushSessionDisplayToLauncher(sessionId);
90         }
91 
92         @Override
93         public void onFinished(int sessionId, boolean success) {
94             // For a finished session, we can't get the session info. So use the
95             // packageName from our local cache.
96             String packageName = mActiveSessions.get(sessionId);
97             mActiveSessions.remove(sessionId);
98 
99             if (packageName != null) {
100                 sendUpdate(new PackageInstallInfo(packageName,
101                         success ? STATUS_INSTALLED : STATUS_FAILED, 0));
102             }
103         }
104 
105         @Override
106         public void onProgressChanged(int sessionId, float progress) {
107             SessionInfo session = mInstaller.getSessionInfo(sessionId);
108             if (session != null && session.getAppPackageName() != null) {
109                 sendUpdate(new PackageInstallInfo(session.getAppPackageName(),
110                         STATUS_INSTALLING,
111                         (int) (session.getProgress() * 100)));
112             }
113         }
114 
115         @Override
116         public void onActiveChanged(int sessionId, boolean active) { }
117 
118         @Override
119         public void onBadgingChanged(int sessionId) {
120             pushSessionDisplayToLauncher(sessionId);
121         }
122 
123         private void pushSessionDisplayToLauncher(int sessionId) {
124             SessionInfo session = mInstaller.getSessionInfo(sessionId);
125             if (session != null && session.getAppPackageName() != null) {
126                 addSessionInfoToCache(session, Process.myUserHandle());
127                 LauncherAppState app = LauncherAppState.getInstanceNoCreate();
128 
129                 if (app != null) {
130                     app.getModel().updateSessionDisplayInfo(session.getAppPackageName());
131                 }
132             }
133         }
134     };
135 }
136