blob: 32c198ab0a4fed634c2604e0f5dc097e25dffc9a [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;
27import android.app.FragmentManager;
28import android.content.ComponentName;
Sunny Goyalf48e5922016-05-12 15:36:20 -070029import android.content.ContentResolver;
Sunny Goyal848cad52017-07-07 03:12:21 -070030import android.content.Context;
31import android.content.DialogInterface;
32import android.content.Intent;
Sunny Goyalba406e22018-04-02 11:36:12 -070033import android.os.Build;
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070034import android.os.Bundle;
Sunny Goyalca187462017-03-31 20:09:34 -070035import android.preference.ListPreference;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -040036import android.preference.Preference;
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070037import android.preference.PreferenceFragment;
Sunny Goyalba406e22018-04-02 11:36:12 -070038import android.preference.PreferenceScreen;
Sunny Goyalf48e5922016-05-12 15:36:20 -070039import android.provider.Settings;
Sunny Goyal3fbca152017-11-02 18:55:44 -070040import android.text.TextUtils;
Sunny Goyal3fbca152017-11-02 18:55:44 -070041import android.view.View;
Sunny Goyal3fbca152017-11-02 18:55:44 -070042import android.widget.Adapter;
Sunny Goyalba406e22018-04-02 11:36:12 -070043import android.widget.ListView;
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070044
Sunny Goyalca187462017-03-31 20:09:34 -070045import com.android.launcher3.graphics.IconShapeOverride;
Sunny Goyal848cad52017-07-07 03:12:21 -070046import com.android.launcher3.notification.NotificationListener;
Sunny Goyalba406e22018-04-02 11:36:12 -070047import com.android.launcher3.util.ListViewHighlighter;
Tonyd48710c2017-07-27 23:23:58 -070048import com.android.launcher3.util.SettingsObserver;
Sunny Goyal848cad52017-07-07 03:12:21 -070049import com.android.launcher3.views.ButtonPreference;
Sunny Goyalba406e22018-04-02 11:36:12 -070050
51import java.util.Objects;
Sunny Goyalca187462017-03-31 20:09:34 -070052
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070053/**
Rahul Chaturvedi799aa042015-06-01 21:26:41 -040054 * Settings activity for Launcher. Currently implements the following setting: Allow rotation
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070055 */
56public class SettingsActivity extends Activity {
Tony Wickham2ab84822017-05-12 14:59:09 -070057
58 private static final String ICON_BADGING_PREFERENCE_KEY = "pref_icon_badging";
Tonyd48710c2017-07-27 23:23:58 -070059 /** Hidden field Settings.Secure.NOTIFICATION_BADGING */
60 public static final String NOTIFICATION_BADGING = "notification_badging";
Sunny Goyal848cad52017-07-07 03:12:21 -070061 /** Hidden field Settings.Secure.ENABLED_NOTIFICATION_LISTENERS */
62 private static final String NOTIFICATION_ENABLED_LISTENERS = "enabled_notification_listeners";
Tony Wickham2ab84822017-05-12 14:59:09 -070063
Sunny Goyal3fbca152017-11-02 18:55:44 -070064 private static final String EXTRA_FRAGMENT_ARG_KEY = ":settings:fragment_args_key";
Sunny Goyald47f2d22018-04-18 14:11:37 -070065 private static final String EXTRA_SHOW_FRAGMENT_ARGS = ":settings:show_fragment_args";
Sunny Goyal3fbca152017-11-02 18:55:44 -070066 private static final int DELAY_HIGHLIGHT_DURATION_MILLIS = 600;
67 private static final String SAVE_HIGHLIGHTED_KEY = "android:preference_highlighted";
68
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070069 @Override
70 protected void onCreate(Bundle savedInstanceState) {
71 super.onCreate(savedInstanceState);
72
Mario Bertschler2de2d672017-06-30 18:51:46 -070073 if (savedInstanceState == null) {
74 // Display the fragment as the main content.
75 getFragmentManager().beginTransaction()
Sunny Goyal3fbca152017-11-02 18:55:44 -070076 .replace(android.R.id.content, getNewFragment())
Mario Bertschler2de2d672017-06-30 18:51:46 -070077 .commit();
78 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070079 }
80
Sunny Goyal3fbca152017-11-02 18:55:44 -070081 protected PreferenceFragment getNewFragment() {
82 return new LauncherSettingsFragment();
83 }
84
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070085 /**
86 * This fragment shows the launcher preferences.
87 */
Sunny Goyalf48e5922016-05-12 15:36:20 -070088 public static class LauncherSettingsFragment extends PreferenceFragment {
89
Tony Wickham2ab84822017-05-12 14:59:09 -070090 private IconBadgingObserver mIconBadgingObserver;
Sunny Goyalf48e5922016-05-12 15:36:20 -070091
Sunny Goyal3fbca152017-11-02 18:55:44 -070092 private String mPreferenceKey;
93 private boolean mPreferenceHighlighted = false;
94
95 @Override
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070096 public void onCreate(Bundle savedInstanceState) {
97 super.onCreate(savedInstanceState);
Sunny Goyal3fbca152017-11-02 18:55:44 -070098 if (savedInstanceState != null) {
99 mPreferenceHighlighted = savedInstanceState.getBoolean(SAVE_HIGHLIGHTED_KEY);
100 }
101
Sunny Goyalf48e5922016-05-12 15:36:20 -0700102 getPreferenceManager().setSharedPreferencesName(LauncherFiles.SHARED_PREFERENCES_KEY);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700103 addPreferencesFromResource(R.xml.launcher_preferences);
Sunny Goyal7779d622015-06-11 16:18:39 -0700104
Tony Wickham2ab84822017-05-12 14:59:09 -0700105 ContentResolver resolver = getActivity().getContentResolver();
106
Sunny Goyal848cad52017-07-07 03:12:21 -0700107 ButtonPreference iconBadgingPref =
108 (ButtonPreference) findPreference(ICON_BADGING_PREFERENCE_KEY);
Hyunyoung Songe24cb632017-09-11 11:18:03 -0700109 if (!Utilities.ATLEAST_OREO) {
Sunny Goyal4179e9b2017-03-08 14:25:09 -0800110 getPreferenceScreen().removePreference(
111 findPreference(SessionCommitReceiver.ADD_ICON_PREFERENCE_KEY));
Tony Wickham2ab84822017-05-12 14:59:09 -0700112 getPreferenceScreen().removePreference(iconBadgingPref);
Tony Wickhamc1385152017-08-14 14:31:03 -0700113 } else if (!getResources().getBoolean(R.bool.notification_badging_enabled)) {
114 getPreferenceScreen().removePreference(iconBadgingPref);
Tony Wickham2ab84822017-05-12 14:59:09 -0700115 } else {
116 // Listen to system notification badge settings while this UI is active.
Sunny Goyal848cad52017-07-07 03:12:21 -0700117 mIconBadgingObserver = new IconBadgingObserver(
118 iconBadgingPref, resolver, getFragmentManager());
Tonyd48710c2017-07-27 23:23:58 -0700119 mIconBadgingObserver.register(NOTIFICATION_BADGING, NOTIFICATION_ENABLED_LISTENERS);
Sunny Goyal4179e9b2017-03-08 14:25:09 -0800120 }
Sunny Goyalca187462017-03-31 20:09:34 -0700121
122 Preference iconShapeOverride = findPreference(IconShapeOverride.KEY_PREFERENCE);
123 if (iconShapeOverride != null) {
124 if (IconShapeOverride.isSupported(getActivity())) {
125 IconShapeOverride.handlePreferenceUi((ListPreference) iconShapeOverride);
126 } else {
127 getPreferenceScreen().removePreference(iconShapeOverride);
128 }
129 }
Sunny Goyal40253322018-04-19 09:27:38 -0700130
131 // Setup allow rotation preference
132 Preference rotationPref = findPreference(ALLOW_ROTATION_PREFERENCE_KEY);
133 if (getResources().getBoolean(R.bool.allow_rotation)) {
134 // Launcher supports rotation by default. No need to show this setting.
135 getPreferenceScreen().removePreference(rotationPref);
136 } else {
Sunny Goyal40253322018-04-19 09:27:38 -0700137 // Initialize the UI once
138 rotationPref.setDefaultValue(getAllowRotationDefaultValue());
139 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700140 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400141
142 @Override
Sunny Goyal3fbca152017-11-02 18:55:44 -0700143 public void onSaveInstanceState(Bundle outState) {
144 super.onSaveInstanceState(outState);
145 outState.putBoolean(SAVE_HIGHLIGHTED_KEY, mPreferenceHighlighted);
146 }
147
148 @Override
149 public void onResume() {
150 super.onResume();
151
152 Intent intent = getActivity().getIntent();
153 mPreferenceKey = intent.getStringExtra(EXTRA_FRAGMENT_ARG_KEY);
154 if (isAdded() && !mPreferenceHighlighted && !TextUtils.isEmpty(mPreferenceKey)) {
155 getView().postDelayed(this::highlightPreference, DELAY_HIGHLIGHT_DURATION_MILLIS);
156 }
157 }
158
159 private void highlightPreference() {
Sunny Goyal3fbca152017-11-02 18:55:44 -0700160 Preference pref = findPreference(mPreferenceKey);
Sunny Goyalba406e22018-04-02 11:36:12 -0700161 if (pref == null || getPreferenceScreen() == null) {
Sunny Goyal3fbca152017-11-02 18:55:44 -0700162 return;
163 }
Sunny Goyalba406e22018-04-02 11:36:12 -0700164 PreferenceScreen screen = getPreferenceScreen();
165 if (Utilities.ATLEAST_OREO) {
166 screen = selectPreferenceRecursive(pref, screen);
167 }
168 if (screen == null) {
169 return;
170 }
171
172 View root = screen.getDialog() != null
173 ? screen.getDialog().getWindow().getDecorView() : getView();
174 ListView list = root.findViewById(android.R.id.list);
175 if (list == null || list.getAdapter() == null) {
176 return;
177 }
178 Adapter adapter = list.getAdapter();
Sunny Goyal3fbca152017-11-02 18:55:44 -0700179
180 // Find the position
181 int position = -1;
182 for (int i = adapter.getCount() - 1; i >= 0; i--) {
183 if (pref == adapter.getItem(i)) {
184 position = i;
185 break;
186 }
187 }
Sunny Goyalba406e22018-04-02 11:36:12 -0700188 new ListViewHighlighter(list, position);
Sunny Goyal3fbca152017-11-02 18:55:44 -0700189 mPreferenceHighlighted = true;
190 }
191
192 @Override
Sunny Goyalf48e5922016-05-12 15:36:20 -0700193 public void onDestroy() {
Tony Wickham2ab84822017-05-12 14:59:09 -0700194 if (mIconBadgingObserver != null) {
Tonyd48710c2017-07-27 23:23:58 -0700195 mIconBadgingObserver.unregister();
Tony Wickham2ab84822017-05-12 14:59:09 -0700196 mIconBadgingObserver = null;
197 }
Sunny Goyalf48e5922016-05-12 15:36:20 -0700198 super.onDestroy();
199 }
Sunny Goyalba406e22018-04-02 11:36:12 -0700200
201 @TargetApi(Build.VERSION_CODES.O)
202 private PreferenceScreen selectPreferenceRecursive(
203 Preference pref, PreferenceScreen topParent) {
204 if (!(pref.getParent() instanceof PreferenceScreen)) {
205 return null;
206 }
207
208 PreferenceScreen parent = (PreferenceScreen) pref.getParent();
209 if (Objects.equals(parent.getKey(), topParent.getKey())) {
210 return parent;
211 } else if (selectPreferenceRecursive(parent, topParent) != null) {
212 ((PreferenceScreen) parent.getParent())
213 .onItemClick(null, null, parent.getOrder(), 0);
214 return parent;
215 } else {
216 return null;
217 }
218 }
Sunny Goyalf48e5922016-05-12 15:36:20 -0700219 }
220
221 /**
Tony Wickham2ab84822017-05-12 14:59:09 -0700222 * Content observer which listens for system badging setting changes,
223 * and updates the launcher badging setting subtext accordingly.
224 */
Tonyd48710c2017-07-27 23:23:58 -0700225 private static class IconBadgingObserver extends SettingsObserver.Secure
Sunny Goyalb65e13c2017-08-01 11:04:15 -0700226 implements Preference.OnPreferenceClickListener {
Tony Wickham2ab84822017-05-12 14:59:09 -0700227
Sunny Goyal848cad52017-07-07 03:12:21 -0700228 private final ButtonPreference mBadgingPref;
Tony Wickham2ab84822017-05-12 14:59:09 -0700229 private final ContentResolver mResolver;
Sunny Goyal848cad52017-07-07 03:12:21 -0700230 private final FragmentManager mFragmentManager;
Tony Wickham2ab84822017-05-12 14:59:09 -0700231
Sunny Goyal848cad52017-07-07 03:12:21 -0700232 public IconBadgingObserver(ButtonPreference badgingPref, ContentResolver resolver,
233 FragmentManager fragmentManager) {
Tonyd48710c2017-07-27 23:23:58 -0700234 super(resolver);
Tony Wickham2ab84822017-05-12 14:59:09 -0700235 mBadgingPref = badgingPref;
236 mResolver = resolver;
Sunny Goyal848cad52017-07-07 03:12:21 -0700237 mFragmentManager = fragmentManager;
Tony Wickham2ab84822017-05-12 14:59:09 -0700238 }
239
240 @Override
Tonyd48710c2017-07-27 23:23:58 -0700241 public void onSettingChanged(boolean enabled) {
Sunny Goyal848cad52017-07-07 03:12:21 -0700242 int summary = enabled ? R.string.icon_badging_desc_on : R.string.icon_badging_desc_off;
243
244 boolean serviceEnabled = true;
245 if (enabled) {
246 // Check if the listener is enabled or not.
247 String enabledListeners =
248 Settings.Secure.getString(mResolver, NOTIFICATION_ENABLED_LISTENERS);
249 ComponentName myListener =
250 new ComponentName(mBadgingPref.getContext(), NotificationListener.class);
251 serviceEnabled = enabledListeners != null &&
252 (enabledListeners.contains(myListener.flattenToString()) ||
253 enabledListeners.contains(myListener.flattenToShortString()));
254 if (!serviceEnabled) {
255 summary = R.string.title_missing_notification_access;
256 }
257 }
Sunny Goyalb65e13c2017-08-01 11:04:15 -0700258 mBadgingPref.setWidgetFrameVisible(!serviceEnabled);
259 mBadgingPref.setOnPreferenceClickListener(serviceEnabled ? null : this);
Sunny Goyal848cad52017-07-07 03:12:21 -0700260 mBadgingPref.setSummary(summary);
261
262 }
263
264 @Override
Sunny Goyalb65e13c2017-08-01 11:04:15 -0700265 public boolean onPreferenceClick(Preference preference) {
Sunny Goyal848cad52017-07-07 03:12:21 -0700266 new NotificationAccessConfirmation().show(mFragmentManager, "notification_access");
Sunny Goyalb65e13c2017-08-01 11:04:15 -0700267 return true;
Tony Wickham2ab84822017-05-12 14:59:09 -0700268 }
269 }
270
Sunny Goyal848cad52017-07-07 03:12:21 -0700271 public static class NotificationAccessConfirmation
272 extends DialogFragment implements DialogInterface.OnClickListener {
273
274 @Override
275 public Dialog onCreateDialog(Bundle savedInstanceState) {
276 final Context context = getActivity();
277 String msg = context.getString(R.string.msg_missing_notification_access,
278 context.getString(R.string.derived_app_name));
279 return new AlertDialog.Builder(context)
280 .setTitle(R.string.title_missing_notification_access)
281 .setMessage(msg)
282 .setNegativeButton(android.R.string.cancel, null)
283 .setPositiveButton(R.string.title_change_settings, this)
284 .create();
285 }
286
287 @Override
288 public void onClick(DialogInterface dialogInterface, int i) {
289 ComponentName cn = new ComponentName(getActivity(), NotificationListener.class);
Sunny Goyald47f2d22018-04-18 14:11:37 -0700290 Bundle showFragmentArgs = new Bundle();
291 showFragmentArgs.putString(EXTRA_FRAGMENT_ARG_KEY, cn.flattenToString());
292
Sunny Goyal848cad52017-07-07 03:12:21 -0700293 Intent intent = new Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS)
294 .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
Sunny Goyald47f2d22018-04-18 14:11:37 -0700295 .putExtra(EXTRA_FRAGMENT_ARG_KEY, cn.flattenToString())
296 .putExtra(EXTRA_SHOW_FRAGMENT_ARGS, showFragmentArgs);
Sunny Goyal848cad52017-07-07 03:12:21 -0700297 getActivity().startActivity(intent);
298 }
299 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700300}