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; |
| 21 | import android.content.BroadcastReceiver; |
| 22 | import android.content.ContentResolver; |
| 23 | import android.content.Context; |
| 24 | import android.content.Intent; |
| 25 | import android.content.IntentFilter; |
| 26 | import android.os.Bundle; |
| 27 | import android.preference.Preference; |
| 28 | import android.preference.PreferenceActivity; |
| 29 | import android.preference.PreferenceScreen; |
| 30 | |
| 31 | import com.android.settings.bluetooth.DockEventReceiver; |
| 32 | |
| 33 | public class DockSettings extends PreferenceActivity { |
| 34 | |
| 35 | private static final int DIALOG_NOT_DOCKED = 1; |
| 36 | private static final String KEY_AUDIO_SETTINGS = "dock_audio"; |
| 37 | private Preference mAudioSettings; |
| 38 | private Intent mDockIntent; |
| 39 | |
| 40 | private BroadcastReceiver mReceiver = new BroadcastReceiver() { |
| 41 | @Override |
| 42 | public void onReceive(Context context, Intent intent) { |
| 43 | if (intent.getAction().equals(Intent.ACTION_DOCK_EVENT)) { |
| 44 | handleDockChange(intent); |
| 45 | } |
| 46 | } |
| 47 | }; |
| 48 | |
| 49 | @Override |
| 50 | protected void onCreate(Bundle savedInstanceState) { |
| 51 | super.onCreate(savedInstanceState); |
| 52 | ContentResolver resolver = getContentResolver(); |
| 53 | addPreferencesFromResource(R.xml.dock_settings); |
| 54 | |
| 55 | initDockSettings(); |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | protected void onResume() { |
| 60 | super.onResume(); |
| 61 | |
| 62 | IntentFilter filter = new IntentFilter(Intent.ACTION_DOCK_EVENT); |
| 63 | registerReceiver(mReceiver, filter); |
| 64 | } |
| 65 | |
| 66 | @Override |
| 67 | protected void onPause() { |
| 68 | super.onPause(); |
| 69 | |
| 70 | unregisterReceiver(mReceiver); |
| 71 | } |
| 72 | |
| 73 | private void initDockSettings() { |
| 74 | mAudioSettings = findPreference(KEY_AUDIO_SETTINGS); |
| 75 | } |
| 76 | |
| 77 | private void handleDockChange(Intent intent) { |
| 78 | if (mAudioSettings != null) { |
| 79 | int dockState = intent.getIntExtra(Intent.EXTRA_DOCK_STATE, 0); |
| 80 | mDockIntent = intent; |
| 81 | int resId = R.string.dock_audio_summary_unknown; |
| 82 | switch (dockState) { |
| 83 | case Intent.EXTRA_DOCK_STATE_CAR: |
| 84 | resId = R.string.dock_audio_summary_car; |
| 85 | break; |
| 86 | case Intent.EXTRA_DOCK_STATE_DESK: |
| 87 | resId = R.string.dock_audio_summary_desk; |
| 88 | break; |
| 89 | case Intent.EXTRA_DOCK_STATE_UNDOCKED: |
| 90 | resId = R.string.dock_audio_summary_none; |
| 91 | } |
| 92 | mAudioSettings.setSummary(resId); |
| 93 | if (dockState != Intent.EXTRA_DOCK_STATE_UNDOCKED) { |
| 94 | // remove undocked dialog if currently showing. |
| 95 | try { |
| 96 | dismissDialog(DIALOG_NOT_DOCKED); |
| 97 | } catch (IllegalArgumentException iae) { |
| 98 | // Maybe it was already dismissed |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) { |
| 106 | if (preference == mAudioSettings) { |
| 107 | int dockState = mDockIntent.getIntExtra(Intent.EXTRA_DOCK_STATE, 0); |
| 108 | if (dockState == Intent.EXTRA_DOCK_STATE_UNDOCKED) { |
| 109 | showDialog(DIALOG_NOT_DOCKED); |
| 110 | } else { |
| 111 | Intent i = new Intent(mDockIntent); |
| 112 | i.setAction(DockEventReceiver.ACTION_DOCK_SHOW_UI); |
| 113 | i.setClass(this, DockEventReceiver.class); |
| 114 | sendBroadcast(i); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | return true; |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public Dialog onCreateDialog(int id) { |
| 123 | if (id == DIALOG_NOT_DOCKED) { |
| 124 | return createUndockedMessage(); |
| 125 | } |
| 126 | return null; |
| 127 | } |
| 128 | |
| 129 | private Dialog createUndockedMessage() { |
| 130 | final AlertDialog.Builder ab = new AlertDialog.Builder(this); |
| 131 | ab.setTitle(R.string.dock_not_found_title); |
| 132 | ab.setMessage(R.string.dock_not_found_text); |
| 133 | ab.setPositiveButton(android.R.string.ok, null); |
| 134 | return ab.create(); |
| 135 | } |
| 136 | } |