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