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