PackageInstallerCompatVL.java revision 800a4f217a5b0b7817a70a24974fc13d6b7e4591
1e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal/*
2e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal * Copyright (C) 2014 The Android Open Source Project
3e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal *
4e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal * Licensed under the Apache License, Version 2.0 (the "License");
5e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal * you may not use this file except in compliance with the License.
6e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal * You may obtain a copy of the License at
7e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal *
8e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal *      http://www.apache.org/licenses/LICENSE-2.0
9e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal *
10e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal * Unless required by applicable law or agreed to in writing, software
11e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal * distributed under the License is distributed on an "AS IS" BASIS,
12e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal * See the License for the specific language governing permissions and
14e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal * limitations under the License.
15e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal */
16e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal
17e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyalpackage com.android.launcher3.compat;
18e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal
19e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyalimport android.content.Context;
20e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyalimport android.content.pm.PackageInstaller;
21e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyalimport android.content.pm.PackageInstaller.SessionCallback;
22800a4f217a5b0b7817a70a24974fc13d6b7e4591Sunny Goyalimport android.content.pm.PackageInstaller.SessionInfo;
23e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyalimport android.util.Log;
24e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyalimport android.util.SparseArray;
25e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal
26e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyalimport com.android.launcher3.LauncherAppState;
27e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyalimport com.android.launcher3.ShortcutInfo;
28e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal
29e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyalimport java.util.ArrayList;
30e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal
31e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyalpublic class PackageInstallerCompatVL extends PackageInstallerCompat {
32e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal
33e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal    private static final String TAG = "PackageInstallerCompatVL";
34e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal    private static final boolean DEBUG = false;
35e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal
36800a4f217a5b0b7817a70a24974fc13d6b7e4591Sunny Goyal    private final SparseArray<SessionInfo> mPendingReplays = new SparseArray<SessionInfo>();
37e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal    private final PackageInstaller mInstaller;
38e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal
39e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal    private boolean mResumed;
40e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal    private boolean mBound;
41e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal
42e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal    PackageInstallerCompatVL(Context context) {
43e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal        mInstaller = context.getPackageManager().getPackageInstaller();
44e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal
45e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal        mResumed = false;
46e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal        mBound = false;
47e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal
48e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal        mInstaller.addSessionCallback(mCallback);
49e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal        // On start, send updates for all active sessions
50800a4f217a5b0b7817a70a24974fc13d6b7e4591Sunny Goyal        for (SessionInfo info : mInstaller.getAllSessions()) {
51e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal            mPendingReplays.append(info.getSessionId(), info);
52e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal        }
53e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal    }
54e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal
55e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal    @Override
56e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal    public void onStop() {
57e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal        mInstaller.removeSessionCallback(mCallback);
58e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal    }
59e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal
60e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal    @Override
61e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal    public void onFinishBind() {
62e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal        mBound = true;
63e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal        replayUpdates(null);
64e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal    }
65e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal
66e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal    @Override
67e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal    public void onPause() {
68e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal        mResumed = false;
69e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal    }
70e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal
71e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal    @Override
72e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal    public void onResume() {
73e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal        mResumed = true;
74e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal        replayUpdates(null);
75e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal    }
76e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal
77e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal    @Override
78e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal    public void recordPackageUpdate(String packageName, int state, int progress) {
79e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal        // No op
80e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal    }
81e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal
82e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal    private void replayUpdates(PackageInstallInfo newInfo) {
83e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal        if (DEBUG) Log.d(TAG, "updates resumed");
84e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal        if (!mResumed || !mBound) {
85e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal            // Not yet ready
86e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal            return;
87e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal        }
88e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal        if ((mPendingReplays.size() == 0) && (newInfo == null)) {
89e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal            // Nothing to update
90e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal            return;
91e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal        }
92e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal
93e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal        LauncherAppState app = LauncherAppState.getInstanceNoCreate();
94e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal        if (app == null) {
95e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal            // Try again later
96e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal            if (DEBUG) Log.d(TAG, "app is null, delaying send");
97e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal            return;
98e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal        }
99e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal
10065c6011d0954223c14a0e6127cf307c18f7c2c0dSameer Padala        ArrayList<PackageInstallInfo> updates = new ArrayList<PackageInstallInfo>();
101e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal        if (newInfo != null) {
102e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal            updates.add(newInfo);
103e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal        }
104e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal        for (int i = mPendingReplays.size() - 1; i > 0; i--) {
105800a4f217a5b0b7817a70a24974fc13d6b7e4591Sunny Goyal            SessionInfo session = mPendingReplays.valueAt(i);
106e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal            if (session.getAppPackageName() != null) {
107e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal                updates.add(new PackageInstallInfo(session.getAppPackageName(),
108e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal                        ShortcutInfo.PACKAGE_STATE_INSTALLING,
109e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal                        (int) (session.getProgress() * 100)));
110e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal            }
111e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal        }
112e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal        mPendingReplays.clear();
113e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal        if (!updates.isEmpty()) {
114e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal            app.setPackageState(updates);
115e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal        }
116e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal    }
117e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal
118e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal    private final SessionCallback mCallback = new SessionCallback() {
119e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal
120e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal        @Override
121e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal        public void onCreated(int sessionId) {
122800a4f217a5b0b7817a70a24974fc13d6b7e4591Sunny Goyal            SessionInfo session = mInstaller.getSessionInfo(sessionId);
123e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal            if (session != null) {
124e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal                mPendingReplays.put(sessionId, session);
125e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal                replayUpdates(null);
126e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal            }
127e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal        }
128e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal
129e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal        @Override
130e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal        public void onFinished(int sessionId, boolean success) {
131e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal            mPendingReplays.remove(sessionId);
132800a4f217a5b0b7817a70a24974fc13d6b7e4591Sunny Goyal            SessionInfo session = mInstaller.getSessionInfo(sessionId);
133e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal            if ((session != null) && (session.getAppPackageName() != null)) {
134e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal                // Replay all updates with a one time update for this installed package. No
135e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal                // need to store this record for future updates, as the app list will get
136e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal                // refreshed on resume.
137e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal                replayUpdates(new PackageInstallInfo(session.getAppPackageName(),
138e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal                        success ? ShortcutInfo.PACKAGE_STATE_DEFAULT
139e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal                                : ShortcutInfo.PACKAGE_STATE_ERROR, 0));
140e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal            }
141e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal        }
142e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal
143e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal        @Override
144e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal        public void onProgressChanged(int sessionId, float progress) {
145800a4f217a5b0b7817a70a24974fc13d6b7e4591Sunny Goyal            SessionInfo session = mInstaller.getSessionInfo(sessionId);
146e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal            if (session != null) {
147e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal                mPendingReplays.put(sessionId, session);
148e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal                replayUpdates(null);
149e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal            }
150e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal        }
151e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal
152e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal        @Override
153e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal        public void onOpened(int sessionId) { }
154e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal
155e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal        @Override
156e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal        public void onClosed(int sessionId) { }
157e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal
158e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal    };
159e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal}
160