blob: 552e24ae4ba353c02fd044f5a2cbfe26336baf70 [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;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -040024import android.preference.Preference;
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070025import android.preference.PreferenceFragment;
Sunny Goyalf48e5922016-05-12 15:36:20 -070026import android.provider.Settings;
27import android.provider.Settings.System;
Sunny Goyal4179e9b2017-03-08 14:25:09 -080028import android.support.v4.os.BuildCompat;
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070029
30/**
Rahul Chaturvedi799aa042015-06-01 21:26:41 -040031 * Settings activity for Launcher. Currently implements the following setting: Allow rotation
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070032 */
33public class SettingsActivity extends Activity {
34 @Override
35 protected void onCreate(Bundle savedInstanceState) {
36 super.onCreate(savedInstanceState);
37
38 // Display the fragment as the main content.
39 getFragmentManager().beginTransaction()
40 .replace(android.R.id.content, new LauncherSettingsFragment())
41 .commit();
42 }
43
44 /**
45 * This fragment shows the launcher preferences.
46 */
Sunny Goyalf48e5922016-05-12 15:36:20 -070047 public static class LauncherSettingsFragment extends PreferenceFragment {
48
49 private SystemDisplayRotationLockObserver mRotationLockObserver;
50
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070051 @Override
52 public void onCreate(Bundle savedInstanceState) {
53 super.onCreate(savedInstanceState);
Sunny Goyalf48e5922016-05-12 15:36:20 -070054 getPreferenceManager().setSharedPreferencesName(LauncherFiles.SHARED_PREFERENCES_KEY);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070055 addPreferencesFromResource(R.xml.launcher_preferences);
Sunny Goyal7779d622015-06-11 16:18:39 -070056
Sunny Goyalf48e5922016-05-12 15:36:20 -070057 // Setup allow rotation preference
58 Preference rotationPref = findPreference(Utilities.ALLOW_ROTATION_PREFERENCE_KEY);
59 if (getResources().getBoolean(R.bool.allow_rotation)) {
60 // Launcher supports rotation by default. No need to show this setting.
61 getPreferenceScreen().removePreference(rotationPref);
62 } else {
Sunny Goyalab069992016-06-08 12:00:02 -070063 ContentResolver resolver = getActivity().getContentResolver();
Sunny Goyalf48e5922016-05-12 15:36:20 -070064 mRotationLockObserver = new SystemDisplayRotationLockObserver(rotationPref, resolver);
65
66 // Register a content observer to listen for system setting changes while
67 // this UI is active.
68 resolver.registerContentObserver(
69 Settings.System.getUriFor(System.ACCELEROMETER_ROTATION),
70 false, mRotationLockObserver);
71
72 // Initialize the UI once
73 mRotationLockObserver.onChange(true);
Sunny Goyalab069992016-06-08 12:00:02 -070074 rotationPref.setDefaultValue(Utilities.getAllowRotationDefaultValue(getActivity()));
Sunny Goyal745bad92016-05-02 10:54:12 -070075 }
Sunny Goyal4179e9b2017-03-08 14:25:09 -080076
77 if (!BuildCompat.isAtLeastO()) {
78 getPreferenceScreen().removePreference(
79 findPreference(SessionCommitReceiver.ADD_ICON_PREFERENCE_KEY));
80 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070081 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -040082
83 @Override
Sunny Goyalf48e5922016-05-12 15:36:20 -070084 public void onDestroy() {
85 if (mRotationLockObserver != null) {
Sunny Goyalab069992016-06-08 12:00:02 -070086 getActivity().getContentResolver().unregisterContentObserver(mRotationLockObserver);
Sunny Goyalf48e5922016-05-12 15:36:20 -070087 mRotationLockObserver = null;
88 }
89 super.onDestroy();
90 }
91 }
92
93 /**
94 * Content observer which listens for system auto-rotate setting changes, and enables/disables
95 * the launcher rotation setting accordingly.
96 */
97 private static class SystemDisplayRotationLockObserver extends ContentObserver {
98
99 private final Preference mRotationPref;
100 private final ContentResolver mResolver;
101
102 public SystemDisplayRotationLockObserver(
103 Preference rotationPref, ContentResolver resolver) {
104 super(new Handler());
105 mRotationPref = rotationPref;
106 mResolver = resolver;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400107 }
Sunny Goyal745bad92016-05-02 10:54:12 -0700108
Sunny Goyalf48e5922016-05-12 15:36:20 -0700109 @Override
110 public void onChange(boolean selfChange) {
111 boolean enabled = Settings.System.getInt(mResolver,
112 Settings.System.ACCELEROMETER_ROTATION, 1) == 1;
113 mRotationPref.setEnabled(enabled);
114 mRotationPref.setSummary(enabled
115 ? R.string.allow_rotation_desc : R.string.allow_rotation_blocked_desc);
Sunny Goyal745bad92016-05-02 10:54:12 -0700116 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700117 }
118}