blob: 0d46ce9a9b2c06153108dc786806691418073b6e [file] [log] [blame]
Amith Yamasani0e2ab4f2010-01-14 16:12:21 -08001/*
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
17package com.android.settings;
18
19import android.app.AlertDialog;
20import android.app.Dialog;
Daniel Sandlerc0a51ab2010-03-12 14:53:49 -050021import android.bluetooth.BluetoothDevice;
Amith Yamasani0e2ab4f2010-01-14 16:12:21 -080022import android.content.BroadcastReceiver;
23import android.content.ContentResolver;
24import android.content.Context;
25import android.content.Intent;
26import android.content.IntentFilter;
27import android.os.Bundle;
Daniel Sandlerfc01d062010-03-02 20:24:49 -050028import android.preference.CheckBoxPreference;
Amith Yamasani0e2ab4f2010-01-14 16:12:21 -080029import android.preference.Preference;
30import android.preference.PreferenceActivity;
31import android.preference.PreferenceScreen;
Daniel Sandlerfc01d062010-03-02 20:24:49 -050032import android.provider.Settings;
Amith Yamasani0e2ab4f2010-01-14 16:12:21 -080033
34import com.android.settings.bluetooth.DockEventReceiver;
35
36public 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 Sandlerfc01d062010-03-02 20:24:49 -050040 private static final String KEY_DOCK_SOUNDS = "dock_sounds";
Amith Yamasani0e2ab4f2010-01-14 16:12:21 -080041 private Preference mAudioSettings;
Daniel Sandlerfc01d062010-03-02 20:24:49 -050042 private CheckBoxPreference mDockSounds;
Amith Yamasani0e2ab4f2010-01-14 16:12:21 -080043 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 Sandlerfc01d062010-03-02 20:24:49 -050079 ContentResolver resolver = getContentResolver();
80
Amith Yamasani0e2ab4f2010-01-14 16:12:21 -080081 mAudioSettings = findPreference(KEY_AUDIO_SETTINGS);
Amith Yamasani6ff80dc2010-01-15 19:10:55 -080082 if (mAudioSettings != null) {
83 mAudioSettings.setSummary(R.string.dock_audio_summary_none);
84 }
Daniel Sandlerfc01d062010-03-02 20:24:49 -050085
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 Yamasani0e2ab4f2010-01-14 16:12:21 -080090 }
91
92 private void handleDockChange(Intent intent) {
93 if (mAudioSettings != null) {
94 int dockState = intent.getIntExtra(Intent.EXTRA_DOCK_STATE, 0);
Daniel Sandlerc0a51ab2010-03-12 14:53:49 -050095
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 Yamasani0e2ab4f2010-01-14 16:12:21 -0800118 }
Daniel Sandlerc0a51ab2010-03-12 14:53:49 -0500119
Amith Yamasani0e2ab4f2010-01-14 16:12:21 -0800120 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 Yamasani6ff80dc2010-01-15 19:10:55 -0800134 int dockState = mDockIntent != null
135 ? mDockIntent.getIntExtra(Intent.EXTRA_DOCK_STATE, 0)
136 : Intent.EXTRA_DOCK_STATE_UNDOCKED;
Amith Yamasani0e2ab4f2010-01-14 16:12:21 -0800137 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 Sandlerfc01d062010-03-02 20:24:49 -0500145 } else if (preference == mDockSounds) {
146 Settings.System.putInt(getContentResolver(), Settings.System.DOCK_SOUNDS_ENABLED,
147 mDockSounds.isChecked() ? 1 : 0);
Amith Yamasani0e2ab4f2010-01-14 16:12:21 -0800148 }
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}