1 /* 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 17 package com.android.launcher3; 18 19 import android.app.Activity; 20 import android.content.ContentResolver; 21 import android.database.ContentObserver; 22 import android.os.Bundle; 23 import android.os.Handler; 24 import android.preference.Preference; 25 import android.preference.PreferenceFragment; 26 import android.provider.Settings; 27 import android.provider.Settings.System; 28 29 /** 30 * Settings activity for Launcher. Currently implements the following setting: Allow rotation 31 */ 32 public 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 */ 46 public static class LauncherSettingsFragment extends PreferenceFragment { 47 48 private SystemDisplayRotationLockObserver mRotationLockObserver; 49 50 @Override 51 public void onCreate(Bundle savedInstanceState) { 52 super.onCreate(savedInstanceState); 53 getPreferenceManager().setSharedPreferencesName(LauncherFiles.SHARED_PREFERENCES_KEY); 54 addPreferencesFromResource(R.xml.launcher_preferences); 55 56 // 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 = getActivity().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(getActivity())); 74 } 75 } 76 77 @Override 78 public void onDestroy() { 79 if (mRotationLockObserver != null) { 80 getActivity().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; 101 } 102 103 @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); 110 } 111 } 112 } 113