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 package com.android.settings.utils;
17 
18 import android.app.ActivityManager;
19 import android.app.NotificationManager;
20 import android.content.ComponentName;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.pm.ActivityInfo;
24 import android.content.pm.ComponentInfo;
25 import android.content.pm.PackageManager;
26 import android.content.pm.ResolveInfo;
27 import android.content.pm.ServiceInfo;
28 import android.util.ArraySet;
29 import android.util.Slog;
30 
31 import java.util.ArrayList;
32 import java.util.List;
33 import java.util.Set;
34 
35 public class ZenServiceListing {
36 
37     private final Context mContext;
38     private final ManagedServiceSettings.Config mConfig;
39     private final Set<ComponentInfo> mApprovedComponents = new ArraySet<>();
40     private final List<Callback> mZenCallbacks = new ArrayList<>();
41     private final NotificationManager mNm;
42 
ZenServiceListing(Context context, ManagedServiceSettings.Config config)43     public ZenServiceListing(Context context, ManagedServiceSettings.Config config) {
44         mContext = context;
45         mConfig = config;
46         mNm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
47     }
48 
findService(final ComponentName cn)49     public ComponentInfo findService(final ComponentName cn) {
50         if (cn == null) {
51             return null;
52         }
53         for (ComponentInfo component : mApprovedComponents) {
54             final ComponentName ci = new ComponentName(component.packageName, component.name);
55             if (ci.equals(cn)) {
56                 return component;
57             }
58         }
59         return null;
60     }
61 
addZenCallback(Callback callback)62     public void addZenCallback(Callback callback) {
63         mZenCallbacks.add(callback);
64     }
65 
removeZenCallback(Callback callback)66     public void removeZenCallback(Callback callback) {
67         mZenCallbacks.remove(callback);
68     }
69 
reloadApprovedServices()70     public void reloadApprovedServices() {
71         mApprovedComponents.clear();
72 
73         List<String> enabledNotificationListenerPkgs = mNm.getEnabledNotificationListenerPackages();
74         List<ComponentInfo> components = new ArrayList<>();
75         getServices(mConfig, components, mContext.getPackageManager());
76         getActivities(mConfig, components, mContext.getPackageManager());
77         for (ComponentInfo componentInfo : components) {
78             final String pkg = componentInfo.getComponentName().getPackageName();
79             if (mNm.isNotificationPolicyAccessGrantedForPackage(pkg)
80                 || enabledNotificationListenerPkgs.contains(pkg)) {
81                 mApprovedComponents.add(componentInfo);
82             }
83         }
84 
85         if (!mApprovedComponents.isEmpty()) {
86             for (Callback callback : mZenCallbacks) {
87                 callback.onComponentsReloaded(mApprovedComponents);
88             }
89         }
90     }
91 
getServices(ManagedServiceSettings.Config c, List<ComponentInfo> list, PackageManager pm)92     private static void getServices(ManagedServiceSettings.Config c, List<ComponentInfo> list,
93             PackageManager pm) {
94         final int user = ActivityManager.getCurrentUser();
95 
96         List<ResolveInfo> installedServices = pm.queryIntentServicesAsUser(
97                 new Intent(c.intentAction),
98                 PackageManager.GET_SERVICES | PackageManager.GET_META_DATA,
99                 user);
100 
101         for (int i = 0, count = installedServices.size(); i < count; i++) {
102             ResolveInfo resolveInfo = installedServices.get(i);
103             ServiceInfo info = resolveInfo.serviceInfo;
104 
105             if (!c.permission.equals(info.permission)) {
106                 Slog.w(c.tag, "Skipping " + c.noun + " service "
107                         + info.packageName + "/" + info.name
108                         + ": it does not require the permission "
109                         + c.permission);
110                 continue;
111             }
112             if (list != null) {
113                 list.add(info);
114             }
115         }
116     }
117 
getActivities(ManagedServiceSettings.Config c, List<ComponentInfo> list, PackageManager pm)118     private static void getActivities(ManagedServiceSettings.Config c, List<ComponentInfo> list,
119             PackageManager pm) {
120         final int user = ActivityManager.getCurrentUser();
121 
122         List<ResolveInfo> resolveInfos = pm.queryIntentActivitiesAsUser(
123                 new Intent(c.configIntentAction),
124                 PackageManager.GET_ACTIVITIES | PackageManager.GET_META_DATA,
125                 user);
126 
127         for (int i = 0, count = resolveInfos.size(); i < count; i++) {
128             ResolveInfo resolveInfo = resolveInfos.get(i);
129             ActivityInfo info = resolveInfo.activityInfo;
130             if (list != null) {
131                 list.add(info);
132             }
133         }
134     }
135 
136     public interface Callback {
onComponentsReloaded(Set<ComponentInfo> components)137         void onComponentsReloaded(Set<ComponentInfo> components);
138     }
139 }
140