Amith Yamasani | 0e2ab4f | 2010-01-14 16:12:21 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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; |
Daniel Sandler | c0a51ab | 2010-03-12 14:53:49 -0500 | [diff] [blame^] | 21 | import android.bluetooth.BluetoothDevice; |
Amith Yamasani | 0e2ab4f | 2010-01-14 16:12:21 -0800 | [diff] [blame] | 22 | import android.content.BroadcastReceiver; |
| 23 | import android.content.ContentResolver; |
| 24 | import android.content.Context; |
| 25 | import android.content.Intent; |
| 26 | import android.content.IntentFilter; |
| 27 | import android.os.Bundle; |
Daniel Sandler | fc01d06 | 2010-03-02 20:24:49 -0500 | [diff] [blame] | 28 | import android.preference.CheckBoxPreference; |
Amith Yamasani | 0e2ab4f | 2010-01-14 16:12:21 -0800 | [diff] [blame] | 29 | import android.preference.Preference; |
| 30 | import android.preference.PreferenceActivity; |
| 31 | import android.preference.PreferenceScreen; |
Daniel Sandler | fc01d06 | 2010-03-02 20:24:49 -0500 | [diff] [blame] | 32 | import android.provider.Settings; |
Amith Yamasani | 0e2ab4f | 2010-01-14 16:12:21 -0800 | [diff] [blame] | 33 | |
| 34 | import com.android.settings.bluetooth.DockEventReceiver; |
| 35 | |
| 36 | public class DockSettings extends PreferenceActivity { |
| 37 | |
| 38 | private static final int DIALOG_NOT_DOCKED = 1; |
| 39 | private static final String KEY_AUDIO_SETTINGS = "dock_audio"; |
Daniel Sandler | fc01d06 | 2010-03-02 20:24:49 -0500 | [diff] [blame] | 40 | private static final String KEY_DOCK_SOUNDS = "dock_sounds"; |
Amith Yamasani | 0e2ab4f | 2010-01-14 16:12:21 -0800 | [diff] [blame] | 41 | private Preference mAudioSettings; |
Daniel Sandler | fc01d06 | 2010-03-02 20:24:49 -0500 | [diff] [blame] | 42 | private CheckBoxPreference mDockSounds; |
Amith Yamasani | 0e2ab4f | 2010-01-14 16:12:21 -0800 | [diff] [blame] | 43 | private Intent mDockIntent; |
| 44 | |
| 45 | private BroadcastReceiver mReceiver = new BroadcastReceiver() { |
| 46 | @Override |
| 47 | public void onReceive(Context context, Intent intent) { |
| 48 | if (intent.getAction().equals(Intent.ACTION_DOCK_EVENT)) { |
| 49 | handleDockChange(intent); |
| 50 | } |
| 51 | } |
| 52 | }; |
| 53 | |
| 54 | @Override |
| 55 | protected void onCreate(Bundle savedInstanceState) { |
| 56 | super.onCreate(savedInstanceState); |
| 57 | ContentResolver resolver = getContentResolver(); |
| 58 | addPreferencesFromResource(R.xml.dock_settings); |
| 59 | |
| 60 | initDockSettings(); |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | protected void onResume() { |
| 65 | super.onResume(); |
| 66 | |
| 67 | IntentFilter filter = new IntentFilter(Intent.ACTION_DOCK_EVENT); |
| 68 | registerReceiver(mReceiver, filter); |
| 69 | } |
| 70 | |
| 71 | @Override |
| 72 | protected void onPause() { |
| 73 | super.onPause(); |
| 74 | |
| 75 | unregisterReceiver(mReceiver); |
| 76 | } |
| 77 | |
| 78 | private void initDockSettings() { |
Daniel Sandler | fc01d06 | 2010-03-02 20:24:49 -0500 | [diff] [blame] | 79 | ContentResolver resolver = getContentResolver(); |
| 80 | |
Amith Yamasani | 0e2ab4f | 2010-01-14 16:12:21 -0800 | [diff] [blame] | 81 | mAudioSettings = findPreference(KEY_AUDIO_SETTINGS); |
Amith Yamasani | 6ff80dc | 2010-01-15 19:10:55 -0800 | [diff] [blame] | 82 | if (mAudioSettings != null) { |
| 83 | mAudioSettings.setSummary(R.string.dock_audio_summary_none); |
| 84 | } |
Daniel Sandler | fc01d06 | 2010-03-02 20:24:49 -0500 | [diff] [blame] | 85 | |
| 86 | mDockSounds = (CheckBoxPreference) findPreference(KEY_DOCK_SOUNDS); |
| 87 | mDockSounds.setPersistent(false); |
| 88 | mDockSounds.setChecked(Settings.System.getInt(resolver, |
| 89 | Settings.System.DOCK_SOUNDS_ENABLED, 0) != 0); |
Amith Yamasani | 0e2ab4f | 2010-01-14 16:12:21 -0800 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | private void handleDockChange(Intent intent) { |
| 93 | if (mAudioSettings != null) { |
| 94 | int dockState = intent.getIntExtra(Intent.EXTRA_DOCK_STATE, 0); |
Daniel Sandler | c0a51ab | 2010-03-12 14:53:49 -0500 | [diff] [blame^] | 95 | |
| 96 | boolean isBluetooth = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE) != null; |
| 97 | |
| 98 | if (!isBluetooth) { |
| 99 | // No dock audio if not on Bluetooth. |
| 100 | mAudioSettings.setEnabled(false); |
| 101 | mAudioSettings.setSummary(R.string.dock_audio_summary_unknown); |
| 102 | } else { |
| 103 | mAudioSettings.setEnabled(true); |
| 104 | |
| 105 | mDockIntent = intent; |
| 106 | int resId = R.string.dock_audio_summary_unknown; |
| 107 | switch (dockState) { |
| 108 | case Intent.EXTRA_DOCK_STATE_CAR: |
| 109 | resId = R.string.dock_audio_summary_car; |
| 110 | break; |
| 111 | case Intent.EXTRA_DOCK_STATE_DESK: |
| 112 | resId = R.string.dock_audio_summary_desk; |
| 113 | break; |
| 114 | case Intent.EXTRA_DOCK_STATE_UNDOCKED: |
| 115 | resId = R.string.dock_audio_summary_none; |
| 116 | } |
| 117 | mAudioSettings.setSummary(resId); |
Amith Yamasani | 0e2ab4f | 2010-01-14 16:12:21 -0800 | [diff] [blame] | 118 | } |
Daniel Sandler | c0a51ab | 2010-03-12 14:53:49 -0500 | [diff] [blame^] | 119 | |
Amith Yamasani | 0e2ab4f | 2010-01-14 16:12:21 -0800 | [diff] [blame] | 120 | if (dockState != Intent.EXTRA_DOCK_STATE_UNDOCKED) { |
| 121 | // remove undocked dialog if currently showing. |
| 122 | try { |
| 123 | dismissDialog(DIALOG_NOT_DOCKED); |
| 124 | } catch (IllegalArgumentException iae) { |
| 125 | // Maybe it was already dismissed |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | @Override |
| 132 | public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) { |
| 133 | if (preference == mAudioSettings) { |
Amith Yamasani | 6ff80dc | 2010-01-15 19:10:55 -0800 | [diff] [blame] | 134 | int dockState = mDockIntent != null |
| 135 | ? mDockIntent.getIntExtra(Intent.EXTRA_DOCK_STATE, 0) |
| 136 | : Intent.EXTRA_DOCK_STATE_UNDOCKED; |
Amith Yamasani | 0e2ab4f | 2010-01-14 16:12:21 -0800 | [diff] [blame] | 137 | if (dockState == Intent.EXTRA_DOCK_STATE_UNDOCKED) { |
| 138 | showDialog(DIALOG_NOT_DOCKED); |
| 139 | } else { |
| 140 | Intent i = new Intent(mDockIntent); |
| 141 | i.setAction(DockEventReceiver.ACTION_DOCK_SHOW_UI); |
| 142 | i.setClass(this, DockEventReceiver.class); |
| 143 | sendBroadcast(i); |
| 144 | } |
Daniel Sandler | fc01d06 | 2010-03-02 20:24:49 -0500 | [diff] [blame] | 145 | } else if (preference == mDockSounds) { |
| 146 | Settings.System.putInt(getContentResolver(), Settings.System.DOCK_SOUNDS_ENABLED, |
| 147 | mDockSounds.isChecked() ? 1 : 0); |
Amith Yamasani | 0e2ab4f | 2010-01-14 16:12:21 -0800 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | return true; |
| 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public Dialog onCreateDialog(int id) { |
| 155 | if (id == DIALOG_NOT_DOCKED) { |
| 156 | return createUndockedMessage(); |
| 157 | } |
| 158 | return null; |
| 159 | } |
| 160 | |
| 161 | private Dialog createUndockedMessage() { |
| 162 | final AlertDialog.Builder ab = new AlertDialog.Builder(this); |
| 163 | ab.setTitle(R.string.dock_not_found_title); |
| 164 | ab.setMessage(R.string.dock_not_found_text); |
| 165 | ab.setPositiveButton(android.R.string.ok, null); |
| 166 | return ab.create(); |
| 167 | } |
| 168 | } |