blob: b36864021a7821b31df281c8765899f0d9676fde [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);
56 ContentResolver resolver = getContentResolver();
57 addPreferencesFromResource(R.xml.dock_settings);
58
59 initDockSettings();
60 }
61
62 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -070063 public void onResume() {
Amith Yamasani0e2ab4f2010-01-14 16:12:21 -080064 super.onResume();
65
66 IntentFilter filter = new IntentFilter(Intent.ACTION_DOCK_EVENT);
Amith Yamasanid7993472010-08-18 13:59:28 -070067 getActivity().registerReceiver(mReceiver, filter);
Amith Yamasani0e2ab4f2010-01-14 16:12:21 -080068 }
69
70 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -070071 public void onPause() {
Amith Yamasani0e2ab4f2010-01-14 16:12:21 -080072 super.onPause();
73
Amith Yamasanid7993472010-08-18 13:59:28 -070074 getActivity().unregisterReceiver(mReceiver);
Amith Yamasani0e2ab4f2010-01-14 16:12:21 -080075 }
76
77 private void initDockSettings() {
Daniel Sandlerfc01d062010-03-02 20:24:49 -050078 ContentResolver resolver = getContentResolver();
79
Amith Yamasani0e2ab4f2010-01-14 16:12:21 -080080 mAudioSettings = findPreference(KEY_AUDIO_SETTINGS);
Amith Yamasani6ff80dc2010-01-15 19:10:55 -080081 if (mAudioSettings != null) {
82 mAudioSettings.setSummary(R.string.dock_audio_summary_none);
83 }
Daniel Sandlerfc01d062010-03-02 20:24:49 -050084
85 mDockSounds = (CheckBoxPreference) findPreference(KEY_DOCK_SOUNDS);
86 mDockSounds.setPersistent(false);
87 mDockSounds.setChecked(Settings.System.getInt(resolver,
88 Settings.System.DOCK_SOUNDS_ENABLED, 0) != 0);
Amith Yamasani0e2ab4f2010-01-14 16:12:21 -080089 }
90
91 private void handleDockChange(Intent intent) {
92 if (mAudioSettings != null) {
93 int dockState = intent.getIntExtra(Intent.EXTRA_DOCK_STATE, 0);
Daniel Sandlerc0a51ab2010-03-12 14:53:49 -050094
95 boolean isBluetooth = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE) != null;
96
97 if (!isBluetooth) {
98 // No dock audio if not on Bluetooth.
99 mAudioSettings.setEnabled(false);
100 mAudioSettings.setSummary(R.string.dock_audio_summary_unknown);
101 } else {
102 mAudioSettings.setEnabled(true);
103
104 mDockIntent = intent;
105 int resId = R.string.dock_audio_summary_unknown;
106 switch (dockState) {
107 case Intent.EXTRA_DOCK_STATE_CAR:
108 resId = R.string.dock_audio_summary_car;
109 break;
110 case Intent.EXTRA_DOCK_STATE_DESK:
111 resId = R.string.dock_audio_summary_desk;
112 break;
113 case Intent.EXTRA_DOCK_STATE_UNDOCKED:
114 resId = R.string.dock_audio_summary_none;
115 }
116 mAudioSettings.setSummary(resId);
Amith Yamasani0e2ab4f2010-01-14 16:12:21 -0800117 }
Daniel Sandlerc0a51ab2010-03-12 14:53:49 -0500118
Amith Yamasani0e2ab4f2010-01-14 16:12:21 -0800119 if (dockState != Intent.EXTRA_DOCK_STATE_UNDOCKED) {
120 // remove undocked dialog if currently showing.
121 try {
Amith Yamasanid7993472010-08-18 13:59:28 -0700122 removeDialog(DIALOG_NOT_DOCKED);
Amith Yamasani0e2ab4f2010-01-14 16:12:21 -0800123 } catch (IllegalArgumentException iae) {
124 // Maybe it was already dismissed
125 }
126 }
127 }
128 }
129
130 @Override
131 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
132 if (preference == mAudioSettings) {
Amith Yamasani6ff80dc2010-01-15 19:10:55 -0800133 int dockState = mDockIntent != null
134 ? mDockIntent.getIntExtra(Intent.EXTRA_DOCK_STATE, 0)
135 : Intent.EXTRA_DOCK_STATE_UNDOCKED;
Amith Yamasani0e2ab4f2010-01-14 16:12:21 -0800136 if (dockState == Intent.EXTRA_DOCK_STATE_UNDOCKED) {
137 showDialog(DIALOG_NOT_DOCKED);
138 } else {
139 Intent i = new Intent(mDockIntent);
140 i.setAction(DockEventReceiver.ACTION_DOCK_SHOW_UI);
Amith Yamasanid7993472010-08-18 13:59:28 -0700141 i.setClass(getActivity(), DockEventReceiver.class);
142 getActivity().sendBroadcast(i);
Amith Yamasani0e2ab4f2010-01-14 16:12:21 -0800143 }
Daniel Sandlerfc01d062010-03-02 20:24:49 -0500144 } else if (preference == mDockSounds) {
145 Settings.System.putInt(getContentResolver(), Settings.System.DOCK_SOUNDS_ENABLED,
146 mDockSounds.isChecked() ? 1 : 0);
Amith Yamasani0e2ab4f2010-01-14 16:12:21 -0800147 }
148
149 return true;
150 }
151
152 @Override
153 public Dialog onCreateDialog(int id) {
154 if (id == DIALOG_NOT_DOCKED) {
155 return createUndockedMessage();
156 }
157 return null;
158 }
159
160 private Dialog createUndockedMessage() {
Amith Yamasanid7993472010-08-18 13:59:28 -0700161 final AlertDialog.Builder ab = new AlertDialog.Builder(getActivity());
Amith Yamasani0e2ab4f2010-01-14 16:12:21 -0800162 ab.setTitle(R.string.dock_not_found_title);
163 ab.setMessage(R.string.dock_not_found_text);
164 ab.setPositiveButton(android.R.string.ok, null);
165 return ab.create();
166 }
167}