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.content.BroadcastReceiver;
20import android.content.Context;
21import android.content.Intent;
22import android.content.IntentFilter;
23import android.hardware.usb.UsbManager;
24import android.os.Bundle;
25import android.os.UserManager;
26import android.preference.CheckBoxPreference;
27import android.preference.Preference;
28import android.preference.PreferenceScreen;
29import android.util.Log;
30
31import com.android.settings.R;
32import com.android.settings.SettingsPreferenceFragment;
33import com.android.settings.Utils;
34
35/**
36 * USB storage settings.
37 */
38public class UsbSettings extends SettingsPreferenceFragment {
39
40    private static final String TAG = "UsbSettings";
41
42    private static final String KEY_MTP = "usb_mtp";
43    private static final String KEY_PTP = "usb_ptp";
44
45    private UsbManager mUsbManager;
46    private CheckBoxPreference mMtp;
47    private CheckBoxPreference mPtp;
48    private boolean mUsbAccessoryMode;
49
50    private final BroadcastReceiver mStateReceiver = new BroadcastReceiver() {
51        public void onReceive(Context content, Intent intent) {
52            String action = intent.getAction();
53            if (action.equals(UsbManager.ACTION_USB_STATE)) {
54               mUsbAccessoryMode = intent.getBooleanExtra(UsbManager.USB_FUNCTION_ACCESSORY, false);
55               Log.e(TAG, "UsbAccessoryMode " + mUsbAccessoryMode);
56            }
57            updateToggles(mUsbManager.getDefaultFunction());
58        }
59    };
60
61    private PreferenceScreen createPreferenceHierarchy() {
62        PreferenceScreen root = getPreferenceScreen();
63        if (root != null) {
64            root.removeAll();
65        }
66        addPreferencesFromResource(R.xml.usb_settings);
67        root = getPreferenceScreen();
68
69        mMtp = (CheckBoxPreference)root.findPreference(KEY_MTP);
70        mPtp = (CheckBoxPreference)root.findPreference(KEY_PTP);
71
72        UserManager um = (UserManager) getActivity().getSystemService(Context.USER_SERVICE);
73        if (um.hasUserRestriction(UserManager.DISALLOW_USB_FILE_TRANSFER)) {
74            mMtp.setEnabled(false);
75            mPtp.setEnabled(false);
76        }
77
78        return root;
79    }
80
81    @Override
82    public void onCreate(Bundle icicle) {
83        super.onCreate(icicle);
84        mUsbManager = (UsbManager)getSystemService(Context.USB_SERVICE);
85    }
86
87    @Override
88    public void onPause() {
89        super.onPause();
90        getActivity().unregisterReceiver(mStateReceiver);
91    }
92
93    @Override
94    public void onResume() {
95        super.onResume();
96
97        // Make sure we reload the preference hierarchy since some of these settings
98        // depend on others...
99        createPreferenceHierarchy();
100
101        // ACTION_USB_STATE is sticky so this will call updateToggles
102        getActivity().registerReceiver(mStateReceiver,
103                new IntentFilter(UsbManager.ACTION_USB_STATE));
104    }
105
106    private void updateToggles(String function) {
107        if (UsbManager.USB_FUNCTION_MTP.equals(function)) {
108            mMtp.setChecked(true);
109            mPtp.setChecked(false);
110        } else if (UsbManager.USB_FUNCTION_PTP.equals(function)) {
111            mMtp.setChecked(false);
112            mPtp.setChecked(true);
113        } else  {
114            mMtp.setChecked(false);
115            mPtp.setChecked(false);
116        }
117        UserManager um = (UserManager) getActivity().getSystemService(Context.USER_SERVICE);
118        if (um.hasUserRestriction(UserManager.DISALLOW_USB_FILE_TRANSFER)) {
119            Log.e(TAG, "USB is locked down");
120            mMtp.setEnabled(false);
121            mPtp.setEnabled(false);
122        } else if (!mUsbAccessoryMode) {
123            //Enable MTP and PTP switch while USB is not in Accessory Mode, otherwise disable it
124            Log.e(TAG, "USB Normal Mode");
125            mMtp.setEnabled(true);
126            mPtp.setEnabled(true);
127        } else {
128            Log.e(TAG, "USB Accessory Mode");
129            mMtp.setEnabled(false);
130            mPtp.setEnabled(false);
131        }
132    }
133
134    @Override
135    public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
136
137        // Don't allow any changes to take effect as the USB host will be disconnected, killing
138        // the monkeys
139        if (Utils.isMonkeyRunning()) {
140            return true;
141        }
142        // If this user is disallowed from using USB, don't handle their attempts to change the
143        // setting.
144        UserManager um = (UserManager) getActivity().getSystemService(Context.USER_SERVICE);
145        if (um.hasUserRestriction(UserManager.DISALLOW_USB_FILE_TRANSFER)) {
146            return true;
147        }
148
149        String function = "none";
150        if (preference == mMtp && mMtp.isChecked()) {
151            function = UsbManager.USB_FUNCTION_MTP;
152        } else if (preference == mPtp && mPtp.isChecked()) {
153            function = UsbManager.USB_FUNCTION_PTP;
154        }
155
156        mUsbManager.setCurrentFunction(function, true);
157        updateToggles(function);
158
159        return true;
160    }
161}
162