summaryrefslogtreecommitdiff
path: root/Settings/src/com/android/tv/settings/device/apps/specialaccess/NotificationAccess.java
blob: df13211d618f8fa94ce9d83fea280d8eb8eaa0d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.tv.settings.device.apps.specialaccess;

import android.app.NotificationManager;
import android.app.tvsettings.TvSettingsEnums;
import android.content.ComponentName;
import android.content.Context;
import android.content.pm.PackageItemInfo;
import android.content.pm.PackageManager;
import android.content.pm.ServiceInfo;
import android.os.Bundle;
import android.os.UserHandle;
import android.provider.Settings;
import android.service.notification.NotificationListenerService;
import android.util.ArraySet;
import android.util.IconDrawableFactory;
import android.util.Log;

import androidx.annotation.Keep;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;

import com.android.settingslib.applications.ServiceListing;
import com.android.tv.settings.R;
import com.android.tv.settings.SettingsPreferenceFragment;
import com.android.tv.settings.widget.SwitchWithSoundPreference;

import java.util.List;

/**
 * Settings screen for managing notification listener permissions
 */
@Keep
public class NotificationAccess extends SettingsPreferenceFragment {
    private static final String TAG = "NotificationAccess";

    private static final int MAX_CN_LENGTH = 500;

    private static final String HEADER_KEY = "header";

    private static final String DEFAULT_PACKAGES_SEPARATOR = ":";
    private ArraySet<String> mDefaultPackages;

    private NotificationManager mNotificationManager;
    private PackageManager mPackageManager;
    private ServiceListing mServiceListing;
    private IconDrawableFactory mIconDrawableFactory;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mPackageManager = context.getPackageManager();
        mNotificationManager = context.getSystemService(NotificationManager.class);
        mIconDrawableFactory = IconDrawableFactory.newInstance(context);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mServiceListing = new ServiceListing.Builder(getContext())
                .setTag(TAG)
                .setSetting(Settings.Secure.ENABLED_NOTIFICATION_LISTENERS)
                .setIntentAction(NotificationListenerService.SERVICE_INTERFACE)
                .setPermission(android.Manifest.permission.BIND_NOTIFICATION_LISTENER_SERVICE)
                .setNoun("notification listener")
                .setValidator(info -> {
                    if (info.getComponentName().flattenToString().length() > MAX_CN_LENGTH) {
                        return false;
                    }
                    return true;
                 })
                .build();
        mServiceListing.addCallback(this::updateList);
    }

    @Override
    public void onResume() {
        super.onResume();
        mServiceListing.reload();
        mServiceListing.setListening(true);
    }

    @Override
    public void onPause() {
        super.onPause();
        mServiceListing.setListening(false);
    }

    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
        setPreferencesFromResource(R.xml.notification_access, null);

        String packages = getString(
                getResources().getIdentifier("config_defaultListenerAccessPackages",
                        "string", "android"));
        mDefaultPackages = new ArraySet<String>(packages.split(DEFAULT_PACKAGES_SEPARATOR));
    }

    private void updateList(List<ServiceInfo> services) {
        final PreferenceScreen screen = getPreferenceScreen();
        final Preference header = screen.findPreference(HEADER_KEY);
        screen.removeAll();
        if (header != null) {
            screen.addPreference(header);
        }
        services.sort(new PackageItemInfo.DisplayNameComparator(mPackageManager));
        for (ServiceInfo service : services) {
            final ComponentName cn = new ComponentName(service.packageName, service.name);
            CharSequence title = null;
            try {
                title = mPackageManager.getApplicationInfo(
                        service.packageName, 0).loadLabel(mPackageManager);
            } catch (PackageManager.NameNotFoundException e) {
                // unlikely, as we are iterating over live services.
                Log.w(TAG, "can't find package name", e);
            }
            final String summary = service.loadLabel(mPackageManager).toString();
            final SwitchWithSoundPreference pref = new SwitchWithSoundPreference(
                    getPreferenceManager().getContext());
            pref.setPersistent(false);
            pref.setIcon(mIconDrawableFactory.getBadgedIcon(service, service.applicationInfo,
                    UserHandle.getUserId(service.applicationInfo.uid)));
            if (title != null && !title.equals(summary)) {
                pref.setTitle(title);
                pref.setSummary(summary);
            } else {
                pref.setTitle(summary);
            }
            pref.setKey(cn.flattenToString());
            pref.setChecked(mNotificationManager.isNotificationListenerAccessGranted(cn));
            // Prevent the user from removing access from a default notification listener.
            if (mDefaultPackages.contains(cn.getPackageName()) && pref.isChecked()) {
                pref.setEnabled(false);
                pref.setSummary(R.string.default_notification_access_package_summary);
            }
            pref.setOnPreferenceChangeListener((preference, newValue) -> {
                final boolean enable = (boolean) newValue;
                mNotificationManager.setNotificationListenerAccessGranted(cn, enable);
                return true;
            });
            screen.addPreference(pref);
        }
        if (services.isEmpty()) {
            final Preference preference = new Preference(getPreferenceManager().getContext());
            preference.setTitle(R.string.no_notification_listeners);
        }
    }

    @Override
    protected int getPageId() {
        return TvSettingsEnums.APPS_SPECIAL_APP_ACCESS_NOTIFICATION_ACCESS;
    }
}