blob: 90463725fff06ea0d010d1499eb676751781fdee [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);
101 } else {
102 // Listen to system notification badge settings while this UI is active.
Sunny Goyal848cad52017-07-07 03:12:21 -0700103 mIconBadgingObserver = new IconBadgingObserver(
104 iconBadgingPref, resolver, getFragmentManager());
Tonyd48710c2017-07-27 23:23:58 -0700105 mIconBadgingObserver.register(NOTIFICATION_BADGING, NOTIFICATION_ENABLED_LISTENERS);
Sunny Goyal4179e9b2017-03-08 14:25:09 -0800106 }
Sunny Goyalca187462017-03-31 20:09:34 -0700107
108 Preference iconShapeOverride = findPreference(IconShapeOverride.KEY_PREFERENCE);
109 if (iconShapeOverride != null) {
110 if (IconShapeOverride.isSupported(getActivity())) {
111 IconShapeOverride.handlePreferenceUi((ListPreference) iconShapeOverride);
112 } else {
113 getPreferenceScreen().removePreference(iconShapeOverride);
114 }
115 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700116 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400117
118 @Override
Sunny Goyalf48e5922016-05-12 15:36:20 -0700119 public void onDestroy() {
120 if (mRotationLockObserver != null) {
Tonyd48710c2017-07-27 23:23:58 -0700121 mRotationLockObserver.unregister();
Sunny Goyalf48e5922016-05-12 15:36:20 -0700122 mRotationLockObserver = null;
123 }
Tony Wickham2ab84822017-05-12 14:59:09 -0700124 if (mIconBadgingObserver != null) {
Tonyd48710c2017-07-27 23:23:58 -0700125 mIconBadgingObserver.unregister();
Tony Wickham2ab84822017-05-12 14:59:09 -0700126 mIconBadgingObserver = null;
127 }
Sunny Goyalf48e5922016-05-12 15:36:20 -0700128 super.onDestroy();
129 }
130 }
131
132 /**
133 * Content observer which listens for system auto-rotate setting changes, and enables/disables
134 * the launcher rotation setting accordingly.
135 */
Tonyd48710c2017-07-27 23:23:58 -0700136 private static class SystemDisplayRotationLockObserver extends SettingsObserver.System {
Sunny Goyalf48e5922016-05-12 15:36:20 -0700137
138 private final Preference mRotationPref;
Sunny Goyalf48e5922016-05-12 15:36:20 -0700139
140 public SystemDisplayRotationLockObserver(
141 Preference rotationPref, ContentResolver resolver) {
Tonyd48710c2017-07-27 23:23:58 -0700142 super(resolver);
Sunny Goyalf48e5922016-05-12 15:36:20 -0700143 mRotationPref = rotationPref;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400144 }
Sunny Goyal745bad92016-05-02 10:54:12 -0700145
Sunny Goyalf48e5922016-05-12 15:36:20 -0700146 @Override
Tonyd48710c2017-07-27 23:23:58 -0700147 public void onSettingChanged(boolean enabled) {
Sunny Goyalf48e5922016-05-12 15:36:20 -0700148 mRotationPref.setEnabled(enabled);
149 mRotationPref.setSummary(enabled
150 ? R.string.allow_rotation_desc : R.string.allow_rotation_blocked_desc);
Sunny Goyal745bad92016-05-02 10:54:12 -0700151 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700152 }
Tony Wickham2ab84822017-05-12 14:59:09 -0700153
154 /**
155 * Content observer which listens for system badging setting changes,
156 * and updates the launcher badging setting subtext accordingly.
157 */
Tonyd48710c2017-07-27 23:23:58 -0700158 private static class IconBadgingObserver extends SettingsObserver.Secure
Sunny Goyalb65e13c2017-08-01 11:04:15 -0700159 implements Preference.OnPreferenceClickListener {
Tony Wickham2ab84822017-05-12 14:59:09 -0700160
Sunny Goyal848cad52017-07-07 03:12:21 -0700161 private final ButtonPreference mBadgingPref;
Tony Wickham2ab84822017-05-12 14:59:09 -0700162 private final ContentResolver mResolver;
Sunny Goyal848cad52017-07-07 03:12:21 -0700163 private final FragmentManager mFragmentManager;
Tony Wickham2ab84822017-05-12 14:59:09 -0700164
Sunny Goyal848cad52017-07-07 03:12:21 -0700165 public IconBadgingObserver(ButtonPreference badgingPref, ContentResolver resolver,
166 FragmentManager fragmentManager) {
Tonyd48710c2017-07-27 23:23:58 -0700167 super(resolver);
Tony Wickham2ab84822017-05-12 14:59:09 -0700168 mBadgingPref = badgingPref;
169 mResolver = resolver;
Sunny Goyal848cad52017-07-07 03:12:21 -0700170 mFragmentManager = fragmentManager;
Tony Wickham2ab84822017-05-12 14:59:09 -0700171 }
172
173 @Override
Tonyd48710c2017-07-27 23:23:58 -0700174 public void onSettingChanged(boolean enabled) {
Sunny Goyal848cad52017-07-07 03:12:21 -0700175 int summary = enabled ? R.string.icon_badging_desc_on : R.string.icon_badging_desc_off;
176
177 boolean serviceEnabled = true;
178 if (enabled) {
179 // Check if the listener is enabled or not.
180 String enabledListeners =
181 Settings.Secure.getString(mResolver, NOTIFICATION_ENABLED_LISTENERS);
182 ComponentName myListener =
183 new ComponentName(mBadgingPref.getContext(), NotificationListener.class);
184 serviceEnabled = enabledListeners != null &&
185 (enabledListeners.contains(myListener.flattenToString()) ||
186 enabledListeners.contains(myListener.flattenToShortString()));
187 if (!serviceEnabled) {
188 summary = R.string.title_missing_notification_access;
189 }
190 }
Sunny Goyalb65e13c2017-08-01 11:04:15 -0700191 mBadgingPref.setWidgetFrameVisible(!serviceEnabled);
192 mBadgingPref.setOnPreferenceClickListener(serviceEnabled ? null : this);
Sunny Goyal848cad52017-07-07 03:12:21 -0700193 mBadgingPref.setSummary(summary);
194
195 }
196
197 @Override
Sunny Goyalb65e13c2017-08-01 11:04:15 -0700198 public boolean onPreferenceClick(Preference preference) {
Sunny Goyal848cad52017-07-07 03:12:21 -0700199 new NotificationAccessConfirmation().show(mFragmentManager, "notification_access");
Sunny Goyalb65e13c2017-08-01 11:04:15 -0700200 return true;
Tony Wickham2ab84822017-05-12 14:59:09 -0700201 }
202 }
203
Sunny Goyal848cad52017-07-07 03:12:21 -0700204 public static class NotificationAccessConfirmation
205 extends DialogFragment implements DialogInterface.OnClickListener {
206
207 @Override
208 public Dialog onCreateDialog(Bundle savedInstanceState) {
209 final Context context = getActivity();
210 String msg = context.getString(R.string.msg_missing_notification_access,
211 context.getString(R.string.derived_app_name));
212 return new AlertDialog.Builder(context)
213 .setTitle(R.string.title_missing_notification_access)
214 .setMessage(msg)
215 .setNegativeButton(android.R.string.cancel, null)
216 .setPositiveButton(R.string.title_change_settings, this)
217 .create();
218 }
219
220 @Override
221 public void onClick(DialogInterface dialogInterface, int i) {
222 ComponentName cn = new ComponentName(getActivity(), NotificationListener.class);
223 Intent intent = new Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS)
224 .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
225 .putExtra(":settings:fragment_args_key", cn.flattenToString());
226 getActivity().startActivity(intent);
227 }
228 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700229}