blob: fa7769e3f6434869d34668a04e59d7ffd7495254 [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;
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070037
Sunny Goyalca187462017-03-31 20:09:34 -070038import com.android.launcher3.graphics.IconShapeOverride;
Sunny Goyal848cad52017-07-07 03:12:21 -070039import com.android.launcher3.notification.NotificationListener;
40import com.android.launcher3.views.ButtonPreference;
Sunny Goyalca187462017-03-31 20:09:34 -070041
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070042/**
Rahul Chaturvedi799aa042015-06-01 21:26:41 -040043 * Settings activity for Launcher. Currently implements the following setting: Allow rotation
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070044 */
45public class SettingsActivity extends Activity {
Tony Wickham2ab84822017-05-12 14:59:09 -070046
47 private static final String ICON_BADGING_PREFERENCE_KEY = "pref_icon_badging";
48 // TODO: use Settings.Secure.NOTIFICATION_BADGING
49 private static final String NOTIFICATION_BADGING = "notification_badging";
Sunny Goyal848cad52017-07-07 03:12:21 -070050 /** Hidden field Settings.Secure.ENABLED_NOTIFICATION_LISTENERS */
51 private static final String NOTIFICATION_ENABLED_LISTENERS = "enabled_notification_listeners";
Tony Wickham2ab84822017-05-12 14:59:09 -070052
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070053 @Override
54 protected void onCreate(Bundle savedInstanceState) {
55 super.onCreate(savedInstanceState);
56
Mario Bertschler2de2d672017-06-30 18:51:46 -070057 if (savedInstanceState == null) {
58 // Display the fragment as the main content.
59 getFragmentManager().beginTransaction()
60 .replace(android.R.id.content, new LauncherSettingsFragment())
61 .commit();
62 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070063 }
64
65 /**
66 * This fragment shows the launcher preferences.
67 */
Sunny Goyalf48e5922016-05-12 15:36:20 -070068 public static class LauncherSettingsFragment extends PreferenceFragment {
69
70 private SystemDisplayRotationLockObserver mRotationLockObserver;
Tony Wickham2ab84822017-05-12 14:59:09 -070071 private IconBadgingObserver mIconBadgingObserver;
Sunny Goyalf48e5922016-05-12 15:36:20 -070072
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070073 @Override
74 public void onCreate(Bundle savedInstanceState) {
75 super.onCreate(savedInstanceState);
Sunny Goyalf48e5922016-05-12 15:36:20 -070076 getPreferenceManager().setSharedPreferencesName(LauncherFiles.SHARED_PREFERENCES_KEY);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070077 addPreferencesFromResource(R.xml.launcher_preferences);
Sunny Goyal7779d622015-06-11 16:18:39 -070078
Tony Wickham2ab84822017-05-12 14:59:09 -070079 ContentResolver resolver = getActivity().getContentResolver();
80
Sunny Goyalf48e5922016-05-12 15:36:20 -070081 // Setup allow rotation preference
82 Preference rotationPref = findPreference(Utilities.ALLOW_ROTATION_PREFERENCE_KEY);
83 if (getResources().getBoolean(R.bool.allow_rotation)) {
84 // Launcher supports rotation by default. No need to show this setting.
85 getPreferenceScreen().removePreference(rotationPref);
86 } else {
Sunny Goyalf48e5922016-05-12 15:36:20 -070087 mRotationLockObserver = new SystemDisplayRotationLockObserver(rotationPref, resolver);
88
89 // Register a content observer to listen for system setting changes while
90 // this UI is active.
91 resolver.registerContentObserver(
92 Settings.System.getUriFor(System.ACCELEROMETER_ROTATION),
93 false, mRotationLockObserver);
94
95 // Initialize the UI once
96 mRotationLockObserver.onChange(true);
Sunny Goyalab069992016-06-08 12:00:02 -070097 rotationPref.setDefaultValue(Utilities.getAllowRotationDefaultValue(getActivity()));
Sunny Goyal745bad92016-05-02 10:54:12 -070098 }
Sunny Goyal4179e9b2017-03-08 14:25:09 -080099
Sunny Goyal848cad52017-07-07 03:12:21 -0700100 ButtonPreference iconBadgingPref =
101 (ButtonPreference) findPreference(ICON_BADGING_PREFERENCE_KEY);
Sunny Goyalde57caf2017-06-09 08:42:39 -0700102 if (!Utilities.isAtLeastO()) {
Sunny Goyal4179e9b2017-03-08 14:25:09 -0800103 getPreferenceScreen().removePreference(
104 findPreference(SessionCommitReceiver.ADD_ICON_PREFERENCE_KEY));
Tony Wickham2ab84822017-05-12 14:59:09 -0700105 getPreferenceScreen().removePreference(iconBadgingPref);
106 } else {
107 // Listen to system notification badge settings while this UI is active.
Sunny Goyal848cad52017-07-07 03:12:21 -0700108 mIconBadgingObserver = new IconBadgingObserver(
109 iconBadgingPref, resolver, getFragmentManager());
Tony Wickham2ab84822017-05-12 14:59:09 -0700110 resolver.registerContentObserver(
111 Settings.Secure.getUriFor(NOTIFICATION_BADGING),
112 false, mIconBadgingObserver);
Sunny Goyal848cad52017-07-07 03:12:21 -0700113 resolver.registerContentObserver(
114 Settings.Secure.getUriFor(NOTIFICATION_ENABLED_LISTENERS),
115 false, mIconBadgingObserver);
Tony Wickham2ab84822017-05-12 14:59:09 -0700116 mIconBadgingObserver.onChange(true);
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 Goyalf48e5922016-05-12 15:36:20 -0700130 public void onDestroy() {
131 if (mRotationLockObserver != null) {
Sunny Goyalab069992016-06-08 12:00:02 -0700132 getActivity().getContentResolver().unregisterContentObserver(mRotationLockObserver);
Sunny Goyalf48e5922016-05-12 15:36:20 -0700133 mRotationLockObserver = null;
134 }
Tony Wickham2ab84822017-05-12 14:59:09 -0700135 if (mIconBadgingObserver != null) {
136 getActivity().getContentResolver().unregisterContentObserver(mIconBadgingObserver);
137 mIconBadgingObserver = null;
138 }
Sunny Goyalf48e5922016-05-12 15:36:20 -0700139 super.onDestroy();
140 }
141 }
142
143 /**
144 * Content observer which listens for system auto-rotate setting changes, and enables/disables
145 * the launcher rotation setting accordingly.
146 */
147 private static class SystemDisplayRotationLockObserver extends ContentObserver {
148
149 private final Preference mRotationPref;
150 private final ContentResolver mResolver;
151
152 public SystemDisplayRotationLockObserver(
153 Preference rotationPref, ContentResolver resolver) {
154 super(new Handler());
155 mRotationPref = rotationPref;
156 mResolver = resolver;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400157 }
Sunny Goyal745bad92016-05-02 10:54:12 -0700158
Sunny Goyalf48e5922016-05-12 15:36:20 -0700159 @Override
160 public void onChange(boolean selfChange) {
161 boolean enabled = Settings.System.getInt(mResolver,
162 Settings.System.ACCELEROMETER_ROTATION, 1) == 1;
163 mRotationPref.setEnabled(enabled);
164 mRotationPref.setSummary(enabled
165 ? R.string.allow_rotation_desc : R.string.allow_rotation_blocked_desc);
Sunny Goyal745bad92016-05-02 10:54:12 -0700166 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700167 }
Tony Wickham2ab84822017-05-12 14:59:09 -0700168
169 /**
170 * Content observer which listens for system badging setting changes,
171 * and updates the launcher badging setting subtext accordingly.
172 */
Sunny Goyal848cad52017-07-07 03:12:21 -0700173 private static class IconBadgingObserver extends ContentObserver
Sunny Goyalb65e13c2017-08-01 11:04:15 -0700174 implements Preference.OnPreferenceClickListener {
Tony Wickham2ab84822017-05-12 14:59:09 -0700175
Sunny Goyal848cad52017-07-07 03:12:21 -0700176 private final ButtonPreference mBadgingPref;
Tony Wickham2ab84822017-05-12 14:59:09 -0700177 private final ContentResolver mResolver;
Sunny Goyal848cad52017-07-07 03:12:21 -0700178 private final FragmentManager mFragmentManager;
Tony Wickham2ab84822017-05-12 14:59:09 -0700179
Sunny Goyal848cad52017-07-07 03:12:21 -0700180 public IconBadgingObserver(ButtonPreference badgingPref, ContentResolver resolver,
181 FragmentManager fragmentManager) {
Tony Wickham2ab84822017-05-12 14:59:09 -0700182 super(new Handler());
183 mBadgingPref = badgingPref;
184 mResolver = resolver;
Sunny Goyal848cad52017-07-07 03:12:21 -0700185 mFragmentManager = fragmentManager;
Tony Wickham2ab84822017-05-12 14:59:09 -0700186 }
187
188 @Override
189 public void onChange(boolean selfChange) {
190 boolean enabled = Settings.Secure.getInt(mResolver, NOTIFICATION_BADGING, 1) == 1;
Sunny Goyal848cad52017-07-07 03:12:21 -0700191 int summary = enabled ? R.string.icon_badging_desc_on : R.string.icon_badging_desc_off;
192
193 boolean serviceEnabled = true;
194 if (enabled) {
195 // Check if the listener is enabled or not.
196 String enabledListeners =
197 Settings.Secure.getString(mResolver, NOTIFICATION_ENABLED_LISTENERS);
198 ComponentName myListener =
199 new ComponentName(mBadgingPref.getContext(), NotificationListener.class);
200 serviceEnabled = enabledListeners != null &&
201 (enabledListeners.contains(myListener.flattenToString()) ||
202 enabledListeners.contains(myListener.flattenToShortString()));
203 if (!serviceEnabled) {
204 summary = R.string.title_missing_notification_access;
205 }
206 }
Sunny Goyalb65e13c2017-08-01 11:04:15 -0700207 mBadgingPref.setWidgetFrameVisible(!serviceEnabled);
208 mBadgingPref.setOnPreferenceClickListener(serviceEnabled ? null : this);
Sunny Goyal848cad52017-07-07 03:12:21 -0700209 mBadgingPref.setSummary(summary);
210
211 }
212
213 @Override
Sunny Goyalb65e13c2017-08-01 11:04:15 -0700214 public boolean onPreferenceClick(Preference preference) {
Sunny Goyal848cad52017-07-07 03:12:21 -0700215 new NotificationAccessConfirmation().show(mFragmentManager, "notification_access");
Sunny Goyalb65e13c2017-08-01 11:04:15 -0700216 return true;
Tony Wickham2ab84822017-05-12 14:59:09 -0700217 }
218 }
219
Sunny Goyal848cad52017-07-07 03:12:21 -0700220 public static class NotificationAccessConfirmation
221 extends DialogFragment implements DialogInterface.OnClickListener {
222
223 @Override
224 public Dialog onCreateDialog(Bundle savedInstanceState) {
225 final Context context = getActivity();
226 String msg = context.getString(R.string.msg_missing_notification_access,
227 context.getString(R.string.derived_app_name));
228 return new AlertDialog.Builder(context)
229 .setTitle(R.string.title_missing_notification_access)
230 .setMessage(msg)
231 .setNegativeButton(android.R.string.cancel, null)
232 .setPositiveButton(R.string.title_change_settings, this)
233 .create();
234 }
235
236 @Override
237 public void onClick(DialogInterface dialogInterface, int i) {
238 ComponentName cn = new ComponentName(getActivity(), NotificationListener.class);
239 Intent intent = new Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS)
240 .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
241 .putExtra(":settings:fragment_args_key", cn.flattenToString());
242 getActivity().startActivity(intent);
243 }
244 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700245}