• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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.settingslib.utils.applications;
18 
19 import android.content.Context;
20 import android.content.pm.ApplicationInfo;
21 import android.content.pm.PackageManager;
22 import android.os.Build;
23 import android.os.UserManager;
24 import android.util.Log;
25 
26 import androidx.annotation.RequiresApi;
27 
28 import com.android.settingslib.utils.R;
29 
30 public class AppUtils {
31 
32     private static final String TAG = AppUtils.class.getSimpleName();
33 
34     /** Returns the label for a given package. */
getApplicationLabel( PackageManager packageManager, String packageName)35     public static CharSequence getApplicationLabel(
36             PackageManager packageManager, String packageName) {
37         try {
38             final ApplicationInfo appInfo =
39                     packageManager.getApplicationInfo(
40                             packageName,
41                             PackageManager.MATCH_DISABLED_COMPONENTS
42                                     | PackageManager.MATCH_ANY_USER);
43             return appInfo.loadLabel(packageManager);
44         } catch (PackageManager.NameNotFoundException e) {
45             Log.w(TAG, "Unable to find info for package: " + packageName);
46         }
47         return null;
48     }
49 
50     /**
51      * Returns a content description of an app name which distinguishes a personal app from a
52      * work app for accessibility purpose.
53      * If the app is in a work profile, then add a "work" prefix to the app name.
54      */
55     @RequiresApi(Build.VERSION_CODES.M)
getAppContentDescription(Context context, String packageName, int userId)56     public static String getAppContentDescription(Context context, String packageName,
57             int userId) {
58         final CharSequence appLabel = getApplicationLabel(context.getPackageManager(), packageName);
59         if (appLabel == null) {
60             return "";
61         }
62         return context.getSystemService(UserManager.class).isManagedProfile(userId)
63                 ? context.getString(R.string.accessibility_work_profile_app_description, appLabel)
64                 : appLabel.toString();
65     }
66 }
67