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.tv.settings.device.apps.specialaccess; 18 19 import android.app.NotificationManager; 20 import android.content.ComponentName; 21 import android.content.Context; 22 import android.content.pm.PackageItemInfo; 23 import android.content.pm.PackageManager; 24 import android.content.pm.ServiceInfo; 25 import android.os.Bundle; 26 import android.os.UserHandle; 27 import android.provider.Settings; 28 import android.service.notification.NotificationListenerService; 29 import android.support.annotation.Keep; 30 import android.support.v14.preference.SwitchPreference; 31 import android.support.v7.preference.Preference; 32 import android.support.v7.preference.PreferenceScreen; 33 import android.util.IconDrawableFactory; 34 import android.util.Log; 35 36 import com.android.internal.logging.nano.MetricsProto; 37 import com.android.settingslib.applications.ServiceListing; 38 import com.android.tv.settings.R; 39 import com.android.tv.settings.SettingsPreferenceFragment; 40 41 import java.util.List; 42 43 /** 44 * Settings screen for managing notification listener permissions 45 */ 46 @Keep 47 public class NotificationAccess extends SettingsPreferenceFragment { 48 private static final String TAG = "NotificationAccess"; 49 50 private static final String HEADER_KEY = "header"; 51 52 private NotificationManager mNotificationManager; 53 private PackageManager mPackageManager; 54 private ServiceListing mServiceListing; 55 private IconDrawableFactory mIconDrawableFactory; 56 57 @Override 58 public int getMetricsCategory() { 59 return MetricsProto.MetricsEvent.NOTIFICATION_ACCESS; 60 } 61 62 @Override 63 public void onAttach(Context context) { 64 super.onAttach(context); 65 mPackageManager = context.getPackageManager(); 66 mNotificationManager = context.getSystemService(NotificationManager.class); 67 mIconDrawableFactory = IconDrawableFactory.newInstance(context); 68 } 69 70 @Override 71 public void onCreate(Bundle savedInstanceState) { 72 super.onCreate(savedInstanceState); 73 mServiceListing = new ServiceListing.Builder(getContext()) 74 .setTag(TAG) 75 .setSetting(Settings.Secure.ENABLED_NOTIFICATION_LISTENERS) 76 .setIntentAction(NotificationListenerService.SERVICE_INTERFACE) 77 .setPermission(android.Manifest.permission.BIND_NOTIFICATION_LISTENER_SERVICE) 78 .setNoun("notification listener") 79 .build(); 80 mServiceListing.addCallback(this::updateList); 81 } 82 83 @Override 84 public void onResume() { 85 super.onResume(); 86 mServiceListing.reload(); 87 mServiceListing.setListening(true); 88 } 89 90 @Override 91 public void onPause() { 92 super.onPause(); 93 mServiceListing.setListening(false); 94 } 95 96 @Override 97 public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { 98 setPreferencesFromResource(R.xml.notification_access, null); 99 } 100 101 private void updateList(List<ServiceInfo> services) { 102 final PreferenceScreen screen = getPreferenceScreen(); 103 final Preference header = screen.findPreference(HEADER_KEY); 104 screen.removeAll(); 105 if (header != null) { 106 screen.addPreference(header); 107 } 108 services.sort(new PackageItemInfo.DisplayNameComparator(mPackageManager)); 109 for (ServiceInfo service : services) { 110 final ComponentName cn = new ComponentName(service.packageName, service.name); 111 CharSequence title = null; 112 try { 113 title = mPackageManager.getApplicationInfo( 114 service.packageName, 0).loadLabel(mPackageManager); 115 } catch (PackageManager.NameNotFoundException e) { 116 // unlikely, as we are iterating over live services. 117 Log.w(TAG, "can't find package name", e); 118 } 119 final String summary = service.loadLabel(mPackageManager).toString(); 120 final SwitchPreference pref = new SwitchPreference(getPreferenceManager().getContext()); 121 pref.setPersistent(false); 122 pref.setIcon(mIconDrawableFactory.getBadgedIcon(service, service.applicationInfo, 123 UserHandle.getUserId(service.applicationInfo.uid))); 124 if (title != null && !title.equals(summary)) { 125 pref.setTitle(title); 126 pref.setSummary(summary); 127 } else { 128 pref.setTitle(summary); 129 } 130 pref.setKey(cn.flattenToString()); 131 pref.setChecked(mNotificationManager.isNotificationListenerAccessGranted(cn)); 132 pref.setOnPreferenceChangeListener((preference, newValue) -> { 133 final boolean enable = (boolean) newValue; 134 mNotificationManager.setNotificationListenerAccessGranted(cn, enable); 135 return true; 136 }); 137 screen.addPreference(pref); 138 } 139 if (services.isEmpty()) { 140 final Preference preference = new Preference(getPreferenceManager().getContext()); 141 preference.setTitle(R.string.no_notification_listeners); 142 } 143 } 144 145 } 146