blob: 0902b2029d84657622570f1b6bc7e262092173c6 [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;
Sunny Goyal4179e9b2017-03-08 14:25:09 -080029import android.support.v4.os.BuildCompat;
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070030
Sunny Goyalca187462017-03-31 20:09:34 -070031import com.android.launcher3.graphics.IconShapeOverride;
32
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070033/**
Rahul Chaturvedi799aa042015-06-01 21:26:41 -040034 * Settings activity for Launcher. Currently implements the following setting: Allow rotation
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070035 */
36public class SettingsActivity extends Activity {
Tony Wickham2ab84822017-05-12 14:59:09 -070037
38 private static final String ICON_BADGING_PREFERENCE_KEY = "pref_icon_badging";
39 // TODO: use Settings.Secure.NOTIFICATION_BADGING
40 private static final String NOTIFICATION_BADGING = "notification_badging";
41
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070042 @Override
43 protected void onCreate(Bundle savedInstanceState) {
44 super.onCreate(savedInstanceState);
45
46 // Display the fragment as the main content.
47 getFragmentManager().beginTransaction()
48 .replace(android.R.id.content, new LauncherSettingsFragment())
49 .commit();
50 }
51
52 /**
53 * This fragment shows the launcher preferences.
54 */
Sunny Goyalf48e5922016-05-12 15:36:20 -070055 public static class LauncherSettingsFragment extends PreferenceFragment {
56
57 private SystemDisplayRotationLockObserver mRotationLockObserver;
Tony Wickham2ab84822017-05-12 14:59:09 -070058 private IconBadgingObserver mIconBadgingObserver;
Sunny Goyalf48e5922016-05-12 15:36:20 -070059
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070060 @Override
61 public void onCreate(Bundle savedInstanceState) {
62 super.onCreate(savedInstanceState);
Sunny Goyalf48e5922016-05-12 15:36:20 -070063 getPreferenceManager().setSharedPreferencesName(LauncherFiles.SHARED_PREFERENCES_KEY);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070064 addPreferencesFromResource(R.xml.launcher_preferences);
Sunny Goyal7779d622015-06-11 16:18:39 -070065
Tony Wickham2ab84822017-05-12 14:59:09 -070066 ContentResolver resolver = getActivity().getContentResolver();
67
Sunny Goyalf48e5922016-05-12 15:36:20 -070068 // Setup allow rotation preference
69 Preference rotationPref = findPreference(Utilities.ALLOW_ROTATION_PREFERENCE_KEY);
70 if (getResources().getBoolean(R.bool.allow_rotation)) {
71 // Launcher supports rotation by default. No need to show this setting.
72 getPreferenceScreen().removePreference(rotationPref);
73 } else {
Sunny Goyalf48e5922016-05-12 15:36:20 -070074 mRotationLockObserver = new SystemDisplayRotationLockObserver(rotationPref, resolver);
75
76 // Register a content observer to listen for system setting changes while
77 // this UI is active.
78 resolver.registerContentObserver(
79 Settings.System.getUriFor(System.ACCELEROMETER_ROTATION),
80 false, mRotationLockObserver);
81
82 // Initialize the UI once
83 mRotationLockObserver.onChange(true);
Sunny Goyalab069992016-06-08 12:00:02 -070084 rotationPref.setDefaultValue(Utilities.getAllowRotationDefaultValue(getActivity()));
Sunny Goyal745bad92016-05-02 10:54:12 -070085 }
Sunny Goyal4179e9b2017-03-08 14:25:09 -080086
Tony Wickham2ab84822017-05-12 14:59:09 -070087 Preference iconBadgingPref = findPreference(ICON_BADGING_PREFERENCE_KEY);
Sunny Goyal4179e9b2017-03-08 14:25:09 -080088 if (!BuildCompat.isAtLeastO()) {
89 getPreferenceScreen().removePreference(
90 findPreference(SessionCommitReceiver.ADD_ICON_PREFERENCE_KEY));
Tony Wickham2ab84822017-05-12 14:59:09 -070091 getPreferenceScreen().removePreference(iconBadgingPref);
92 } else {
93 // Listen to system notification badge settings while this UI is active.
94 mIconBadgingObserver = new IconBadgingObserver(iconBadgingPref, resolver);
95 resolver.registerContentObserver(
96 Settings.Secure.getUriFor(NOTIFICATION_BADGING),
97 false, mIconBadgingObserver);
98 mIconBadgingObserver.onChange(true);
Sunny Goyal4179e9b2017-03-08 14:25:09 -080099 }
Sunny Goyalca187462017-03-31 20:09:34 -0700100
101 Preference iconShapeOverride = findPreference(IconShapeOverride.KEY_PREFERENCE);
102 if (iconShapeOverride != null) {
103 if (IconShapeOverride.isSupported(getActivity())) {
104 IconShapeOverride.handlePreferenceUi((ListPreference) iconShapeOverride);
105 } else {
106 getPreferenceScreen().removePreference(iconShapeOverride);
107 }
108 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700109 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400110
111 @Override
Sunny Goyalf48e5922016-05-12 15:36:20 -0700112 public void onDestroy() {
113 if (mRotationLockObserver != null) {
Sunny Goyalab069992016-06-08 12:00:02 -0700114 getActivity().getContentResolver().unregisterContentObserver(mRotationLockObserver);
Sunny Goyalf48e5922016-05-12 15:36:20 -0700115 mRotationLockObserver = null;
116 }
Tony Wickham2ab84822017-05-12 14:59:09 -0700117 if (mIconBadgingObserver != null) {
118 getActivity().getContentResolver().unregisterContentObserver(mIconBadgingObserver);
119 mIconBadgingObserver = null;
120 }
Sunny Goyalf48e5922016-05-12 15:36:20 -0700121 super.onDestroy();
122 }
123 }
124
125 /**
126 * Content observer which listens for system auto-rotate setting changes, and enables/disables
127 * the launcher rotation setting accordingly.
128 */
129 private static class SystemDisplayRotationLockObserver extends ContentObserver {
130
131 private final Preference mRotationPref;
132 private final ContentResolver mResolver;
133
134 public SystemDisplayRotationLockObserver(
135 Preference rotationPref, ContentResolver resolver) {
136 super(new Handler());
137 mRotationPref = rotationPref;
138 mResolver = resolver;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400139 }
Sunny Goyal745bad92016-05-02 10:54:12 -0700140
Sunny Goyalf48e5922016-05-12 15:36:20 -0700141 @Override
142 public void onChange(boolean selfChange) {
143 boolean enabled = Settings.System.getInt(mResolver,
144 Settings.System.ACCELEROMETER_ROTATION, 1) == 1;
145 mRotationPref.setEnabled(enabled);
146 mRotationPref.setSummary(enabled
147 ? R.string.allow_rotation_desc : R.string.allow_rotation_blocked_desc);
Sunny Goyal745bad92016-05-02 10:54:12 -0700148 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700149 }
Tony Wickham2ab84822017-05-12 14:59:09 -0700150
151 /**
152 * Content observer which listens for system badging setting changes,
153 * and updates the launcher badging setting subtext accordingly.
154 */
155 private static class IconBadgingObserver extends ContentObserver {
156
157 private final Preference mBadgingPref;
158 private final ContentResolver mResolver;
159
160 public IconBadgingObserver(Preference badgingPref, ContentResolver resolver) {
161 super(new Handler());
162 mBadgingPref = badgingPref;
163 mResolver = resolver;
164 }
165
166 @Override
167 public void onChange(boolean selfChange) {
168 boolean enabled = Settings.Secure.getInt(mResolver, NOTIFICATION_BADGING, 1) == 1;
169 mBadgingPref.setSummary(enabled
170 ? R.string.icon_badging_desc_on
171 : R.string.icon_badging_desc_off);
172 }
173 }
174
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700175}