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.app.Dialog;
21import android.content.DialogInterface;
22import android.os.BatteryManager;
23import android.os.Bundle;
24import android.os.SystemProperties;
25import android.preference.Preference;
26import android.preference.PreferenceActivity;
27import android.preference.PreferenceScreen;
28import android.preference.CheckBoxPreference;
29import android.provider.Settings;
30import android.text.TextUtils;
31
32/*
33 * Displays preferences for application developers.
34 */
35public 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        if (Utils.isMonkeyRunning()) {
78            return false;
79        }
80
81        if (preference == mEnableAdb) {
82            if (mEnableAdb.isChecked()) {
83                mOkClicked = false;
84                if (mOkDialog != null) dismissDialog();
85                mOkDialog = new AlertDialog.Builder(this).setMessage(
86                        getResources().getString(R.string.adb_warning_message))
87                        .setTitle(R.string.adb_warning_title)
88                        .setIcon(android.R.drawable.ic_dialog_alert)
89                        .setPositiveButton(android.R.string.yes, this)
90                        .setNegativeButton(android.R.string.no, this)
91                        .show();
92                mOkDialog.setOnDismissListener(this);
93            } else {
94                Settings.Secure.putInt(getContentResolver(), Settings.Secure.ADB_ENABLED, 0);
95            }
96        } else if (preference == mKeepScreenOn) {
97            Settings.System.putInt(getContentResolver(), Settings.System.STAY_ON_WHILE_PLUGGED_IN,
98                    mKeepScreenOn.isChecked() ?
99                    (BatteryManager.BATTERY_PLUGGED_AC | BatteryManager.BATTERY_PLUGGED_USB) : 0);
100        } else if (preference == mAllowMockLocation) {
101            Settings.Secure.putInt(getContentResolver(), Settings.Secure.ALLOW_MOCK_LOCATION,
102                    mAllowMockLocation.isChecked() ? 1 : 0);
103        }
104
105        return false;
106    }
107
108    private void dismissDialog() {
109        if (mOkDialog == null) return;
110        mOkDialog.dismiss();
111        mOkDialog = null;
112    }
113
114    public void onClick(DialogInterface dialog, int which) {
115        if (which == DialogInterface.BUTTON_POSITIVE) {
116            mOkClicked = true;
117            Settings.Secure.putInt(getContentResolver(), Settings.Secure.ADB_ENABLED, 1);
118        } else {
119            // Reset the toggle
120            mEnableAdb.setChecked(false);
121        }
122    }
123
124    public void onDismiss(DialogInterface dialog) {
125        // Assuming that onClick gets called first
126        if (!mOkClicked) {
127            mEnableAdb.setChecked(false);
128        }
129    }
130
131    @Override
132    public void onDestroy() {
133        dismissDialog();
134        super.onDestroy();
135    }
136}
137