OSDN Git Service

Merge "Fix wifi launch intent" into pi-dev
[android-x86/packages-apps-Settings.git] / src / com / android / settings / notification / AppNotificationSettings.java
1 /*
2  * Copyright (C) 2014 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.notification;
18
19 import android.app.NotificationChannel;
20 import android.app.NotificationChannelGroup;
21 import android.content.Context;
22 import android.os.AsyncTask;
23 import android.support.v14.preference.SwitchPreference;
24 import android.support.v7.preference.Preference;
25 import android.support.v7.preference.PreferenceCategory;
26 import android.support.v7.preference.PreferenceGroup;
27 import android.text.TextUtils;
28 import android.util.Log;
29
30 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
31 import com.android.internal.widget.LockPatternUtils;
32 import com.android.settings.R;
33 import com.android.settings.widget.MasterCheckBoxPreference;
34 import com.android.settingslib.RestrictedSwitchPreference;
35 import com.android.settingslib.core.AbstractPreferenceController;
36
37 import java.util.ArrayList;
38 import java.util.Collections;
39 import java.util.Comparator;
40 import java.util.List;
41
42 /** These settings are per app, so should not be returned in global search results. */
43 public class AppNotificationSettings extends NotificationSettingsBase {
44     private static final String TAG = "AppNotificationSettings";
45     private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
46
47     private static String KEY_GENERAL_CATEGORY = "categories";
48
49     private List<NotificationChannelGroup> mChannelGroupList;
50
51     @Override
52     public int getMetricsCategory() {
53         return MetricsEvent.NOTIFICATION_APP_NOTIFICATION;
54     }
55
56     @Override
57     public void onResume() {
58         super.onResume();
59
60         if (mUid < 0 || TextUtils.isEmpty(mPkg) || mPkgInfo == null) {
61             Log.w(TAG, "Missing package or uid or packageinfo");
62             finish();
63             return;
64         }
65
66         if (getPreferenceScreen() != null) {
67             getPreferenceScreen().removeAll();
68             mDynamicPreferences.clear();
69         }
70
71         if (mShowLegacyChannelConfig) {
72             addPreferencesFromResource(R.xml.channel_notification_settings);
73         } else {
74             addPreferencesFromResource(R.xml.app_notification_settings);
75             // Load channel settings
76             new AsyncTask<Void, Void, Void>() {
77                 @Override
78                 protected Void doInBackground(Void... unused) {
79                     mChannelGroupList = mBackend.getGroups(mPkg, mUid).getList();
80                     Collections.sort(mChannelGroupList, mChannelGroupComparator);
81                     return null;
82                 }
83
84                 @Override
85                 protected void onPostExecute(Void unused) {
86                     if (getHost() == null) {
87                         return;
88                     }
89                     populateList();
90                 }
91             }.execute();
92         }
93         getPreferenceScreen().setOrderingAsAdded(true);
94
95         for (NotificationPreferenceController controller : mControllers) {
96             controller.onResume(mAppRow, mChannel, mChannelGroup, mSuspendedAppsAdmin);
97             controller.displayPreference(getPreferenceScreen());
98         }
99         updatePreferenceStates();
100     }
101
102     @Override
103     protected String getLogTag() {
104         return TAG;
105     }
106
107     @Override
108     protected int getPreferenceScreenResId() {
109         return R.xml.notification_settings;
110     }
111
112     @Override
113     protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
114         mControllers = new ArrayList<>();
115         mControllers.add(new HeaderPreferenceController(context, this));
116         mControllers.add(new BlockPreferenceController(context, mImportanceListener, mBackend));
117         mControllers.add(new BadgePreferenceController(context, mBackend));
118         mControllers.add(new AllowSoundPreferenceController(
119                 context, mImportanceListener, mBackend));
120         mControllers.add(new ImportancePreferenceController(
121                 context, mImportanceListener, mBackend));
122         mControllers.add(new SoundPreferenceController(context, this,
123                 mImportanceListener, mBackend));
124         mControllers.add(new LightsPreferenceController(context, mBackend));
125         mControllers.add(new VibrationPreferenceController(context, mBackend));
126         mControllers.add(new VisibilityPreferenceController(context, new LockPatternUtils(context),
127                 mBackend));
128         mControllers.add(new DndPreferenceController(context, mBackend));
129         mControllers.add(new AppLinkPreferenceController(context));
130         mControllers.add(new DescriptionPreferenceController(context));
131         mControllers.add(new NotificationsOffPreferenceController(context));
132         mControllers.add(new DeletedChannelsPreferenceController(context, mBackend));
133         return new ArrayList<>(mControllers);
134     }
135
136     private void populateList() {
137         if (!mDynamicPreferences.isEmpty()) {
138             // If there's anything in mChannelGroups, we've called populateChannelList twice.
139             // Clear out existing channels and log.
140             Log.w(TAG, "Notification channel group posted twice to settings - old size " +
141                     mDynamicPreferences.size() + ", new size " + mChannelGroupList.size());
142             for (Preference p : mDynamicPreferences) {
143                 getPreferenceScreen().removePreference(p);
144             }
145         }
146         if (mChannelGroupList.isEmpty()) {
147             PreferenceCategory groupCategory = new PreferenceCategory(getPrefContext());
148             groupCategory.setTitle(R.string.notification_channels);
149             groupCategory.setKey(KEY_GENERAL_CATEGORY);
150             getPreferenceScreen().addPreference(groupCategory);
151             mDynamicPreferences.add(groupCategory);
152
153             Preference empty = new Preference(getPrefContext());
154             empty.setTitle(R.string.no_channels);
155             empty.setEnabled(false);
156             groupCategory.addPreference(empty);
157         } else {
158             populateGroupList();
159             mImportanceListener.onImportanceChanged();
160         }
161     }
162
163     private void populateGroupList() {
164         for (NotificationChannelGroup group : mChannelGroupList) {
165             PreferenceCategory groupCategory = new PreferenceCategory(getPrefContext());
166             groupCategory.setOrderingAsAdded(true);
167             getPreferenceScreen().addPreference(groupCategory);
168             mDynamicPreferences.add(groupCategory);
169             if (group.getId() == null) {
170                 if (mChannelGroupList.size() > 1) {
171                     groupCategory.setTitle(R.string.notification_channels_other);
172                 }
173                 groupCategory.setKey(KEY_GENERAL_CATEGORY);
174             } else {
175                 groupCategory.setTitle(group.getName());
176                 groupCategory.setKey(group.getId());
177                 populateGroupToggle(groupCategory, group);
178             }
179             if (!group.isBlocked()) {
180                 final List<NotificationChannel> channels = group.getChannels();
181                 Collections.sort(channels, mChannelComparator);
182                 int N = channels.size();
183                 for (int i = 0; i < N; i++) {
184                     final NotificationChannel channel = channels.get(i);
185                     populateSingleChannelPrefs(groupCategory, channel, group.isBlocked());
186                 }
187             }
188         }
189     }
190
191     protected void populateGroupToggle(final PreferenceGroup parent,
192             NotificationChannelGroup group) {
193         RestrictedSwitchPreference preference = new RestrictedSwitchPreference(getPrefContext());
194         preference.setTitle(R.string.notification_switch_label);
195         preference.setEnabled(mSuspendedAppsAdmin == null
196                 && isChannelGroupBlockable(group));
197         preference.setChecked(!group.isBlocked());
198         preference.setOnPreferenceClickListener(preference1 -> {
199             final boolean allowGroup = ((SwitchPreference) preference1).isChecked();
200             group.setBlocked(!allowGroup);
201             mBackend.updateChannelGroup(mAppRow.pkg, mAppRow.uid, group);
202
203             onGroupBlockStateChanged(group);
204             return true;
205         });
206
207         parent.addPreference(preference);
208     }
209
210     private Comparator<NotificationChannelGroup> mChannelGroupComparator =
211             new Comparator<NotificationChannelGroup>() {
212
213                 @Override
214                 public int compare(NotificationChannelGroup left, NotificationChannelGroup right) {
215                     // Non-grouped channels (in placeholder group with a null id) come last
216                     if (left.getId() == null && right.getId() != null) {
217                         return 1;
218                     } else if (right.getId() == null && left.getId() != null) {
219                         return -1;
220                     }
221                     return left.getId().compareTo(right.getId());
222                 }
223             };
224
225     protected void onGroupBlockStateChanged(NotificationChannelGroup group) {
226         if (group == null) {
227             return;
228         }
229         PreferenceGroup groupGroup = (
230                 PreferenceGroup) getPreferenceScreen().findPreference(group.getId());
231
232         if (groupGroup != null) {
233             if (group.isBlocked()) {
234                 List<Preference> toRemove = new ArrayList<>();
235                 int childCount = groupGroup.getPreferenceCount();
236                 for (int i = 0; i < childCount; i++) {
237                     Preference pref = groupGroup.getPreference(i);
238                     if (pref instanceof MasterCheckBoxPreference) {
239                         toRemove.add(pref);
240                     }
241                 }
242                 for (Preference pref : toRemove) {
243                     groupGroup.removePreference(pref);
244                 }
245             } else {
246                 final List<NotificationChannel> channels = group.getChannels();
247                 Collections.sort(channels, mChannelComparator);
248                 int N = channels.size();
249                 for (int i = 0; i < N; i++) {
250                     final NotificationChannel channel = channels.get(i);
251                     populateSingleChannelPrefs(groupGroup, channel, group.isBlocked());
252                 }
253             }
254         }
255     }
256
257 }