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 | |
| 19 | import android.app.Activity; |
Sunny Goyal | 848cad5 | 2017-07-07 03:12:21 -0700 | [diff] [blame] | 20 | import android.app.AlertDialog; |
| 21 | import android.app.Dialog; |
| 22 | import android.app.DialogFragment; |
| 23 | import android.app.FragmentManager; |
| 24 | import android.content.ComponentName; |
Sunny Goyal | f48e592 | 2016-05-12 15:36:20 -0700 | [diff] [blame] | 25 | import android.content.ContentResolver; |
Sunny Goyal | 848cad5 | 2017-07-07 03:12:21 -0700 | [diff] [blame] | 26 | import android.content.Context; |
| 27 | import android.content.DialogInterface; |
| 28 | import android.content.Intent; |
Rahul Chaturvedi | 7fc77ca | 2015-05-19 18:02:16 -0700 | [diff] [blame] | 29 | import android.os.Bundle; |
Sunny Goyal | ca18746 | 2017-03-31 20:09:34 -0700 | [diff] [blame] | 30 | import android.preference.ListPreference; |
Rahul Chaturvedi | 799aa04 | 2015-06-01 21:26:41 -0400 | [diff] [blame] | 31 | import android.preference.Preference; |
Rahul Chaturvedi | 7fc77ca | 2015-05-19 18:02:16 -0700 | [diff] [blame] | 32 | import android.preference.PreferenceFragment; |
Sunny Goyal | f48e592 | 2016-05-12 15:36:20 -0700 | [diff] [blame] | 33 | import android.provider.Settings; |
Rahul Chaturvedi | 7fc77ca | 2015-05-19 18:02:16 -0700 | [diff] [blame] | 34 | |
Sunny Goyal | ca18746 | 2017-03-31 20:09:34 -0700 | [diff] [blame] | 35 | import com.android.launcher3.graphics.IconShapeOverride; |
Sunny Goyal | 848cad5 | 2017-07-07 03:12:21 -0700 | [diff] [blame] | 36 | import com.android.launcher3.notification.NotificationListener; |
Tony | d48710c | 2017-07-27 23:23:58 -0700 | [diff] [blame^] | 37 | import com.android.launcher3.util.SettingsObserver; |
Sunny Goyal | 848cad5 | 2017-07-07 03:12:21 -0700 | [diff] [blame] | 38 | import com.android.launcher3.views.ButtonPreference; |
Sunny Goyal | ca18746 | 2017-03-31 20:09:34 -0700 | [diff] [blame] | 39 | |
Rahul Chaturvedi | 7fc77ca | 2015-05-19 18:02:16 -0700 | [diff] [blame] | 40 | /** |
Rahul Chaturvedi | 799aa04 | 2015-06-01 21:26:41 -0400 | [diff] [blame] | 41 | * Settings activity for Launcher. Currently implements the following setting: Allow rotation |
Rahul Chaturvedi | 7fc77ca | 2015-05-19 18:02:16 -0700 | [diff] [blame] | 42 | */ |
| 43 | public class SettingsActivity extends Activity { |
Tony Wickham | 2ab8482 | 2017-05-12 14:59:09 -0700 | [diff] [blame] | 44 | |
| 45 | private static final String ICON_BADGING_PREFERENCE_KEY = "pref_icon_badging"; |
Tony | d48710c | 2017-07-27 23:23:58 -0700 | [diff] [blame^] | 46 | /** Hidden field Settings.Secure.NOTIFICATION_BADGING */ |
| 47 | public static final String NOTIFICATION_BADGING = "notification_badging"; |
Sunny Goyal | 848cad5 | 2017-07-07 03:12:21 -0700 | [diff] [blame] | 48 | /** Hidden field Settings.Secure.ENABLED_NOTIFICATION_LISTENERS */ |
| 49 | private static final String NOTIFICATION_ENABLED_LISTENERS = "enabled_notification_listeners"; |
Tony Wickham | 2ab8482 | 2017-05-12 14:59:09 -0700 | [diff] [blame] | 50 | |
Rahul Chaturvedi | 7fc77ca | 2015-05-19 18:02:16 -0700 | [diff] [blame] | 51 | @Override |
| 52 | protected void onCreate(Bundle savedInstanceState) { |
| 53 | super.onCreate(savedInstanceState); |
| 54 | |
Mario Bertschler | 2de2d67 | 2017-06-30 18:51:46 -0700 | [diff] [blame] | 55 | if (savedInstanceState == null) { |
| 56 | // Display the fragment as the main content. |
| 57 | getFragmentManager().beginTransaction() |
| 58 | .replace(android.R.id.content, new LauncherSettingsFragment()) |
| 59 | .commit(); |
| 60 | } |
Rahul Chaturvedi | 7fc77ca | 2015-05-19 18:02:16 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | /** |
| 64 | * This fragment shows the launcher preferences. |
| 65 | */ |
Sunny Goyal | f48e592 | 2016-05-12 15:36:20 -0700 | [diff] [blame] | 66 | public static class LauncherSettingsFragment extends PreferenceFragment { |
| 67 | |
| 68 | private SystemDisplayRotationLockObserver mRotationLockObserver; |
Tony Wickham | 2ab8482 | 2017-05-12 14:59:09 -0700 | [diff] [blame] | 69 | private IconBadgingObserver mIconBadgingObserver; |
Sunny Goyal | f48e592 | 2016-05-12 15:36:20 -0700 | [diff] [blame] | 70 | |
Rahul Chaturvedi | 7fc77ca | 2015-05-19 18:02:16 -0700 | [diff] [blame] | 71 | @Override |
| 72 | public void onCreate(Bundle savedInstanceState) { |
| 73 | super.onCreate(savedInstanceState); |
Sunny Goyal | f48e592 | 2016-05-12 15:36:20 -0700 | [diff] [blame] | 74 | getPreferenceManager().setSharedPreferencesName(LauncherFiles.SHARED_PREFERENCES_KEY); |
Rahul Chaturvedi | 7fc77ca | 2015-05-19 18:02:16 -0700 | [diff] [blame] | 75 | addPreferencesFromResource(R.xml.launcher_preferences); |
Sunny Goyal | 7779d62 | 2015-06-11 16:18:39 -0700 | [diff] [blame] | 76 | |
Tony Wickham | 2ab8482 | 2017-05-12 14:59:09 -0700 | [diff] [blame] | 77 | ContentResolver resolver = getActivity().getContentResolver(); |
| 78 | |
Sunny Goyal | f48e592 | 2016-05-12 15:36:20 -0700 | [diff] [blame] | 79 | // Setup allow rotation preference |
| 80 | Preference rotationPref = findPreference(Utilities.ALLOW_ROTATION_PREFERENCE_KEY); |
| 81 | if (getResources().getBoolean(R.bool.allow_rotation)) { |
| 82 | // Launcher supports rotation by default. No need to show this setting. |
| 83 | getPreferenceScreen().removePreference(rotationPref); |
| 84 | } else { |
Sunny Goyal | f48e592 | 2016-05-12 15:36:20 -0700 | [diff] [blame] | 85 | mRotationLockObserver = new SystemDisplayRotationLockObserver(rotationPref, resolver); |
| 86 | |
| 87 | // Register a content observer to listen for system setting changes while |
| 88 | // this UI is active. |
Tony | d48710c | 2017-07-27 23:23:58 -0700 | [diff] [blame^] | 89 | mRotationLockObserver.register(Settings.System.ACCELEROMETER_ROTATION); |
Sunny Goyal | f48e592 | 2016-05-12 15:36:20 -0700 | [diff] [blame] | 90 | |
| 91 | // Initialize the UI once |
Sunny Goyal | ab06999 | 2016-06-08 12:00:02 -0700 | [diff] [blame] | 92 | rotationPref.setDefaultValue(Utilities.getAllowRotationDefaultValue(getActivity())); |
Sunny Goyal | 745bad9 | 2016-05-02 10:54:12 -0700 | [diff] [blame] | 93 | } |
Sunny Goyal | 4179e9b | 2017-03-08 14:25:09 -0800 | [diff] [blame] | 94 | |
Sunny Goyal | 848cad5 | 2017-07-07 03:12:21 -0700 | [diff] [blame] | 95 | ButtonPreference iconBadgingPref = |
| 96 | (ButtonPreference) findPreference(ICON_BADGING_PREFERENCE_KEY); |
Sunny Goyal | de57caf | 2017-06-09 08:42:39 -0700 | [diff] [blame] | 97 | if (!Utilities.isAtLeastO()) { |
Sunny Goyal | 4179e9b | 2017-03-08 14:25:09 -0800 | [diff] [blame] | 98 | getPreferenceScreen().removePreference( |
| 99 | findPreference(SessionCommitReceiver.ADD_ICON_PREFERENCE_KEY)); |
Tony Wickham | 2ab8482 | 2017-05-12 14:59:09 -0700 | [diff] [blame] | 100 | getPreferenceScreen().removePreference(iconBadgingPref); |
| 101 | } else { |
| 102 | // Listen to system notification badge settings while this UI is active. |
Sunny Goyal | 848cad5 | 2017-07-07 03:12:21 -0700 | [diff] [blame] | 103 | mIconBadgingObserver = new IconBadgingObserver( |
| 104 | iconBadgingPref, resolver, getFragmentManager()); |
Tony | d48710c | 2017-07-27 23:23:58 -0700 | [diff] [blame^] | 105 | mIconBadgingObserver.register(NOTIFICATION_BADGING, NOTIFICATION_ENABLED_LISTENERS); |
Sunny Goyal | 4179e9b | 2017-03-08 14:25:09 -0800 | [diff] [blame] | 106 | } |
Sunny Goyal | ca18746 | 2017-03-31 20:09:34 -0700 | [diff] [blame] | 107 | |
| 108 | Preference iconShapeOverride = findPreference(IconShapeOverride.KEY_PREFERENCE); |
| 109 | if (iconShapeOverride != null) { |
| 110 | if (IconShapeOverride.isSupported(getActivity())) { |
| 111 | IconShapeOverride.handlePreferenceUi((ListPreference) iconShapeOverride); |
| 112 | } else { |
| 113 | getPreferenceScreen().removePreference(iconShapeOverride); |
| 114 | } |
| 115 | } |
Rahul Chaturvedi | 7fc77ca | 2015-05-19 18:02:16 -0700 | [diff] [blame] | 116 | } |
Rahul Chaturvedi | 799aa04 | 2015-06-01 21:26:41 -0400 | [diff] [blame] | 117 | |
| 118 | @Override |
Sunny Goyal | f48e592 | 2016-05-12 15:36:20 -0700 | [diff] [blame] | 119 | public void onDestroy() { |
| 120 | if (mRotationLockObserver != null) { |
Tony | d48710c | 2017-07-27 23:23:58 -0700 | [diff] [blame^] | 121 | mRotationLockObserver.unregister(); |
Sunny Goyal | f48e592 | 2016-05-12 15:36:20 -0700 | [diff] [blame] | 122 | mRotationLockObserver = null; |
| 123 | } |
Tony Wickham | 2ab8482 | 2017-05-12 14:59:09 -0700 | [diff] [blame] | 124 | if (mIconBadgingObserver != null) { |
Tony | d48710c | 2017-07-27 23:23:58 -0700 | [diff] [blame^] | 125 | mIconBadgingObserver.unregister(); |
Tony Wickham | 2ab8482 | 2017-05-12 14:59:09 -0700 | [diff] [blame] | 126 | mIconBadgingObserver = null; |
| 127 | } |
Sunny Goyal | f48e592 | 2016-05-12 15:36:20 -0700 | [diff] [blame] | 128 | super.onDestroy(); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Content observer which listens for system auto-rotate setting changes, and enables/disables |
| 134 | * the launcher rotation setting accordingly. |
| 135 | */ |
Tony | d48710c | 2017-07-27 23:23:58 -0700 | [diff] [blame^] | 136 | private static class SystemDisplayRotationLockObserver extends SettingsObserver.System { |
Sunny Goyal | f48e592 | 2016-05-12 15:36:20 -0700 | [diff] [blame] | 137 | |
| 138 | private final Preference mRotationPref; |
Sunny Goyal | f48e592 | 2016-05-12 15:36:20 -0700 | [diff] [blame] | 139 | |
| 140 | public SystemDisplayRotationLockObserver( |
| 141 | Preference rotationPref, ContentResolver resolver) { |
Tony | d48710c | 2017-07-27 23:23:58 -0700 | [diff] [blame^] | 142 | super(resolver); |
Sunny Goyal | f48e592 | 2016-05-12 15:36:20 -0700 | [diff] [blame] | 143 | mRotationPref = rotationPref; |
Rahul Chaturvedi | 799aa04 | 2015-06-01 21:26:41 -0400 | [diff] [blame] | 144 | } |
Sunny Goyal | 745bad9 | 2016-05-02 10:54:12 -0700 | [diff] [blame] | 145 | |
Sunny Goyal | f48e592 | 2016-05-12 15:36:20 -0700 | [diff] [blame] | 146 | @Override |
Tony | d48710c | 2017-07-27 23:23:58 -0700 | [diff] [blame^] | 147 | public void onSettingChanged(boolean enabled) { |
Sunny Goyal | f48e592 | 2016-05-12 15:36:20 -0700 | [diff] [blame] | 148 | mRotationPref.setEnabled(enabled); |
| 149 | mRotationPref.setSummary(enabled |
| 150 | ? R.string.allow_rotation_desc : R.string.allow_rotation_blocked_desc); |
Sunny Goyal | 745bad9 | 2016-05-02 10:54:12 -0700 | [diff] [blame] | 151 | } |
Rahul Chaturvedi | 7fc77ca | 2015-05-19 18:02:16 -0700 | [diff] [blame] | 152 | } |
Tony Wickham | 2ab8482 | 2017-05-12 14:59:09 -0700 | [diff] [blame] | 153 | |
| 154 | /** |
| 155 | * Content observer which listens for system badging setting changes, |
| 156 | * and updates the launcher badging setting subtext accordingly. |
| 157 | */ |
Tony | d48710c | 2017-07-27 23:23:58 -0700 | [diff] [blame^] | 158 | private static class IconBadgingObserver extends SettingsObserver.Secure |
Sunny Goyal | b65e13c | 2017-08-01 11:04:15 -0700 | [diff] [blame] | 159 | implements Preference.OnPreferenceClickListener { |
Tony Wickham | 2ab8482 | 2017-05-12 14:59:09 -0700 | [diff] [blame] | 160 | |
Sunny Goyal | 848cad5 | 2017-07-07 03:12:21 -0700 | [diff] [blame] | 161 | private final ButtonPreference mBadgingPref; |
Tony Wickham | 2ab8482 | 2017-05-12 14:59:09 -0700 | [diff] [blame] | 162 | private final ContentResolver mResolver; |
Sunny Goyal | 848cad5 | 2017-07-07 03:12:21 -0700 | [diff] [blame] | 163 | private final FragmentManager mFragmentManager; |
Tony Wickham | 2ab8482 | 2017-05-12 14:59:09 -0700 | [diff] [blame] | 164 | |
Sunny Goyal | 848cad5 | 2017-07-07 03:12:21 -0700 | [diff] [blame] | 165 | public IconBadgingObserver(ButtonPreference badgingPref, ContentResolver resolver, |
| 166 | FragmentManager fragmentManager) { |
Tony | d48710c | 2017-07-27 23:23:58 -0700 | [diff] [blame^] | 167 | super(resolver); |
Tony Wickham | 2ab8482 | 2017-05-12 14:59:09 -0700 | [diff] [blame] | 168 | mBadgingPref = badgingPref; |
| 169 | mResolver = resolver; |
Sunny Goyal | 848cad5 | 2017-07-07 03:12:21 -0700 | [diff] [blame] | 170 | mFragmentManager = fragmentManager; |
Tony Wickham | 2ab8482 | 2017-05-12 14:59:09 -0700 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | @Override |
Tony | d48710c | 2017-07-27 23:23:58 -0700 | [diff] [blame^] | 174 | public void onSettingChanged(boolean enabled) { |
Sunny Goyal | 848cad5 | 2017-07-07 03:12:21 -0700 | [diff] [blame] | 175 | int summary = enabled ? R.string.icon_badging_desc_on : R.string.icon_badging_desc_off; |
| 176 | |
| 177 | boolean serviceEnabled = true; |
| 178 | if (enabled) { |
| 179 | // Check if the listener is enabled or not. |
| 180 | String enabledListeners = |
| 181 | Settings.Secure.getString(mResolver, NOTIFICATION_ENABLED_LISTENERS); |
| 182 | ComponentName myListener = |
| 183 | new ComponentName(mBadgingPref.getContext(), NotificationListener.class); |
| 184 | serviceEnabled = enabledListeners != null && |
| 185 | (enabledListeners.contains(myListener.flattenToString()) || |
| 186 | enabledListeners.contains(myListener.flattenToShortString())); |
| 187 | if (!serviceEnabled) { |
| 188 | summary = R.string.title_missing_notification_access; |
| 189 | } |
| 190 | } |
Sunny Goyal | b65e13c | 2017-08-01 11:04:15 -0700 | [diff] [blame] | 191 | mBadgingPref.setWidgetFrameVisible(!serviceEnabled); |
| 192 | mBadgingPref.setOnPreferenceClickListener(serviceEnabled ? null : this); |
Sunny Goyal | 848cad5 | 2017-07-07 03:12:21 -0700 | [diff] [blame] | 193 | mBadgingPref.setSummary(summary); |
| 194 | |
| 195 | } |
| 196 | |
| 197 | @Override |
Sunny Goyal | b65e13c | 2017-08-01 11:04:15 -0700 | [diff] [blame] | 198 | public boolean onPreferenceClick(Preference preference) { |
Sunny Goyal | 848cad5 | 2017-07-07 03:12:21 -0700 | [diff] [blame] | 199 | new NotificationAccessConfirmation().show(mFragmentManager, "notification_access"); |
Sunny Goyal | b65e13c | 2017-08-01 11:04:15 -0700 | [diff] [blame] | 200 | return true; |
Tony Wickham | 2ab8482 | 2017-05-12 14:59:09 -0700 | [diff] [blame] | 201 | } |
| 202 | } |
| 203 | |
Sunny Goyal | 848cad5 | 2017-07-07 03:12:21 -0700 | [diff] [blame] | 204 | public static class NotificationAccessConfirmation |
| 205 | extends DialogFragment implements DialogInterface.OnClickListener { |
| 206 | |
| 207 | @Override |
| 208 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
| 209 | final Context context = getActivity(); |
| 210 | String msg = context.getString(R.string.msg_missing_notification_access, |
| 211 | context.getString(R.string.derived_app_name)); |
| 212 | return new AlertDialog.Builder(context) |
| 213 | .setTitle(R.string.title_missing_notification_access) |
| 214 | .setMessage(msg) |
| 215 | .setNegativeButton(android.R.string.cancel, null) |
| 216 | .setPositiveButton(R.string.title_change_settings, this) |
| 217 | .create(); |
| 218 | } |
| 219 | |
| 220 | @Override |
| 221 | public void onClick(DialogInterface dialogInterface, int i) { |
| 222 | ComponentName cn = new ComponentName(getActivity(), NotificationListener.class); |
| 223 | Intent intent = new Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS) |
| 224 | .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) |
| 225 | .putExtra(":settings:fragment_args_key", cn.flattenToString()); |
| 226 | getActivity().startActivity(intent); |
| 227 | } |
| 228 | } |
Rahul Chaturvedi | 7fc77ca | 2015-05-19 18:02:16 -0700 | [diff] [blame] | 229 | } |