blob: 5ef6dd53162194e14c970db531506a13d391c6be [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;
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070028
29/**
Rahul Chaturvedi799aa042015-06-01 21:26:41 -040030 * Settings activity for Launcher. Currently implements the following setting: Allow rotation
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070031 */
32public class SettingsActivity extends Activity {
33 @Override
34 protected void onCreate(Bundle savedInstanceState) {
35 super.onCreate(savedInstanceState);
36
37 // Display the fragment as the main content.
38 getFragmentManager().beginTransaction()
39 .replace(android.R.id.content, new LauncherSettingsFragment())
40 .commit();
41 }
42
43 /**
44 * This fragment shows the launcher preferences.
45 */
Sunny Goyalf48e5922016-05-12 15:36:20 -070046 public static class LauncherSettingsFragment extends PreferenceFragment {
47
48 private SystemDisplayRotationLockObserver mRotationLockObserver;
49
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070050 @Override
51 public void onCreate(Bundle savedInstanceState) {
52 super.onCreate(savedInstanceState);
Sunny Goyalf48e5922016-05-12 15:36:20 -070053 getPreferenceManager().setSharedPreferencesName(LauncherFiles.SHARED_PREFERENCES_KEY);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070054 addPreferencesFromResource(R.xml.launcher_preferences);
Sunny Goyal7779d622015-06-11 16:18:39 -070055
Sunny Goyalf48e5922016-05-12 15:36:20 -070056 // Setup allow rotation preference
57 Preference rotationPref = findPreference(Utilities.ALLOW_ROTATION_PREFERENCE_KEY);
58 if (getResources().getBoolean(R.bool.allow_rotation)) {
59 // Launcher supports rotation by default. No need to show this setting.
60 getPreferenceScreen().removePreference(rotationPref);
61 } else {
62 ContentResolver resolver = getContext().getContentResolver();
63 mRotationLockObserver = new SystemDisplayRotationLockObserver(rotationPref, resolver);
64
65 // Register a content observer to listen for system setting changes while
66 // this UI is active.
67 resolver.registerContentObserver(
68 Settings.System.getUriFor(System.ACCELEROMETER_ROTATION),
69 false, mRotationLockObserver);
70
71 // Initialize the UI once
72 mRotationLockObserver.onChange(true);
73 rotationPref.setDefaultValue(Utilities.getAllowRotationDefaultValue(getContext()));
Sunny Goyal745bad92016-05-02 10:54:12 -070074 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070075 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -040076
77 @Override
Sunny Goyalf48e5922016-05-12 15:36:20 -070078 public void onDestroy() {
79 if (mRotationLockObserver != null) {
80 getContext().getContentResolver().unregisterContentObserver(mRotationLockObserver);
81 mRotationLockObserver = null;
82 }
83 super.onDestroy();
84 }
85 }
86
87 /**
88 * Content observer which listens for system auto-rotate setting changes, and enables/disables
89 * the launcher rotation setting accordingly.
90 */
91 private static class SystemDisplayRotationLockObserver extends ContentObserver {
92
93 private final Preference mRotationPref;
94 private final ContentResolver mResolver;
95
96 public SystemDisplayRotationLockObserver(
97 Preference rotationPref, ContentResolver resolver) {
98 super(new Handler());
99 mRotationPref = rotationPref;
100 mResolver = resolver;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400101 }
Sunny Goyal745bad92016-05-02 10:54:12 -0700102
Sunny Goyalf48e5922016-05-12 15:36:20 -0700103 @Override
104 public void onChange(boolean selfChange) {
105 boolean enabled = Settings.System.getInt(mResolver,
106 Settings.System.ACCELEROMETER_ROTATION, 1) == 1;
107 mRotationPref.setEnabled(enabled);
108 mRotationPref.setSummary(enabled
109 ? R.string.allow_rotation_desc : R.string.allow_rotation_blocked_desc);
Sunny Goyal745bad92016-05-02 10:54:12 -0700110 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700111 }
112}