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 23 import java.util.HashMap; 24 import java.util.List; 25 26 import androidx.annotation.NonNull; 27 28 public abstract class PackageInstallerCompat { 29 30 public static final int STATUS_INSTALLED = 0; 31 public static final int STATUS_INSTALLING = 1; 32 public static final int STATUS_FAILED = 2; 33 34 private static final Object sInstanceLock = new Object(); 35 private static PackageInstallerCompat sInstance; 36 getInstance(Context context)37 public static PackageInstallerCompat getInstance(Context context) { 38 synchronized (sInstanceLock) { 39 if (sInstance == null) { 40 sInstance = new PackageInstallerCompatVL(context); 41 } 42 return sInstance; 43 } 44 } 45 46 /** 47 * @return a map of active installs to their progress 48 */ updateAndGetActiveSessionCache()49 public abstract HashMap<String, PackageInstaller.SessionInfo> updateAndGetActiveSessionCache(); 50 onStop()51 public abstract void onStop(); 52 53 public static final class PackageInstallInfo { 54 public final ComponentName componentName; 55 public final String packageName; 56 public final int state; 57 public final int progress; 58 PackageInstallInfo(@onNull PackageInstaller.SessionInfo info)59 private PackageInstallInfo(@NonNull PackageInstaller.SessionInfo info) { 60 this.state = STATUS_INSTALLING; 61 this.packageName = info.getAppPackageName(); 62 this.componentName = new ComponentName(packageName, ""); 63 this.progress = (int) (info.getProgress() * 100f); 64 } 65 PackageInstallInfo(String packageName, int state, int progress)66 public PackageInstallInfo(String packageName, int state, int progress) { 67 this.state = state; 68 this.packageName = packageName; 69 this.componentName = new ComponentName(packageName, ""); 70 this.progress = progress; 71 } 72 fromInstallingState(PackageInstaller.SessionInfo info)73 public static PackageInstallInfo fromInstallingState(PackageInstaller.SessionInfo info) { 74 return new PackageInstallInfo(info); 75 } 76 fromState(int state, String packageName)77 public static PackageInstallInfo fromState(int state, String packageName) { 78 return new PackageInstallInfo(packageName, state, 0 /* progress */); 79 } 80 81 } 82 getAllVerifiedSessions()83 public abstract List<PackageInstaller.SessionInfo> getAllVerifiedSessions(); 84 } 85