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