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; |
Sunny Goyal | 3fbca15 | 2017-11-02 18:55:44 -0700 | [diff] [blame^] | 34 | import android.text.TextUtils; |
| 35 | import android.view.LayoutInflater; |
| 36 | import android.view.View; |
| 37 | import android.view.ViewGroup; |
| 38 | import android.widget.Adapter; |
Rahul Chaturvedi | 7fc77ca | 2015-05-19 18:02:16 -0700 | [diff] [blame] | 39 | |
Sunny Goyal | ca18746 | 2017-03-31 20:09:34 -0700 | [diff] [blame] | 40 | import com.android.launcher3.graphics.IconShapeOverride; |
Sunny Goyal | 848cad5 | 2017-07-07 03:12:21 -0700 | [diff] [blame] | 41 | import com.android.launcher3.notification.NotificationListener; |
Tony | d48710c | 2017-07-27 23:23:58 -0700 | [diff] [blame] | 42 | import com.android.launcher3.util.SettingsObserver; |
Sunny Goyal | 848cad5 | 2017-07-07 03:12:21 -0700 | [diff] [blame] | 43 | import com.android.launcher3.views.ButtonPreference; |
Sunny Goyal | 3fbca15 | 2017-11-02 18:55:44 -0700 | [diff] [blame^] | 44 | import com.android.launcher3.views.HighlightableListView; |
Sunny Goyal | ca18746 | 2017-03-31 20:09:34 -0700 | [diff] [blame] | 45 | |
Rahul Chaturvedi | 7fc77ca | 2015-05-19 18:02:16 -0700 | [diff] [blame] | 46 | /** |
Rahul Chaturvedi | 799aa04 | 2015-06-01 21:26:41 -0400 | [diff] [blame] | 47 | * Settings activity for Launcher. Currently implements the following setting: Allow rotation |
Rahul Chaturvedi | 7fc77ca | 2015-05-19 18:02:16 -0700 | [diff] [blame] | 48 | */ |
| 49 | public class SettingsActivity extends Activity { |
Tony Wickham | 2ab8482 | 2017-05-12 14:59:09 -0700 | [diff] [blame] | 50 | |
| 51 | private static final String ICON_BADGING_PREFERENCE_KEY = "pref_icon_badging"; |
Tony | d48710c | 2017-07-27 23:23:58 -0700 | [diff] [blame] | 52 | /** Hidden field Settings.Secure.NOTIFICATION_BADGING */ |
| 53 | public static final String NOTIFICATION_BADGING = "notification_badging"; |
Sunny Goyal | 848cad5 | 2017-07-07 03:12:21 -0700 | [diff] [blame] | 54 | /** Hidden field Settings.Secure.ENABLED_NOTIFICATION_LISTENERS */ |
| 55 | private static final String NOTIFICATION_ENABLED_LISTENERS = "enabled_notification_listeners"; |
Tony Wickham | 2ab8482 | 2017-05-12 14:59:09 -0700 | [diff] [blame] | 56 | |
Sunny Goyal | 3fbca15 | 2017-11-02 18:55:44 -0700 | [diff] [blame^] | 57 | private static final String EXTRA_FRAGMENT_ARG_KEY = ":settings:fragment_args_key"; |
| 58 | private static final int DELAY_HIGHLIGHT_DURATION_MILLIS = 600; |
| 59 | private static final String SAVE_HIGHLIGHTED_KEY = "android:preference_highlighted"; |
| 60 | |
Rahul Chaturvedi | 7fc77ca | 2015-05-19 18:02:16 -0700 | [diff] [blame] | 61 | @Override |
| 62 | protected void onCreate(Bundle savedInstanceState) { |
| 63 | super.onCreate(savedInstanceState); |
| 64 | |
Mario Bertschler | 2de2d67 | 2017-06-30 18:51:46 -0700 | [diff] [blame] | 65 | if (savedInstanceState == null) { |
| 66 | // Display the fragment as the main content. |
| 67 | getFragmentManager().beginTransaction() |
Sunny Goyal | 3fbca15 | 2017-11-02 18:55:44 -0700 | [diff] [blame^] | 68 | .replace(android.R.id.content, getNewFragment()) |
Mario Bertschler | 2de2d67 | 2017-06-30 18:51:46 -0700 | [diff] [blame] | 69 | .commit(); |
| 70 | } |
Rahul Chaturvedi | 7fc77ca | 2015-05-19 18:02:16 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Sunny Goyal | 3fbca15 | 2017-11-02 18:55:44 -0700 | [diff] [blame^] | 73 | protected PreferenceFragment getNewFragment() { |
| 74 | return new LauncherSettingsFragment(); |
| 75 | } |
| 76 | |
Rahul Chaturvedi | 7fc77ca | 2015-05-19 18:02:16 -0700 | [diff] [blame] | 77 | /** |
| 78 | * This fragment shows the launcher preferences. |
| 79 | */ |
Sunny Goyal | f48e592 | 2016-05-12 15:36:20 -0700 | [diff] [blame] | 80 | public static class LauncherSettingsFragment extends PreferenceFragment { |
| 81 | |
Tony Wickham | 2ab8482 | 2017-05-12 14:59:09 -0700 | [diff] [blame] | 82 | private IconBadgingObserver mIconBadgingObserver; |
Sunny Goyal | f48e592 | 2016-05-12 15:36:20 -0700 | [diff] [blame] | 83 | |
Sunny Goyal | 3fbca15 | 2017-11-02 18:55:44 -0700 | [diff] [blame^] | 84 | private String mPreferenceKey; |
| 85 | private boolean mPreferenceHighlighted = false; |
| 86 | |
| 87 | @Override |
| 88 | public View onCreateView(LayoutInflater inflater, ViewGroup container, |
| 89 | Bundle savedInstanceState) { |
| 90 | return inflater.inflate(R.layout.launcher_preference, container, false); |
| 91 | } |
| 92 | |
Rahul Chaturvedi | 7fc77ca | 2015-05-19 18:02:16 -0700 | [diff] [blame] | 93 | @Override |
| 94 | public void onCreate(Bundle savedInstanceState) { |
| 95 | super.onCreate(savedInstanceState); |
Sunny Goyal | 3fbca15 | 2017-11-02 18:55:44 -0700 | [diff] [blame^] | 96 | if (savedInstanceState != null) { |
| 97 | mPreferenceHighlighted = savedInstanceState.getBoolean(SAVE_HIGHLIGHTED_KEY); |
| 98 | } |
| 99 | |
Sunny Goyal | f48e592 | 2016-05-12 15:36:20 -0700 | [diff] [blame] | 100 | getPreferenceManager().setSharedPreferencesName(LauncherFiles.SHARED_PREFERENCES_KEY); |
Rahul Chaturvedi | 7fc77ca | 2015-05-19 18:02:16 -0700 | [diff] [blame] | 101 | addPreferencesFromResource(R.xml.launcher_preferences); |
Sunny Goyal | 7779d62 | 2015-06-11 16:18:39 -0700 | [diff] [blame] | 102 | |
Tony Wickham | 2ab8482 | 2017-05-12 14:59:09 -0700 | [diff] [blame] | 103 | ContentResolver resolver = getActivity().getContentResolver(); |
| 104 | |
Sunny Goyal | 848cad5 | 2017-07-07 03:12:21 -0700 | [diff] [blame] | 105 | ButtonPreference iconBadgingPref = |
| 106 | (ButtonPreference) findPreference(ICON_BADGING_PREFERENCE_KEY); |
Hyunyoung Song | e24cb63 | 2017-09-11 11:18:03 -0700 | [diff] [blame] | 107 | if (!Utilities.ATLEAST_OREO) { |
Sunny Goyal | 4179e9b | 2017-03-08 14:25:09 -0800 | [diff] [blame] | 108 | getPreferenceScreen().removePreference( |
| 109 | findPreference(SessionCommitReceiver.ADD_ICON_PREFERENCE_KEY)); |
Tony Wickham | 2ab8482 | 2017-05-12 14:59:09 -0700 | [diff] [blame] | 110 | getPreferenceScreen().removePreference(iconBadgingPref); |
Tony Wickham | c138515 | 2017-08-14 14:31:03 -0700 | [diff] [blame] | 111 | } else if (!getResources().getBoolean(R.bool.notification_badging_enabled)) { |
| 112 | getPreferenceScreen().removePreference(iconBadgingPref); |
Tony Wickham | 2ab8482 | 2017-05-12 14:59:09 -0700 | [diff] [blame] | 113 | } else { |
| 114 | // Listen to system notification badge settings while this UI is active. |
Sunny Goyal | 848cad5 | 2017-07-07 03:12:21 -0700 | [diff] [blame] | 115 | mIconBadgingObserver = new IconBadgingObserver( |
| 116 | iconBadgingPref, resolver, getFragmentManager()); |
Tony | d48710c | 2017-07-27 23:23:58 -0700 | [diff] [blame] | 117 | mIconBadgingObserver.register(NOTIFICATION_BADGING, NOTIFICATION_ENABLED_LISTENERS); |
Sunny Goyal | 4179e9b | 2017-03-08 14:25:09 -0800 | [diff] [blame] | 118 | } |
Sunny Goyal | ca18746 | 2017-03-31 20:09:34 -0700 | [diff] [blame] | 119 | |
| 120 | Preference iconShapeOverride = findPreference(IconShapeOverride.KEY_PREFERENCE); |
| 121 | if (iconShapeOverride != null) { |
| 122 | if (IconShapeOverride.isSupported(getActivity())) { |
| 123 | IconShapeOverride.handlePreferenceUi((ListPreference) iconShapeOverride); |
| 124 | } else { |
| 125 | getPreferenceScreen().removePreference(iconShapeOverride); |
| 126 | } |
| 127 | } |
Rahul Chaturvedi | 7fc77ca | 2015-05-19 18:02:16 -0700 | [diff] [blame] | 128 | } |
Rahul Chaturvedi | 799aa04 | 2015-06-01 21:26:41 -0400 | [diff] [blame] | 129 | |
| 130 | @Override |
Sunny Goyal | 3fbca15 | 2017-11-02 18:55:44 -0700 | [diff] [blame^] | 131 | public void onSaveInstanceState(Bundle outState) { |
| 132 | super.onSaveInstanceState(outState); |
| 133 | outState.putBoolean(SAVE_HIGHLIGHTED_KEY, mPreferenceHighlighted); |
| 134 | } |
| 135 | |
| 136 | @Override |
| 137 | public void onResume() { |
| 138 | super.onResume(); |
| 139 | |
| 140 | Intent intent = getActivity().getIntent(); |
| 141 | mPreferenceKey = intent.getStringExtra(EXTRA_FRAGMENT_ARG_KEY); |
| 142 | if (isAdded() && !mPreferenceHighlighted && !TextUtils.isEmpty(mPreferenceKey)) { |
| 143 | getView().postDelayed(this::highlightPreference, DELAY_HIGHLIGHT_DURATION_MILLIS); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | private void highlightPreference() { |
| 148 | HighlightableListView list = getView().findViewById(android.R.id.list); |
| 149 | Preference pref = findPreference(mPreferenceKey); |
| 150 | Adapter adapter = list.getAdapter(); |
| 151 | if (adapter == null) { |
| 152 | return; |
| 153 | } |
| 154 | |
| 155 | // Find the position |
| 156 | int position = -1; |
| 157 | for (int i = adapter.getCount() - 1; i >= 0; i--) { |
| 158 | if (pref == adapter.getItem(i)) { |
| 159 | position = i; |
| 160 | break; |
| 161 | } |
| 162 | } |
| 163 | list.highlightPosition(position); |
| 164 | mPreferenceHighlighted = true; |
| 165 | } |
| 166 | |
| 167 | @Override |
Sunny Goyal | f48e592 | 2016-05-12 15:36:20 -0700 | [diff] [blame] | 168 | public void onDestroy() { |
Tony Wickham | 2ab8482 | 2017-05-12 14:59:09 -0700 | [diff] [blame] | 169 | if (mIconBadgingObserver != null) { |
Tony | d48710c | 2017-07-27 23:23:58 -0700 | [diff] [blame] | 170 | mIconBadgingObserver.unregister(); |
Tony Wickham | 2ab8482 | 2017-05-12 14:59:09 -0700 | [diff] [blame] | 171 | mIconBadgingObserver = null; |
| 172 | } |
Sunny Goyal | f48e592 | 2016-05-12 15:36:20 -0700 | [diff] [blame] | 173 | super.onDestroy(); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | /** |
Tony Wickham | 2ab8482 | 2017-05-12 14:59:09 -0700 | [diff] [blame] | 178 | * Content observer which listens for system badging setting changes, |
| 179 | * and updates the launcher badging setting subtext accordingly. |
| 180 | */ |
Tony | d48710c | 2017-07-27 23:23:58 -0700 | [diff] [blame] | 181 | private static class IconBadgingObserver extends SettingsObserver.Secure |
Sunny Goyal | b65e13c | 2017-08-01 11:04:15 -0700 | [diff] [blame] | 182 | implements Preference.OnPreferenceClickListener { |
Tony Wickham | 2ab8482 | 2017-05-12 14:59:09 -0700 | [diff] [blame] | 183 | |
Sunny Goyal | 848cad5 | 2017-07-07 03:12:21 -0700 | [diff] [blame] | 184 | private final ButtonPreference mBadgingPref; |
Tony Wickham | 2ab8482 | 2017-05-12 14:59:09 -0700 | [diff] [blame] | 185 | private final ContentResolver mResolver; |
Sunny Goyal | 848cad5 | 2017-07-07 03:12:21 -0700 | [diff] [blame] | 186 | private final FragmentManager mFragmentManager; |
Tony Wickham | 2ab8482 | 2017-05-12 14:59:09 -0700 | [diff] [blame] | 187 | |
Sunny Goyal | 848cad5 | 2017-07-07 03:12:21 -0700 | [diff] [blame] | 188 | public IconBadgingObserver(ButtonPreference badgingPref, ContentResolver resolver, |
| 189 | FragmentManager fragmentManager) { |
Tony | d48710c | 2017-07-27 23:23:58 -0700 | [diff] [blame] | 190 | super(resolver); |
Tony Wickham | 2ab8482 | 2017-05-12 14:59:09 -0700 | [diff] [blame] | 191 | mBadgingPref = badgingPref; |
| 192 | mResolver = resolver; |
Sunny Goyal | 848cad5 | 2017-07-07 03:12:21 -0700 | [diff] [blame] | 193 | mFragmentManager = fragmentManager; |
Tony Wickham | 2ab8482 | 2017-05-12 14:59:09 -0700 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | @Override |
Tony | d48710c | 2017-07-27 23:23:58 -0700 | [diff] [blame] | 197 | public void onSettingChanged(boolean enabled) { |
Sunny Goyal | 848cad5 | 2017-07-07 03:12:21 -0700 | [diff] [blame] | 198 | int summary = enabled ? R.string.icon_badging_desc_on : R.string.icon_badging_desc_off; |
| 199 | |
| 200 | boolean serviceEnabled = true; |
| 201 | if (enabled) { |
| 202 | // Check if the listener is enabled or not. |
| 203 | String enabledListeners = |
| 204 | Settings.Secure.getString(mResolver, NOTIFICATION_ENABLED_LISTENERS); |
| 205 | ComponentName myListener = |
| 206 | new ComponentName(mBadgingPref.getContext(), NotificationListener.class); |
| 207 | serviceEnabled = enabledListeners != null && |
| 208 | (enabledListeners.contains(myListener.flattenToString()) || |
| 209 | enabledListeners.contains(myListener.flattenToShortString())); |
| 210 | if (!serviceEnabled) { |
| 211 | summary = R.string.title_missing_notification_access; |
| 212 | } |
| 213 | } |
Sunny Goyal | b65e13c | 2017-08-01 11:04:15 -0700 | [diff] [blame] | 214 | mBadgingPref.setWidgetFrameVisible(!serviceEnabled); |
| 215 | mBadgingPref.setOnPreferenceClickListener(serviceEnabled ? null : this); |
Sunny Goyal | 848cad5 | 2017-07-07 03:12:21 -0700 | [diff] [blame] | 216 | mBadgingPref.setSummary(summary); |
| 217 | |
| 218 | } |
| 219 | |
| 220 | @Override |
Sunny Goyal | b65e13c | 2017-08-01 11:04:15 -0700 | [diff] [blame] | 221 | public boolean onPreferenceClick(Preference preference) { |
Sunny Goyal | 848cad5 | 2017-07-07 03:12:21 -0700 | [diff] [blame] | 222 | new NotificationAccessConfirmation().show(mFragmentManager, "notification_access"); |
Sunny Goyal | b65e13c | 2017-08-01 11:04:15 -0700 | [diff] [blame] | 223 | return true; |
Tony Wickham | 2ab8482 | 2017-05-12 14:59:09 -0700 | [diff] [blame] | 224 | } |
| 225 | } |
| 226 | |
Sunny Goyal | 848cad5 | 2017-07-07 03:12:21 -0700 | [diff] [blame] | 227 | public static class NotificationAccessConfirmation |
| 228 | extends DialogFragment implements DialogInterface.OnClickListener { |
| 229 | |
| 230 | @Override |
| 231 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
| 232 | final Context context = getActivity(); |
| 233 | String msg = context.getString(R.string.msg_missing_notification_access, |
| 234 | context.getString(R.string.derived_app_name)); |
| 235 | return new AlertDialog.Builder(context) |
| 236 | .setTitle(R.string.title_missing_notification_access) |
| 237 | .setMessage(msg) |
| 238 | .setNegativeButton(android.R.string.cancel, null) |
| 239 | .setPositiveButton(R.string.title_change_settings, this) |
| 240 | .create(); |
| 241 | } |
| 242 | |
| 243 | @Override |
| 244 | public void onClick(DialogInterface dialogInterface, int i) { |
| 245 | ComponentName cn = new ComponentName(getActivity(), NotificationListener.class); |
| 246 | Intent intent = new Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS) |
| 247 | .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) |
| 248 | .putExtra(":settings:fragment_args_key", cn.flattenToString()); |
| 249 | getActivity().startActivity(intent); |
| 250 | } |
| 251 | } |
Rahul Chaturvedi | 7fc77ca | 2015-05-19 18:02:16 -0700 | [diff] [blame] | 252 | } |