ApplicationSettings.java revision abc48f80d8747b4fc051b7dd364355ee667a9bac
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 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) { 57 if (preference == mToggleAppInstallation) { 58 if (mToggleAppInstallation.isChecked()) { 59 mToggleAppInstallation.setChecked(false); 60 warnAppInstallation(); 61 } else { 62 setNonMarketAppsAllowed(false); 63 } 64 } 65 66 return super.onPreferenceTreeClick(preferenceScreen, preference); 67 } 68 69 public void onClick(DialogInterface dialog, int which) { 70 if (dialog == mWarnInstallApps && which == DialogInterface.BUTTON1) { 71 setNonMarketAppsAllowed(true); 72 mToggleAppInstallation.setChecked(true); 73 } 74 } 75 76 private void setNonMarketAppsAllowed(boolean enabled) { 77 // Change the system setting 78 Settings.Secure.putInt(getContentResolver(), Settings.Secure.INSTALL_NON_MARKET_APPS, 79 enabled ? 1 : 0); 80 } 81 82 private boolean isNonMarketAppsAllowed() { 83 return Settings.Secure.getInt(getContentResolver(), 84 Settings.Secure.INSTALL_NON_MARKET_APPS, 0) > 0; 85 } 86 87 private void warnAppInstallation() { 88 mWarnInstallApps = new AlertDialog.Builder(this) 89 .setTitle(getString(R.string.error_title)) 90 .setIcon(com.android.internal.R.drawable.ic_dialog_alert) 91 .setMessage(getResources().getString(R.string.install_all_warning)) 92 .setPositiveButton(android.R.string.yes, this) 93 .setNegativeButton(android.R.string.no, null) 94 .show(); 95 } 96 97 98} 99