OSDN Git Service

Display a warning dialog when the user enables USB debugging.
[android-x86/packages-apps-Settings.git] / src / com / android / settings / DevelopmentSettings.java
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.BatteryManager;
22 import android.os.Bundle;
23 import android.os.SystemProperties;
24 import android.preference.Preference;
25 import android.preference.PreferenceActivity;
26 import android.preference.PreferenceScreen;
27 import android.preference.CheckBoxPreference;
28 import android.provider.Settings;
29 import android.text.TextUtils;
30
31 /*
32  * Displays preferences for application developers.
33  */
34 public class DevelopmentSettings extends PreferenceActivity
35         implements DialogInterface.OnClickListener, DialogInterface.OnDismissListener {
36
37     private static final String ENABLE_ADB = "enable_adb";
38     private static final String KEEP_SCREEN_ON = "keep_screen_on";
39     private static final String ALLOW_MOCK_LOCATION = "allow_mock_location";
40
41     private CheckBoxPreference mEnableAdb;
42     private CheckBoxPreference mKeepScreenOn;
43     private CheckBoxPreference mAllowMockLocation;
44
45     // To track whether Yes was clicked in the adb warning dialog
46     private boolean mOkClicked;
47
48     @Override
49     protected void onCreate(Bundle icicle) {
50         super.onCreate(icicle);
51
52         addPreferencesFromResource(R.xml.development_prefs);
53
54         mEnableAdb = (CheckBoxPreference) findPreference(ENABLE_ADB);
55         mKeepScreenOn = (CheckBoxPreference) findPreference(KEEP_SCREEN_ON);
56         mAllowMockLocation = (CheckBoxPreference) findPreference(ALLOW_MOCK_LOCATION);
57     }
58
59     @Override
60     protected void onResume() {
61         super.onResume();
62         
63         mEnableAdb.setChecked(Settings.Secure.getInt(getContentResolver(),
64                 Settings.Secure.ADB_ENABLED, 0) != 0);
65         mKeepScreenOn.setChecked(Settings.System.getInt(getContentResolver(),
66                 Settings.System.STAY_ON_WHILE_PLUGGED_IN, 0) != 0);
67         mAllowMockLocation.setChecked(Settings.Secure.getInt(getContentResolver(),
68                 Settings.Secure.ALLOW_MOCK_LOCATION, 0) != 0);
69     }
70
71     @Override
72     public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
73
74         // Those monkeys kept committing suicide, so we add this property
75         // to disable this functionality
76         if (!TextUtils.isEmpty(SystemProperties.get("ro.monkey"))) {
77             return false;
78         }
79
80         if (preference == mEnableAdb) {
81             if (mEnableAdb.isChecked()) {
82                 mOkClicked = false;
83                 new AlertDialog.Builder(this).setMessage(
84                         getResources().getString(R.string.adb_warning_message))
85                         .setTitle(R.string.adb_warning_title)
86                         .setIcon(android.R.drawable.ic_dialog_alert)
87                         .setPositiveButton(android.R.string.yes, this)
88                         .setNegativeButton(android.R.string.no, this)
89                         .show()
90                         .setOnDismissListener(this);
91             } else {
92                 Settings.Secure.putInt(getContentResolver(), Settings.Secure.ADB_ENABLED, 0);
93             }
94         } else if (preference == mKeepScreenOn) {
95             Settings.System.putInt(getContentResolver(), Settings.System.STAY_ON_WHILE_PLUGGED_IN, 
96                     mKeepScreenOn.isChecked() ? 
97                     (BatteryManager.BATTERY_PLUGGED_AC | BatteryManager.BATTERY_PLUGGED_USB) : 0);
98         } else if (preference == mAllowMockLocation) {
99             Settings.Secure.putInt(getContentResolver(), Settings.Secure.ALLOW_MOCK_LOCATION,
100                     mAllowMockLocation.isChecked() ? 1 : 0);
101         }
102         
103         return false;
104     }
105
106     public void onClick(DialogInterface dialog, int which) {
107         if (which == DialogInterface.BUTTON_POSITIVE) {
108             mOkClicked = true;
109             Settings.Secure.putInt(getContentResolver(), Settings.Secure.ADB_ENABLED, 1);
110         } else {
111             // Reset the toggle
112             mEnableAdb.setChecked(false);
113         }
114     }
115
116     public void onDismiss(DialogInterface dialog) {
117         // Assuming that onClick gets called first
118         if (!mOkClicked) {
119             mEnableAdb.setChecked(false);
120         }
121     }
122 }