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