UsbSettings.java revision 688605ec02f99ecd424aeaf31f5e966bbee5f6be
1/* 2 * Copyright (C) 2011 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.deviceinfo; 18 19import android.app.AlertDialog; 20import android.app.Dialog; 21import android.content.BroadcastReceiver; 22import android.content.ContentQueryMap; 23import android.content.ContentResolver; 24import android.content.Context; 25import android.content.Intent; 26import android.content.IntentFilter; 27import android.hardware.usb.UsbManager; 28import android.os.Bundle; 29import android.preference.CheckBoxPreference; 30import android.preference.Preference; 31import android.preference.PreferenceScreen; 32import android.provider.Settings; 33import android.util.Log; 34 35import com.android.settings.R; 36import com.android.settings.SettingsPreferenceFragment; 37 38/** 39 * USB storage settings. 40 */ 41public class UsbSettings extends SettingsPreferenceFragment { 42 43 private static final String TAG = "UsbSettings"; 44 45 private static final String KEY_MTP = "usb_mtp"; 46 private static final String KEY_PTP = "usb_ptp"; 47 48 private UsbManager mUsbManager; 49 private CheckBoxPreference mMtp; 50 private CheckBoxPreference mPtp; 51 52 private final BroadcastReceiver mStateReceiver = new BroadcastReceiver() { 53 public void onReceive(Context content, Intent intent) { 54 updateToggles(); 55 } 56 }; 57 58 private PreferenceScreen createPreferenceHierarchy() { 59 PreferenceScreen root = getPreferenceScreen(); 60 if (root != null) { 61 root.removeAll(); 62 } 63 addPreferencesFromResource(R.xml.usb_settings); 64 root = getPreferenceScreen(); 65 66 mMtp = (CheckBoxPreference)root.findPreference(KEY_MTP); 67 mPtp = (CheckBoxPreference)root.findPreference(KEY_PTP); 68 69 return root; 70 } 71 72 @Override 73 public void onCreate(Bundle icicle) { 74 super.onCreate(icicle); 75 mUsbManager = (UsbManager)getSystemService(Context.USB_SERVICE); 76 } 77 78 @Override 79 public void onPause() { 80 super.onPause(); 81 getActivity().unregisterReceiver(mStateReceiver); 82 } 83 84 @Override 85 public void onResume() { 86 super.onResume(); 87 88 // Make sure we reload the preference hierarchy since some of these settings 89 // depend on others... 90 createPreferenceHierarchy(); 91 92 // ACTION_USB_STATE is sticky so this will call updateToggles 93 getActivity().registerReceiver(mStateReceiver, 94 new IntentFilter(UsbManager.ACTION_USB_STATE)); 95 } 96 97 private void updateToggles() { 98 String function = mUsbManager.getDefaultFunction(); 99 if (UsbManager.USB_FUNCTION_MTP.equals(function)) { 100 mMtp.setChecked(true); 101 mPtp.setChecked(false); 102 } else if (UsbManager.USB_FUNCTION_PTP.equals(function)) { 103 mMtp.setChecked(false); 104 mPtp.setChecked(true); 105 } else { 106 mMtp.setChecked(false); 107 mPtp.setChecked(false); 108 } 109 } 110 111 @Override 112 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) { 113 114 // temporary hack - using check boxes as radio buttons 115 // don't allow unchecking them 116 if (preference instanceof CheckBoxPreference) { 117 CheckBoxPreference checkBox = (CheckBoxPreference)preference; 118 if (!checkBox.isChecked()) { 119 checkBox.setChecked(true); 120 return true; 121 } 122 } 123 if (preference == mMtp) { 124 mUsbManager.setCurrentFunction(UsbManager.USB_FUNCTION_MTP, true); 125 } else if (preference == mPtp) { 126 mUsbManager.setCurrentFunction(UsbManager.USB_FUNCTION_PTP, true); 127 } 128 updateToggles(); 129 return true; 130 } 131} 132