1 /*
2  * Copyright (C) 2019 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.specialaccess.notificationaccess;
18 
19 import android.app.Activity;
20 import android.app.NotificationManager;
21 import android.app.settings.SettingsEnums;
22 import android.content.ComponentName;
23 import android.content.Context;
24 import android.content.Intent;
25 import android.content.pm.PackageManager;
26 import android.content.pm.ResolveInfo;
27 import android.content.pm.ServiceInfo;
28 import android.os.AsyncTask;
29 import android.os.Bundle;
30 import android.os.UserManager;
31 import android.provider.Settings;
32 import android.service.notification.NotificationListenerService;
33 import android.util.IconDrawableFactory;
34 import android.util.Log;
35 import android.util.Slog;
36 
37 import androidx.annotation.VisibleForTesting;
38 import androidx.appcompat.app.AlertDialog;
39 import androidx.preference.Preference;
40 import androidx.preference.SwitchPreference;
41 
42 import com.android.settings.R;
43 import com.android.settings.applications.AppInfoBase;
44 import com.android.settings.overlay.FeatureFactory;
45 import com.android.settings.widget.EntityHeaderController;
46 import com.android.settingslib.applications.AppUtils;
47 
48 import java.util.List;
49 import java.util.Objects;
50 
51 public class NotificationAccessDetails extends AppInfoBase {
52     private static final String TAG = "NotifAccessDetails";
53     private static final String SWITCH_PREF_KEY = "notification_access_switch";
54 
55     private boolean mCreated;
56     private ComponentName mComponentName;
57     private CharSequence mServiceName;
58     private boolean mIsNls;
59 
60     private NotificationManager mNm;
61     private PackageManager mPm;
62 
63     @Override
onCreate(Bundle savedInstanceState)64     public void onCreate(Bundle savedInstanceState) {
65         final Intent intent = getIntent();
66         if (mComponentName == null && intent != null) {
67             String cn = intent.getStringExtra(Settings.EXTRA_NOTIFICATION_LISTENER_COMPONENT_NAME);
68             if (cn != null) {
69                 mComponentName = ComponentName.unflattenFromString(cn);
70                 if (mComponentName != null) {
71                     final Bundle args = getArguments();
72                     args.putString(ARG_PACKAGE_NAME, mComponentName.getPackageName());
73                 }
74             }
75         }
76         super.onCreate(savedInstanceState);
77         mNm = getContext().getSystemService(NotificationManager.class);
78         mPm = getPackageManager();
79         addPreferencesFromResource(R.xml.notification_access_permission_details);
80     }
81 
82     @Override
onActivityCreated(Bundle savedInstanceState)83     public void onActivityCreated(Bundle savedInstanceState) {
84         super.onActivityCreated(savedInstanceState);
85         if (mCreated) {
86             Log.w(TAG, "onActivityCreated: ignoring duplicate call");
87             return;
88         }
89         mCreated = true;
90         if (mPackageInfo == null) return;
91         loadNotificationListenerService();
92         final Activity activity = getActivity();
93         final Preference pref = EntityHeaderController
94                 .newInstance(activity, this, null /* header */)
95                 .setRecyclerView(getListView(), getSettingsLifecycle())
96                 .setIcon(IconDrawableFactory.newInstance(getContext())
97                         .getBadgedIcon(mPackageInfo.applicationInfo))
98                 .setLabel(mPackageInfo.applicationInfo.loadLabel(mPm))
99                 .setSummary(mServiceName)
100                 .setIsInstantApp(AppUtils.isInstant(mPackageInfo.applicationInfo))
101                 .setPackageName(mPackageName)
102                 .setUid(mPackageInfo.applicationInfo.uid)
103                 .setHasAppInfoLink(true)
104                 .setButtonActions(EntityHeaderController.ActionType.ACTION_NONE,
105                         EntityHeaderController.ActionType.ACTION_NONE)
106                 .done(activity, getPrefContext());
107         getPreferenceScreen().addPreference(pref);
108     }
109 
110     @Override
getMetricsCategory()111     public int getMetricsCategory() {
112         return SettingsEnums.NOTIFICATION_ACCESS_DETAIL;
113     }
114 
115     @Override
refreshUi()116     protected boolean refreshUi() {
117         final Context context = getContext();
118         if (mComponentName == null) {
119             // No service given
120             Slog.d(TAG, "No component name provided");
121             return false;
122         }
123         if (!mIsNls) {
124             // This component doesn't have the right androidmanifest definition to be an NLS
125             Slog.d(TAG, "Provided component name is not an NLS");
126             return false;
127         }
128         if (UserManager.get(getContext()).isManagedProfile()) {
129             // Apps in the work profile do not support notification listeners.
130             Slog.d(TAG, "NLSes aren't allowed in work profiles");
131             return false;
132         }
133         updatePreference(findPreference(SWITCH_PREF_KEY));
134         return true;
135     }
136 
137     @Override
createDialog(int id, int errorCode)138     protected AlertDialog createDialog(int id, int errorCode) {
139         return null;
140     }
141 
updatePreference(SwitchPreference preference)142     public void updatePreference(SwitchPreference preference) {
143         final CharSequence label = mPackageInfo.applicationInfo.loadLabel(mPm);
144         preference.setChecked(isServiceEnabled(mComponentName));
145         preference.setOnPreferenceChangeListener((p, newValue) -> {
146             final boolean access = (Boolean) newValue;
147             if (!access) {
148                 if (!isServiceEnabled(mComponentName)) {
149                     return true; // already disabled
150                 }
151                 // show a friendly dialog
152                 new FriendlyWarningDialogFragment()
153                         .setServiceInfo(mComponentName, label, this)
154                         .show(getFragmentManager(), "friendlydialog");
155                 return false;
156             } else {
157                 if (isServiceEnabled(mComponentName)) {
158                     return true; // already enabled
159                 }
160                 // show a scary dialog
161                 new ScaryWarningDialogFragment()
162                         .setServiceInfo(mComponentName, label, this)
163                         .show(getFragmentManager(), "dialog");
164                 return false;
165             }
166         });
167     }
168 
169     @VisibleForTesting
logSpecialPermissionChange(boolean enable, String packageName)170     void logSpecialPermissionChange(boolean enable, String packageName) {
171         int logCategory = enable ? SettingsEnums.APP_SPECIAL_PERMISSION_NOTIVIEW_ALLOW
172                 : SettingsEnums.APP_SPECIAL_PERMISSION_NOTIVIEW_DENY;
173         FeatureFactory.getFactory(getContext()).getMetricsFeatureProvider().action(getContext(),
174                 logCategory, packageName);
175     }
176 
disable(final ComponentName cn)177     public void disable(final ComponentName cn) {
178         logSpecialPermissionChange(true, cn.getPackageName());
179         mNm.setNotificationListenerAccessGranted(cn, false);
180         AsyncTask.execute(() -> {
181             if (!mNm.isNotificationPolicyAccessGrantedForPackage(
182                     cn.getPackageName())) {
183                 mNm.removeAutomaticZenRules(cn.getPackageName());
184             }
185         });
186         refreshUi();
187     }
188 
enable(ComponentName cn)189     protected void enable(ComponentName cn) {
190         logSpecialPermissionChange(true, cn.getPackageName());
191         mNm.setNotificationListenerAccessGranted(cn, true);
192         refreshUi();
193     }
194 
isServiceEnabled(ComponentName cn)195     protected boolean isServiceEnabled(ComponentName cn) {
196         return mNm.isNotificationListenerAccessGranted(cn);
197     }
198 
loadNotificationListenerService()199     protected void loadNotificationListenerService() {
200         mIsNls = false;
201 
202         if (mComponentName == null) {
203             return;
204         }
205         Intent intent = new Intent(NotificationListenerService.SERVICE_INTERFACE)
206                 .setComponent(mComponentName);
207         List<ResolveInfo> installedServices = mPm.queryIntentServicesAsUser(
208                 intent, PackageManager.GET_SERVICES | PackageManager.GET_META_DATA, mUserId);
209         for (ResolveInfo resolveInfo : installedServices) {
210             ServiceInfo info = resolveInfo.serviceInfo;
211             if (android.Manifest.permission.BIND_NOTIFICATION_LISTENER_SERVICE.equals(
212                     info.permission)) {
213                 if (Objects.equals(mComponentName, info.getComponentName())) {
214                     mIsNls = true;
215                     mServiceName = info.loadLabel(mPm);
216                     break;
217                 }
218             }
219         }
220     }
221 }