blob: a34e469ea27ce8d92cb134057f83ac400184adca [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 Goyalf48e5922016-05-12 15:36:20 -070020import android.content.ContentResolver;
21import android.database.ContentObserver;
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070022import android.os.Bundle;
Sunny Goyalf48e5922016-05-12 15:36:20 -070023import android.os.Handler;
Sunny Goyalca187462017-03-31 20:09:34 -070024import android.preference.ListPreference;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -040025import android.preference.Preference;
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070026import android.preference.PreferenceFragment;
Sunny Goyalf48e5922016-05-12 15:36:20 -070027import android.provider.Settings;
28import android.provider.Settings.System;
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070029
Sunny Goyalca187462017-03-31 20:09:34 -070030import com.android.launcher3.graphics.IconShapeOverride;
31
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070032/**
Rahul Chaturvedi799aa042015-06-01 21:26:41 -040033 * Settings activity for Launcher. Currently implements the following setting: Allow rotation
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070034 */
35public class SettingsActivity extends Activity {
Tony Wickham2ab84822017-05-12 14:59:09 -070036
37 private static final String ICON_BADGING_PREFERENCE_KEY = "pref_icon_badging";
38 // TODO: use Settings.Secure.NOTIFICATION_BADGING
39 private static final String NOTIFICATION_BADGING = "notification_badging";
40
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070041 @Override
42 protected void onCreate(Bundle savedInstanceState) {
43 super.onCreate(savedInstanceState);
44
Mario Bertschler2de2d672017-06-30 18:51:46 -070045 if (savedInstanceState == null) {
46 // Display the fragment as the main content.
47 getFragmentManager().beginTransaction()
48 .replace(android.R.id.content, new LauncherSettingsFragment())
49 .commit();
50 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070051 }
52
53 /**
54 * This fragment shows the launcher preferences.
55 */
Sunny Goyalf48e5922016-05-12 15:36:20 -070056 public static class LauncherSettingsFragment extends PreferenceFragment {
57
58 private SystemDisplayRotationLockObserver mRotationLockObserver;
Tony Wickham2ab84822017-05-12 14:59:09 -070059 private IconBadgingObserver mIconBadgingObserver;
Sunny Goyalf48e5922016-05-12 15:36:20 -070060
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070061 @Override
62 public void onCreate(Bundle savedInstanceState) {
63 super.onCreate(savedInstanceState);
Sunny Goyalf48e5922016-05-12 15:36:20 -070064 getPreferenceManager().setSharedPreferencesName(LauncherFiles.SHARED_PREFERENCES_KEY);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070065 addPreferencesFromResource(R.xml.launcher_preferences);
Sunny Goyal7779d622015-06-11 16:18:39 -070066
Tony Wickham2ab84822017-05-12 14:59:09 -070067 ContentResolver resolver = getActivity().getContentResolver();
68
Sunny Goyalf48e5922016-05-12 15:36:20 -070069 // Setup allow rotation preference
70 Preference rotationPref = findPreference(Utilities.ALLOW_ROTATION_PREFERENCE_KEY);
71 if (getResources().getBoolean(R.bool.allow_rotation)) {
72 // Launcher supports rotation by default. No need to show this setting.
73 getPreferenceScreen().removePreference(rotationPref);
74 } else {
Sunny Goyalf48e5922016-05-12 15:36:20 -070075 mRotationLockObserver = new SystemDisplayRotationLockObserver(rotationPref, resolver);
76
77 // Register a content observer to listen for system setting changes while
78 // this UI is active.
79 resolver.registerContentObserver(
80 Settings.System.getUriFor(System.ACCELEROMETER_ROTATION),
81 false, mRotationLockObserver);
82
83 // Initialize the UI once
84 mRotationLockObserver.onChange(true);
Sunny Goyalab069992016-06-08 12:00:02 -070085 rotationPref.setDefaultValue(Utilities.getAllowRotationDefaultValue(getActivity()));
Sunny Goyal745bad92016-05-02 10:54:12 -070086 }
Sunny Goyal4179e9b2017-03-08 14:25:09 -080087
Tony Wickham2ab84822017-05-12 14:59:09 -070088 Preference iconBadgingPref = findPreference(ICON_BADGING_PREFERENCE_KEY);
Sunny Goyalde57caf2017-06-09 08:42:39 -070089 if (!Utilities.isAtLeastO()) {
Sunny Goyal4179e9b2017-03-08 14:25:09 -080090 getPreferenceScreen().removePreference(
91 findPreference(SessionCommitReceiver.ADD_ICON_PREFERENCE_KEY));
Tony Wickham2ab84822017-05-12 14:59:09 -070092 getPreferenceScreen().removePreference(iconBadgingPref);
93 } else {
94 // Listen to system notification badge settings while this UI is active.
95 mIconBadgingObserver = new IconBadgingObserver(iconBadgingPref, resolver);
96 resolver.registerContentObserver(
97 Settings.Secure.getUriFor(NOTIFICATION_BADGING),
98 false, mIconBadgingObserver);
99 mIconBadgingObserver.onChange(true);
Sunny Goyal4179e9b2017-03-08 14:25:09 -0800100 }
Sunny Goyalca187462017-03-31 20:09:34 -0700101
102 Preference iconShapeOverride = findPreference(IconShapeOverride.KEY_PREFERENCE);
103 if (iconShapeOverride != null) {
104 if (IconShapeOverride.isSupported(getActivity())) {
105 IconShapeOverride.handlePreferenceUi((ListPreference) iconShapeOverride);
106 } else {
107 getPreferenceScreen().removePreference(iconShapeOverride);
108 }
109 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700110 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400111
112 @Override
Sunny Goyalf48e5922016-05-12 15:36:20 -0700113 public void onDestroy() {
114 if (mRotationLockObserver != null) {
Sunny Goyalab069992016-06-08 12:00:02 -0700115 getActivity().getContentResolver().unregisterContentObserver(mRotationLockObserver);
Sunny Goyalf48e5922016-05-12 15:36:20 -0700116 mRotationLockObserver = null;
117 }
Tony Wickham2ab84822017-05-12 14:59:09 -0700118 if (mIconBadgingObserver != null) {
119 getActivity().getContentResolver().unregisterContentObserver(mIconBadgingObserver);
120 mIconBadgingObserver = null;
121 }
Sunny Goyalf48e5922016-05-12 15:36:20 -0700122 super.onDestroy();
123 }
124 }
125
126 /**
127 * Content observer which listens for system auto-rotate setting changes, and enables/disables
128 * the launcher rotation setting accordingly.
129 */
130 private static class SystemDisplayRotationLockObserver extends ContentObserver {
131
132 private final Preference mRotationPref;
133 private final ContentResolver mResolver;
134
135 public SystemDisplayRotationLockObserver(
136 Preference rotationPref, ContentResolver resolver) {
137 super(new Handler());
138 mRotationPref = rotationPref;
139 mResolver = resolver;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400140 }
Sunny Goyal745bad92016-05-02 10:54:12 -0700141
Sunny Goyalf48e5922016-05-12 15:36:20 -0700142 @Override
143 public void onChange(boolean selfChange) {
144 boolean enabled = Settings.System.getInt(mResolver,
145 Settings.System.ACCELEROMETER_ROTATION, 1) == 1;
146 mRotationPref.setEnabled(enabled);
147 mRotationPref.setSummary(enabled
148 ? R.string.allow_rotation_desc : R.string.allow_rotation_blocked_desc);
Sunny Goyal745bad92016-05-02 10:54:12 -0700149 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700150 }
Tony Wickham2ab84822017-05-12 14:59:09 -0700151
152 /**
153 * Content observer which listens for system badging setting changes,
154 * and updates the launcher badging setting subtext accordingly.
155 */
156 private static class IconBadgingObserver extends ContentObserver {
157
158 private final Preference mBadgingPref;
159 private final ContentResolver mResolver;
160
161 public IconBadgingObserver(Preference badgingPref, ContentResolver resolver) {
162 super(new Handler());
163 mBadgingPref = badgingPref;
164 mResolver = resolver;
165 }
166
167 @Override
168 public void onChange(boolean selfChange) {
169 boolean enabled = Settings.Secure.getInt(mResolver, NOTIFICATION_BADGING, 1) == 1;
170 mBadgingPref.setSummary(enabled
171 ? R.string.icon_badging_desc_on
172 : R.string.icon_badging_desc_off);
173 }
174 }
175
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700176}