OSDN Git Service

Fix crash on rotation
[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             // Load channel settings
73             new AsyncTask<Void, Void, Void>() {
74                 @Override
75                 protected Void doInBackground(Void... unused) {
76                     mChannelGroupList = mBackend.getGroups(mPkg, mUid).getList();
77                     Collections.sort(mChannelGroupList, mChannelGroupComparator);
78                     return null;
79                 }
80
81                 @Override
82                 protected void onPostExecute(Void unused) {
83                     if (getHost() == null) {
84                         return;
85                     }
86                     populateList();
87                 }
88             }.execute();
89         }
90         getPreferenceScreen().setOrderingAsAdded(true);
91
92         for (NotificationPreferenceController controller : mControllers) {
93             controller.onResume(mAppRow, mChannel, mChannelGroup, mSuspendedAppsAdmin);
94             controller.displayPreference(getPreferenceScreen());
95         }
96         updatePreferenceStates();
97     }
98
99     @Override
100     protected String getLogTag() {
101         return TAG;
102     }
103
104     @Override
105     protected int getPreferenceScreenResId() {
106         return R.xml.app_notification_settings;
107     }
108
109     @Override
110     protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
111         mControllers = new ArrayList<>();
112         mControllers.add(new HeaderPreferenceController(context, this));
113         mControllers.add(new BlockPreferenceController(context, mImportanceListener, mBackend));
114         mControllers.add(new BadgePreferenceController(context, mBackend));
115         mControllers.add(new AllowSoundPreferenceController(
116                 context, mImportanceListener, mBackend));
117         mControllers.add(new ImportancePreferenceController(
118                 context, mImportanceListener, mBackend));
119         mControllers.add(new SoundPreferenceController(context, this,
120                 mImportanceListener, mBackend));
121         mControllers.add(new LightsPreferenceController(context, mBackend));
122         mControllers.add(new VibrationPreferenceController(context, mBackend));
123         mControllers.add(new VisibilityPreferenceController(context, new LockPatternUtils(context),
124                 mBackend));
125         mControllers.add(new DndPreferenceController(context, mBackend));
126         mControllers.add(new AppLinkPreferenceController(context));
127         mControllers.add(new DescriptionPreferenceController(context));
128         mControllers.add(new NotificationsOffPreferenceController(context));
129         mControllers.add(new DeletedChannelsPreferenceController(context, mBackend));
130         return new ArrayList<>(mControllers);
131     }
132
133     private void populateList() {
134         if (!mDynamicPreferences.isEmpty()) {
135             // If there's anything in mChannelGroups, we've called populateChannelList twice.
136             // Clear out existing channels and log.
137             Log.w(TAG, "Notification channel group posted twice to settings - old size " +
138                     mDynamicPreferences.size() + ", new size " + mChannelGroupList.size());
139             for (Preference p : mDynamicPreferences) {
140                 getPreferenceScreen().removePreference(p);
141             }
142         }
143         if (mChannelGroupList.isEmpty()) {
144             PreferenceCategory groupCategory = new PreferenceCategory(getPrefContext());
145             groupCategory.setTitle(R.string.notification_channels);
146             groupCategory.setKey(KEY_GENERAL_CATEGORY);
147             getPreferenceScreen().addPreference(groupCategory);
148             mDynamicPreferences.add(groupCategory);
149
150             Preference empty = new Preference(getPrefContext());
151             empty.setTitle(R.string.no_channels);
152             empty.setEnabled(false);
153             groupCategory.addPreference(empty);
154         } else {
155             populateGroupList();
156             mImportanceListener.onImportanceChanged();
157         }
158     }
159
160     private void populateGroupList() {
161         for (NotificationChannelGroup group : mChannelGroupList) {
162             PreferenceCategory groupCategory = new PreferenceCategory(getPrefContext());
163             groupCategory.setOrderingAsAdded(true);
164             getPreferenceScreen().addPreference(groupCategory);
165             mDynamicPreferences.add(groupCategory);
166             if (group.getId() == null) {
167                 if (mChannelGroupList.size() > 1) {
168                     groupCategory.setTitle(R.string.notification_channels_other);
169                 }
170                 groupCategory.setKey(KEY_GENERAL_CATEGORY);
171             } else {
172                 groupCategory.setTitle(group.getName());
173                 groupCategory.setKey(group.getId());
174                 populateGroupToggle(groupCategory, group);
175             }
176             if (!group.isBlocked()) {
177                 final List<NotificationChannel> channels = group.getChannels();
178                 Collections.sort(channels, mChannelComparator);
179                 int N = channels.size();
180                 for (int i = 0; i < N; i++) {
181                     final NotificationChannel channel = channels.get(i);
182                     populateSingleChannelPrefs(groupCategory, channel, group.isBlocked());
183                 }
184             }
185         }
186     }
187
188     protected void populateGroupToggle(final PreferenceGroup parent,
189             NotificationChannelGroup group) {
190         RestrictedSwitchPreference preference = new RestrictedSwitchPreference(getPrefContext());
191         preference.setTitle(R.string.notification_switch_label);
192         preference.setEnabled(mSuspendedAppsAdmin == null
193                 && isChannelGroupBlockable(group));
194         preference.setChecked(!group.isBlocked());
195         preference.setOnPreferenceClickListener(preference1 -> {
196             final boolean allowGroup = ((SwitchPreference) preference1).isChecked();
197             group.setBlocked(!allowGroup);
198             mBackend.updateChannelGroup(mAppRow.pkg, mAppRow.uid, group);
199
200             onGroupBlockStateChanged(group);
201             return true;
202         });
203
204         parent.addPreference(preference);
205     }
206
207     private Comparator<NotificationChannelGroup> mChannelGroupComparator =
208             new Comparator<NotificationChannelGroup>() {
209
210                 @Override
211                 public int compare(NotificationChannelGroup left, NotificationChannelGroup right) {
212                     // Non-grouped channels (in placeholder group with a null id) come last
213                     if (left.getId() == null && right.getId() != null) {
214                         return 1;
215                     } else if (right.getId() == null && left.getId() != null) {
216                         return -1;
217                     }
218                     return left.getId().compareTo(right.getId());
219                 }
220             };
221
222     protected void onGroupBlockStateChanged(NotificationChannelGroup group) {
223         if (group == null) {
224             return;
225         }
226         PreferenceGroup groupGroup = (
227                 PreferenceGroup) getPreferenceScreen().findPreference(group.getId());
228
229         if (groupGroup != null) {
230             if (group.isBlocked()) {
231                 List<Preference> toRemove = new ArrayList<>();
232                 int childCount = groupGroup.getPreferenceCount();
233                 for (int i = 0; i < childCount; i++) {
234                     Preference pref = groupGroup.getPreference(i);
235                     if (pref instanceof MasterCheckBoxPreference) {
236                         toRemove.add(pref);
237                     }
238                 }
239                 for (Preference pref : toRemove) {
240                     groupGroup.removePreference(pref);
241                 }
242             } else {
243                 final List<NotificationChannel> channels = group.getChannels();
244                 Collections.sort(channels, mChannelComparator);
245                 int N = channels.size();
246                 for (int i = 0; i < N; i++) {
247                     final NotificationChannel channel = channels.get(i);
248                     populateSingleChannelPrefs(groupGroup, channel, group.isBlocked());
249                 }
250             }
251         }
252     }
253
254 }