1 /*
2  * Copyright (C) 2017 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.applications.defaultapps;
18 
19 import android.app.ActivityManager;
20 import android.content.ComponentName;
21 import android.content.Intent;
22 import android.content.pm.PackageManager;
23 import android.content.pm.ResolveInfo;
24 import android.content.pm.ServiceInfo;
25 import android.provider.Settings;
26 import android.service.notification.NotificationAssistantService;
27 import android.util.Slog;
28 
29 import com.android.settings.R;
30 import com.android.settings.utils.ManagedServiceSettings;
31 
32 import java.util.ArrayList;
33 import java.util.List;
34 
35 public class DefaultNotificationAssistantPicker extends DefaultAppPickerFragment {
36     private static final String TAG = "DefaultNotiAssist";
37 
38     private final ManagedServiceSettings.Config mConfig = getConfig();
39 
40     @Override
getMetricsCategory()41     public int getMetricsCategory() {
42         return 0;
43     }
44 
45     @Override
getDefaultKey()46     protected String getDefaultKey() {
47         return Settings.Secure.getString(getContext().getContentResolver(), mConfig.setting);
48     }
49 
50     @Override
setDefaultKey(String value)51     protected boolean setDefaultKey(String value) {
52         Settings.Secure.putString(getContext().getContentResolver(), mConfig.setting, value);
53         return true;
54     }
55 
56     @Override
getCandidates()57     protected List<DefaultAppInfo> getCandidates() {
58         List<DefaultAppInfo> candidates = new ArrayList<>();
59         final int user = ActivityManager.getCurrentUser();
60 
61         List<ResolveInfo> installedServices = mPm.queryIntentServicesAsUser(
62                 new Intent(mConfig.intentAction),
63                 PackageManager.GET_SERVICES | PackageManager.GET_META_DATA,
64                 user);
65 
66         for (int i = 0, count = installedServices.size(); i < count; i++) {
67             ResolveInfo resolveInfo = installedServices.get(i);
68             ServiceInfo info = resolveInfo.serviceInfo;
69 
70             if (!mConfig.permission.equals(info.permission)) {
71                 Slog.w(mConfig.tag, "Skipping " + mConfig.noun + " service "
72                         + info.packageName + "/" + info.name
73                         + ": it does not require the permission "
74                         + mConfig.permission);
75                 continue;
76             }
77 
78             candidates.add(new DefaultAppInfo(mPm,
79                     mUserId, new ComponentName(info.packageName, info.name)));
80         }
81         return candidates;
82     }
83 
84     @Override
shouldShowItemNone()85     protected boolean shouldShowItemNone() {
86         return true;
87     }
88 
getConfig()89     private ManagedServiceSettings.Config getConfig() {
90         final ManagedServiceSettings.Config c = new ManagedServiceSettings.Config();
91         c.tag = TAG;
92         c.setting = Settings.Secure.ENABLED_NOTIFICATION_ASSISTANT;
93         c.intentAction = NotificationAssistantService.SERVICE_INTERFACE;
94         c.permission = android.Manifest.permission.BIND_NOTIFICATION_ASSISTANT_SERVICE;
95         c.noun = "notification assistant";
96         c.warningDialogTitle = R.string.notification_listener_security_warning_title;
97         c.warningDialogSummary = R.string.notification_listener_security_warning_summary;
98         c.emptyText = R.string.no_notification_listeners;
99         return c;
100     }
101 }
102