1 /*
2  * Copyright (C) 2019 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 package com.android.launcher3.pm;
17 
18 import android.content.ComponentName;
19 import android.content.pm.PackageInstaller;
20 import android.os.UserHandle;
21 
22 import androidx.annotation.NonNull;
23 
24 public final class PackageInstallInfo {
25     private static final String TAG = "PackageInstallInfo";
26 
27     public static final int STATUS_INSTALLED = 0;
28     public static final int STATUS_INSTALLING = 1;
29     public static final int STATUS_INSTALLED_DOWNLOADING = 2;
30     public static final int STATUS_FAILED = 3;
31 
32     public final ComponentName componentName;
33     public final String packageName;
34     public final int state;
35     public final int progress;
36     public final UserHandle user;
37 
PackageInstallInfo(@onNull PackageInstaller.SessionInfo info)38     private PackageInstallInfo(@NonNull PackageInstaller.SessionInfo info) {
39         this.state = STATUS_INSTALLING;
40         this.packageName = info.getAppPackageName();
41         this.componentName = new ComponentName(packageName, "");
42         this.progress = (int) (info.getProgress() * 100f);
43         this.user = InstallSessionHelper.getUserHandle(info);
44     }
45 
PackageInstallInfo(String packageName, int state, int progress, UserHandle user)46     public PackageInstallInfo(String packageName, int state, int progress, UserHandle user) {
47         this.state = state;
48         this.packageName = packageName;
49         this.componentName = new ComponentName(packageName, "");
50         this.progress = progress;
51         this.user = user;
52     }
53 
fromInstallingState(PackageInstaller.SessionInfo info)54     public static PackageInstallInfo fromInstallingState(PackageInstaller.SessionInfo info) {
55         return new PackageInstallInfo(info);
56     }
57 
fromState(int state, String packageName, UserHandle user)58     public static PackageInstallInfo fromState(int state, String packageName, UserHandle user) {
59         return new PackageInstallInfo(packageName, state, 0 /* progress */, user);
60     }
61 
62 
63     @Override
toString()64     public String toString() {
65         return TAG + "(" + dumpProperties() + ")";
66     }
67 
dumpProperties()68     private String dumpProperties() {
69         return "componentName=" + componentName
70                 + "packageName=" + packageName
71                 + " state=" + stateToString()
72                 + " progress=" + progress
73                 + " user=" + user;
74     }
75 
stateToString()76     private String stateToString() {
77         switch (state) {
78             case STATUS_INSTALLED : return "STATUS_INSTALLED";
79             case STATUS_INSTALLING : return "STATUS_INSTALLING";
80             case STATUS_INSTALLED_DOWNLOADING : return "STATUS_INSTALLED_DOWNLOADING";
81             case STATUS_FAILED : return "STATUS_FAILED";
82             default : return "INVALID STATE";
83         }
84     }
85 }
86