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.app;
18 
19 import static com.android.server.notification.Flags.notificationHideUnusedChannels;
20 
21 
22 import android.app.settings.SettingsEnums;
23 import android.content.Context;
24 import android.text.TextUtils;
25 import android.util.Log;
26 import android.view.Menu;
27 import android.view.MenuInflater;
28 import android.view.MenuItem;
29 
30 import com.android.internal.widget.LockPatternUtils;
31 import com.android.settings.R;
32 import com.android.settingslib.core.AbstractPreferenceController;
33 
34 import java.util.ArrayList;
35 import java.util.List;
36 
37 /** These settings are per app, so should not be returned in global search results. */
38 public class AppNotificationSettings extends NotificationSettings {
39     private static final String TAG = "AppNotificationSettings";
40     private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
41 
42     boolean mShowAll = false;
43 
44     @Override
getMetricsCategory()45     public int getMetricsCategory() {
46         return SettingsEnums.NOTIFICATION_APP_NOTIFICATION;
47     }
48 
49 
50     @Override
onResume()51     public void onResume() {
52         super.onResume();
53 
54         if (mUid < 0 || TextUtils.isEmpty(mPkg) || mPkgInfo == null) {
55             Log.w(TAG, "Missing package or uid or packageinfo");
56             finish();
57             return;
58         }
59 
60         getActivity().setTitle(mAppRow.label);
61 
62         for (NotificationPreferenceController controller : mControllers) {
63             controller.onResume(mAppRow, mChannel, mChannelGroup, null, null, mSuspendedAppsAdmin,
64                     null);
65             controller.displayPreference(getPreferenceScreen());
66         }
67         updatePreferenceStates();
68     }
69 
70     @Override
getLogTag()71     protected String getLogTag() {
72         return TAG;
73     }
74 
75     @Override
getPreferenceScreenResId()76     protected int getPreferenceScreenResId() {
77         return R.xml.app_notification_settings;
78     }
79 
80     @Override
createPreferenceControllers(Context context)81     protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
82         mControllers = new ArrayList<>();
83         mControllers.add(new HeaderPreferenceController(context, this));
84         mControllers.add(new BlockPreferenceController(context, mDependentFieldListener, mBackend));
85         mControllers.add(new FullScreenIntentPermissionPreferenceController(context, mBackend));
86         mControllers.add(new BadgePreferenceController(context, mBackend));
87         mControllers.add(new AllowSoundPreferenceController(
88                 context, mDependentFieldListener, mBackend));
89         mControllers.add(new ImportancePreferenceController(
90                 context, mDependentFieldListener, mBackend));
91         mControllers.add(new MinImportancePreferenceController(
92                 context, mDependentFieldListener, mBackend));
93         mControllers.add(new HighImportancePreferenceController(
94                 context, mDependentFieldListener, mBackend));
95         mControllers.add(new SoundPreferenceController(context, this,
96                 mDependentFieldListener, mBackend));
97         mControllers.add(new LightsPreferenceController(context, mBackend));
98         mControllers.add(new VibrationPreferenceController(context, mBackend));
99         mControllers.add(new VisibilityPreferenceController(context, new LockPatternUtils(context),
100                 mBackend));
101         mControllers.add(new DndPreferenceController(context, mBackend));
102         mControllers.add(new AppLinkPreferenceController(context));
103         mControllers.add(new ChannelListPreferenceController(context, mBackend));
104         mControllers.add(new AppConversationListPreferenceController(context, mBackend));
105         mControllers.add(new InvalidConversationInfoPreferenceController(context, mBackend));
106         mControllers.add(new InvalidConversationPreferenceController(context, mBackend));
107         mControllers.add(new BubbleSummaryPreferenceController(context, mBackend));
108         mControllers.add(new NotificationsOffPreferenceController(context));
109         mControllers.add(new DeletedChannelsPreferenceController(context, mBackend));
110         return new ArrayList<>(mControllers);
111     }
112 
113     private final int SHOW_ALL_CHANNELS = 1;
114 
115     @Override
onCreateOptionsMenu(Menu menu, MenuInflater inflater)116     public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
117         if (notificationHideUnusedChannels()) {
118             menu.add(Menu.NONE, SHOW_ALL_CHANNELS, Menu.NONE,
119                     mShowAll ? R.string.hide_unused_channels : R.string.show_unused_channels);
120         }
121         super.onCreateOptionsMenu(menu, inflater);
122     }
123 
124     @Override
onOptionsItemSelected(MenuItem item)125     public boolean onOptionsItemSelected(MenuItem item) {
126         if (!notificationHideUnusedChannels()) {
127             return super.onOptionsItemSelected(item);
128         }
129         switch (item.getItemId()) {
130             case SHOW_ALL_CHANNELS:
131                 mShowAll = !mShowAll;
132                 item.setTitle(mShowAll
133                         ? R.string.hide_unused_channels
134                         : R.string.show_unused_channels);
135                 ChannelListPreferenceController list =
136                         use(ChannelListPreferenceController.class);
137                 list.setShowAll(mShowAll);
138                 list.updateState(findPreference(list.getPreferenceKey()));
139                 return true;
140             default:
141                 return super.onOptionsItemSelected(item);
142         }
143     }
144 }
145