ApplicationSettings.java revision 519e3216099ebfca22451ccd437e6af244011c35
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.Preference; 25import android.preference.PreferenceActivity; 26import android.preference.PreferenceScreen; 27import android.provider.Settings; 28 29public class ApplicationSettings extends PreferenceActivity implements 30 DialogInterface.OnClickListener { 31 32 private static final String KEY_TOGGLE_INSTALL_APPLICATIONS = "toggle_install_applications"; 33 private static final String KEY_QUICK_LAUNCH = "quick_launch"; 34 35 private CheckBoxPreference mToggleAppInstallation; 36 37 private DialogInterface mWarnInstallApps; 38 39 @Override 40 protected void onCreate(Bundle icicle) { 41 super.onCreate(icicle); 42 43 addPreferencesFromResource(R.xml.application_settings); 44 45 mToggleAppInstallation = (CheckBoxPreference) findPreference(KEY_TOGGLE_INSTALL_APPLICATIONS); 46 mToggleAppInstallation.setChecked(isNonMarketAppsAllowed()); 47 48 if (getResources().getConfiguration().keyboard == Configuration.KEYBOARD_NOKEYS) { 49 // No hard keyboard, remove the setting for quick launch 50 Preference quickLaunchSetting = findPreference(KEY_QUICK_LAUNCH); 51 getPreferenceScreen().removePreference(quickLaunchSetting); 52 } 53 } 54 55 @Override 56 protected void onDestroy() { 57 super.onDestroy(); 58 if (mWarnInstallApps != null) { 59 mWarnInstallApps.dismiss(); 60 } 61 } 62 63 @Override 64 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) { 65 if (preference == mToggleAppInstallation) { 66 if (mToggleAppInstallation.isChecked()) { 67 mToggleAppInstallation.setChecked(false); 68 warnAppInstallation(); 69 } else { 70 setNonMarketAppsAllowed(false); 71 } 72 } 73 74 return super.onPreferenceTreeClick(preferenceScreen, preference); 75 } 76 77 public void onClick(DialogInterface dialog, int which) { 78 if (dialog == mWarnInstallApps && which == DialogInterface.BUTTON1) { 79 setNonMarketAppsAllowed(true); 80 mToggleAppInstallation.setChecked(true); 81 } 82 } 83 84 private void setNonMarketAppsAllowed(boolean enabled) { 85 // Change the system setting 86 Settings.Secure.putInt(getContentResolver(), Settings.Secure.INSTALL_NON_MARKET_APPS, 87 enabled ? 1 : 0); 88 } 89 90 private boolean isNonMarketAppsAllowed() { 91 return Settings.Secure.getInt(getContentResolver(), 92 Settings.Secure.INSTALL_NON_MARKET_APPS, 0) > 0; 93 } 94 95 private void warnAppInstallation() { 96 mWarnInstallApps = new AlertDialog.Builder(this) 97 .setTitle(getString(R.string.error_title)) 98 .setIcon(com.android.internal.R.drawable.ic_dialog_alert) 99 .setMessage(getResources().getString(R.string.install_all_warning)) 100 .setPositiveButton(android.R.string.yes, this) 101 .setNegativeButton(android.R.string.no, null) 102 .show(); 103 } 104 105 106} 107