1 /*
2  * Copyright 2016, 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.managedprovisioning.common;
18 
19 import static com.android.internal.util.Preconditions.checkNotNull;
20 
21 import android.annotation.NonNull;
22 import android.annotation.Nullable;
23 import android.content.Context;
24 import android.content.pm.ApplicationInfo;
25 import android.content.pm.PackageManager;
26 import android.graphics.drawable.Drawable;
27 
28 import com.android.internal.annotations.Immutable;
29 import com.android.managedprovisioning.ProvisionLogger;
30 
31 /**
32  * Information relating to the currently installed MDM package manager.
33  */
34 @Immutable
35 public final class MdmPackageInfo {
36     // ToDo: add toString and equals for better testability.
37 
38     public final Drawable packageIcon;
39     public final String appLabel;
40 
41     /**
42      * Default constructor.
43      */
MdmPackageInfo(@onNull Drawable packageIcon, @NonNull String appLabel)44     public MdmPackageInfo(@NonNull Drawable packageIcon, @NonNull String appLabel) {
45         this.packageIcon = checkNotNull(packageIcon, "package icon must not be null");
46         this.appLabel = checkNotNull(appLabel, "app label must not be null");
47     }
48 
49     /**
50      * Constructs an {@link MdmPackageInfo} object by reading the data from the package manager.
51      *
52      * @param context a {@link Context} object
53      * @param packageName the name of the mdm package
54      * @return an {@link MdMPackageInfo} object for the given package, or {@code null} if the
55      *         package does not exist on the device
56      */
57     @Nullable
createFromPackageName(@onNull Context context, @Nullable String packageName)58     public static MdmPackageInfo createFromPackageName(@NonNull Context context,
59             @Nullable String packageName) {
60         if (packageName != null) {
61             PackageManager pm = context.getPackageManager();
62             try {
63                 ApplicationInfo ai = pm.getApplicationInfo(packageName, /* default flags */ 0);
64                 return new MdmPackageInfo(pm.getApplicationIcon(packageName),
65                         pm.getApplicationLabel(ai).toString());
66             } catch (PackageManager.NameNotFoundException e) {
67                 // Package does not exist, ignore. Should never happen.
68                 ProvisionLogger.logw("Package not currently installed: " + packageName);
69             }
70         }
71         return null;
72     }
73 }
74