Rahul Chaturvedi | 7fc77ca | 2015-05-19 18:02:16 -0700 | [diff] [blame] | 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.launcher3; |
| 18 | |
Sunny Goyal | 4025332 | 2018-04-19 09:27:38 -0700 | [diff] [blame] | 19 | import static com.android.launcher3.states.RotationHelper.ALLOW_ROTATION_PREFERENCE_KEY; |
| 20 | import static com.android.launcher3.states.RotationHelper.getAllowRotationDefaultValue; |
| 21 | |
Sunny Goyal | ba406e2 | 2018-04-02 11:36:12 -0700 | [diff] [blame] | 22 | import android.annotation.TargetApi; |
Rahul Chaturvedi | 7fc77ca | 2015-05-19 18:02:16 -0700 | [diff] [blame] | 23 | import android.app.Activity; |
Sunny Goyal | 848cad5 | 2017-07-07 03:12:21 -0700 | [diff] [blame] | 24 | import android.app.AlertDialog; |
| 25 | import android.app.Dialog; |
| 26 | import android.app.DialogFragment; |
Sunny Goyal | 7f920b8 | 2018-06-27 15:47:49 -0700 | [diff] [blame^] | 27 | import android.app.Fragment; |
Sunny Goyal | 848cad5 | 2017-07-07 03:12:21 -0700 | [diff] [blame] | 28 | import android.app.FragmentManager; |
| 29 | import android.content.ComponentName; |
Sunny Goyal | f48e592 | 2016-05-12 15:36:20 -0700 | [diff] [blame] | 30 | import android.content.ContentResolver; |
Sunny Goyal | 848cad5 | 2017-07-07 03:12:21 -0700 | [diff] [blame] | 31 | import android.content.Context; |
| 32 | import android.content.DialogInterface; |
| 33 | import android.content.Intent; |
Sunny Goyal | ba406e2 | 2018-04-02 11:36:12 -0700 | [diff] [blame] | 34 | import android.os.Build; |
Rahul Chaturvedi | 7fc77ca | 2015-05-19 18:02:16 -0700 | [diff] [blame] | 35 | import android.os.Bundle; |
Sunny Goyal | ca18746 | 2017-03-31 20:09:34 -0700 | [diff] [blame] | 36 | import android.preference.ListPreference; |
Rahul Chaturvedi | 799aa04 | 2015-06-01 21:26:41 -0400 | [diff] [blame] | 37 | import android.preference.Preference; |
Rahul Chaturvedi | 7fc77ca | 2015-05-19 18:02:16 -0700 | [diff] [blame] | 38 | import android.preference.PreferenceFragment; |
Sunny Goyal | ba406e2 | 2018-04-02 11:36:12 -0700 | [diff] [blame] | 39 | import android.preference.PreferenceScreen; |
Sunny Goyal | f48e592 | 2016-05-12 15:36:20 -0700 | [diff] [blame] | 40 | import android.provider.Settings; |
Sunny Goyal | 3fbca15 | 2017-11-02 18:55:44 -0700 | [diff] [blame] | 41 | import android.text.TextUtils; |
Sunny Goyal | 3fbca15 | 2017-11-02 18:55:44 -0700 | [diff] [blame] | 42 | import android.view.View; |
Sunny Goyal | 3fbca15 | 2017-11-02 18:55:44 -0700 | [diff] [blame] | 43 | import android.widget.Adapter; |
Sunny Goyal | ba406e2 | 2018-04-02 11:36:12 -0700 | [diff] [blame] | 44 | import android.widget.ListView; |
Rahul Chaturvedi | 7fc77ca | 2015-05-19 18:02:16 -0700 | [diff] [blame] | 45 | |
Sunny Goyal | ca18746 | 2017-03-31 20:09:34 -0700 | [diff] [blame] | 46 | import com.android.launcher3.graphics.IconShapeOverride; |
Sunny Goyal | 848cad5 | 2017-07-07 03:12:21 -0700 | [diff] [blame] | 47 | import com.android.launcher3.notification.NotificationListener; |
Sunny Goyal | ba406e2 | 2018-04-02 11:36:12 -0700 | [diff] [blame] | 48 | import com.android.launcher3.util.ListViewHighlighter; |
Tony | d48710c | 2017-07-27 23:23:58 -0700 | [diff] [blame] | 49 | import com.android.launcher3.util.SettingsObserver; |
Sunny Goyal | 848cad5 | 2017-07-07 03:12:21 -0700 | [diff] [blame] | 50 | import com.android.launcher3.views.ButtonPreference; |
Sunny Goyal | ba406e2 | 2018-04-02 11:36:12 -0700 | [diff] [blame] | 51 | |
| 52 | import java.util.Objects; |
Sunny Goyal | ca18746 | 2017-03-31 20:09:34 -0700 | [diff] [blame] | 53 | |
Rahul Chaturvedi | 7fc77ca | 2015-05-19 18:02:16 -0700 | [diff] [blame] | 54 | /** |
Rahul Chaturvedi | 799aa04 | 2015-06-01 21:26:41 -0400 | [diff] [blame] | 55 | * Settings activity for Launcher. Currently implements the following setting: Allow rotation |
Rahul Chaturvedi | 7fc77ca | 2015-05-19 18:02:16 -0700 | [diff] [blame] | 56 | */ |
Sunny Goyal | 7f920b8 | 2018-06-27 15:47:49 -0700 | [diff] [blame^] | 57 | public class SettingsActivity extends Activity |
| 58 | implements PreferenceFragment.OnPreferenceStartFragmentCallback { |
Tony Wickham | 2ab8482 | 2017-05-12 14:59:09 -0700 | [diff] [blame] | 59 | |
| 60 | private static final String ICON_BADGING_PREFERENCE_KEY = "pref_icon_badging"; |
Tony | d48710c | 2017-07-27 23:23:58 -0700 | [diff] [blame] | 61 | /** Hidden field Settings.Secure.NOTIFICATION_BADGING */ |
| 62 | public static final String NOTIFICATION_BADGING = "notification_badging"; |
Sunny Goyal | 848cad5 | 2017-07-07 03:12:21 -0700 | [diff] [blame] | 63 | /** Hidden field Settings.Secure.ENABLED_NOTIFICATION_LISTENERS */ |
| 64 | private static final String NOTIFICATION_ENABLED_LISTENERS = "enabled_notification_listeners"; |
Tony Wickham | 2ab8482 | 2017-05-12 14:59:09 -0700 | [diff] [blame] | 65 | |
Sunny Goyal | 3fbca15 | 2017-11-02 18:55:44 -0700 | [diff] [blame] | 66 | private static final String EXTRA_FRAGMENT_ARG_KEY = ":settings:fragment_args_key"; |
Sunny Goyal | d47f2d2 | 2018-04-18 14:11:37 -0700 | [diff] [blame] | 67 | private static final String EXTRA_SHOW_FRAGMENT_ARGS = ":settings:show_fragment_args"; |
Sunny Goyal | 3fbca15 | 2017-11-02 18:55:44 -0700 | [diff] [blame] | 68 | private static final int DELAY_HIGHLIGHT_DURATION_MILLIS = 600; |
| 69 | private static final String SAVE_HIGHLIGHTED_KEY = "android:preference_highlighted"; |
| 70 | |
Rahul Chaturvedi | 7fc77ca | 2015-05-19 18:02:16 -0700 | [diff] [blame] | 71 | @Override |
| 72 | protected void onCreate(Bundle savedInstanceState) { |
| 73 | super.onCreate(savedInstanceState); |
| 74 | |
Mario Bertschler | 2de2d67 | 2017-06-30 18:51:46 -0700 | [diff] [blame] | 75 | if (savedInstanceState == null) { |
Sunny Goyal | 7f920b8 | 2018-06-27 15:47:49 -0700 | [diff] [blame^] | 76 | Fragment f = Fragment.instantiate(this, getString(R.string.settings_fragment_name)); |
Mario Bertschler | 2de2d67 | 2017-06-30 18:51:46 -0700 | [diff] [blame] | 77 | // Display the fragment as the main content. |
| 78 | getFragmentManager().beginTransaction() |
Sunny Goyal | 7f920b8 | 2018-06-27 15:47:49 -0700 | [diff] [blame^] | 79 | .replace(android.R.id.content, f) |
Mario Bertschler | 2de2d67 | 2017-06-30 18:51:46 -0700 | [diff] [blame] | 80 | .commit(); |
| 81 | } |
Rahul Chaturvedi | 7fc77ca | 2015-05-19 18:02:16 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Sunny Goyal | 3fbca15 | 2017-11-02 18:55:44 -0700 | [diff] [blame] | 84 | protected PreferenceFragment getNewFragment() { |
| 85 | return new LauncherSettingsFragment(); |
| 86 | } |
| 87 | |
Sunny Goyal | 7f920b8 | 2018-06-27 15:47:49 -0700 | [diff] [blame^] | 88 | @Override |
| 89 | public boolean onPreferenceStartFragment( |
| 90 | PreferenceFragment preferenceFragment, Preference pref) { |
| 91 | Fragment f = Fragment.instantiate(this, pref.getFragment(), pref.getExtras()); |
| 92 | if (f instanceof DialogFragment) { |
| 93 | ((DialogFragment) f).show(getFragmentManager(), pref.getKey()); |
| 94 | } else { |
| 95 | getFragmentManager() |
| 96 | .beginTransaction() |
| 97 | .replace(android.R.id.content, f) |
| 98 | .addToBackStack(pref.getKey()) |
| 99 | .commit(); |
| 100 | } |
| 101 | return true; |
| 102 | } |
| 103 | |
Rahul Chaturvedi | 7fc77ca | 2015-05-19 18:02:16 -0700 | [diff] [blame] | 104 | /** |
| 105 | * This fragment shows the launcher preferences. |
| 106 | */ |
Sunny Goyal | f48e592 | 2016-05-12 15:36:20 -0700 | [diff] [blame] | 107 | public static class LauncherSettingsFragment extends PreferenceFragment { |
| 108 | |
Tony Wickham | 2ab8482 | 2017-05-12 14:59:09 -0700 | [diff] [blame] | 109 | private IconBadgingObserver mIconBadgingObserver; |
Sunny Goyal | f48e592 | 2016-05-12 15:36:20 -0700 | [diff] [blame] | 110 | |
Sunny Goyal | 3fbca15 | 2017-11-02 18:55:44 -0700 | [diff] [blame] | 111 | private String mPreferenceKey; |
| 112 | private boolean mPreferenceHighlighted = false; |
| 113 | |
| 114 | @Override |
Rahul Chaturvedi | 7fc77ca | 2015-05-19 18:02:16 -0700 | [diff] [blame] | 115 | public void onCreate(Bundle savedInstanceState) { |
| 116 | super.onCreate(savedInstanceState); |
Sunny Goyal | 3fbca15 | 2017-11-02 18:55:44 -0700 | [diff] [blame] | 117 | if (savedInstanceState != null) { |
| 118 | mPreferenceHighlighted = savedInstanceState.getBoolean(SAVE_HIGHLIGHTED_KEY); |
| 119 | } |
| 120 | |
Sunny Goyal | f48e592 | 2016-05-12 15:36:20 -0700 | [diff] [blame] | 121 | getPreferenceManager().setSharedPreferencesName(LauncherFiles.SHARED_PREFERENCES_KEY); |
Rahul Chaturvedi | 7fc77ca | 2015-05-19 18:02:16 -0700 | [diff] [blame] | 122 | addPreferencesFromResource(R.xml.launcher_preferences); |
Sunny Goyal | 7779d62 | 2015-06-11 16:18:39 -0700 | [diff] [blame] | 123 | |
Tony Wickham | 2ab8482 | 2017-05-12 14:59:09 -0700 | [diff] [blame] | 124 | ContentResolver resolver = getActivity().getContentResolver(); |
| 125 | |
Sunny Goyal | 848cad5 | 2017-07-07 03:12:21 -0700 | [diff] [blame] | 126 | ButtonPreference iconBadgingPref = |
| 127 | (ButtonPreference) findPreference(ICON_BADGING_PREFERENCE_KEY); |
Hyunyoung Song | e24cb63 | 2017-09-11 11:18:03 -0700 | [diff] [blame] | 128 | if (!Utilities.ATLEAST_OREO) { |
Sunny Goyal | 4179e9b | 2017-03-08 14:25:09 -0800 | [diff] [blame] | 129 | getPreferenceScreen().removePreference( |
| 130 | findPreference(SessionCommitReceiver.ADD_ICON_PREFERENCE_KEY)); |
Tony Wickham | 2ab8482 | 2017-05-12 14:59:09 -0700 | [diff] [blame] | 131 | getPreferenceScreen().removePreference(iconBadgingPref); |
Tony Wickham | c138515 | 2017-08-14 14:31:03 -0700 | [diff] [blame] | 132 | } else if (!getResources().getBoolean(R.bool.notification_badging_enabled)) { |
| 133 | getPreferenceScreen().removePreference(iconBadgingPref); |
Tony Wickham | 2ab8482 | 2017-05-12 14:59:09 -0700 | [diff] [blame] | 134 | } else { |
| 135 | // Listen to system notification badge settings while this UI is active. |
Sunny Goyal | 848cad5 | 2017-07-07 03:12:21 -0700 | [diff] [blame] | 136 | mIconBadgingObserver = new IconBadgingObserver( |
| 137 | iconBadgingPref, resolver, getFragmentManager()); |
Tony | d48710c | 2017-07-27 23:23:58 -0700 | [diff] [blame] | 138 | mIconBadgingObserver.register(NOTIFICATION_BADGING, NOTIFICATION_ENABLED_LISTENERS); |
Sunny Goyal | 4179e9b | 2017-03-08 14:25:09 -0800 | [diff] [blame] | 139 | } |
Sunny Goyal | ca18746 | 2017-03-31 20:09:34 -0700 | [diff] [blame] | 140 | |
| 141 | Preference iconShapeOverride = findPreference(IconShapeOverride.KEY_PREFERENCE); |
| 142 | if (iconShapeOverride != null) { |
| 143 | if (IconShapeOverride.isSupported(getActivity())) { |
| 144 | IconShapeOverride.handlePreferenceUi((ListPreference) iconShapeOverride); |
| 145 | } else { |
| 146 | getPreferenceScreen().removePreference(iconShapeOverride); |
| 147 | } |
| 148 | } |
Sunny Goyal | 4025332 | 2018-04-19 09:27:38 -0700 | [diff] [blame] | 149 | |
| 150 | // Setup allow rotation preference |
| 151 | Preference rotationPref = findPreference(ALLOW_ROTATION_PREFERENCE_KEY); |
| 152 | if (getResources().getBoolean(R.bool.allow_rotation)) { |
| 153 | // Launcher supports rotation by default. No need to show this setting. |
| 154 | getPreferenceScreen().removePreference(rotationPref); |
| 155 | } else { |
Sunny Goyal | 4025332 | 2018-04-19 09:27:38 -0700 | [diff] [blame] | 156 | // Initialize the UI once |
| 157 | rotationPref.setDefaultValue(getAllowRotationDefaultValue()); |
| 158 | } |
Rahul Chaturvedi | 7fc77ca | 2015-05-19 18:02:16 -0700 | [diff] [blame] | 159 | } |
Rahul Chaturvedi | 799aa04 | 2015-06-01 21:26:41 -0400 | [diff] [blame] | 160 | |
| 161 | @Override |
Sunny Goyal | 3fbca15 | 2017-11-02 18:55:44 -0700 | [diff] [blame] | 162 | public void onSaveInstanceState(Bundle outState) { |
| 163 | super.onSaveInstanceState(outState); |
| 164 | outState.putBoolean(SAVE_HIGHLIGHTED_KEY, mPreferenceHighlighted); |
| 165 | } |
| 166 | |
| 167 | @Override |
| 168 | public void onResume() { |
| 169 | super.onResume(); |
| 170 | |
| 171 | Intent intent = getActivity().getIntent(); |
| 172 | mPreferenceKey = intent.getStringExtra(EXTRA_FRAGMENT_ARG_KEY); |
| 173 | if (isAdded() && !mPreferenceHighlighted && !TextUtils.isEmpty(mPreferenceKey)) { |
| 174 | getView().postDelayed(this::highlightPreference, DELAY_HIGHLIGHT_DURATION_MILLIS); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | private void highlightPreference() { |
Sunny Goyal | 3fbca15 | 2017-11-02 18:55:44 -0700 | [diff] [blame] | 179 | Preference pref = findPreference(mPreferenceKey); |
Sunny Goyal | ba406e2 | 2018-04-02 11:36:12 -0700 | [diff] [blame] | 180 | if (pref == null || getPreferenceScreen() == null) { |
Sunny Goyal | 3fbca15 | 2017-11-02 18:55:44 -0700 | [diff] [blame] | 181 | return; |
| 182 | } |
Sunny Goyal | ba406e2 | 2018-04-02 11:36:12 -0700 | [diff] [blame] | 183 | PreferenceScreen screen = getPreferenceScreen(); |
| 184 | if (Utilities.ATLEAST_OREO) { |
| 185 | screen = selectPreferenceRecursive(pref, screen); |
| 186 | } |
| 187 | if (screen == null) { |
| 188 | return; |
| 189 | } |
| 190 | |
| 191 | View root = screen.getDialog() != null |
| 192 | ? screen.getDialog().getWindow().getDecorView() : getView(); |
| 193 | ListView list = root.findViewById(android.R.id.list); |
| 194 | if (list == null || list.getAdapter() == null) { |
| 195 | return; |
| 196 | } |
| 197 | Adapter adapter = list.getAdapter(); |
Sunny Goyal | 3fbca15 | 2017-11-02 18:55:44 -0700 | [diff] [blame] | 198 | |
| 199 | // Find the position |
| 200 | int position = -1; |
| 201 | for (int i = adapter.getCount() - 1; i >= 0; i--) { |
| 202 | if (pref == adapter.getItem(i)) { |
| 203 | position = i; |
| 204 | break; |
| 205 | } |
| 206 | } |
Sunny Goyal | ba406e2 | 2018-04-02 11:36:12 -0700 | [diff] [blame] | 207 | new ListViewHighlighter(list, position); |
Sunny Goyal | 3fbca15 | 2017-11-02 18:55:44 -0700 | [diff] [blame] | 208 | mPreferenceHighlighted = true; |
| 209 | } |
| 210 | |
| 211 | @Override |
Sunny Goyal | f48e592 | 2016-05-12 15:36:20 -0700 | [diff] [blame] | 212 | public void onDestroy() { |
Tony Wickham | 2ab8482 | 2017-05-12 14:59:09 -0700 | [diff] [blame] | 213 | if (mIconBadgingObserver != null) { |
Tony | d48710c | 2017-07-27 23:23:58 -0700 | [diff] [blame] | 214 | mIconBadgingObserver.unregister(); |
Tony Wickham | 2ab8482 | 2017-05-12 14:59:09 -0700 | [diff] [blame] | 215 | mIconBadgingObserver = null; |
| 216 | } |
Sunny Goyal | f48e592 | 2016-05-12 15:36:20 -0700 | [diff] [blame] | 217 | super.onDestroy(); |
| 218 | } |
Sunny Goyal | ba406e2 | 2018-04-02 11:36:12 -0700 | [diff] [blame] | 219 | |
| 220 | @TargetApi(Build.VERSION_CODES.O) |
| 221 | private PreferenceScreen selectPreferenceRecursive( |
| 222 | Preference pref, PreferenceScreen topParent) { |
| 223 | if (!(pref.getParent() instanceof PreferenceScreen)) { |
| 224 | return null; |
| 225 | } |
| 226 | |
| 227 | PreferenceScreen parent = (PreferenceScreen) pref.getParent(); |
| 228 | if (Objects.equals(parent.getKey(), topParent.getKey())) { |
| 229 | return parent; |
| 230 | } else if (selectPreferenceRecursive(parent, topParent) != null) { |
| 231 | ((PreferenceScreen) parent.getParent()) |
| 232 | .onItemClick(null, null, parent.getOrder(), 0); |
| 233 | return parent; |
| 234 | } else { |
| 235 | return null; |
| 236 | } |
| 237 | } |
Sunny Goyal | f48e592 | 2016-05-12 15:36:20 -0700 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | /** |
Tony Wickham | 2ab8482 | 2017-05-12 14:59:09 -0700 | [diff] [blame] | 241 | * Content observer which listens for system badging setting changes, |
| 242 | * and updates the launcher badging setting subtext accordingly. |
| 243 | */ |
Tony | d48710c | 2017-07-27 23:23:58 -0700 | [diff] [blame] | 244 | private static class IconBadgingObserver extends SettingsObserver.Secure |
Sunny Goyal | b65e13c | 2017-08-01 11:04:15 -0700 | [diff] [blame] | 245 | implements Preference.OnPreferenceClickListener { |
Tony Wickham | 2ab8482 | 2017-05-12 14:59:09 -0700 | [diff] [blame] | 246 | |
Sunny Goyal | 848cad5 | 2017-07-07 03:12:21 -0700 | [diff] [blame] | 247 | private final ButtonPreference mBadgingPref; |
Tony Wickham | 2ab8482 | 2017-05-12 14:59:09 -0700 | [diff] [blame] | 248 | private final ContentResolver mResolver; |
Sunny Goyal | 848cad5 | 2017-07-07 03:12:21 -0700 | [diff] [blame] | 249 | private final FragmentManager mFragmentManager; |
Tony Wickham | 2ab8482 | 2017-05-12 14:59:09 -0700 | [diff] [blame] | 250 | |
Sunny Goyal | 848cad5 | 2017-07-07 03:12:21 -0700 | [diff] [blame] | 251 | public IconBadgingObserver(ButtonPreference badgingPref, ContentResolver resolver, |
| 252 | FragmentManager fragmentManager) { |
Tony | d48710c | 2017-07-27 23:23:58 -0700 | [diff] [blame] | 253 | super(resolver); |
Tony Wickham | 2ab8482 | 2017-05-12 14:59:09 -0700 | [diff] [blame] | 254 | mBadgingPref = badgingPref; |
| 255 | mResolver = resolver; |
Sunny Goyal | 848cad5 | 2017-07-07 03:12:21 -0700 | [diff] [blame] | 256 | mFragmentManager = fragmentManager; |
Tony Wickham | 2ab8482 | 2017-05-12 14:59:09 -0700 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | @Override |
Tony | d48710c | 2017-07-27 23:23:58 -0700 | [diff] [blame] | 260 | public void onSettingChanged(boolean enabled) { |
Sunny Goyal | 848cad5 | 2017-07-07 03:12:21 -0700 | [diff] [blame] | 261 | int summary = enabled ? R.string.icon_badging_desc_on : R.string.icon_badging_desc_off; |
| 262 | |
| 263 | boolean serviceEnabled = true; |
| 264 | if (enabled) { |
| 265 | // Check if the listener is enabled or not. |
| 266 | String enabledListeners = |
| 267 | Settings.Secure.getString(mResolver, NOTIFICATION_ENABLED_LISTENERS); |
| 268 | ComponentName myListener = |
| 269 | new ComponentName(mBadgingPref.getContext(), NotificationListener.class); |
| 270 | serviceEnabled = enabledListeners != null && |
| 271 | (enabledListeners.contains(myListener.flattenToString()) || |
| 272 | enabledListeners.contains(myListener.flattenToShortString())); |
| 273 | if (!serviceEnabled) { |
| 274 | summary = R.string.title_missing_notification_access; |
| 275 | } |
| 276 | } |
Sunny Goyal | b65e13c | 2017-08-01 11:04:15 -0700 | [diff] [blame] | 277 | mBadgingPref.setWidgetFrameVisible(!serviceEnabled); |
| 278 | mBadgingPref.setOnPreferenceClickListener(serviceEnabled ? null : this); |
Sunny Goyal | 848cad5 | 2017-07-07 03:12:21 -0700 | [diff] [blame] | 279 | mBadgingPref.setSummary(summary); |
| 280 | |
| 281 | } |
| 282 | |
| 283 | @Override |
Sunny Goyal | b65e13c | 2017-08-01 11:04:15 -0700 | [diff] [blame] | 284 | public boolean onPreferenceClick(Preference preference) { |
Sunny Goyal | 848cad5 | 2017-07-07 03:12:21 -0700 | [diff] [blame] | 285 | new NotificationAccessConfirmation().show(mFragmentManager, "notification_access"); |
Sunny Goyal | b65e13c | 2017-08-01 11:04:15 -0700 | [diff] [blame] | 286 | return true; |
Tony Wickham | 2ab8482 | 2017-05-12 14:59:09 -0700 | [diff] [blame] | 287 | } |
| 288 | } |
| 289 | |
Sunny Goyal | 848cad5 | 2017-07-07 03:12:21 -0700 | [diff] [blame] | 290 | public static class NotificationAccessConfirmation |
| 291 | extends DialogFragment implements DialogInterface.OnClickListener { |
| 292 | |
| 293 | @Override |
| 294 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
| 295 | final Context context = getActivity(); |
| 296 | String msg = context.getString(R.string.msg_missing_notification_access, |
| 297 | context.getString(R.string.derived_app_name)); |
| 298 | return new AlertDialog.Builder(context) |
| 299 | .setTitle(R.string.title_missing_notification_access) |
| 300 | .setMessage(msg) |
| 301 | .setNegativeButton(android.R.string.cancel, null) |
| 302 | .setPositiveButton(R.string.title_change_settings, this) |
| 303 | .create(); |
| 304 | } |
| 305 | |
| 306 | @Override |
| 307 | public void onClick(DialogInterface dialogInterface, int i) { |
| 308 | ComponentName cn = new ComponentName(getActivity(), NotificationListener.class); |
Sunny Goyal | d47f2d2 | 2018-04-18 14:11:37 -0700 | [diff] [blame] | 309 | Bundle showFragmentArgs = new Bundle(); |
| 310 | showFragmentArgs.putString(EXTRA_FRAGMENT_ARG_KEY, cn.flattenToString()); |
| 311 | |
Sunny Goyal | 848cad5 | 2017-07-07 03:12:21 -0700 | [diff] [blame] | 312 | Intent intent = new Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS) |
| 313 | .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) |
Sunny Goyal | d47f2d2 | 2018-04-18 14:11:37 -0700 | [diff] [blame] | 314 | .putExtra(EXTRA_FRAGMENT_ARG_KEY, cn.flattenToString()) |
| 315 | .putExtra(EXTRA_SHOW_FRAGMENT_ARGS, showFragmentArgs); |
Sunny Goyal | 848cad5 | 2017-07-07 03:12:21 -0700 | [diff] [blame] | 316 | getActivity().startActivity(intent); |
| 317 | } |
| 318 | } |
Rahul Chaturvedi | 7fc77ca | 2015-05-19 18:02:16 -0700 | [diff] [blame] | 319 | } |