ApplicationSettings.java revision 5246e7f76d5fe62f7ffd99e889d90116b37b7f35
1/*
2 * Copyright (C) 2008 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.settings;
18
19import android.content.Intent;
20import android.os.Bundle;
21import android.preference.CheckBoxPreference;
22import android.preference.ListPreference;
23import android.preference.Preference;
24import android.preference.Preference.OnPreferenceChangeListener;
25import android.preference.PreferenceScreen;
26import android.provider.Settings;
27
28public class ApplicationSettings extends SettingsPreferenceFragment {
29
30    private static final String KEY_TOGGLE_ADVANCED_SETTINGS = "toggle_advanced_settings";
31    private static final String KEY_APP_INSTALL_LOCATION = "app_install_location";
32
33    // App installation location. Default is ask the user.
34    private static final int APP_INSTALL_AUTO = 0;
35    private static final int APP_INSTALL_DEVICE = 1;
36    private static final int APP_INSTALL_SDCARD = 2;
37
38    private static final String APP_INSTALL_DEVICE_ID = "device";
39    private static final String APP_INSTALL_SDCARD_ID = "sdcard";
40    private static final String APP_INSTALL_AUTO_ID = "auto";
41
42    private CheckBoxPreference mToggleAdvancedSettings;
43    private ListPreference mInstallLocation;
44
45    @Override
46    public void onCreate(Bundle icicle) {
47        super.onCreate(icicle);
48
49        addPreferencesFromResource(R.xml.application_settings);
50
51        mToggleAdvancedSettings = (CheckBoxPreference)findPreference(
52                KEY_TOGGLE_ADVANCED_SETTINGS);
53        mToggleAdvancedSettings.setChecked(isAdvancedSettingsEnabled());
54        getPreferenceScreen().removePreference(mToggleAdvancedSettings);
55
56        // not ready for prime time yet
57        if (false) {
58            getPreferenceScreen().removePreference(mInstallLocation);
59        }
60
61        mInstallLocation = (ListPreference) findPreference(KEY_APP_INSTALL_LOCATION);
62        // Is app default install location set?
63        boolean userSetInstLocation = (Settings.Global.getInt(getContentResolver(),
64                Settings.Global.SET_INSTALL_LOCATION, 0) != 0);
65        if (!userSetInstLocation) {
66            getPreferenceScreen().removePreference(mInstallLocation);
67        } else {
68            mInstallLocation.setValue(getAppInstallLocation());
69            mInstallLocation.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
70                public boolean onPreferenceChange(Preference preference, Object newValue) {
71                    String value = (String) newValue;
72                    handleUpdateAppInstallLocation(value);
73                    return false;
74                }
75            });
76        }
77    }
78
79    protected void handleUpdateAppInstallLocation(final String value) {
80        if(APP_INSTALL_DEVICE_ID.equals(value)) {
81            Settings.Global.putInt(getContentResolver(),
82                    Settings.Global.DEFAULT_INSTALL_LOCATION, APP_INSTALL_DEVICE);
83        } else if (APP_INSTALL_SDCARD_ID.equals(value)) {
84            Settings.Global.putInt(getContentResolver(),
85                    Settings.Global.DEFAULT_INSTALL_LOCATION, APP_INSTALL_SDCARD);
86        } else if (APP_INSTALL_AUTO_ID.equals(value)) {
87            Settings.Global.putInt(getContentResolver(),
88                    Settings.Global.DEFAULT_INSTALL_LOCATION, APP_INSTALL_AUTO);
89        } else {
90            // Should not happen, default to prompt...
91            Settings.Global.putInt(getContentResolver(),
92                    Settings.Global.DEFAULT_INSTALL_LOCATION, APP_INSTALL_AUTO);
93        }
94        mInstallLocation.setValue(value);
95    }
96
97    @Override
98    public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
99        if (preference == mToggleAdvancedSettings) {
100            boolean value = mToggleAdvancedSettings.isChecked();
101            setAdvancedSettingsEnabled(value);
102        }
103
104        return super.onPreferenceTreeClick(preferenceScreen, preference);
105    }
106
107    private boolean isAdvancedSettingsEnabled() {
108        return Settings.System.getInt(getContentResolver(),
109                                      Settings.System.ADVANCED_SETTINGS,
110                                      Settings.System.ADVANCED_SETTINGS_DEFAULT) > 0;
111    }
112
113    private void setAdvancedSettingsEnabled(boolean enabled) {
114        int value = enabled ? 1 : 0;
115        // Change the system setting
116        Settings.Secure.putInt(getContentResolver(), Settings.System.ADVANCED_SETTINGS, value);
117        // TODO: the settings thing should broadcast this for thread safety purposes.
118        Intent intent = new Intent(Intent.ACTION_ADVANCED_SETTINGS_CHANGED);
119        intent.putExtra("state", value);
120        getActivity().sendBroadcast(intent);
121    }
122
123    private String getAppInstallLocation() {
124        int selectedLocation = Settings.Global.getInt(getContentResolver(),
125                Settings.Global.DEFAULT_INSTALL_LOCATION, APP_INSTALL_AUTO);
126        if (selectedLocation == APP_INSTALL_DEVICE) {
127            return APP_INSTALL_DEVICE_ID;
128        } else if (selectedLocation == APP_INSTALL_SDCARD) {
129            return APP_INSTALL_SDCARD_ID;
130        } else  if (selectedLocation == APP_INSTALL_AUTO) {
131            return APP_INSTALL_AUTO_ID;
132        } else {
133            // Default value, should not happen.
134            return APP_INSTALL_AUTO_ID;
135        }
136    }
137}
138