blob: 1f802267c91c8e0c1d888f7624db31fde1cd04ea [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
Sunny Goyalca187462017-03-31 20:09:34 -070046import com.android.launcher3.graphics.IconShapeOverride;
Sunny Goyal848cad52017-07-07 03:12:21 -070047import com.android.launcher3.notification.NotificationListener;
Sunny Goyalba406e22018-04-02 11:36:12 -070048import com.android.launcher3.util.ListViewHighlighter;
Tonyd48710c2017-07-27 23:23:58 -070049import com.android.launcher3.util.SettingsObserver;
Sunny Goyal848cad52017-07-07 03:12:21 -070050import com.android.launcher3.views.ButtonPreference;
Sunny Goyalba406e22018-04-02 11:36:12 -070051
52import java.util.Objects;
Sunny Goyalca187462017-03-31 20:09:34 -070053
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070054/**
Rahul Chaturvedi799aa042015-06-01 21:26:41 -040055 * Settings activity for Launcher. Currently implements the following setting: Allow rotation
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070056 */
Sunny Goyal7f920b82018-06-27 15:47:49 -070057public class SettingsActivity extends Activity
58 implements PreferenceFragment.OnPreferenceStartFragmentCallback {
Tony Wickham2ab84822017-05-12 14:59:09 -070059
60 private static final String ICON_BADGING_PREFERENCE_KEY = "pref_icon_badging";
Tonyd48710c2017-07-27 23:23:58 -070061 /** Hidden field Settings.Secure.NOTIFICATION_BADGING */
62 public static final String NOTIFICATION_BADGING = "notification_badging";
Sunny Goyal848cad52017-07-07 03:12:21 -070063 /** Hidden field Settings.Secure.ENABLED_NOTIFICATION_LISTENERS */
64 private static final String NOTIFICATION_ENABLED_LISTENERS = "enabled_notification_listeners";
Tony Wickham2ab84822017-05-12 14:59:09 -070065
Sunny Goyal3fbca152017-11-02 18:55:44 -070066 private static final String EXTRA_FRAGMENT_ARG_KEY = ":settings:fragment_args_key";
Sunny Goyald47f2d22018-04-18 14:11:37 -070067 private static final String EXTRA_SHOW_FRAGMENT_ARGS = ":settings:show_fragment_args";
Sunny Goyal3fbca152017-11-02 18:55:44 -070068 private static final int DELAY_HIGHLIGHT_DURATION_MILLIS = 600;
69 private static final String SAVE_HIGHLIGHTED_KEY = "android:preference_highlighted";
70
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070071 @Override
72 protected void onCreate(Bundle savedInstanceState) {
73 super.onCreate(savedInstanceState);
74
Mario Bertschler2de2d672017-06-30 18:51:46 -070075 if (savedInstanceState == null) {
Sunny Goyal7f920b82018-06-27 15:47:49 -070076 Fragment f = Fragment.instantiate(this, getString(R.string.settings_fragment_name));
Mario Bertschler2de2d672017-06-30 18:51:46 -070077 // Display the fragment as the main content.
78 getFragmentManager().beginTransaction()
Sunny Goyal7f920b82018-06-27 15:47:49 -070079 .replace(android.R.id.content, f)
Mario Bertschler2de2d672017-06-30 18:51:46 -070080 .commit();
81 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070082 }
83
Sunny Goyal3fbca152017-11-02 18:55:44 -070084 protected PreferenceFragment getNewFragment() {
85 return new LauncherSettingsFragment();
86 }
87
Sunny Goyal7f920b82018-06-27 15:47:49 -070088 @Override
89 public boolean onPreferenceStartFragment(
90 PreferenceFragment preferenceFragment, Preference pref) {
Sunny Goyal5077aef2018-09-17 15:22:13 -070091 if (getFragmentManager().isStateSaved()) {
92 // Sometimes onClick can come after onPause because of being posted on the handler.
93 // Skip starting new fragments in that case.
94 return false;
95 }
Sunny Goyal7f920b82018-06-27 15:47:49 -070096 Fragment f = Fragment.instantiate(this, pref.getFragment(), pref.getExtras());
97 if (f instanceof DialogFragment) {
98 ((DialogFragment) f).show(getFragmentManager(), pref.getKey());
99 } else {
100 getFragmentManager()
101 .beginTransaction()
102 .replace(android.R.id.content, f)
103 .addToBackStack(pref.getKey())
104 .commit();
105 }
106 return true;
107 }
108
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700109 /**
110 * This fragment shows the launcher preferences.
111 */
Sunny Goyalf48e5922016-05-12 15:36:20 -0700112 public static class LauncherSettingsFragment extends PreferenceFragment {
113
Tony Wickham2ab84822017-05-12 14:59:09 -0700114 private IconBadgingObserver mIconBadgingObserver;
Sunny Goyalf48e5922016-05-12 15:36:20 -0700115
Sunny Goyal3fbca152017-11-02 18:55:44 -0700116 private String mPreferenceKey;
117 private boolean mPreferenceHighlighted = false;
118
119 @Override
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700120 public void onCreate(Bundle savedInstanceState) {
121 super.onCreate(savedInstanceState);
Sunny Goyal3fbca152017-11-02 18:55:44 -0700122 if (savedInstanceState != null) {
123 mPreferenceHighlighted = savedInstanceState.getBoolean(SAVE_HIGHLIGHTED_KEY);
124 }
125
Sunny Goyalf48e5922016-05-12 15:36:20 -0700126 getPreferenceManager().setSharedPreferencesName(LauncherFiles.SHARED_PREFERENCES_KEY);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700127 addPreferencesFromResource(R.xml.launcher_preferences);
Sunny Goyal7779d622015-06-11 16:18:39 -0700128
Tony Wickham2ab84822017-05-12 14:59:09 -0700129 ContentResolver resolver = getActivity().getContentResolver();
130
Sunny Goyal848cad52017-07-07 03:12:21 -0700131 ButtonPreference iconBadgingPref =
132 (ButtonPreference) findPreference(ICON_BADGING_PREFERENCE_KEY);
Hyunyoung Songe24cb632017-09-11 11:18:03 -0700133 if (!Utilities.ATLEAST_OREO) {
Sunny Goyal4179e9b2017-03-08 14:25:09 -0800134 getPreferenceScreen().removePreference(
135 findPreference(SessionCommitReceiver.ADD_ICON_PREFERENCE_KEY));
Tony Wickham2ab84822017-05-12 14:59:09 -0700136 getPreferenceScreen().removePreference(iconBadgingPref);
Tony Wickhamc1385152017-08-14 14:31:03 -0700137 } else if (!getResources().getBoolean(R.bool.notification_badging_enabled)) {
138 getPreferenceScreen().removePreference(iconBadgingPref);
Tony Wickham2ab84822017-05-12 14:59:09 -0700139 } else {
140 // Listen to system notification badge settings while this UI is active.
Sunny Goyal848cad52017-07-07 03:12:21 -0700141 mIconBadgingObserver = new IconBadgingObserver(
142 iconBadgingPref, resolver, getFragmentManager());
Tonyd48710c2017-07-27 23:23:58 -0700143 mIconBadgingObserver.register(NOTIFICATION_BADGING, NOTIFICATION_ENABLED_LISTENERS);
Sunny Goyal4179e9b2017-03-08 14:25:09 -0800144 }
Sunny Goyalca187462017-03-31 20:09:34 -0700145
146 Preference iconShapeOverride = findPreference(IconShapeOverride.KEY_PREFERENCE);
147 if (iconShapeOverride != null) {
148 if (IconShapeOverride.isSupported(getActivity())) {
149 IconShapeOverride.handlePreferenceUi((ListPreference) iconShapeOverride);
150 } else {
151 getPreferenceScreen().removePreference(iconShapeOverride);
152 }
153 }
Sunny Goyal40253322018-04-19 09:27:38 -0700154
155 // Setup allow rotation preference
156 Preference rotationPref = findPreference(ALLOW_ROTATION_PREFERENCE_KEY);
157 if (getResources().getBoolean(R.bool.allow_rotation)) {
158 // Launcher supports rotation by default. No need to show this setting.
159 getPreferenceScreen().removePreference(rotationPref);
160 } else {
Sunny Goyal40253322018-04-19 09:27:38 -0700161 // Initialize the UI once
162 rotationPref.setDefaultValue(getAllowRotationDefaultValue());
163 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700164 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400165
166 @Override
Sunny Goyal3fbca152017-11-02 18:55:44 -0700167 public void onSaveInstanceState(Bundle outState) {
168 super.onSaveInstanceState(outState);
169 outState.putBoolean(SAVE_HIGHLIGHTED_KEY, mPreferenceHighlighted);
170 }
171
172 @Override
173 public void onResume() {
174 super.onResume();
175
176 Intent intent = getActivity().getIntent();
177 mPreferenceKey = intent.getStringExtra(EXTRA_FRAGMENT_ARG_KEY);
178 if (isAdded() && !mPreferenceHighlighted && !TextUtils.isEmpty(mPreferenceKey)) {
179 getView().postDelayed(this::highlightPreference, DELAY_HIGHLIGHT_DURATION_MILLIS);
180 }
181 }
182
183 private void highlightPreference() {
Sunny Goyal3fbca152017-11-02 18:55:44 -0700184 Preference pref = findPreference(mPreferenceKey);
Sunny Goyalba406e22018-04-02 11:36:12 -0700185 if (pref == null || getPreferenceScreen() == null) {
Sunny Goyal3fbca152017-11-02 18:55:44 -0700186 return;
187 }
Sunny Goyalba406e22018-04-02 11:36:12 -0700188 PreferenceScreen screen = getPreferenceScreen();
189 if (Utilities.ATLEAST_OREO) {
190 screen = selectPreferenceRecursive(pref, screen);
191 }
192 if (screen == null) {
193 return;
194 }
195
196 View root = screen.getDialog() != null
197 ? screen.getDialog().getWindow().getDecorView() : getView();
198 ListView list = root.findViewById(android.R.id.list);
199 if (list == null || list.getAdapter() == null) {
200 return;
201 }
202 Adapter adapter = list.getAdapter();
Sunny Goyal3fbca152017-11-02 18:55:44 -0700203
204 // Find the position
205 int position = -1;
206 for (int i = adapter.getCount() - 1; i >= 0; i--) {
207 if (pref == adapter.getItem(i)) {
208 position = i;
209 break;
210 }
211 }
Sunny Goyalba406e22018-04-02 11:36:12 -0700212 new ListViewHighlighter(list, position);
Sunny Goyal3fbca152017-11-02 18:55:44 -0700213 mPreferenceHighlighted = true;
214 }
215
216 @Override
Sunny Goyalf48e5922016-05-12 15:36:20 -0700217 public void onDestroy() {
Tony Wickham2ab84822017-05-12 14:59:09 -0700218 if (mIconBadgingObserver != null) {
Tonyd48710c2017-07-27 23:23:58 -0700219 mIconBadgingObserver.unregister();
Tony Wickham2ab84822017-05-12 14:59:09 -0700220 mIconBadgingObserver = null;
221 }
Sunny Goyalf48e5922016-05-12 15:36:20 -0700222 super.onDestroy();
223 }
Sunny Goyalba406e22018-04-02 11:36:12 -0700224
225 @TargetApi(Build.VERSION_CODES.O)
226 private PreferenceScreen selectPreferenceRecursive(
227 Preference pref, PreferenceScreen topParent) {
228 if (!(pref.getParent() instanceof PreferenceScreen)) {
229 return null;
230 }
231
232 PreferenceScreen parent = (PreferenceScreen) pref.getParent();
233 if (Objects.equals(parent.getKey(), topParent.getKey())) {
234 return parent;
235 } else if (selectPreferenceRecursive(parent, topParent) != null) {
236 ((PreferenceScreen) parent.getParent())
237 .onItemClick(null, null, parent.getOrder(), 0);
238 return parent;
239 } else {
240 return null;
241 }
242 }
Sunny Goyalf48e5922016-05-12 15:36:20 -0700243 }
244
245 /**
Tony Wickham2ab84822017-05-12 14:59:09 -0700246 * Content observer which listens for system badging setting changes,
247 * and updates the launcher badging setting subtext accordingly.
248 */
Sunny Goyal5077aef2018-09-17 15:22:13 -0700249 private static class IconBadgingObserver extends SettingsObserver.Secure {
Tony Wickham2ab84822017-05-12 14:59:09 -0700250
Sunny Goyal848cad52017-07-07 03:12:21 -0700251 private final ButtonPreference mBadgingPref;
Tony Wickham2ab84822017-05-12 14:59:09 -0700252 private final ContentResolver mResolver;
Sunny Goyal848cad52017-07-07 03:12:21 -0700253 private final FragmentManager mFragmentManager;
Tony Wickham2ab84822017-05-12 14:59:09 -0700254
Sunny Goyal848cad52017-07-07 03:12:21 -0700255 public IconBadgingObserver(ButtonPreference badgingPref, ContentResolver resolver,
256 FragmentManager fragmentManager) {
Tonyd48710c2017-07-27 23:23:58 -0700257 super(resolver);
Tony Wickham2ab84822017-05-12 14:59:09 -0700258 mBadgingPref = badgingPref;
259 mResolver = resolver;
Sunny Goyal848cad52017-07-07 03:12:21 -0700260 mFragmentManager = fragmentManager;
Tony Wickham2ab84822017-05-12 14:59:09 -0700261 }
262
263 @Override
Tonyd48710c2017-07-27 23:23:58 -0700264 public void onSettingChanged(boolean enabled) {
Sunny Goyal848cad52017-07-07 03:12:21 -0700265 int summary = enabled ? R.string.icon_badging_desc_on : R.string.icon_badging_desc_off;
266
267 boolean serviceEnabled = true;
268 if (enabled) {
269 // Check if the listener is enabled or not.
270 String enabledListeners =
271 Settings.Secure.getString(mResolver, NOTIFICATION_ENABLED_LISTENERS);
272 ComponentName myListener =
273 new ComponentName(mBadgingPref.getContext(), NotificationListener.class);
274 serviceEnabled = enabledListeners != null &&
275 (enabledListeners.contains(myListener.flattenToString()) ||
276 enabledListeners.contains(myListener.flattenToShortString()));
277 if (!serviceEnabled) {
278 summary = R.string.title_missing_notification_access;
279 }
280 }
Sunny Goyalb65e13c2017-08-01 11:04:15 -0700281 mBadgingPref.setWidgetFrameVisible(!serviceEnabled);
Sunny Goyal5077aef2018-09-17 15:22:13 -0700282 mBadgingPref.setFragment(
283 serviceEnabled ? null : NotificationAccessConfirmation.class.getName());
Sunny Goyal848cad52017-07-07 03:12:21 -0700284 mBadgingPref.setSummary(summary);
285
286 }
Tony Wickham2ab84822017-05-12 14:59:09 -0700287 }
288
Sunny Goyal848cad52017-07-07 03:12:21 -0700289 public static class NotificationAccessConfirmation
290 extends DialogFragment implements DialogInterface.OnClickListener {
291
292 @Override
293 public Dialog onCreateDialog(Bundle savedInstanceState) {
294 final Context context = getActivity();
295 String msg = context.getString(R.string.msg_missing_notification_access,
296 context.getString(R.string.derived_app_name));
297 return new AlertDialog.Builder(context)
298 .setTitle(R.string.title_missing_notification_access)
299 .setMessage(msg)
300 .setNegativeButton(android.R.string.cancel, null)
301 .setPositiveButton(R.string.title_change_settings, this)
302 .create();
303 }
304
305 @Override
306 public void onClick(DialogInterface dialogInterface, int i) {
307 ComponentName cn = new ComponentName(getActivity(), NotificationListener.class);
Sunny Goyald47f2d22018-04-18 14:11:37 -0700308 Bundle showFragmentArgs = new Bundle();
309 showFragmentArgs.putString(EXTRA_FRAGMENT_ARG_KEY, cn.flattenToString());
310
Sunny Goyal848cad52017-07-07 03:12:21 -0700311 Intent intent = new Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS)
312 .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
Sunny Goyald47f2d22018-04-18 14:11:37 -0700313 .putExtra(EXTRA_FRAGMENT_ARG_KEY, cn.flattenToString())
314 .putExtra(EXTRA_SHOW_FRAGMENT_ARGS, showFragmentArgs);
Sunny Goyal848cad52017-07-07 03:12:21 -0700315 getActivity().startActivity(intent);
316 }
317 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700318}