2 * Copyright (C) 2017 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings.development;
19 import android.content.BroadcastReceiver;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.IntentFilter;
23 import android.content.pm.PackageManager;
24 import android.hardware.usb.UsbManager;
25 import android.os.Bundle;
26 import android.support.annotation.VisibleForTesting;
27 import android.support.v7.preference.ListPreference;
28 import android.support.v7.preference.Preference;
29 import android.support.v7.preference.PreferenceScreen;
31 import com.android.settings.R;
32 import com.android.settings.Utils;
33 import com.android.settings.core.PreferenceControllerMixin;
34 import com.android.settings.connecteddevice.usb.UsbBackend;
35 import com.android.settingslib.core.lifecycle.Lifecycle;
36 import com.android.settingslib.core.lifecycle.LifecycleObserver;
37 import com.android.settingslib.core.lifecycle.events.OnCreate;
38 import com.android.settingslib.core.lifecycle.events.OnDestroy;
39 import com.android.settingslib.development.DeveloperOptionsPreferenceController;
41 public class SelectUsbConfigPreferenceController extends
42 DeveloperOptionsPreferenceController implements
43 Preference.OnPreferenceChangeListener, LifecycleObserver, OnCreate, OnDestroy,
44 PreferenceControllerMixin {
46 private static final String USB_CONFIGURATION_KEY = "select_usb_configuration";
48 private final String[] mListValues;
49 private final String[] mListSummaries;
50 private final UsbManager mUsbManager;
52 UsbBackend.UsbManagerPassThrough mUsbManagerPassThrough;
53 private BroadcastReceiver mUsbReceiver;
54 private ListPreference mPreference;
56 public SelectUsbConfigPreferenceController(Context context, Lifecycle lifecycle) {
59 mListValues = context.getResources().getStringArray(R.array.usb_configuration_values);
60 mListSummaries = context.getResources().getStringArray(R.array.usb_configuration_titles);
61 mUsbManager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
62 mUsbManagerPassThrough = new UsbBackend.UsbManagerPassThrough(mUsbManager);
63 mUsbReceiver = new BroadcastReceiver() {
65 public void onReceive(Context context, Intent intent) {
66 if (mPreference != null) {
67 updateUsbConfigurationValues();
71 if (lifecycle != null) {
72 lifecycle.addObserver(this);
77 public void onCreate(Bundle savedInstanceState) {
78 IntentFilter filter = new IntentFilter();
79 filter.addAction(UsbManager.ACTION_USB_STATE);
80 mContext.registerReceiver(mUsbReceiver, filter);
84 public String getPreferenceKey() {
85 return USB_CONFIGURATION_KEY;
89 public void displayPreference(PreferenceScreen screen) {
90 super.displayPreference(screen);
92 mPreference = (ListPreference) screen.findPreference(getPreferenceKey());
96 public boolean onPreferenceChange(Preference preference, Object newValue) {
97 if (Utils.isMonkeyRunning()) {
101 writeUsbConfigurationOption(mUsbManagerPassThrough
102 .usbFunctionsFromString(newValue.toString()));
103 updateUsbConfigurationValues();
108 public void updateState(Preference preference) {
109 updateUsbConfigurationValues();
113 public void onDestroy() {
114 mContext.unregisterReceiver(mUsbReceiver);
118 public boolean isAvailable() {
119 final PackageManager packageManager = mContext.getPackageManager();
121 return packageManager.hasSystemFeature(PackageManager.FEATURE_USB_HOST)
122 || packageManager.hasSystemFeature(PackageManager.FEATURE_USB_ACCESSORY);
126 protected void onDeveloperOptionsSwitchEnabled() {
127 mPreference.setEnabled(true);
131 protected void onDeveloperOptionsSwitchDisabled() {
132 mPreference.setEnabled(false);
136 void setCurrentFunctions(long functions) {
137 mUsbManager.setCurrentFunctions(functions);
140 private void updateUsbConfigurationValues() {
141 long functions = mUsbManagerPassThrough.getCurrentFunctions();
143 for (int i = 0; i < mListValues.length; i++) {
144 if (functions == mUsbManagerPassThrough.usbFunctionsFromString(mListValues[i])) {
149 mPreference.setValue(mListValues[index]);
150 mPreference.setSummary(mListSummaries[index]);
153 private void writeUsbConfigurationOption(long newValue) {
154 setCurrentFunctions(newValue);