OSDN Git Service

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