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.ComponentName; 20 import android.content.Context; 21 import android.content.pm.PackageInstaller; 22 import android.content.pm.PackageInstaller.SessionInfo; 23 import android.os.Process; 24 import android.os.UserHandle; 25 26 import java.util.Collections; 27 import java.util.HashMap; 28 import java.util.List; 29 30 import androidx.annotation.NonNull; 31 32 import com.android.launcher3.Utilities; 33 import com.android.launcher3.util.PackageUserKey; 34 35 public abstract class PackageInstallerCompat { 36 37 // Set<String> of session ids of promise icons that have been added to the home screen 38 // as FLAG_PROMISE_NEW_INSTALLS. 39 protected static final String PROMISE_ICON_IDS = "promise_icon_ids"; 40 41 public static final int STATUS_INSTALLED = 0; 42 public static final int STATUS_INSTALLING = 1; 43 public static final int STATUS_FAILED = 2; 44 45 private static final Object sInstanceLock = new Object(); 46 private static PackageInstallerCompat sInstance; 47 getInstance(Context context)48 public static PackageInstallerCompat getInstance(Context context) { 49 synchronized (sInstanceLock) { 50 if (sInstance == null) { 51 sInstance = new PackageInstallerCompatVL(context); 52 } 53 return sInstance; 54 } 55 } 56 getUserHandle(SessionInfo info)57 public static UserHandle getUserHandle(SessionInfo info) { 58 return Utilities.ATLEAST_Q ? info.getUser() : Process.myUserHandle(); 59 } 60 61 /** 62 * @return a map of active installs to their progress 63 */ updateAndGetActiveSessionCache()64 public abstract HashMap<PackageUserKey, SessionInfo> updateAndGetActiveSessionCache(); 65 66 /** 67 * @return an active SessionInfo for {@param pkg} or null if none exists. 68 */ getActiveSessionInfo(UserHandle user, String pkg)69 public abstract SessionInfo getActiveSessionInfo(UserHandle user, String pkg); 70 onStop()71 public abstract void onStop(); 72 73 public static final class PackageInstallInfo { 74 public final ComponentName componentName; 75 public final String packageName; 76 public final int state; 77 public final int progress; 78 public final UserHandle user; 79 PackageInstallInfo(@onNull SessionInfo info)80 private PackageInstallInfo(@NonNull SessionInfo info) { 81 this.state = STATUS_INSTALLING; 82 this.packageName = info.getAppPackageName(); 83 this.componentName = new ComponentName(packageName, ""); 84 this.progress = (int) (info.getProgress() * 100f); 85 this.user = getUserHandle(info); 86 } 87 PackageInstallInfo(String packageName, int state, int progress, UserHandle user)88 public PackageInstallInfo(String packageName, int state, int progress, UserHandle user) { 89 this.state = state; 90 this.packageName = packageName; 91 this.componentName = new ComponentName(packageName, ""); 92 this.progress = progress; 93 this.user = user; 94 } 95 fromInstallingState(SessionInfo info)96 public static PackageInstallInfo fromInstallingState(SessionInfo info) { 97 return new PackageInstallInfo(info); 98 } 99 fromState(int state, String packageName, UserHandle user)100 public static PackageInstallInfo fromState(int state, String packageName, UserHandle user) { 101 return new PackageInstallInfo(packageName, state, 0 /* progress */, user); 102 } 103 104 } 105 getAllVerifiedSessions()106 public abstract List<SessionInfo> getAllVerifiedSessions(); 107 108 /** 109 * Returns true if a promise icon was already added to the home screen for {@param sessionId}. 110 * Applicable only for icons with flag FLAG_PROMISE_NEW_INSTALLS. 111 */ promiseIconAddedForId(int sessionId)112 public abstract boolean promiseIconAddedForId(int sessionId); 113 114 /** 115 * Applicable only for icons with flag FLAG_PROMISE_NEW_INSTALLS. 116 */ removePromiseIconId(int sessionId)117 public abstract void removePromiseIconId(int sessionId); 118 } 119