1 /*
2  * Copyright (C) 2015 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 static android.app.admin.DevicePolicyResources.Strings.Settings.WORK_PROFILE_LOCK_SCREEN_REDACT_NOTIFICATION_SUMMARY;
20 import static android.app.admin.DevicePolicyResources.Strings.Settings.WORK_PROFILE_LOCK_SCREEN_REDACT_NOTIFICATION_TITLE;
21 
22 import android.app.Activity;
23 import android.app.Application;
24 import android.app.settings.SettingsEnums;
25 import android.content.ComponentName;
26 import android.content.Context;
27 import android.content.Intent;
28 import android.os.Bundle;
29 import android.os.UserHandle;
30 
31 import androidx.annotation.VisibleForTesting;
32 import androidx.fragment.app.Fragment;
33 import androidx.preference.Preference;
34 import androidx.preference.PreferenceScreen;
35 
36 import com.android.settings.R;
37 import com.android.settings.RingtonePreference;
38 import com.android.settings.core.OnActivityResultListener;
39 import com.android.settings.dashboard.DashboardFragment;
40 import com.android.settings.search.BaseSearchIndexProvider;
41 import com.android.settingslib.core.AbstractPreferenceController;
42 import com.android.settingslib.search.SearchIndexable;
43 
44 import java.util.ArrayList;
45 import java.util.List;
46 
47 @SearchIndexable
48 public class ConfigureNotificationSettings extends DashboardFragment implements
49         OnActivityResultListener {
50     private static final String TAG = "ConfigNotiSettings";
51 
52     @VisibleForTesting
53     static final String KEY_SWIPE_DOWN = "gesture_swipe_down_fingerprint_notifications";
54     static final String KEY_LOCKSCREEN = "lock_screen_notifications";
55 
56     private static final String KEY_NOTI_DEFAULT_RINGTONE = "notification_default_ringtone";
57     private static final int REQUEST_CODE = 200;
58     private static final String SELECTED_PREFERENCE_KEY = "selected_preference";
59     private static final String KEY_ADVANCED_CATEGORY = "configure_notifications_advanced";
60 
61     private RingtonePreference mRequestPreference;
62 
63     private NotificationAssistantPreferenceController mNotificationAssistantPreferenceController;
64 
65     @Override
getMetricsCategory()66     public int getMetricsCategory() {
67         return SettingsEnums.CONFIGURE_NOTIFICATION;
68     }
69 
70     @Override
getLogTag()71     protected String getLogTag() {
72         return TAG;
73     }
74 
75     @Override
onCreate(Bundle icicle)76     public void onCreate(Bundle icicle) {
77         super.onCreate(icicle);
78         replaceEnterpriseStringTitle("lock_screen_work_redact",
79                 WORK_PROFILE_LOCK_SCREEN_REDACT_NOTIFICATION_TITLE,
80                 R.string.lock_screen_notifs_redact_work);
81         replaceEnterpriseStringSummary("lock_screen_work_redact",
82                 WORK_PROFILE_LOCK_SCREEN_REDACT_NOTIFICATION_SUMMARY,
83                 R.string.lock_screen_notifs_redact_work_summary);
84     }
85 
86     @Override
getPreferenceScreenResId()87     protected int getPreferenceScreenResId() {
88         return R.xml.configure_notification_settings;
89     }
90 
91     @Override
createPreferenceControllers(Context context)92     protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
93         final Activity activity = getActivity();
94         final Application app;
95         if (activity != null) {
96             app = activity.getApplication();
97         } else {
98             app = null;
99         }
100         return buildPreferenceControllers(context, app, this);
101     }
102 
103     @Override
onAttach(Context context)104     public void onAttach(Context context) {
105         super.onAttach(context);
106 
107         mNotificationAssistantPreferenceController =
108                 use(NotificationAssistantPreferenceController.class);
109         mNotificationAssistantPreferenceController.setFragment(this);
110     }
111 
buildPreferenceControllers(Context context, Application app, Fragment host)112     private static List<AbstractPreferenceController> buildPreferenceControllers(Context context,
113             Application app, Fragment host) {
114         final List<AbstractPreferenceController> controllers = new ArrayList<>();
115         controllers.add(new ShowOnLockScreenNotificationPreferenceController(
116                 context, KEY_LOCKSCREEN));
117         controllers.add(new NotificationRingtonePreferenceController(context) {
118             @Override
119             public String getPreferenceKey() {
120                 return KEY_NOTI_DEFAULT_RINGTONE;
121             }
122 
123         });
124         controllers.add(new EmergencyBroadcastPreferenceController(context,
125                 "app_and_notif_cell_broadcast_settings"));
126 
127         return controllers;
128     }
129 
130     @Override
onPreferenceTreeClick(Preference preference)131     public boolean onPreferenceTreeClick(Preference preference) {
132         if (preference instanceof RingtonePreference) {
133             writePreferenceClickMetric(preference);
134             mRequestPreference = (RingtonePreference) preference;
135             mRequestPreference.onPrepareRingtonePickerIntent(mRequestPreference.getIntent());
136             getActivity().startActivityForResultAsUser(
137                     mRequestPreference.getIntent(),
138                     REQUEST_CODE,
139                     null,
140                     UserHandle.of(mRequestPreference.getUserId()));
141             return true;
142         }
143         return super.onPreferenceTreeClick(preference);
144     }
145 
146     @Override
onActivityResult(int requestCode, int resultCode, Intent data)147     public void onActivityResult(int requestCode, int resultCode, Intent data) {
148         if (mRequestPreference != null) {
149             mRequestPreference.onActivityResult(requestCode, resultCode, data);
150             mRequestPreference = null;
151         }
152     }
153 
154     @Override
onSaveInstanceState(Bundle outState)155     public void onSaveInstanceState(Bundle outState) {
156         super.onSaveInstanceState(outState);
157         if (mRequestPreference != null) {
158             outState.putString(SELECTED_PREFERENCE_KEY, mRequestPreference.getKey());
159         }
160     }
161 
162     /**
163      * For Search.
164      */
165     public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
166             new BaseSearchIndexProvider(R.xml.configure_notification_settings) {
167 
168                 @Override
169                 public List<AbstractPreferenceController> createPreferenceControllers(
170                         Context context) {
171                     return buildPreferenceControllers(context, null, null);
172                 }
173 
174                 @Override
175                 public List<String> getNonIndexableKeys(Context context) {
176                     final List<String> keys = super.getNonIndexableKeys(context);
177                     keys.add(KEY_SWIPE_DOWN);
178                     return keys;
179                 }
180             };
181 
182     // Dialogs only have access to the parent fragment, not the controller, so pass the information
183     // along to keep business logic out of this file
enableNAS(ComponentName cn)184     protected void enableNAS(ComponentName cn) {
185         final PreferenceScreen screen = getPreferenceScreen();
186         NotificationAssistantPreferenceController napc =
187                 use(NotificationAssistantPreferenceController.class);
188         napc.setNotificationAssistantGranted(cn);
189         napc.updateState(screen.findPreference(napc.getPreferenceKey()));
190     }
191 }
192