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 com.android.settings.SettingsActivity.EXTRA_FRAGMENT_ARG_KEY; 20 21 import android.app.Activity; 22 import android.app.Application; 23 import android.app.settings.SettingsEnums; 24 import android.app.usage.IUsageStatsManager; 25 import android.content.Context; 26 import android.content.Intent; 27 import android.os.Bundle; 28 import android.os.ServiceManager; 29 import android.os.UserHandle; 30 import android.os.UserManager; 31 import android.text.TextUtils; 32 33 import androidx.annotation.VisibleForTesting; 34 import androidx.fragment.app.Fragment; 35 import androidx.preference.Preference; 36 import androidx.preference.PreferenceCategory; 37 import androidx.preference.PreferenceScreen; 38 39 import com.android.settings.R; 40 import com.android.settings.RingtonePreference; 41 import com.android.settings.core.OnActivityResultListener; 42 import com.android.settings.dashboard.DashboardFragment; 43 import com.android.settings.search.BaseSearchIndexProvider; 44 import com.android.settingslib.core.AbstractPreferenceController; 45 import com.android.settingslib.search.SearchIndexable; 46 47 import java.util.ArrayList; 48 import java.util.List; 49 50 @SearchIndexable 51 public class ConfigureNotificationSettings extends DashboardFragment implements 52 OnActivityResultListener { 53 private static final String TAG = "ConfigNotiSettings"; 54 55 @VisibleForTesting 56 static final String KEY_SWIPE_DOWN = "gesture_swipe_down_fingerprint_notifications"; 57 static final String KEY_LOCKSCREEN = "lock_screen_notifications"; 58 59 private static final String KEY_NOTI_DEFAULT_RINGTONE = "notification_default_ringtone"; 60 private static final int REQUEST_CODE = 200; 61 private static final String SELECTED_PREFERENCE_KEY = "selected_preference"; 62 private static final String KEY_ADVANCED_CATEGORY = "configure_notifications_advanced"; 63 64 private RingtonePreference mRequestPreference; 65 66 @Override getMetricsCategory()67 public int getMetricsCategory() { 68 return SettingsEnums.CONFIGURE_NOTIFICATION; 69 } 70 71 @Override getLogTag()72 protected String getLogTag() { 73 return TAG; 74 } 75 76 @Override getPreferenceScreenResId()77 protected int getPreferenceScreenResId() { 78 return R.xml.configure_notification_settings; 79 } 80 81 @Override createPreferenceControllers(Context context)82 protected List<AbstractPreferenceController> createPreferenceControllers(Context context) { 83 final Activity activity = getActivity(); 84 final Application app; 85 if (activity != null) { 86 app = activity.getApplication(); 87 } else { 88 app = null; 89 } 90 return buildPreferenceControllers(context, app, this); 91 } 92 93 @Override isParalleledControllers()94 protected boolean isParalleledControllers() { 95 return true; 96 } 97 buildPreferenceControllers(Context context, Application app, Fragment host)98 private static List<AbstractPreferenceController> buildPreferenceControllers(Context context, 99 Application app, Fragment host) { 100 final List<AbstractPreferenceController> controllers = new ArrayList<>(); 101 controllers.add(new RecentNotifyingAppsPreferenceController( 102 context, new NotificationBackend(), IUsageStatsManager.Stub.asInterface( 103 ServiceManager.getService(Context.USAGE_STATS_SERVICE)), 104 context.getSystemService(UserManager.class), app, host)); 105 controllers.add(new ShowOnLockScreenNotificationPreferenceController( 106 context, KEY_LOCKSCREEN)); 107 controllers.add(new NotificationRingtonePreferenceController(context) { 108 @Override 109 public String getPreferenceKey() { 110 return KEY_NOTI_DEFAULT_RINGTONE; 111 } 112 113 }); 114 return controllers; 115 } 116 117 @Override onCreate(Bundle icicle)118 public void onCreate(Bundle icicle) { 119 super.onCreate(icicle); 120 final PreferenceScreen screen = getPreferenceScreen(); 121 final Bundle arguments = getArguments(); 122 123 if (screen == null) { 124 return; 125 } 126 if (arguments != null) { 127 final String highlightKey = arguments.getString(EXTRA_FRAGMENT_ARG_KEY); 128 if (!TextUtils.isEmpty(highlightKey)) { 129 final PreferenceCategory advancedCategory = 130 screen.findPreference(KEY_ADVANCED_CATEGORY); 131 // Has highlight row - expand everything 132 advancedCategory.setInitialExpandedChildrenCount(Integer.MAX_VALUE); 133 scrollToPreference(advancedCategory); 134 } 135 } 136 } 137 138 @Override onPreferenceTreeClick(Preference preference)139 public boolean onPreferenceTreeClick(Preference preference) { 140 if (preference instanceof RingtonePreference) { 141 writePreferenceClickMetric(preference); 142 mRequestPreference = (RingtonePreference) preference; 143 mRequestPreference.onPrepareRingtonePickerIntent(mRequestPreference.getIntent()); 144 getActivity().startActivityForResultAsUser( 145 mRequestPreference.getIntent(), 146 REQUEST_CODE, 147 null, 148 UserHandle.of(mRequestPreference.getUserId())); 149 return true; 150 } 151 return super.onPreferenceTreeClick(preference); 152 } 153 154 @Override onActivityResult(int requestCode, int resultCode, Intent data)155 public void onActivityResult(int requestCode, int resultCode, Intent data) { 156 if (mRequestPreference != null) { 157 mRequestPreference.onActivityResult(requestCode, resultCode, data); 158 mRequestPreference = null; 159 } 160 } 161 162 @Override onSaveInstanceState(Bundle outState)163 public void onSaveInstanceState(Bundle outState) { 164 super.onSaveInstanceState(outState); 165 if (mRequestPreference != null) { 166 outState.putString(SELECTED_PREFERENCE_KEY, mRequestPreference.getKey()); 167 } 168 } 169 170 /** 171 * For Search. 172 */ 173 public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER = 174 new BaseSearchIndexProvider(R.xml.configure_notification_settings) { 175 176 @Override 177 public List<AbstractPreferenceController> createPreferenceControllers( 178 Context context) { 179 return buildPreferenceControllers(context, null, null); 180 } 181 182 @Override 183 public List<String> getNonIndexableKeys(Context context) { 184 final List<String> keys = super.getNonIndexableKeys(context); 185 keys.add(KEY_SWIPE_DOWN); 186 return keys; 187 } 188 }; 189 } 190