Home | History | Annotate | Download | only in settings
      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 com.android.settings.bluetooth.DockEventReceiver;
     20 
     21 import android.app.AlertDialog;
     22 import android.app.Dialog;
     23 import android.bluetooth.BluetoothDevice;
     24 import android.content.BroadcastReceiver;
     25 import android.content.ContentResolver;
     26 import android.content.Context;
     27 import android.content.Intent;
     28 import android.content.IntentFilter;
     29 import android.os.Bundle;
     30 import android.preference.CheckBoxPreference;
     31 import android.preference.Preference;
     32 import android.preference.PreferenceScreen;
     33 import android.provider.Settings;
     34 
     35 public class DockSettings extends SettingsPreferenceFragment {
     36 
     37     private static final int DIALOG_NOT_DOCKED = 1;
     38     private static final String KEY_AUDIO_SETTINGS = "dock_audio";
     39     private static final String KEY_DOCK_SOUNDS = "dock_sounds";
     40     private Preference mAudioSettings;
     41     private CheckBoxPreference mDockSounds;
     42     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
     54     public void onCreate(Bundle savedInstanceState) {
     55         super.onCreate(savedInstanceState);
     56         addPreferencesFromResource(R.xml.dock_settings);
     57 
     58         initDockSettings();
     59     }
     60 
     61     @Override
     62     public void onResume() {
     63         super.onResume();
     64 
     65         IntentFilter filter = new IntentFilter(Intent.ACTION_DOCK_EVENT);
     66         getActivity().registerReceiver(mReceiver, filter);
     67     }
     68 
     69     @Override
     70     public void onPause() {
     71         super.onPause();
     72 
     73         getActivity().unregisterReceiver(mReceiver);
     74     }
     75 
     76     private void initDockSettings() {
     77         ContentResolver resolver = getContentResolver();
     78 
     79         mAudioSettings = findPreference(KEY_AUDIO_SETTINGS);
     80         if (mAudioSettings != null) {
     81             mAudioSettings.setSummary(R.string.dock_audio_summary_none);
     82         }
     83 
     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);
     88     }
     89 
     90     private void handleDockChange(Intent intent) {
     91         if (mAudioSettings != null) {
     92             int dockState = intent.getIntExtra(Intent.EXTRA_DOCK_STATE, 0);
     93 
     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                 case Intent.EXTRA_DOCK_STATE_LE_DESK:
    111                 case Intent.EXTRA_DOCK_STATE_HE_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);
    118             }
    119 
    120             if (dockState != Intent.EXTRA_DOCK_STATE_UNDOCKED) {
    121                 // remove undocked dialog if currently showing.
    122                 try {
    123                     removeDialog(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) {
    134             int dockState = mDockIntent != null
    135                     ? mDockIntent.getIntExtra(Intent.EXTRA_DOCK_STATE, 0)
    136                     : Intent.EXTRA_DOCK_STATE_UNDOCKED;
    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(getActivity(), DockEventReceiver.class);
    143                 getActivity().sendBroadcast(i);
    144             }
    145         } else if (preference == mDockSounds) {
    146             Settings.System.putInt(getContentResolver(), Settings.System.DOCK_SOUNDS_ENABLED,
    147                     mDockSounds.isChecked() ? 1 : 0);
    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(getActivity());
    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 }
    169