• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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.adservices.service.consent;
18 
19 import android.annotation.NonNull;
20 import android.content.Context;
21 import android.content.pm.ApplicationInfo;
22 import android.content.pm.PackageManager;
23 import android.content.pm.PackageManager.NameNotFoundException;
24 import android.graphics.drawable.Drawable;
25 
26 import com.android.adservices.service.common.compat.PackageManagerCompatUtils;
27 
28 /**
29  * POJO Represents a App.
30  *
31  * @hide
32  */
33 public class App {
34 
35     private final String mPackageName;
36 
App(String packageName)37     App(String packageName) {
38         this.mPackageName = packageName;
39     }
40 
41     /** Creates an instance of an App. */
42     @NonNull
create(String packageName)43     public static App create(String packageName) {
44         return new App(packageName);
45     }
46 
47     /** Returns a String represents the app identifier (i.e. packageName). */
getPackageName()48     public String getPackageName() {
49         return mPackageName;
50     }
51 
52     /** @return an application name using provided {@link PackageManager}. */
getAppDisplayName(@onNull PackageManager packageManager)53     public String getAppDisplayName(@NonNull PackageManager packageManager) {
54         ApplicationInfo ai;
55         try {
56             ai = PackageManagerCompatUtils.getApplicationInfo(packageManager, getPackageName(), 0);
57         } catch (NameNotFoundException e) {
58             return "";
59         }
60         return packageManager.getApplicationLabel(ai).toString();
61     }
62 
63     /**
64      * @return an application icon using provided {@link PackageManager} or null if operation
65      *     failed.
66      */
getAppIcon(@onNull Context context)67     public Drawable getAppIcon(@NonNull Context context) {
68         try {
69             return context.getPackageManager().getApplicationIcon(getPackageName());
70         } catch (Exception e) {
71             return null;
72         }
73     }
74 }
75