blob: d7c20b8a91b6a8f0f6e2f09a65877cab9f620886 [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 Goyalba406e22018-04-02 11:36:12 -070019import android.annotation.TargetApi;
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070020import android.app.Activity;
Sunny Goyal848cad52017-07-07 03:12:21 -070021import android.app.AlertDialog;
22import android.app.Dialog;
23import android.app.DialogFragment;
24import android.app.FragmentManager;
25import android.content.ComponentName;
Sunny Goyalf48e5922016-05-12 15:36:20 -070026import android.content.ContentResolver;
Sunny Goyal848cad52017-07-07 03:12:21 -070027import android.content.Context;
28import android.content.DialogInterface;
29import android.content.Intent;
Sunny Goyalba406e22018-04-02 11:36:12 -070030import android.os.Build;
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070031import android.os.Bundle;
Sunny Goyalca187462017-03-31 20:09:34 -070032import android.preference.ListPreference;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -040033import android.preference.Preference;
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070034import android.preference.PreferenceFragment;
Sunny Goyalba406e22018-04-02 11:36:12 -070035import android.preference.PreferenceScreen;
Sunny Goyalf48e5922016-05-12 15:36:20 -070036import android.provider.Settings;
Sunny Goyal3fbca152017-11-02 18:55:44 -070037import android.text.TextUtils;
Sunny Goyal3fbca152017-11-02 18:55:44 -070038import android.view.View;
Sunny Goyal3fbca152017-11-02 18:55:44 -070039import android.widget.Adapter;
Sunny Goyalba406e22018-04-02 11:36:12 -070040import android.widget.ListView;
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070041
Sunny Goyalca187462017-03-31 20:09:34 -070042import com.android.launcher3.graphics.IconShapeOverride;
Sunny Goyal848cad52017-07-07 03:12:21 -070043import com.android.launcher3.notification.NotificationListener;
Sunny Goyalba406e22018-04-02 11:36:12 -070044import com.android.launcher3.util.ListViewHighlighter;
Tonyd48710c2017-07-27 23:23:58 -070045import com.android.launcher3.util.SettingsObserver;
Sunny Goyal848cad52017-07-07 03:12:21 -070046import com.android.launcher3.views.ButtonPreference;
Sunny Goyalba406e22018-04-02 11:36:12 -070047
48import java.util.Objects;
Sunny Goyalca187462017-03-31 20:09:34 -070049
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070050/**
Rahul Chaturvedi799aa042015-06-01 21:26:41 -040051 * Settings activity for Launcher. Currently implements the following setting: Allow rotation
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070052 */
53public class SettingsActivity extends Activity {
Tony Wickham2ab84822017-05-12 14:59:09 -070054
55 private static final String ICON_BADGING_PREFERENCE_KEY = "pref_icon_badging";
Tonyd48710c2017-07-27 23:23:58 -070056 /** Hidden field Settings.Secure.NOTIFICATION_BADGING */
57 public static final String NOTIFICATION_BADGING = "notification_badging";
Sunny Goyal848cad52017-07-07 03:12:21 -070058 /** Hidden field Settings.Secure.ENABLED_NOTIFICATION_LISTENERS */
59 private static final String NOTIFICATION_ENABLED_LISTENERS = "enabled_notification_listeners";
Tony Wickham2ab84822017-05-12 14:59:09 -070060
Sunny Goyal3fbca152017-11-02 18:55:44 -070061 private static final String EXTRA_FRAGMENT_ARG_KEY = ":settings:fragment_args_key";
Sunny Goyald47f2d22018-04-18 14:11:37 -070062 private static final String EXTRA_SHOW_FRAGMENT_ARGS = ":settings:show_fragment_args";
Sunny Goyal3fbca152017-11-02 18:55:44 -070063 private static final int DELAY_HIGHLIGHT_DURATION_MILLIS = 600;
64 private static final String SAVE_HIGHLIGHTED_KEY = "android:preference_highlighted";
65
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070066 @Override
67 protected void onCreate(Bundle savedInstanceState) {
68 super.onCreate(savedInstanceState);
69
Mario Bertschler2de2d672017-06-30 18:51:46 -070070 if (savedInstanceState == null) {
71 // Display the fragment as the main content.
72 getFragmentManager().beginTransaction()
Sunny Goyal3fbca152017-11-02 18:55:44 -070073 .replace(android.R.id.content, getNewFragment())
Mario Bertschler2de2d672017-06-30 18:51:46 -070074 .commit();
75 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070076 }
77
Sunny Goyal3fbca152017-11-02 18:55:44 -070078 protected PreferenceFragment getNewFragment() {
79 return new LauncherSettingsFragment();
80 }
81
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070082 /**
83 * This fragment shows the launcher preferences.
84 */
Sunny Goyalf48e5922016-05-12 15:36:20 -070085 public static class LauncherSettingsFragment extends PreferenceFragment {
86
Tony Wickham2ab84822017-05-12 14:59:09 -070087 private IconBadgingObserver mIconBadgingObserver;
Sunny Goyalf48e5922016-05-12 15:36:20 -070088
Sunny Goyal3fbca152017-11-02 18:55:44 -070089 private String mPreferenceKey;
90 private boolean mPreferenceHighlighted = false;
91
92 @Override
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070093 public void onCreate(Bundle savedInstanceState) {
94 super.onCreate(savedInstanceState);
Sunny Goyal3fbca152017-11-02 18:55:44 -070095 if (savedInstanceState != null) {
96 mPreferenceHighlighted = savedInstanceState.getBoolean(SAVE_HIGHLIGHTED_KEY);
97 }
98
Sunny Goyalf48e5922016-05-12 15:36:20 -070099 getPreferenceManager().setSharedPreferencesName(LauncherFiles.SHARED_PREFERENCES_KEY);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700100 addPreferencesFromResource(R.xml.launcher_preferences);
Sunny Goyal7779d622015-06-11 16:18:39 -0700101
Tony Wickham2ab84822017-05-12 14:59:09 -0700102 ContentResolver resolver = getActivity().getContentResolver();
103
Sunny Goyal848cad52017-07-07 03:12:21 -0700104 ButtonPreference iconBadgingPref =
105 (ButtonPreference) findPreference(ICON_BADGING_PREFERENCE_KEY);
Hyunyoung Songe24cb632017-09-11 11:18:03 -0700106 if (!Utilities.ATLEAST_OREO) {
Sunny Goyal4179e9b2017-03-08 14:25:09 -0800107 getPreferenceScreen().removePreference(
108 findPreference(SessionCommitReceiver.ADD_ICON_PREFERENCE_KEY));
Tony Wickham2ab84822017-05-12 14:59:09 -0700109 getPreferenceScreen().removePreference(iconBadgingPref);
Tony Wickhamc1385152017-08-14 14:31:03 -0700110 } else if (!getResources().getBoolean(R.bool.notification_badging_enabled)) {
111 getPreferenceScreen().removePreference(iconBadgingPref);
Tony Wickham2ab84822017-05-12 14:59:09 -0700112 } else {
113 // Listen to system notification badge settings while this UI is active.
Sunny Goyal848cad52017-07-07 03:12:21 -0700114 mIconBadgingObserver = new IconBadgingObserver(
115 iconBadgingPref, resolver, getFragmentManager());
Tonyd48710c2017-07-27 23:23:58 -0700116 mIconBadgingObserver.register(NOTIFICATION_BADGING, NOTIFICATION_ENABLED_LISTENERS);
Sunny Goyal4179e9b2017-03-08 14:25:09 -0800117 }
Sunny Goyalca187462017-03-31 20:09:34 -0700118
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 Chaturvedi7fc77ca2015-05-19 18:02:16 -0700127 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400128
129 @Override
Sunny Goyal3fbca152017-11-02 18:55:44 -0700130 public void onSaveInstanceState(Bundle outState) {
131 super.onSaveInstanceState(outState);
132 outState.putBoolean(SAVE_HIGHLIGHTED_KEY, mPreferenceHighlighted);
133 }
134
135 @Override
136 public void onResume() {
137 super.onResume();
138
139 Intent intent = getActivity().getIntent();
140 mPreferenceKey = intent.getStringExtra(EXTRA_FRAGMENT_ARG_KEY);
141 if (isAdded() && !mPreferenceHighlighted && !TextUtils.isEmpty(mPreferenceKey)) {
142 getView().postDelayed(this::highlightPreference, DELAY_HIGHLIGHT_DURATION_MILLIS);
143 }
144 }
145
146 private void highlightPreference() {
Sunny Goyal3fbca152017-11-02 18:55:44 -0700147 Preference pref = findPreference(mPreferenceKey);
Sunny Goyalba406e22018-04-02 11:36:12 -0700148 if (pref == null || getPreferenceScreen() == null) {
Sunny Goyal3fbca152017-11-02 18:55:44 -0700149 return;
150 }
Sunny Goyalba406e22018-04-02 11:36:12 -0700151 PreferenceScreen screen = getPreferenceScreen();
152 if (Utilities.ATLEAST_OREO) {
153 screen = selectPreferenceRecursive(pref, screen);
154 }
155 if (screen == null) {
156 return;
157 }
158
159 View root = screen.getDialog() != null
160 ? screen.getDialog().getWindow().getDecorView() : getView();
161 ListView list = root.findViewById(android.R.id.list);
162 if (list == null || list.getAdapter() == null) {
163 return;
164 }
165 Adapter adapter = list.getAdapter();
Sunny Goyal3fbca152017-11-02 18:55:44 -0700166
167 // Find the position
168 int position = -1;
169 for (int i = adapter.getCount() - 1; i >= 0; i--) {
170 if (pref == adapter.getItem(i)) {
171 position = i;
172 break;
173 }
174 }
Sunny Goyalba406e22018-04-02 11:36:12 -0700175 new ListViewHighlighter(list, position);
Sunny Goyal3fbca152017-11-02 18:55:44 -0700176 mPreferenceHighlighted = true;
177 }
178
179 @Override
Sunny Goyalf48e5922016-05-12 15:36:20 -0700180 public void onDestroy() {
Tony Wickham2ab84822017-05-12 14:59:09 -0700181 if (mIconBadgingObserver != null) {
Tonyd48710c2017-07-27 23:23:58 -0700182 mIconBadgingObserver.unregister();
Tony Wickham2ab84822017-05-12 14:59:09 -0700183 mIconBadgingObserver = null;
184 }
Sunny Goyalf48e5922016-05-12 15:36:20 -0700185 super.onDestroy();
186 }
Sunny Goyalba406e22018-04-02 11:36:12 -0700187
188 @TargetApi(Build.VERSION_CODES.O)
189 private PreferenceScreen selectPreferenceRecursive(
190 Preference pref, PreferenceScreen topParent) {
191 if (!(pref.getParent() instanceof PreferenceScreen)) {
192 return null;
193 }
194
195 PreferenceScreen parent = (PreferenceScreen) pref.getParent();
196 if (Objects.equals(parent.getKey(), topParent.getKey())) {
197 return parent;
198 } else if (selectPreferenceRecursive(parent, topParent) != null) {
199 ((PreferenceScreen) parent.getParent())
200 .onItemClick(null, null, parent.getOrder(), 0);
201 return parent;
202 } else {
203 return null;
204 }
205 }
Sunny Goyalf48e5922016-05-12 15:36:20 -0700206 }
207
208 /**
Tony Wickham2ab84822017-05-12 14:59:09 -0700209 * Content observer which listens for system badging setting changes,
210 * and updates the launcher badging setting subtext accordingly.
211 */
Tonyd48710c2017-07-27 23:23:58 -0700212 private static class IconBadgingObserver extends SettingsObserver.Secure
Sunny Goyalb65e13c2017-08-01 11:04:15 -0700213 implements Preference.OnPreferenceClickListener {
Tony Wickham2ab84822017-05-12 14:59:09 -0700214
Sunny Goyal848cad52017-07-07 03:12:21 -0700215 private final ButtonPreference mBadgingPref;
Tony Wickham2ab84822017-05-12 14:59:09 -0700216 private final ContentResolver mResolver;
Sunny Goyal848cad52017-07-07 03:12:21 -0700217 private final FragmentManager mFragmentManager;
Tony Wickham2ab84822017-05-12 14:59:09 -0700218
Sunny Goyal848cad52017-07-07 03:12:21 -0700219 public IconBadgingObserver(ButtonPreference badgingPref, ContentResolver resolver,
220 FragmentManager fragmentManager) {
Tonyd48710c2017-07-27 23:23:58 -0700221 super(resolver);
Tony Wickham2ab84822017-05-12 14:59:09 -0700222 mBadgingPref = badgingPref;
223 mResolver = resolver;
Sunny Goyal848cad52017-07-07 03:12:21 -0700224 mFragmentManager = fragmentManager;
Tony Wickham2ab84822017-05-12 14:59:09 -0700225 }
226
227 @Override
Tonyd48710c2017-07-27 23:23:58 -0700228 public void onSettingChanged(boolean enabled) {
Sunny Goyal848cad52017-07-07 03:12:21 -0700229 int summary = enabled ? R.string.icon_badging_desc_on : R.string.icon_badging_desc_off;
230
231 boolean serviceEnabled = true;
232 if (enabled) {
233 // Check if the listener is enabled or not.
234 String enabledListeners =
235 Settings.Secure.getString(mResolver, NOTIFICATION_ENABLED_LISTENERS);
236 ComponentName myListener =
237 new ComponentName(mBadgingPref.getContext(), NotificationListener.class);
238 serviceEnabled = enabledListeners != null &&
239 (enabledListeners.contains(myListener.flattenToString()) ||
240 enabledListeners.contains(myListener.flattenToShortString()));
241 if (!serviceEnabled) {
242 summary = R.string.title_missing_notification_access;
243 }
244 }
Sunny Goyalb65e13c2017-08-01 11:04:15 -0700245 mBadgingPref.setWidgetFrameVisible(!serviceEnabled);
246 mBadgingPref.setOnPreferenceClickListener(serviceEnabled ? null : this);
Sunny Goyal848cad52017-07-07 03:12:21 -0700247 mBadgingPref.setSummary(summary);
248
249 }
250
251 @Override
Sunny Goyalb65e13c2017-08-01 11:04:15 -0700252 public boolean onPreferenceClick(Preference preference) {
Sunny Goyal848cad52017-07-07 03:12:21 -0700253 new NotificationAccessConfirmation().show(mFragmentManager, "notification_access");
Sunny Goyalb65e13c2017-08-01 11:04:15 -0700254 return true;
Tony Wickham2ab84822017-05-12 14:59:09 -0700255 }
256 }
257
Sunny Goyal848cad52017-07-07 03:12:21 -0700258 public static class NotificationAccessConfirmation
259 extends DialogFragment implements DialogInterface.OnClickListener {
260
261 @Override
262 public Dialog onCreateDialog(Bundle savedInstanceState) {
263 final Context context = getActivity();
264 String msg = context.getString(R.string.msg_missing_notification_access,
265 context.getString(R.string.derived_app_name));
266 return new AlertDialog.Builder(context)
267 .setTitle(R.string.title_missing_notification_access)
268 .setMessage(msg)
269 .setNegativeButton(android.R.string.cancel, null)
270 .setPositiveButton(R.string.title_change_settings, this)
271 .create();
272 }
273
274 @Override
275 public void onClick(DialogInterface dialogInterface, int i) {
276 ComponentName cn = new ComponentName(getActivity(), NotificationListener.class);
Sunny Goyald47f2d22018-04-18 14:11:37 -0700277 Bundle showFragmentArgs = new Bundle();
278 showFragmentArgs.putString(EXTRA_FRAGMENT_ARG_KEY, cn.flattenToString());
279
Sunny Goyal848cad52017-07-07 03:12:21 -0700280 Intent intent = new Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS)
281 .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
Sunny Goyald47f2d22018-04-18 14:11:37 -0700282 .putExtra(EXTRA_FRAGMENT_ARG_KEY, cn.flattenToString())
283 .putExtra(EXTRA_SHOW_FRAGMENT_ARGS, showFragmentArgs);
Sunny Goyal848cad52017-07-07 03:12:21 -0700284 getActivity().startActivity(intent);
285 }
286 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700287}