blob: b7b75f87d85d8503e3d0501b093b11aec1357df2 [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
45 // Display the fragment as the main content.
46 getFragmentManager().beginTransaction()
47 .replace(android.R.id.content, new LauncherSettingsFragment())
48 .commit();
49 }
50
51 /**
52 * This fragment shows the launcher preferences.
53 */
Sunny Goyalf48e5922016-05-12 15:36:20 -070054 public static class LauncherSettingsFragment extends PreferenceFragment {
55
56 private SystemDisplayRotationLockObserver mRotationLockObserver;
Tony Wickham2ab84822017-05-12 14:59:09 -070057 private IconBadgingObserver mIconBadgingObserver;
Sunny Goyalf48e5922016-05-12 15:36:20 -070058
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070059 @Override
60 public void onCreate(Bundle savedInstanceState) {
61 super.onCreate(savedInstanceState);
Sunny Goyalf48e5922016-05-12 15:36:20 -070062 getPreferenceManager().setSharedPreferencesName(LauncherFiles.SHARED_PREFERENCES_KEY);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070063 addPreferencesFromResource(R.xml.launcher_preferences);
Sunny Goyal7779d622015-06-11 16:18:39 -070064
Tony Wickham2ab84822017-05-12 14:59:09 -070065 ContentResolver resolver = getActivity().getContentResolver();
66
Sunny Goyalf48e5922016-05-12 15:36:20 -070067 // Setup allow rotation preference
68 Preference rotationPref = findPreference(Utilities.ALLOW_ROTATION_PREFERENCE_KEY);
69 if (getResources().getBoolean(R.bool.allow_rotation)) {
70 // Launcher supports rotation by default. No need to show this setting.
71 getPreferenceScreen().removePreference(rotationPref);
72 } else {
Sunny Goyalf48e5922016-05-12 15:36:20 -070073 mRotationLockObserver = new SystemDisplayRotationLockObserver(rotationPref, resolver);
74
75 // Register a content observer to listen for system setting changes while
76 // this UI is active.
77 resolver.registerContentObserver(
78 Settings.System.getUriFor(System.ACCELEROMETER_ROTATION),
79 false, mRotationLockObserver);
80
81 // Initialize the UI once
82 mRotationLockObserver.onChange(true);
Sunny Goyalab069992016-06-08 12:00:02 -070083 rotationPref.setDefaultValue(Utilities.getAllowRotationDefaultValue(getActivity()));
Sunny Goyal745bad92016-05-02 10:54:12 -070084 }
Sunny Goyal4179e9b2017-03-08 14:25:09 -080085
Tony Wickham2ab84822017-05-12 14:59:09 -070086 Preference iconBadgingPref = findPreference(ICON_BADGING_PREFERENCE_KEY);
Sunny Goyalde57caf2017-06-09 08:42:39 -070087 if (!Utilities.isAtLeastO()) {
Sunny Goyal4179e9b2017-03-08 14:25:09 -080088 getPreferenceScreen().removePreference(
89 findPreference(SessionCommitReceiver.ADD_ICON_PREFERENCE_KEY));
Tony Wickham2ab84822017-05-12 14:59:09 -070090 getPreferenceScreen().removePreference(iconBadgingPref);
91 } else {
92 // Listen to system notification badge settings while this UI is active.
93 mIconBadgingObserver = new IconBadgingObserver(iconBadgingPref, resolver);
94 resolver.registerContentObserver(
95 Settings.Secure.getUriFor(NOTIFICATION_BADGING),
96 false, mIconBadgingObserver);
97 mIconBadgingObserver.onChange(true);
Sunny Goyal4179e9b2017-03-08 14:25:09 -080098 }
Sunny Goyalca187462017-03-31 20:09:34 -070099
100 Preference iconShapeOverride = findPreference(IconShapeOverride.KEY_PREFERENCE);
101 if (iconShapeOverride != null) {
102 if (IconShapeOverride.isSupported(getActivity())) {
103 IconShapeOverride.handlePreferenceUi((ListPreference) iconShapeOverride);
104 } else {
105 getPreferenceScreen().removePreference(iconShapeOverride);
106 }
107 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700108 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400109
110 @Override
Sunny Goyalf48e5922016-05-12 15:36:20 -0700111 public void onDestroy() {
112 if (mRotationLockObserver != null) {
Sunny Goyalab069992016-06-08 12:00:02 -0700113 getActivity().getContentResolver().unregisterContentObserver(mRotationLockObserver);
Sunny Goyalf48e5922016-05-12 15:36:20 -0700114 mRotationLockObserver = null;
115 }
Tony Wickham2ab84822017-05-12 14:59:09 -0700116 if (mIconBadgingObserver != null) {
117 getActivity().getContentResolver().unregisterContentObserver(mIconBadgingObserver);
118 mIconBadgingObserver = null;
119 }
Sunny Goyalf48e5922016-05-12 15:36:20 -0700120 super.onDestroy();
121 }
122 }
123
124 /**
125 * Content observer which listens for system auto-rotate setting changes, and enables/disables
126 * the launcher rotation setting accordingly.
127 */
128 private static class SystemDisplayRotationLockObserver extends ContentObserver {
129
130 private final Preference mRotationPref;
131 private final ContentResolver mResolver;
132
133 public SystemDisplayRotationLockObserver(
134 Preference rotationPref, ContentResolver resolver) {
135 super(new Handler());
136 mRotationPref = rotationPref;
137 mResolver = resolver;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400138 }
Sunny Goyal745bad92016-05-02 10:54:12 -0700139
Sunny Goyalf48e5922016-05-12 15:36:20 -0700140 @Override
141 public void onChange(boolean selfChange) {
142 boolean enabled = Settings.System.getInt(mResolver,
143 Settings.System.ACCELEROMETER_ROTATION, 1) == 1;
144 mRotationPref.setEnabled(enabled);
145 mRotationPref.setSummary(enabled
146 ? R.string.allow_rotation_desc : R.string.allow_rotation_blocked_desc);
Sunny Goyal745bad92016-05-02 10:54:12 -0700147 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700148 }
Tony Wickham2ab84822017-05-12 14:59:09 -0700149
150 /**
151 * Content observer which listens for system badging setting changes,
152 * and updates the launcher badging setting subtext accordingly.
153 */
154 private static class IconBadgingObserver extends ContentObserver {
155
156 private final Preference mBadgingPref;
157 private final ContentResolver mResolver;
158
159 public IconBadgingObserver(Preference badgingPref, ContentResolver resolver) {
160 super(new Handler());
161 mBadgingPref = badgingPref;
162 mResolver = resolver;
163 }
164
165 @Override
166 public void onChange(boolean selfChange) {
167 boolean enabled = Settings.Secure.getInt(mResolver, NOTIFICATION_BADGING, 1) == 1;
168 mBadgingPref.setSummary(enabled
169 ? R.string.icon_badging_desc_on
170 : R.string.icon_badging_desc_off);
171 }
172 }
173
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700174}