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