1 /*
2  * Copyright (C) 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.settings.notification;
18 
19 import android.content.ComponentName;
20 import android.content.Context;
21 import android.content.pm.PackageManager;
22 import android.content.pm.ServiceInfo;
23 import android.util.Log;
24 
25 import androidx.annotation.VisibleForTesting;
26 
27 public class SuppressorHelper {
28     private static final String TAG = "SuppressorHelper";
29 
getSuppressionText(Context context, ComponentName suppressor)30     public static String getSuppressionText(Context context, ComponentName suppressor) {
31         return suppressor != null ?
32                 context.getString(com.android.internal.R.string.muted_by,
33                         getSuppressorCaption(context, suppressor)) : null;
34     }
35 
36     @VisibleForTesting
getSuppressorCaption(Context context, ComponentName suppressor)37     static String getSuppressorCaption(Context context, ComponentName suppressor) {
38         final PackageManager pm = context.getPackageManager();
39         try {
40             final ServiceInfo info = pm.getServiceInfo(suppressor, 0);
41             if (info != null) {
42                 final CharSequence seq = info.loadLabel(pm);
43                 if (seq != null) {
44                     final String str = seq.toString().trim();
45                     if (str.length() > 0) {
46                         return str;
47                     }
48                 }
49             }
50         } catch (Throwable e) {
51             Log.w(TAG, "Error loading suppressor caption", e);
52         }
53         return suppressor.getPackageName();
54     }
55 
56 }
57