blob: 5bdc1f5bdc71e7534bb67a7a8f8d685ae14c20ca [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;
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070029import android.os.Bundle;
Sunny Goyalca187462017-03-31 20:09:34 -070030import android.preference.ListPreference;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -040031import android.preference.Preference;
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070032import android.preference.PreferenceFragment;
Sunny Goyalf48e5922016-05-12 15:36:20 -070033import android.provider.Settings;
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070034
Sunny Goyalca187462017-03-31 20:09:34 -070035import com.android.launcher3.graphics.IconShapeOverride;
Sunny Goyal848cad52017-07-07 03:12:21 -070036import com.android.launcher3.notification.NotificationListener;
Tonyd48710c2017-07-27 23:23:58 -070037import com.android.launcher3.util.SettingsObserver;
Sunny Goyal848cad52017-07-07 03:12:21 -070038import com.android.launcher3.views.ButtonPreference;
Sunny Goyalca187462017-03-31 20:09:34 -070039
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070040/**
Rahul Chaturvedi799aa042015-06-01 21:26:41 -040041 * Settings activity for Launcher. Currently implements the following setting: Allow rotation
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070042 */
43public class SettingsActivity extends Activity {
Tony Wickham2ab84822017-05-12 14:59:09 -070044
45 private static final String ICON_BADGING_PREFERENCE_KEY = "pref_icon_badging";
Tonyd48710c2017-07-27 23:23:58 -070046 /** Hidden field Settings.Secure.NOTIFICATION_BADGING */
47 public static final String NOTIFICATION_BADGING = "notification_badging";
Sunny Goyal848cad52017-07-07 03:12:21 -070048 /** Hidden field Settings.Secure.ENABLED_NOTIFICATION_LISTENERS */
49 private static final String NOTIFICATION_ENABLED_LISTENERS = "enabled_notification_listeners";
Tony Wickham2ab84822017-05-12 14:59:09 -070050
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070051 @Override
52 protected void onCreate(Bundle savedInstanceState) {
53 super.onCreate(savedInstanceState);
54
Mario Bertschler2de2d672017-06-30 18:51:46 -070055 if (savedInstanceState == null) {
56 // Display the fragment as the main content.
57 getFragmentManager().beginTransaction()
58 .replace(android.R.id.content, new LauncherSettingsFragment())
59 .commit();
60 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070061 }
62
63 /**
64 * This fragment shows the launcher preferences.
65 */
Sunny Goyalf48e5922016-05-12 15:36:20 -070066 public static class LauncherSettingsFragment extends PreferenceFragment {
67
68 private SystemDisplayRotationLockObserver mRotationLockObserver;
Tony Wickham2ab84822017-05-12 14:59:09 -070069 private IconBadgingObserver mIconBadgingObserver;
Sunny Goyalf48e5922016-05-12 15:36:20 -070070
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070071 @Override
72 public void onCreate(Bundle savedInstanceState) {
73 super.onCreate(savedInstanceState);
Sunny Goyalf48e5922016-05-12 15:36:20 -070074 getPreferenceManager().setSharedPreferencesName(LauncherFiles.SHARED_PREFERENCES_KEY);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070075 addPreferencesFromResource(R.xml.launcher_preferences);
Sunny Goyal7779d622015-06-11 16:18:39 -070076
Tony Wickham2ab84822017-05-12 14:59:09 -070077 ContentResolver resolver = getActivity().getContentResolver();
78
Sunny Goyalf48e5922016-05-12 15:36:20 -070079 // Setup allow rotation preference
80 Preference rotationPref = findPreference(Utilities.ALLOW_ROTATION_PREFERENCE_KEY);
81 if (getResources().getBoolean(R.bool.allow_rotation)) {
82 // Launcher supports rotation by default. No need to show this setting.
83 getPreferenceScreen().removePreference(rotationPref);
84 } else {
Sunny Goyalf48e5922016-05-12 15:36:20 -070085 mRotationLockObserver = new SystemDisplayRotationLockObserver(rotationPref, resolver);
86
87 // Register a content observer to listen for system setting changes while
88 // this UI is active.
Tonyd48710c2017-07-27 23:23:58 -070089 mRotationLockObserver.register(Settings.System.ACCELEROMETER_ROTATION);
Sunny Goyalf48e5922016-05-12 15:36:20 -070090
91 // Initialize the UI once
Sunny Goyalab069992016-06-08 12:00:02 -070092 rotationPref.setDefaultValue(Utilities.getAllowRotationDefaultValue(getActivity()));
Sunny Goyal745bad92016-05-02 10:54:12 -070093 }
Sunny Goyal4179e9b2017-03-08 14:25:09 -080094
Sunny Goyal848cad52017-07-07 03:12:21 -070095 ButtonPreference iconBadgingPref =
96 (ButtonPreference) findPreference(ICON_BADGING_PREFERENCE_KEY);
Sunny Goyalde57caf2017-06-09 08:42:39 -070097 if (!Utilities.isAtLeastO()) {
Sunny Goyal4179e9b2017-03-08 14:25:09 -080098 getPreferenceScreen().removePreference(
99 findPreference(SessionCommitReceiver.ADD_ICON_PREFERENCE_KEY));
Tony Wickham2ab84822017-05-12 14:59:09 -0700100 getPreferenceScreen().removePreference(iconBadgingPref);
Tony Wickhamc1385152017-08-14 14:31:03 -0700101 } else if (!getResources().getBoolean(R.bool.notification_badging_enabled)) {
102 getPreferenceScreen().removePreference(iconBadgingPref);
Tony Wickham2ab84822017-05-12 14:59:09 -0700103 } else {
104 // Listen to system notification badge settings while this UI is active.
Sunny Goyal848cad52017-07-07 03:12:21 -0700105 mIconBadgingObserver = new IconBadgingObserver(
106 iconBadgingPref, resolver, getFragmentManager());
Tonyd48710c2017-07-27 23:23:58 -0700107 mIconBadgingObserver.register(NOTIFICATION_BADGING, NOTIFICATION_ENABLED_LISTENERS);
Sunny Goyal4179e9b2017-03-08 14:25:09 -0800108 }
Sunny Goyalca187462017-03-31 20:09:34 -0700109
110 Preference iconShapeOverride = findPreference(IconShapeOverride.KEY_PREFERENCE);
111 if (iconShapeOverride != null) {
112 if (IconShapeOverride.isSupported(getActivity())) {
113 IconShapeOverride.handlePreferenceUi((ListPreference) iconShapeOverride);
114 } else {
115 getPreferenceScreen().removePreference(iconShapeOverride);
116 }
117 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700118 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400119
120 @Override
Sunny Goyalf48e5922016-05-12 15:36:20 -0700121 public void onDestroy() {
122 if (mRotationLockObserver != null) {
Tonyd48710c2017-07-27 23:23:58 -0700123 mRotationLockObserver.unregister();
Sunny Goyalf48e5922016-05-12 15:36:20 -0700124 mRotationLockObserver = null;
125 }
Tony Wickham2ab84822017-05-12 14:59:09 -0700126 if (mIconBadgingObserver != null) {
Tonyd48710c2017-07-27 23:23:58 -0700127 mIconBadgingObserver.unregister();
Tony Wickham2ab84822017-05-12 14:59:09 -0700128 mIconBadgingObserver = null;
129 }
Sunny Goyalf48e5922016-05-12 15:36:20 -0700130 super.onDestroy();
131 }
132 }
133
134 /**
135 * Content observer which listens for system auto-rotate setting changes, and enables/disables
136 * the launcher rotation setting accordingly.
137 */
Tonyd48710c2017-07-27 23:23:58 -0700138 private static class SystemDisplayRotationLockObserver extends SettingsObserver.System {
Sunny Goyalf48e5922016-05-12 15:36:20 -0700139
140 private final Preference mRotationPref;
Sunny Goyalf48e5922016-05-12 15:36:20 -0700141
142 public SystemDisplayRotationLockObserver(
143 Preference rotationPref, ContentResolver resolver) {
Tonyd48710c2017-07-27 23:23:58 -0700144 super(resolver);
Sunny Goyalf48e5922016-05-12 15:36:20 -0700145 mRotationPref = rotationPref;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400146 }
Sunny Goyal745bad92016-05-02 10:54:12 -0700147
Sunny Goyalf48e5922016-05-12 15:36:20 -0700148 @Override
Tonyd48710c2017-07-27 23:23:58 -0700149 public void onSettingChanged(boolean enabled) {
Sunny Goyalf48e5922016-05-12 15:36:20 -0700150 mRotationPref.setEnabled(enabled);
151 mRotationPref.setSummary(enabled
152 ? R.string.allow_rotation_desc : R.string.allow_rotation_blocked_desc);
Sunny Goyal745bad92016-05-02 10:54:12 -0700153 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700154 }
Tony Wickham2ab84822017-05-12 14:59:09 -0700155
156 /**
157 * Content observer which listens for system badging setting changes,
158 * and updates the launcher badging setting subtext accordingly.
159 */
Tonyd48710c2017-07-27 23:23:58 -0700160 private static class IconBadgingObserver extends SettingsObserver.Secure
Sunny Goyalb65e13c2017-08-01 11:04:15 -0700161 implements Preference.OnPreferenceClickListener {
Tony Wickham2ab84822017-05-12 14:59:09 -0700162
Sunny Goyal848cad52017-07-07 03:12:21 -0700163 private final ButtonPreference mBadgingPref;
Tony Wickham2ab84822017-05-12 14:59:09 -0700164 private final ContentResolver mResolver;
Sunny Goyal848cad52017-07-07 03:12:21 -0700165 private final FragmentManager mFragmentManager;
Tony Wickham2ab84822017-05-12 14:59:09 -0700166
Sunny Goyal848cad52017-07-07 03:12:21 -0700167 public IconBadgingObserver(ButtonPreference badgingPref, ContentResolver resolver,
168 FragmentManager fragmentManager) {
Tonyd48710c2017-07-27 23:23:58 -0700169 super(resolver);
Tony Wickham2ab84822017-05-12 14:59:09 -0700170 mBadgingPref = badgingPref;
171 mResolver = resolver;
Sunny Goyal848cad52017-07-07 03:12:21 -0700172 mFragmentManager = fragmentManager;
Tony Wickham2ab84822017-05-12 14:59:09 -0700173 }
174
175 @Override
Tonyd48710c2017-07-27 23:23:58 -0700176 public void onSettingChanged(boolean enabled) {
Sunny Goyal848cad52017-07-07 03:12:21 -0700177 int summary = enabled ? R.string.icon_badging_desc_on : R.string.icon_badging_desc_off;
178
179 boolean serviceEnabled = true;
180 if (enabled) {
181 // Check if the listener is enabled or not.
182 String enabledListeners =
183 Settings.Secure.getString(mResolver, NOTIFICATION_ENABLED_LISTENERS);
184 ComponentName myListener =
185 new ComponentName(mBadgingPref.getContext(), NotificationListener.class);
186 serviceEnabled = enabledListeners != null &&
187 (enabledListeners.contains(myListener.flattenToString()) ||
188 enabledListeners.contains(myListener.flattenToShortString()));
189 if (!serviceEnabled) {
190 summary = R.string.title_missing_notification_access;
191 }
192 }
Sunny Goyalb65e13c2017-08-01 11:04:15 -0700193 mBadgingPref.setWidgetFrameVisible(!serviceEnabled);
194 mBadgingPref.setOnPreferenceClickListener(serviceEnabled ? null : this);
Sunny Goyal848cad52017-07-07 03:12:21 -0700195 mBadgingPref.setSummary(summary);
196
197 }
198
199 @Override
Sunny Goyalb65e13c2017-08-01 11:04:15 -0700200 public boolean onPreferenceClick(Preference preference) {
Sunny Goyal848cad52017-07-07 03:12:21 -0700201 new NotificationAccessConfirmation().show(mFragmentManager, "notification_access");
Sunny Goyalb65e13c2017-08-01 11:04:15 -0700202 return true;
Tony Wickham2ab84822017-05-12 14:59:09 -0700203 }
204 }
205
Sunny Goyal848cad52017-07-07 03:12:21 -0700206 public static class NotificationAccessConfirmation
207 extends DialogFragment implements DialogInterface.OnClickListener {
208
209 @Override
210 public Dialog onCreateDialog(Bundle savedInstanceState) {
211 final Context context = getActivity();
212 String msg = context.getString(R.string.msg_missing_notification_access,
213 context.getString(R.string.derived_app_name));
214 return new AlertDialog.Builder(context)
215 .setTitle(R.string.title_missing_notification_access)
216 .setMessage(msg)
217 .setNegativeButton(android.R.string.cancel, null)
218 .setPositiveButton(R.string.title_change_settings, this)
219 .create();
220 }
221
222 @Override
223 public void onClick(DialogInterface dialogInterface, int i) {
224 ComponentName cn = new ComponentName(getActivity(), NotificationListener.class);
225 Intent intent = new Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS)
226 .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
227 .putExtra(":settings:fragment_args_key", cn.flattenToString());
228 getActivity().startActivity(intent);
229 }
230 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700231}