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
19817afa34472dba49b1dee0489da11f410ff09fcdMario Bertschlerimport android.content.ComponentName;
20e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyalimport android.content.Context;
21817afa34472dba49b1dee0489da11f410ff09fcdMario Bertschlerimport android.content.pm.PackageInstaller;
22817afa34472dba49b1dee0489da11f410ff09fcdMario Bertschlerimport android.support.annotation.NonNull;
23e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal
24756adbc3e41ee1edb53c580b8c679f343924fab5Sunny Goyalimport java.util.HashMap;
25817afa34472dba49b1dee0489da11f410ff09fcdMario Bertschlerimport java.util.List;
269448536b113afa6ceefce26604a1b44618d1d4f2Sunny Goyal
27e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyalpublic abstract class PackageInstallerCompat {
28e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal
29349426234e8c5a0e5bcf2c8d94dbb9844b5f724aSunny Goyal    public static final int STATUS_INSTALLED = 0;
30349426234e8c5a0e5bcf2c8d94dbb9844b5f724aSunny Goyal    public static final int STATUS_INSTALLING = 1;
31349426234e8c5a0e5bcf2c8d94dbb9844b5f724aSunny Goyal    public static final int STATUS_FAILED = 2;
32349426234e8c5a0e5bcf2c8d94dbb9844b5f724aSunny Goyal
33e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal    private static final Object sInstanceLock = new Object();
34e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal    private static PackageInstallerCompat sInstance;
35e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal
36e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal    public static PackageInstallerCompat getInstance(Context context) {
37e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal        synchronized (sInstanceLock) {
38e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal            if (sInstance == null) {
39a52ecb0390c85afb385371bb844bb496c59ddf87Sunny Goyal                sInstance = new PackageInstallerCompatVL(context);
40e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal            }
41e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal            return sInstance;
42e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal        }
43e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal    }
44e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal
45756adbc3e41ee1edb53c580b8c679f343924fab5Sunny Goyal    /**
46756adbc3e41ee1edb53c580b8c679f343924fab5Sunny Goyal     * @return a map of active installs to their progress
47756adbc3e41ee1edb53c580b8c679f343924fab5Sunny Goyal     */
48756adbc3e41ee1edb53c580b8c679f343924fab5Sunny Goyal    public abstract HashMap<String, Integer> updateAndGetActiveSessionCache();
49e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal
50e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal    public abstract void onStop();
51e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal
52e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal    public static final class PackageInstallInfo {
53817afa34472dba49b1dee0489da11f410ff09fcdMario Bertschler        public final ComponentName componentName;
54e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal        public final String packageName;
55817afa34472dba49b1dee0489da11f410ff09fcdMario Bertschler        public final int state;
56817afa34472dba49b1dee0489da11f410ff09fcdMario Bertschler        public final int progress;
57e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal
58817afa34472dba49b1dee0489da11f410ff09fcdMario Bertschler        private PackageInstallInfo(@NonNull PackageInstaller.SessionInfo info) {
59817afa34472dba49b1dee0489da11f410ff09fcdMario Bertschler            this.state = STATUS_INSTALLING;
60817afa34472dba49b1dee0489da11f410ff09fcdMario Bertschler            this.packageName = info.getAppPackageName();
61817afa34472dba49b1dee0489da11f410ff09fcdMario Bertschler            this.componentName = new ComponentName(packageName, "");
62817afa34472dba49b1dee0489da11f410ff09fcdMario Bertschler            this.progress = (int) (info.getProgress() * 100f);
63e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal        }
64e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal
65e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal        public PackageInstallInfo(String packageName, int state, int progress) {
66e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal            this.state = state;
67817afa34472dba49b1dee0489da11f410ff09fcdMario Bertschler            this.packageName = packageName;
68817afa34472dba49b1dee0489da11f410ff09fcdMario Bertschler            this.componentName = new ComponentName(packageName, "");
69e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal            this.progress = progress;
70e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal        }
71817afa34472dba49b1dee0489da11f410ff09fcdMario Bertschler
72817afa34472dba49b1dee0489da11f410ff09fcdMario Bertschler        public static PackageInstallInfo fromInstallingState(PackageInstaller.SessionInfo info) {
73817afa34472dba49b1dee0489da11f410ff09fcdMario Bertschler            return new PackageInstallInfo(info);
74817afa34472dba49b1dee0489da11f410ff09fcdMario Bertschler        }
75817afa34472dba49b1dee0489da11f410ff09fcdMario Bertschler
76817afa34472dba49b1dee0489da11f410ff09fcdMario Bertschler        public static PackageInstallInfo fromState(int state, String packageName) {
77817afa34472dba49b1dee0489da11f410ff09fcdMario Bertschler            return new PackageInstallInfo(packageName, state, 0 /* progress */);
78817afa34472dba49b1dee0489da11f410ff09fcdMario Bertschler        }
79817afa34472dba49b1dee0489da11f410ff09fcdMario Bertschler
80e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal    }
81817afa34472dba49b1dee0489da11f410ff09fcdMario Bertschler
82817afa34472dba49b1dee0489da11f410ff09fcdMario Bertschler    public abstract List<PackageInstaller.SessionInfo> getAllVerifiedSessions();
83e755d469d40b95e763a9dcb67d0e4f511d1948ddSunny Goyal}
84