• 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.annotation.TargetApi;
20 import android.content.Context;
21 import android.content.pm.PackageInstaller;
22 import android.content.pm.PackageInstaller.SessionCallback;
23 import android.content.pm.PackageInstaller.SessionInfo;
24 import android.os.Build;
25 import android.os.Handler;
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 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
36 public class PackageInstallerCompatVL extends PackageInstallerCompat {
37 
38     @Thunk final SparseArray<String> mActiveSessions = new SparseArray<>();
39 
40     @Thunk final PackageInstaller mInstaller;
41     private final IconCache mCache;
42     private final Handler mWorker;
43 
PackageInstallerCompatVL(Context context)44     PackageInstallerCompatVL(Context context) {
45         mInstaller = context.getPackageManager().getPackageInstaller();
46         LauncherAppState.setApplicationContext(context.getApplicationContext());
47         mCache = LauncherAppState.getInstance().getIconCache();
48         mWorker = new Handler(LauncherModel.getWorkerLooper());
49 
50         mInstaller.registerSessionCallback(mCallback, mWorker);
51     }
52 
53     @Override
updateAndGetActiveSessionCache()54     public HashMap<String, Integer> updateAndGetActiveSessionCache() {
55         HashMap<String, Integer> activePackages = new HashMap<>();
56         UserHandleCompat user = UserHandleCompat.myUserHandle();
57         for (SessionInfo info : mInstaller.getAllSessions()) {
58             addSessionInfoToCahce(info, user);
59             if (info.getAppPackageName() != null) {
60                 activePackages.put(info.getAppPackageName(), (int) (info.getProgress() * 100));
61                 mActiveSessions.put(info.getSessionId(), info.getAppPackageName());
62             }
63         }
64         return activePackages;
65     }
66 
addSessionInfoToCahce(SessionInfo info, UserHandleCompat user)67     @Thunk void addSessionInfoToCahce(SessionInfo info, UserHandleCompat user) {
68         String packageName = info.getAppPackageName();
69         if (packageName != null) {
70             mCache.cachePackageInstallInfo(packageName, user, info.getAppIcon(),
71                     info.getAppLabel());
72         }
73     }
74 
75     @Override
onStop()76     public void onStop() {
77         mInstaller.unregisterSessionCallback(mCallback);
78     }
79 
sendUpdate(PackageInstallInfo info)80     @Thunk void sendUpdate(PackageInstallInfo info) {
81         LauncherAppState app = LauncherAppState.getInstanceNoCreate();
82         if (app != null) {
83             app.getModel().setPackageState(info);
84         }
85     }
86 
87     private final SessionCallback mCallback = new SessionCallback() {
88 
89         @Override
90         public void onCreated(int sessionId) {
91             pushSessionDisplayToLauncher(sessionId);
92         }
93 
94         @Override
95         public void onFinished(int sessionId, boolean success) {
96             // For a finished session, we can't get the session info. So use the
97             // packageName from our local cache.
98             String packageName = mActiveSessions.get(sessionId);
99             mActiveSessions.remove(sessionId);
100 
101             if (packageName != null) {
102                 sendUpdate(new PackageInstallInfo(packageName,
103                         success ? STATUS_INSTALLED : STATUS_FAILED, 0));
104             }
105         }
106 
107         @Override
108         public void onProgressChanged(int sessionId, float progress) {
109             SessionInfo session = mInstaller.getSessionInfo(sessionId);
110             if (session != null) {
111                 sendUpdate(new PackageInstallInfo(session.getAppPackageName(),
112                         STATUS_INSTALLING,
113                         (int) (session.getProgress() * 100)));
114             }
115         }
116 
117         @Override
118         public void onActiveChanged(int sessionId, boolean active) { }
119 
120         @Override
121         public void onBadgingChanged(int sessionId) {
122             pushSessionDisplayToLauncher(sessionId);
123         }
124 
125         private void pushSessionDisplayToLauncher(int sessionId) {
126             SessionInfo session = mInstaller.getSessionInfo(sessionId);
127             if (session != null) {
128                 addSessionInfoToCahce(session, UserHandleCompat.myUserHandle());
129                 LauncherAppState app = LauncherAppState.getInstanceNoCreate();
130 
131                 if (app != null) {
132                     app.getModel().updateSessionDisplayInfo(session.getAppPackageName());
133                 }
134             }
135         }
136     };
137 }
138