The Android Open Source Project | de2d9f5 | 2008-10-21 07:00:00 -0700 | [diff] [blame^] | 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 | |
| 17 | package com.android.settings; |
| 18 | |
| 19 | import android.app.AlertDialog; |
| 20 | import android.content.DialogInterface; |
| 21 | import android.os.Bundle; |
| 22 | import android.preference.CheckBoxPreference; |
| 23 | import android.preference.Preference; |
| 24 | import android.preference.PreferenceActivity; |
| 25 | import android.preference.PreferenceScreen; |
| 26 | import android.provider.Settings; |
| 27 | |
| 28 | public class ApplicationSettings extends PreferenceActivity implements |
| 29 | DialogInterface.OnClickListener { |
| 30 | |
| 31 | private static final String KEY_TOGGLE_INSTALL_APPLICATIONS = "toggle_install_applications"; |
| 32 | |
| 33 | private CheckBoxPreference mToggleAppInstallation; |
| 34 | |
| 35 | private DialogInterface mWarnInstallApps; |
| 36 | |
| 37 | @Override |
| 38 | protected void onCreate(Bundle icicle) { |
| 39 | super.onCreate(icicle); |
| 40 | |
| 41 | addPreferencesFromResource(R.xml.application_settings); |
| 42 | |
| 43 | mToggleAppInstallation = (CheckBoxPreference) findPreference(KEY_TOGGLE_INSTALL_APPLICATIONS); |
| 44 | mToggleAppInstallation.setChecked(isNonMarketAppsAllowed()); |
| 45 | |
| 46 | } |
| 47 | |
| 48 | @Override |
| 49 | public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) { |
| 50 | if (preference == mToggleAppInstallation) { |
| 51 | if (mToggleAppInstallation.isChecked()) { |
| 52 | mToggleAppInstallation.setChecked(false); |
| 53 | warnAppInstallation(); |
| 54 | } else { |
| 55 | setNonMarketAppsAllowed(false); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | return super.onPreferenceTreeClick(preferenceScreen, preference); |
| 60 | } |
| 61 | |
| 62 | public void onClick(DialogInterface dialog, int which) { |
| 63 | if (dialog == mWarnInstallApps && which == DialogInterface.BUTTON1) { |
| 64 | setNonMarketAppsAllowed(true); |
| 65 | mToggleAppInstallation.setChecked(true); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | private void setNonMarketAppsAllowed(boolean enabled) { |
| 70 | // Change the system setting |
| 71 | Settings.System.putInt(getContentResolver(), Settings.System.INSTALL_NON_MARKET_APPS, |
| 72 | enabled ? 1 : 0); |
| 73 | } |
| 74 | |
| 75 | private boolean isNonMarketAppsAllowed() { |
| 76 | return Settings.System.getInt(getContentResolver(), |
| 77 | Settings.System.INSTALL_NON_MARKET_APPS, 0) > 0; |
| 78 | } |
| 79 | |
| 80 | private void warnAppInstallation() { |
| 81 | mWarnInstallApps = new AlertDialog.Builder(this) |
| 82 | .setTitle(getString(R.string.error_title)) |
| 83 | .setIcon(com.android.internal.R.drawable.ic_dialog_alert) |
| 84 | .setMessage(getResources().getString(R.string.install_all_warning)) |
| 85 | .setPositiveButton(android.R.string.yes, this) |
| 86 | .setNegativeButton(android.R.string.no, null) |
| 87 | .show(); |
| 88 | } |
| 89 | |
| 90 | |
| 91 | } |