ApplicationSettings.java revision a110a718bac811c014b80c123a447e45ab1b1dd9
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.app.AlertDialog;
20import android.content.DialogInterface;
21import android.content.res.Configuration;
22import android.os.Bundle;
23import android.preference.CheckBoxPreference;
24import android.preference.ListPreference;
25import android.preference.Preference;
26import android.preference.PreferenceActivity;
27import android.preference.PreferenceScreen;
28import android.preference.Preference.OnPreferenceChangeListener;
29import android.provider.Settings;
30
31public class ApplicationSettings extends PreferenceActivity implements
32        DialogInterface.OnClickListener {
33
34    private static final String KEY_TOGGLE_INSTALL_APPLICATIONS = "toggle_install_applications";
35    private static final String KEY_APP_INSTALL_LOCATION = "app_install_location";
36    private static final String KEY_QUICK_LAUNCH = "quick_launch";
37
38    // App installation location. Default is ask the user.
39    private static final int APP_INSTALL_AUTO = 0;
40    private static final int APP_INSTALL_DEVICE = 1;
41    private static final int APP_INSTALL_SDCARD = 2;
42
43    private static final String APP_INSTALL_DEVICE_ID = "device";
44    private static final String APP_INSTALL_SDCARD_ID = "sdcard";
45    private static final String APP_INSTALL_AUTO_ID = "auto";
46
47    private CheckBoxPreference mToggleAppInstallation;
48
49    private ListPreference mInstallLocation;
50
51    private DialogInterface mWarnInstallApps;
52
53    @Override
54    protected void onCreate(Bundle icicle) {
55        super.onCreate(icicle);
56
57        addPreferencesFromResource(R.xml.application_settings);
58
59        mToggleAppInstallation = (CheckBoxPreference) findPreference(KEY_TOGGLE_INSTALL_APPLICATIONS);
60        mToggleAppInstallation.setChecked(isNonMarketAppsAllowed());
61
62        mInstallLocation = (ListPreference) findPreference(KEY_APP_INSTALL_LOCATION);
63        // Is app default install location set?
64        boolean userSetInstLocation = (Settings.System.getInt(getContentResolver(),
65                Settings.System.SET_INSTALL_LOCATION, 0) != 0);
66        if (!userSetInstLocation) {
67            getPreferenceScreen().removePreference(mInstallLocation);
68        } else {
69            mInstallLocation.setValue(getAppInstallLocation());
70            mInstallLocation.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
71                public boolean onPreferenceChange(Preference preference, Object newValue) {
72                    String value = (String) newValue;
73                    handleUpdateAppInstallLocation(value);
74                    return false;
75                }
76            });
77        }
78
79        if (getResources().getConfiguration().keyboard == Configuration.KEYBOARD_NOKEYS) {
80            // No hard keyboard, remove the setting for quick launch
81            Preference quickLaunchSetting = findPreference(KEY_QUICK_LAUNCH);
82            getPreferenceScreen().removePreference(quickLaunchSetting);
83        }
84    }
85
86    protected void handleUpdateAppInstallLocation(final String value) {
87        if(APP_INSTALL_DEVICE_ID.equals(value)) {
88            Settings.System.putInt(getContentResolver(),
89                    Settings.System.DEFAULT_INSTALL_LOCATION, APP_INSTALL_DEVICE);
90        } else if (APP_INSTALL_SDCARD_ID.equals(value)) {
91            Settings.System.putInt(getContentResolver(),
92                    Settings.System.DEFAULT_INSTALL_LOCATION, APP_INSTALL_SDCARD);
93        } else if (APP_INSTALL_AUTO_ID.equals(value)) {
94            Settings.System.putInt(getContentResolver(),
95                    Settings.System.DEFAULT_INSTALL_LOCATION, APP_INSTALL_AUTO);
96        } else {
97            // Should not happen, default to prompt...
98            Settings.System.putInt(getContentResolver(),
99                    Settings.System.DEFAULT_INSTALL_LOCATION, APP_INSTALL_AUTO);
100        }
101        mInstallLocation.setValue(value);
102    }
103
104    @Override
105    protected void onDestroy() {
106        super.onDestroy();
107        if (mWarnInstallApps != null) {
108            mWarnInstallApps.dismiss();
109        }
110    }
111
112    @Override
113    public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
114        if (preference == mToggleAppInstallation) {
115            if (mToggleAppInstallation.isChecked()) {
116                mToggleAppInstallation.setChecked(false);
117                warnAppInstallation();
118            } else {
119                setNonMarketAppsAllowed(false);
120            }
121        }
122
123        return super.onPreferenceTreeClick(preferenceScreen, preference);
124    }
125
126    public void onClick(DialogInterface dialog, int which) {
127        if (dialog == mWarnInstallApps && which == DialogInterface.BUTTON1) {
128            setNonMarketAppsAllowed(true);
129            mToggleAppInstallation.setChecked(true);
130        }
131    }
132
133    private void setNonMarketAppsAllowed(boolean enabled) {
134        // Change the system setting
135        Settings.Secure.putInt(getContentResolver(), Settings.Secure.INSTALL_NON_MARKET_APPS,
136                                enabled ? 1 : 0);
137    }
138
139    private boolean isNonMarketAppsAllowed() {
140        return Settings.Secure.getInt(getContentResolver(),
141                                      Settings.Secure.INSTALL_NON_MARKET_APPS, 0) > 0;
142    }
143
144    private String getAppInstallLocation() {
145        int selectedLocation = Settings.System.getInt(getContentResolver(),
146                Settings.System.DEFAULT_INSTALL_LOCATION, APP_INSTALL_AUTO);
147        if (selectedLocation == APP_INSTALL_DEVICE) {
148            return APP_INSTALL_DEVICE_ID;
149        } else if (selectedLocation == APP_INSTALL_SDCARD) {
150            return APP_INSTALL_SDCARD_ID;
151        } else  if (selectedLocation == APP_INSTALL_AUTO) {
152            return APP_INSTALL_AUTO_ID;
153        } else {
154            // Default value, should not happen.
155            return APP_INSTALL_AUTO_ID;
156        }
157    }
158
159    private void warnAppInstallation() {
160        mWarnInstallApps = new AlertDialog.Builder(this)
161                .setTitle(getString(R.string.error_title))
162                .setIcon(com.android.internal.R.drawable.ic_dialog_alert)
163                .setMessage(getResources().getString(R.string.install_all_warning))
164                .setPositiveButton(android.R.string.yes, this)
165                .setNegativeButton(android.R.string.no, null)
166                .show();
167    }
168}
169