UsbSettings.java revision df5ab21d21fac3107fed231d4469e5bc26a089f6
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;
37import com.android.settings.Utils;
38
39/**
40 * USB storage settings.
41 */
42public 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