UsbSettings.java revision ed9d461818780a6c38a081030a55983ea0883fa6
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(mUsbManager.getDefaultFunction());
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(String function) {
98        if (UsbManager.USB_FUNCTION_MTP.equals(function)) {
99            mMtp.setChecked(true);
100            mPtp.setChecked(false);
101        } else if (UsbManager.USB_FUNCTION_PTP.equals(function)) {
102            mMtp.setChecked(false);
103            mPtp.setChecked(true);
104        } else  {
105            mMtp.setChecked(false);
106            mPtp.setChecked(false);
107        }
108    }
109
110    @Override
111    public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
112
113        // temporary hack - using check boxes as radio buttons
114        // don't allow unchecking them
115        if (preference instanceof CheckBoxPreference) {
116            CheckBoxPreference checkBox = (CheckBoxPreference)preference;
117            if (!checkBox.isChecked()) {
118                checkBox.setChecked(true);
119                return true;
120            }
121        }
122        if (preference == mMtp) {
123            mUsbManager.setCurrentFunction(UsbManager.USB_FUNCTION_MTP, true);
124            updateToggles(UsbManager.USB_FUNCTION_MTP);
125        } else if (preference == mPtp) {
126            mUsbManager.setCurrentFunction(UsbManager.USB_FUNCTION_PTP, true);
127            updateToggles(UsbManager.USB_FUNCTION_PTP);
128        }
129        return true;
130    }
131}
132