blob: d5d5eab76127a4dc493f877c3be1ab5d1728d03b [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
19import android.app.Activity;
Sunny Goyal848cad52017-07-07 03:12:21 -070020import android.app.AlertDialog;
21import android.app.Dialog;
22import android.app.DialogFragment;
23import android.app.FragmentManager;
24import android.content.ComponentName;
Sunny Goyalf48e5922016-05-12 15:36:20 -070025import android.content.ContentResolver;
Sunny Goyal848cad52017-07-07 03:12:21 -070026import android.content.Context;
27import android.content.DialogInterface;
28import android.content.Intent;
Sunny Goyalf48e5922016-05-12 15:36:20 -070029import android.database.ContentObserver;
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070030import android.os.Bundle;
Sunny Goyalf48e5922016-05-12 15:36:20 -070031import android.os.Handler;
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 Goyalf48e5922016-05-12 15:36:20 -070035import android.provider.Settings;
36import android.provider.Settings.System;
Sunny Goyal848cad52017-07-07 03:12:21 -070037import android.view.View;
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070038
Sunny Goyalca187462017-03-31 20:09:34 -070039import com.android.launcher3.graphics.IconShapeOverride;
Sunny Goyal848cad52017-07-07 03:12:21 -070040import com.android.launcher3.notification.NotificationListener;
41import com.android.launcher3.views.ButtonPreference;
Sunny Goyalca187462017-03-31 20:09:34 -070042
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070043/**
Rahul Chaturvedi799aa042015-06-01 21:26:41 -040044 * Settings activity for Launcher. Currently implements the following setting: Allow rotation
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070045 */
46public class SettingsActivity extends Activity {
Tony Wickham2ab84822017-05-12 14:59:09 -070047
48 private static final String ICON_BADGING_PREFERENCE_KEY = "pref_icon_badging";
49 // TODO: use Settings.Secure.NOTIFICATION_BADGING
50 private static final String NOTIFICATION_BADGING = "notification_badging";
Sunny Goyal848cad52017-07-07 03:12:21 -070051 /** Hidden field Settings.Secure.ENABLED_NOTIFICATION_LISTENERS */
52 private static final String NOTIFICATION_ENABLED_LISTENERS = "enabled_notification_listeners";
Tony Wickham2ab84822017-05-12 14:59:09 -070053
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070054 @Override
55 protected void onCreate(Bundle savedInstanceState) {
56 super.onCreate(savedInstanceState);
57
Mario Bertschler2de2d672017-06-30 18:51:46 -070058 if (savedInstanceState == null) {
59 // Display the fragment as the main content.
60 getFragmentManager().beginTransaction()
61 .replace(android.R.id.content, new LauncherSettingsFragment())
62 .commit();
63 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070064 }
65
66 /**
67 * This fragment shows the launcher preferences.
68 */
Sunny Goyalf48e5922016-05-12 15:36:20 -070069 public static class LauncherSettingsFragment extends PreferenceFragment {
70
71 private SystemDisplayRotationLockObserver mRotationLockObserver;
Tony Wickham2ab84822017-05-12 14:59:09 -070072 private IconBadgingObserver mIconBadgingObserver;
Sunny Goyalf48e5922016-05-12 15:36:20 -070073
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070074 @Override
75 public void onCreate(Bundle savedInstanceState) {
76 super.onCreate(savedInstanceState);
Sunny Goyalf48e5922016-05-12 15:36:20 -070077 getPreferenceManager().setSharedPreferencesName(LauncherFiles.SHARED_PREFERENCES_KEY);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070078 addPreferencesFromResource(R.xml.launcher_preferences);
Sunny Goyal7779d622015-06-11 16:18:39 -070079
Tony Wickham2ab84822017-05-12 14:59:09 -070080 ContentResolver resolver = getActivity().getContentResolver();
81
Sunny Goyalf48e5922016-05-12 15:36:20 -070082 // Setup allow rotation preference
83 Preference rotationPref = findPreference(Utilities.ALLOW_ROTATION_PREFERENCE_KEY);
84 if (getResources().getBoolean(R.bool.allow_rotation)) {
85 // Launcher supports rotation by default. No need to show this setting.
86 getPreferenceScreen().removePreference(rotationPref);
87 } else {
Sunny Goyalf48e5922016-05-12 15:36:20 -070088 mRotationLockObserver = new SystemDisplayRotationLockObserver(rotationPref, resolver);
89
90 // Register a content observer to listen for system setting changes while
91 // this UI is active.
92 resolver.registerContentObserver(
93 Settings.System.getUriFor(System.ACCELEROMETER_ROTATION),
94 false, mRotationLockObserver);
95
96 // Initialize the UI once
97 mRotationLockObserver.onChange(true);
Sunny Goyalab069992016-06-08 12:00:02 -070098 rotationPref.setDefaultValue(Utilities.getAllowRotationDefaultValue(getActivity()));
Sunny Goyal745bad92016-05-02 10:54:12 -070099 }
Sunny Goyal4179e9b2017-03-08 14:25:09 -0800100
Sunny Goyal848cad52017-07-07 03:12:21 -0700101 ButtonPreference iconBadgingPref =
102 (ButtonPreference) findPreference(ICON_BADGING_PREFERENCE_KEY);
Sunny Goyalde57caf2017-06-09 08:42:39 -0700103 if (!Utilities.isAtLeastO()) {
Sunny Goyal4179e9b2017-03-08 14:25:09 -0800104 getPreferenceScreen().removePreference(
105 findPreference(SessionCommitReceiver.ADD_ICON_PREFERENCE_KEY));
Tony Wickham2ab84822017-05-12 14:59:09 -0700106 getPreferenceScreen().removePreference(iconBadgingPref);
107 } else {
108 // Listen to system notification badge settings while this UI is active.
Sunny Goyal848cad52017-07-07 03:12:21 -0700109 mIconBadgingObserver = new IconBadgingObserver(
110 iconBadgingPref, resolver, getFragmentManager());
Tony Wickham2ab84822017-05-12 14:59:09 -0700111 resolver.registerContentObserver(
112 Settings.Secure.getUriFor(NOTIFICATION_BADGING),
113 false, mIconBadgingObserver);
Sunny Goyal848cad52017-07-07 03:12:21 -0700114 resolver.registerContentObserver(
115 Settings.Secure.getUriFor(NOTIFICATION_ENABLED_LISTENERS),
116 false, mIconBadgingObserver);
Tony Wickham2ab84822017-05-12 14:59:09 -0700117 mIconBadgingObserver.onChange(true);
Sunny Goyal4179e9b2017-03-08 14:25:09 -0800118 }
Sunny Goyalca187462017-03-31 20:09:34 -0700119
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 Chaturvedi7fc77ca2015-05-19 18:02:16 -0700128 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400129
130 @Override
Sunny Goyalf48e5922016-05-12 15:36:20 -0700131 public void onDestroy() {
132 if (mRotationLockObserver != null) {
Sunny Goyalab069992016-06-08 12:00:02 -0700133 getActivity().getContentResolver().unregisterContentObserver(mRotationLockObserver);
Sunny Goyalf48e5922016-05-12 15:36:20 -0700134 mRotationLockObserver = null;
135 }
Tony Wickham2ab84822017-05-12 14:59:09 -0700136 if (mIconBadgingObserver != null) {
137 getActivity().getContentResolver().unregisterContentObserver(mIconBadgingObserver);
138 mIconBadgingObserver = null;
139 }
Sunny Goyalf48e5922016-05-12 15:36:20 -0700140 super.onDestroy();
141 }
142 }
143
144 /**
145 * Content observer which listens for system auto-rotate setting changes, and enables/disables
146 * the launcher rotation setting accordingly.
147 */
148 private static class SystemDisplayRotationLockObserver extends ContentObserver {
149
150 private final Preference mRotationPref;
151 private final ContentResolver mResolver;
152
153 public SystemDisplayRotationLockObserver(
154 Preference rotationPref, ContentResolver resolver) {
155 super(new Handler());
156 mRotationPref = rotationPref;
157 mResolver = resolver;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400158 }
Sunny Goyal745bad92016-05-02 10:54:12 -0700159
Sunny Goyalf48e5922016-05-12 15:36:20 -0700160 @Override
161 public void onChange(boolean selfChange) {
162 boolean enabled = Settings.System.getInt(mResolver,
163 Settings.System.ACCELEROMETER_ROTATION, 1) == 1;
164 mRotationPref.setEnabled(enabled);
165 mRotationPref.setSummary(enabled
166 ? R.string.allow_rotation_desc : R.string.allow_rotation_blocked_desc);
Sunny Goyal745bad92016-05-02 10:54:12 -0700167 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700168 }
Tony Wickham2ab84822017-05-12 14:59:09 -0700169
170 /**
171 * Content observer which listens for system badging setting changes,
172 * and updates the launcher badging setting subtext accordingly.
173 */
Sunny Goyal848cad52017-07-07 03:12:21 -0700174 private static class IconBadgingObserver extends ContentObserver
175 implements View.OnClickListener {
Tony Wickham2ab84822017-05-12 14:59:09 -0700176
Sunny Goyal848cad52017-07-07 03:12:21 -0700177 private final ButtonPreference mBadgingPref;
Tony Wickham2ab84822017-05-12 14:59:09 -0700178 private final ContentResolver mResolver;
Sunny Goyal848cad52017-07-07 03:12:21 -0700179 private final FragmentManager mFragmentManager;
Tony Wickham2ab84822017-05-12 14:59:09 -0700180
Sunny Goyal848cad52017-07-07 03:12:21 -0700181 public IconBadgingObserver(ButtonPreference badgingPref, ContentResolver resolver,
182 FragmentManager fragmentManager) {
Tony Wickham2ab84822017-05-12 14:59:09 -0700183 super(new Handler());
184 mBadgingPref = badgingPref;
185 mResolver = resolver;
Sunny Goyal848cad52017-07-07 03:12:21 -0700186 mFragmentManager = fragmentManager;
Tony Wickham2ab84822017-05-12 14:59:09 -0700187 }
188
189 @Override
190 public void onChange(boolean selfChange) {
191 boolean enabled = Settings.Secure.getInt(mResolver, NOTIFICATION_BADGING, 1) == 1;
Sunny Goyal848cad52017-07-07 03:12:21 -0700192 int summary = enabled ? R.string.icon_badging_desc_on : R.string.icon_badging_desc_off;
193
194 boolean serviceEnabled = true;
195 if (enabled) {
196 // Check if the listener is enabled or not.
197 String enabledListeners =
198 Settings.Secure.getString(mResolver, NOTIFICATION_ENABLED_LISTENERS);
199 ComponentName myListener =
200 new ComponentName(mBadgingPref.getContext(), NotificationListener.class);
201 serviceEnabled = enabledListeners != null &&
202 (enabledListeners.contains(myListener.flattenToString()) ||
203 enabledListeners.contains(myListener.flattenToShortString()));
204 if (!serviceEnabled) {
205 summary = R.string.title_missing_notification_access;
206 }
207 }
208 mBadgingPref.setButtonOnClickListener(serviceEnabled ? null : this);
209 mBadgingPref.setSummary(summary);
210
211 }
212
213 @Override
214 public void onClick(View view) {
215 new NotificationAccessConfirmation().show(mFragmentManager, "notification_access");
Tony Wickham2ab84822017-05-12 14:59:09 -0700216 }
217 }
218
Sunny Goyal848cad52017-07-07 03:12:21 -0700219 public static class NotificationAccessConfirmation
220 extends DialogFragment implements DialogInterface.OnClickListener {
221
222 @Override
223 public Dialog onCreateDialog(Bundle savedInstanceState) {
224 final Context context = getActivity();
225 String msg = context.getString(R.string.msg_missing_notification_access,
226 context.getString(R.string.derived_app_name));
227 return new AlertDialog.Builder(context)
228 .setTitle(R.string.title_missing_notification_access)
229 .setMessage(msg)
230 .setNegativeButton(android.R.string.cancel, null)
231 .setPositiveButton(R.string.title_change_settings, this)
232 .create();
233 }
234
235 @Override
236 public void onClick(DialogInterface dialogInterface, int i) {
237 ComponentName cn = new ComponentName(getActivity(), NotificationListener.class);
238 Intent intent = new Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS)
239 .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
240 .putExtra(":settings:fragment_args_key", cn.flattenToString());
241 getActivity().startActivity(intent);
242 }
243 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700244}