blob: 4135d5be868eca0a4ef91d039262679dc7f46454 [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 Goyal745bad92016-05-02 10:54:12 -070020import android.os.AsyncTask;
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070021import android.os.Bundle;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -040022import android.preference.Preference;
Sunny Goyal7779d622015-06-11 16:18:39 -070023import android.preference.Preference.OnPreferenceChangeListener;
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070024import android.preference.PreferenceFragment;
Sunny Goyal745bad92016-05-02 10:54:12 -070025import android.preference.PreferenceScreen;
26import android.preference.TwoStatePreference;
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070027
28/**
Rahul Chaturvedi799aa042015-06-01 21:26:41 -040029 * Settings activity for Launcher. Currently implements the following setting: Allow rotation
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070030 */
31public class SettingsActivity extends Activity {
32 @Override
33 protected void onCreate(Bundle savedInstanceState) {
34 super.onCreate(savedInstanceState);
35
36 // Display the fragment as the main content.
37 getFragmentManager().beginTransaction()
38 .replace(android.R.id.content, new LauncherSettingsFragment())
39 .commit();
40 }
41
42 /**
43 * This fragment shows the launcher preferences.
44 */
Sunny Goyal7779d622015-06-11 16:18:39 -070045 public static class LauncherSettingsFragment extends PreferenceFragment
46 implements OnPreferenceChangeListener {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070047 @Override
48 public void onCreate(Bundle savedInstanceState) {
49 super.onCreate(savedInstanceState);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070050 addPreferencesFromResource(R.xml.launcher_preferences);
Sunny Goyal7779d622015-06-11 16:18:39 -070051
Sunny Goyal745bad92016-05-02 10:54:12 -070052 PreferenceScreen screen = getPreferenceScreen();
53 for (int i = screen.getPreferenceCount() - 1; i >= 0; i--) {
54 Preference pref = screen.getPreference(i);
55 if (pref instanceof TwoStatePreference) {
56 setBooleanPrefUsingContentProvider((TwoStatePreference) pref);
57 }
58 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070059 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -040060
61 @Override
Sunny Goyal7779d622015-06-11 16:18:39 -070062 public boolean onPreferenceChange(Preference preference, Object newValue) {
63 Bundle extras = new Bundle();
64 extras.putBoolean(LauncherSettings.Settings.EXTRA_VALUE, (Boolean) newValue);
65 getActivity().getContentResolver().call(
66 LauncherSettings.Settings.CONTENT_URI,
67 LauncherSettings.Settings.METHOD_SET_BOOLEAN,
68 preference.getKey(), extras);
Rahul Chaturvedi799aa042015-06-01 21:26:41 -040069 return true;
70 }
Sunny Goyal745bad92016-05-02 10:54:12 -070071
72 private void setBooleanPrefUsingContentProvider(final TwoStatePreference pref) {
73 pref.setPersistent(false);
74 pref.setEnabled(false);
75
76 new AsyncTask<Void, Void, Boolean>() {
77 @Override
78 protected Boolean doInBackground(Void... params) {
79 Bundle extras = new Bundle();
Sunny Goyal5ae82082016-05-02 12:50:34 -070080 extras.putBoolean(LauncherSettings.Settings.EXTRA_DEFAULT_VALUE, true);
81 Bundle value = pref.getContext().getContentResolver().call(
Sunny Goyal745bad92016-05-02 10:54:12 -070082 LauncherSettings.Settings.CONTENT_URI,
83 LauncherSettings.Settings.METHOD_GET_BOOLEAN,
84 pref.getKey(), extras);
85 return value.getBoolean(LauncherSettings.Settings.EXTRA_VALUE);
86 }
87
88 @Override
89 protected void onPostExecute(Boolean aBoolean) {
90 pref.setChecked(aBoolean);
91 pref.setEnabled(true);
92 pref.setOnPreferenceChangeListener(LauncherSettingsFragment.this);
93 }
94 }.execute();
95 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -070096 }
97}