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