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