blob: 3b3402f7cca93a8e976a8772faf7d86f2d3f60d0 [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
Amith Yamasanid7993472010-08-18 13:59:28 -070019import com.android.settings.bluetooth.DockEventReceiver;
20
Amith Yamasani0e2ab4f2010-01-14 16:12:21 -080021import android.app.AlertDialog;
22import android.app.Dialog;
Daniel Sandlerc0a51ab2010-03-12 14:53:49 -050023import android.bluetooth.BluetoothDevice;
Amith Yamasani0e2ab4f2010-01-14 16:12:21 -080024import android.content.BroadcastReceiver;
25import android.content.ContentResolver;
26import android.content.Context;
27import android.content.Intent;
28import android.content.IntentFilter;
29import android.os.Bundle;
Daniel Sandlerfc01d062010-03-02 20:24:49 -050030import android.preference.CheckBoxPreference;
Amith Yamasani0e2ab4f2010-01-14 16:12:21 -080031import android.preference.Preference;
Amith Yamasani0e2ab4f2010-01-14 16:12:21 -080032import android.preference.PreferenceScreen;
Daniel Sandlerfc01d062010-03-02 20:24:49 -050033import android.provider.Settings;
Amith Yamasani0e2ab4f2010-01-14 16:12:21 -080034
Amith Yamasanid7993472010-08-18 13:59:28 -070035public class DockSettings extends SettingsPreferenceFragment {
Amith Yamasani0e2ab4f2010-01-14 16:12:21 -080036
37 private static final int DIALOG_NOT_DOCKED = 1;
38 private static final String KEY_AUDIO_SETTINGS = "dock_audio";
Daniel Sandlerfc01d062010-03-02 20:24:49 -050039 private static final String KEY_DOCK_SOUNDS = "dock_sounds";
Amith Yamasani0e2ab4f2010-01-14 16:12:21 -080040 private Preference mAudioSettings;
Daniel Sandlerfc01d062010-03-02 20:24:49 -050041 private CheckBoxPreference mDockSounds;
Amith Yamasani0e2ab4f2010-01-14 16:12:21 -080042 private Intent mDockIntent;
43
44 private BroadcastReceiver mReceiver = new BroadcastReceiver() {
45 @Override
46 public void onReceive(Context context, Intent intent) {
47 if (intent.getAction().equals(Intent.ACTION_DOCK_EVENT)) {
48 handleDockChange(intent);
49 }
50 }
51 };
52
53 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -070054 public void onCreate(Bundle savedInstanceState) {
Amith Yamasani0e2ab4f2010-01-14 16:12:21 -080055 super.onCreate(savedInstanceState);
Amith Yamasani0e2ab4f2010-01-14 16:12:21 -080056 addPreferencesFromResource(R.xml.dock_settings);
57
58 initDockSettings();
59 }
60
61 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -070062 public void onResume() {
Amith Yamasani0e2ab4f2010-01-14 16:12:21 -080063 super.onResume();
64
65 IntentFilter filter = new IntentFilter(Intent.ACTION_DOCK_EVENT);
Amith Yamasanid7993472010-08-18 13:59:28 -070066 getActivity().registerReceiver(mReceiver, filter);
Amith Yamasani0e2ab4f2010-01-14 16:12:21 -080067 }
68
69 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -070070 public void onPause() {
Amith Yamasani0e2ab4f2010-01-14 16:12:21 -080071 super.onPause();
72
Amith Yamasanid7993472010-08-18 13:59:28 -070073 getActivity().unregisterReceiver(mReceiver);
Amith Yamasani0e2ab4f2010-01-14 16:12:21 -080074 }
75
76 private void initDockSettings() {
Daniel Sandlerfc01d062010-03-02 20:24:49 -050077 ContentResolver resolver = getContentResolver();
78
Amith Yamasani0e2ab4f2010-01-14 16:12:21 -080079 mAudioSettings = findPreference(KEY_AUDIO_SETTINGS);
Amith Yamasani6ff80dc2010-01-15 19:10:55 -080080 if (mAudioSettings != null) {
81 mAudioSettings.setSummary(R.string.dock_audio_summary_none);
82 }
Daniel Sandlerfc01d062010-03-02 20:24:49 -050083
84 mDockSounds = (CheckBoxPreference) findPreference(KEY_DOCK_SOUNDS);
85 mDockSounds.setPersistent(false);
86 mDockSounds.setChecked(Settings.System.getInt(resolver,
87 Settings.System.DOCK_SOUNDS_ENABLED, 0) != 0);
Amith Yamasani0e2ab4f2010-01-14 16:12:21 -080088 }
89
90 private void handleDockChange(Intent intent) {
91 if (mAudioSettings != null) {
92 int dockState = intent.getIntExtra(Intent.EXTRA_DOCK_STATE, 0);
Daniel Sandlerc0a51ab2010-03-12 14:53:49 -050093
94 boolean isBluetooth = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE) != null;
95
96 if (!isBluetooth) {
97 // No dock audio if not on Bluetooth.
98 mAudioSettings.setEnabled(false);
99 mAudioSettings.setSummary(R.string.dock_audio_summary_unknown);
100 } else {
101 mAudioSettings.setEnabled(true);
102
103 mDockIntent = intent;
104 int resId = R.string.dock_audio_summary_unknown;
105 switch (dockState) {
106 case Intent.EXTRA_DOCK_STATE_CAR:
107 resId = R.string.dock_audio_summary_car;
108 break;
109 case Intent.EXTRA_DOCK_STATE_DESK:
110 resId = R.string.dock_audio_summary_desk;
111 break;
112 case Intent.EXTRA_DOCK_STATE_UNDOCKED:
113 resId = R.string.dock_audio_summary_none;
114 }
115 mAudioSettings.setSummary(resId);
Amith Yamasani0e2ab4f2010-01-14 16:12:21 -0800116 }
Daniel Sandlerc0a51ab2010-03-12 14:53:49 -0500117
Amith Yamasani0e2ab4f2010-01-14 16:12:21 -0800118 if (dockState != Intent.EXTRA_DOCK_STATE_UNDOCKED) {
119 // remove undocked dialog if currently showing.
120 try {
Amith Yamasanid7993472010-08-18 13:59:28 -0700121 removeDialog(DIALOG_NOT_DOCKED);
Amith Yamasani0e2ab4f2010-01-14 16:12:21 -0800122 } catch (IllegalArgumentException iae) {
123 // Maybe it was already dismissed
124 }
125 }
126 }
127 }
128
129 @Override
130 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
131 if (preference == mAudioSettings) {
Amith Yamasani6ff80dc2010-01-15 19:10:55 -0800132 int dockState = mDockIntent != null
133 ? mDockIntent.getIntExtra(Intent.EXTRA_DOCK_STATE, 0)
134 : Intent.EXTRA_DOCK_STATE_UNDOCKED;
Amith Yamasani0e2ab4f2010-01-14 16:12:21 -0800135 if (dockState == Intent.EXTRA_DOCK_STATE_UNDOCKED) {
136 showDialog(DIALOG_NOT_DOCKED);
137 } else {
138 Intent i = new Intent(mDockIntent);
139 i.setAction(DockEventReceiver.ACTION_DOCK_SHOW_UI);
Amith Yamasanid7993472010-08-18 13:59:28 -0700140 i.setClass(getActivity(), DockEventReceiver.class);
141 getActivity().sendBroadcast(i);
Amith Yamasani0e2ab4f2010-01-14 16:12:21 -0800142 }
Daniel Sandlerfc01d062010-03-02 20:24:49 -0500143 } else if (preference == mDockSounds) {
144 Settings.System.putInt(getContentResolver(), Settings.System.DOCK_SOUNDS_ENABLED,
145 mDockSounds.isChecked() ? 1 : 0);
Amith Yamasani0e2ab4f2010-01-14 16:12:21 -0800146 }
147
148 return true;
149 }
150
151 @Override
152 public Dialog onCreateDialog(int id) {
153 if (id == DIALOG_NOT_DOCKED) {
154 return createUndockedMessage();
155 }
156 return null;
157 }
158
159 private Dialog createUndockedMessage() {
Amith Yamasanid7993472010-08-18 13:59:28 -0700160 final AlertDialog.Builder ab = new AlertDialog.Builder(getActivity());
Amith Yamasani0e2ab4f2010-01-14 16:12:21 -0800161 ab.setTitle(R.string.dock_not_found_title);
162 ab.setMessage(R.string.dock_not_found_text);
163 ab.setPositiveButton(android.R.string.ok, null);
164 return ab.create();
165 }
166}