blob: 7ae6b261d6d3f14efa6e1412b8d4e609d2786a40 [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 {
37 @Override
38 protected void onCreate(Bundle savedInstanceState) {
39 super.onCreate(savedInstanceState);
40
41 // Display the fragment as the main content.
42 getFragmentManager().beginTransaction()
43 .replace(android.R.id.content, new LauncherSettingsFragment())
44 .commit();
45 }
46
47 /**
48 * This fragment shows the launcher preferences.
49 */
Sunny Goyalf48e5922016-05-12 15:36:20 -070050 public static class LauncherSettingsFragment extends PreferenceFragment {
51
52 private SystemDisplayRotationLockObserver mRotationLockObserver;
53
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070054 @Override
55 public void onCreate(Bundle savedInstanceState) {
56 super.onCreate(savedInstanceState);
Sunny Goyalf48e5922016-05-12 15:36:20 -070057 getPreferenceManager().setSharedPreferencesName(LauncherFiles.SHARED_PREFERENCES_KEY);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070058 addPreferencesFromResource(R.xml.launcher_preferences);
Sunny Goyal7779d622015-06-11 16:18:39 -070059
Sunny Goyalf48e5922016-05-12 15:36:20 -070060 // Setup allow rotation preference
61 Preference rotationPref = findPreference(Utilities.ALLOW_ROTATION_PREFERENCE_KEY);
62 if (getResources().getBoolean(R.bool.allow_rotation)) {
63 // Launcher supports rotation by default. No need to show this setting.
64 getPreferenceScreen().removePreference(rotationPref);
65 } else {
Sunny Goyalab069992016-06-08 12:00:02 -070066 ContentResolver resolver = getActivity().getContentResolver();
Sunny Goyalf48e5922016-05-12 15:36:20 -070067 mRotationLockObserver = new SystemDisplayRotationLockObserver(rotationPref, resolver);
68
69 // Register a content observer to listen for system setting changes while
70 // this UI is active.
71 resolver.registerContentObserver(
72 Settings.System.getUriFor(System.ACCELEROMETER_ROTATION),
73 false, mRotationLockObserver);
74
75 // Initialize the UI once
76 mRotationLockObserver.onChange(true);
Sunny Goyalab069992016-06-08 12:00:02 -070077 rotationPref.setDefaultValue(Utilities.getAllowRotationDefaultValue(getActivity()));
Sunny Goyal745bad92016-05-02 10:54:12 -070078 }
Sunny Goyal4179e9b2017-03-08 14:25:09 -080079
80 if (!BuildCompat.isAtLeastO()) {
81 getPreferenceScreen().removePreference(
82 findPreference(SessionCommitReceiver.ADD_ICON_PREFERENCE_KEY));
83 }
Sunny Goyalca187462017-03-31 20:09:34 -070084
85 Preference iconShapeOverride = findPreference(IconShapeOverride.KEY_PREFERENCE);
86 if (iconShapeOverride != null) {
87 if (IconShapeOverride.isSupported(getActivity())) {
88 IconShapeOverride.handlePreferenceUi((ListPreference) iconShapeOverride);
89 } else {
90 getPreferenceScreen().removePreference(iconShapeOverride);
91 }
92 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070093 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -040094
95 @Override
Sunny Goyalf48e5922016-05-12 15:36:20 -070096 public void onDestroy() {
97 if (mRotationLockObserver != null) {
Sunny Goyalab069992016-06-08 12:00:02 -070098 getActivity().getContentResolver().unregisterContentObserver(mRotationLockObserver);
Sunny Goyalf48e5922016-05-12 15:36:20 -070099 mRotationLockObserver = null;
100 }
101 super.onDestroy();
102 }
103 }
104
105 /**
106 * Content observer which listens for system auto-rotate setting changes, and enables/disables
107 * the launcher rotation setting accordingly.
108 */
109 private static class SystemDisplayRotationLockObserver extends ContentObserver {
110
111 private final Preference mRotationPref;
112 private final ContentResolver mResolver;
113
114 public SystemDisplayRotationLockObserver(
115 Preference rotationPref, ContentResolver resolver) {
116 super(new Handler());
117 mRotationPref = rotationPref;
118 mResolver = resolver;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400119 }
Sunny Goyal745bad92016-05-02 10:54:12 -0700120
Sunny Goyalf48e5922016-05-12 15:36:20 -0700121 @Override
122 public void onChange(boolean selfChange) {
123 boolean enabled = Settings.System.getInt(mResolver,
124 Settings.System.ACCELEROMETER_ROTATION, 1) == 1;
125 mRotationPref.setEnabled(enabled);
126 mRotationPref.setSummary(enabled
127 ? R.string.allow_rotation_desc : R.string.allow_rotation_blocked_desc);
Sunny Goyal745bad92016-05-02 10:54:12 -0700128 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700129 }
130}