blob: 60edcda323a5ce9ae5a719def09704e31f60e938 [file] [log] [blame]
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001/*
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
17package com.android.launcher3;
18
Sunny Goyal40253322018-04-19 09:27:38 -070019import static com.android.launcher3.states.RotationHelper.ALLOW_ROTATION_PREFERENCE_KEY;
20import static com.android.launcher3.states.RotationHelper.getAllowRotationDefaultValue;
21
Sunny Goyalba406e22018-04-02 11:36:12 -070022import android.annotation.TargetApi;
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070023import android.app.Activity;
Sunny Goyal848cad52017-07-07 03:12:21 -070024import android.app.AlertDialog;
25import android.app.Dialog;
26import android.app.DialogFragment;
Sunny Goyal7f920b82018-06-27 15:47:49 -070027import android.app.Fragment;
Sunny Goyal848cad52017-07-07 03:12:21 -070028import android.app.FragmentManager;
29import android.content.ComponentName;
Sunny Goyalf48e5922016-05-12 15:36:20 -070030import android.content.ContentResolver;
Sunny Goyal848cad52017-07-07 03:12:21 -070031import android.content.Context;
32import android.content.DialogInterface;
33import android.content.Intent;
Sunny Goyalba406e22018-04-02 11:36:12 -070034import android.os.Build;
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070035import android.os.Bundle;
Sunny Goyalca187462017-03-31 20:09:34 -070036import android.preference.ListPreference;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -040037import android.preference.Preference;
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070038import android.preference.PreferenceFragment;
Sunny Goyalba406e22018-04-02 11:36:12 -070039import android.preference.PreferenceScreen;
Sunny Goyalf48e5922016-05-12 15:36:20 -070040import android.provider.Settings;
Sunny Goyal3fbca152017-11-02 18:55:44 -070041import android.text.TextUtils;
Sunny Goyal3fbca152017-11-02 18:55:44 -070042import android.view.View;
Sunny Goyal3fbca152017-11-02 18:55:44 -070043import android.widget.Adapter;
Sunny Goyalba406e22018-04-02 11:36:12 -070044import android.widget.ListView;
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070045
Ryan Lothianfa530cd2018-10-12 14:14:16 -040046import com.android.launcher3.config.FeatureFlags;
Sunny Goyalca187462017-03-31 20:09:34 -070047import com.android.launcher3.graphics.IconShapeOverride;
Sunny Goyal848cad52017-07-07 03:12:21 -070048import com.android.launcher3.notification.NotificationListener;
Sunny Goyalba406e22018-04-02 11:36:12 -070049import com.android.launcher3.util.ListViewHighlighter;
Tonyd48710c2017-07-27 23:23:58 -070050import com.android.launcher3.util.SettingsObserver;
Sunny Goyal848cad52017-07-07 03:12:21 -070051import com.android.launcher3.views.ButtonPreference;
Sunny Goyalba406e22018-04-02 11:36:12 -070052
53import java.util.Objects;
Sunny Goyalca187462017-03-31 20:09:34 -070054
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070055/**
Rahul Chaturvedi799aa042015-06-01 21:26:41 -040056 * Settings activity for Launcher. Currently implements the following setting: Allow rotation
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070057 */
Sunny Goyal7f920b82018-06-27 15:47:49 -070058public class SettingsActivity extends Activity
59 implements PreferenceFragment.OnPreferenceStartFragmentCallback {
Tony Wickham2ab84822017-05-12 14:59:09 -070060
Ryan Lothianfa530cd2018-10-12 14:14:16 -040061 private static final String FLAGS_PREFERENCE_KEY = "flag_toggler";
62
Tony Wickham2ab84822017-05-12 14:59:09 -070063 private static final String ICON_BADGING_PREFERENCE_KEY = "pref_icon_badging";
Tonyd48710c2017-07-27 23:23:58 -070064 /** Hidden field Settings.Secure.NOTIFICATION_BADGING */
65 public static final String NOTIFICATION_BADGING = "notification_badging";
Sunny Goyal848cad52017-07-07 03:12:21 -070066 /** Hidden field Settings.Secure.ENABLED_NOTIFICATION_LISTENERS */
67 private static final String NOTIFICATION_ENABLED_LISTENERS = "enabled_notification_listeners";
Tony Wickham2ab84822017-05-12 14:59:09 -070068
Sunny Goyal3fbca152017-11-02 18:55:44 -070069 private static final String EXTRA_FRAGMENT_ARG_KEY = ":settings:fragment_args_key";
Sunny Goyald47f2d22018-04-18 14:11:37 -070070 private static final String EXTRA_SHOW_FRAGMENT_ARGS = ":settings:show_fragment_args";
Sunny Goyal3fbca152017-11-02 18:55:44 -070071 private static final int DELAY_HIGHLIGHT_DURATION_MILLIS = 600;
72 private static final String SAVE_HIGHLIGHTED_KEY = "android:preference_highlighted";
73
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070074 @Override
75 protected void onCreate(Bundle savedInstanceState) {
76 super.onCreate(savedInstanceState);
77
Mario Bertschler2de2d672017-06-30 18:51:46 -070078 if (savedInstanceState == null) {
Sunny Goyal7f920b82018-06-27 15:47:49 -070079 Fragment f = Fragment.instantiate(this, getString(R.string.settings_fragment_name));
Mario Bertschler2de2d672017-06-30 18:51:46 -070080 // Display the fragment as the main content.
81 getFragmentManager().beginTransaction()
Sunny Goyal7f920b82018-06-27 15:47:49 -070082 .replace(android.R.id.content, f)
Mario Bertschler2de2d672017-06-30 18:51:46 -070083 .commit();
84 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070085 }
86
Sunny Goyal3fbca152017-11-02 18:55:44 -070087 protected PreferenceFragment getNewFragment() {
88 return new LauncherSettingsFragment();
89 }
90
Sunny Goyal7f920b82018-06-27 15:47:49 -070091 @Override
92 public boolean onPreferenceStartFragment(
93 PreferenceFragment preferenceFragment, Preference pref) {
Sunny Goyal5077aef2018-09-17 15:22:13 -070094 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 Goyal7f920b82018-06-27 15:47:49 -070099 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 Chaturvedi7fc77ca2015-05-19 18:02:16 -0700112 /**
113 * This fragment shows the launcher preferences.
114 */
Sunny Goyalf48e5922016-05-12 15:36:20 -0700115 public static class LauncherSettingsFragment extends PreferenceFragment {
116
Tony Wickham2ab84822017-05-12 14:59:09 -0700117 private IconBadgingObserver mIconBadgingObserver;
Sunny Goyalf48e5922016-05-12 15:36:20 -0700118
Sunny Goyal3fbca152017-11-02 18:55:44 -0700119 private String mPreferenceKey;
120 private boolean mPreferenceHighlighted = false;
121
122 @Override
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700123 public void onCreate(Bundle savedInstanceState) {
124 super.onCreate(savedInstanceState);
Sunny Goyal3fbca152017-11-02 18:55:44 -0700125 if (savedInstanceState != null) {
126 mPreferenceHighlighted = savedInstanceState.getBoolean(SAVE_HIGHLIGHTED_KEY);
127 }
128
Sunny Goyalf48e5922016-05-12 15:36:20 -0700129 getPreferenceManager().setSharedPreferencesName(LauncherFiles.SHARED_PREFERENCES_KEY);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700130 addPreferencesFromResource(R.xml.launcher_preferences);
Sunny Goyal7779d622015-06-11 16:18:39 -0700131
Ryan Lothianfa530cd2018-10-12 14:14:16 -0400132 // 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 Wickham2ab84822017-05-12 14:59:09 -0700138 ContentResolver resolver = getActivity().getContentResolver();
139
Sunny Goyal848cad52017-07-07 03:12:21 -0700140 ButtonPreference iconBadgingPref =
141 (ButtonPreference) findPreference(ICON_BADGING_PREFERENCE_KEY);
Hyunyoung Songe24cb632017-09-11 11:18:03 -0700142 if (!Utilities.ATLEAST_OREO) {
Sunny Goyal4179e9b2017-03-08 14:25:09 -0800143 getPreferenceScreen().removePreference(
144 findPreference(SessionCommitReceiver.ADD_ICON_PREFERENCE_KEY));
Tony Wickham2ab84822017-05-12 14:59:09 -0700145 getPreferenceScreen().removePreference(iconBadgingPref);
Tony Wickhamc1385152017-08-14 14:31:03 -0700146 } else if (!getResources().getBoolean(R.bool.notification_badging_enabled)) {
147 getPreferenceScreen().removePreference(iconBadgingPref);
Tony Wickham2ab84822017-05-12 14:59:09 -0700148 } else {
149 // Listen to system notification badge settings while this UI is active.
Sunny Goyal848cad52017-07-07 03:12:21 -0700150 mIconBadgingObserver = new IconBadgingObserver(
151 iconBadgingPref, resolver, getFragmentManager());
Tonyd48710c2017-07-27 23:23:58 -0700152 mIconBadgingObserver.register(NOTIFICATION_BADGING, NOTIFICATION_ENABLED_LISTENERS);
Sunny Goyal4179e9b2017-03-08 14:25:09 -0800153 }
Sunny Goyalca187462017-03-31 20:09:34 -0700154
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 Goyal40253322018-04-19 09:27:38 -0700163
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 Goyal40253322018-04-19 09:27:38 -0700170 // Initialize the UI once
171 rotationPref.setDefaultValue(getAllowRotationDefaultValue());
172 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700173 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400174
175 @Override
Sunny Goyal3fbca152017-11-02 18:55:44 -0700176 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 Goyal3fbca152017-11-02 18:55:44 -0700193 Preference pref = findPreference(mPreferenceKey);
Sunny Goyalba406e22018-04-02 11:36:12 -0700194 if (pref == null || getPreferenceScreen() == null) {
Sunny Goyal3fbca152017-11-02 18:55:44 -0700195 return;
196 }
Sunny Goyalba406e22018-04-02 11:36:12 -0700197 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 Goyal3fbca152017-11-02 18:55:44 -0700212
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 Goyalba406e22018-04-02 11:36:12 -0700221 new ListViewHighlighter(list, position);
Sunny Goyal3fbca152017-11-02 18:55:44 -0700222 mPreferenceHighlighted = true;
223 }
224
225 @Override
Sunny Goyalf48e5922016-05-12 15:36:20 -0700226 public void onDestroy() {
Tony Wickham2ab84822017-05-12 14:59:09 -0700227 if (mIconBadgingObserver != null) {
Tonyd48710c2017-07-27 23:23:58 -0700228 mIconBadgingObserver.unregister();
Tony Wickham2ab84822017-05-12 14:59:09 -0700229 mIconBadgingObserver = null;
230 }
Sunny Goyalf48e5922016-05-12 15:36:20 -0700231 super.onDestroy();
232 }
Sunny Goyalba406e22018-04-02 11:36:12 -0700233
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 Goyalf48e5922016-05-12 15:36:20 -0700252 }
253
254 /**
Tony Wickham2ab84822017-05-12 14:59:09 -0700255 * Content observer which listens for system badging setting changes,
256 * and updates the launcher badging setting subtext accordingly.
257 */
Sunny Goyal5077aef2018-09-17 15:22:13 -0700258 private static class IconBadgingObserver extends SettingsObserver.Secure {
Tony Wickham2ab84822017-05-12 14:59:09 -0700259
Sunny Goyal848cad52017-07-07 03:12:21 -0700260 private final ButtonPreference mBadgingPref;
Tony Wickham2ab84822017-05-12 14:59:09 -0700261 private final ContentResolver mResolver;
Sunny Goyal848cad52017-07-07 03:12:21 -0700262 private final FragmentManager mFragmentManager;
Tony Wickham2ab84822017-05-12 14:59:09 -0700263
Sunny Goyal848cad52017-07-07 03:12:21 -0700264 public IconBadgingObserver(ButtonPreference badgingPref, ContentResolver resolver,
265 FragmentManager fragmentManager) {
Tonyd48710c2017-07-27 23:23:58 -0700266 super(resolver);
Tony Wickham2ab84822017-05-12 14:59:09 -0700267 mBadgingPref = badgingPref;
268 mResolver = resolver;
Sunny Goyal848cad52017-07-07 03:12:21 -0700269 mFragmentManager = fragmentManager;
Tony Wickham2ab84822017-05-12 14:59:09 -0700270 }
271
272 @Override
Tonyd48710c2017-07-27 23:23:58 -0700273 public void onSettingChanged(boolean enabled) {
Sunny Goyal848cad52017-07-07 03:12:21 -0700274 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 Goyalb65e13c2017-08-01 11:04:15 -0700290 mBadgingPref.setWidgetFrameVisible(!serviceEnabled);
Sunny Goyal5077aef2018-09-17 15:22:13 -0700291 mBadgingPref.setFragment(
292 serviceEnabled ? null : NotificationAccessConfirmation.class.getName());
Sunny Goyal848cad52017-07-07 03:12:21 -0700293 mBadgingPref.setSummary(summary);
294
295 }
Tony Wickham2ab84822017-05-12 14:59:09 -0700296 }
297
Sunny Goyal848cad52017-07-07 03:12:21 -0700298 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 Goyald47f2d22018-04-18 14:11:37 -0700317 Bundle showFragmentArgs = new Bundle();
318 showFragmentArgs.putString(EXTRA_FRAGMENT_ARG_KEY, cn.flattenToString());
319
Sunny Goyal848cad52017-07-07 03:12:21 -0700320 Intent intent = new Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS)
321 .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
Sunny Goyald47f2d22018-04-18 14:11:37 -0700322 .putExtra(EXTRA_FRAGMENT_ARG_KEY, cn.flattenToString())
323 .putExtra(EXTRA_SHOW_FRAGMENT_ARGS, showFragmentArgs);
Sunny Goyal848cad52017-07-07 03:12:21 -0700324 getActivity().startActivity(intent);
325 }
326 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700327}