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