Files
app_Settings/src/com/android/settings/development/SelectUsbConfigPreferenceController.java
Jerry Zhang d18d6f1022 Add new USB details screen for Connected Devices 2.0
Also updated UsbBackend to use the new UsbFunctions
api.

Added new unit tests for UsbDetailsHeaderController
and UsbDetailsProfilesController.

Bug: 69333961
Test: make RunSettingsRoboTests
Change-Id: I133750190bb61dfe0e20b06f50e50ea13b347f1e
2018-01-31 11:23:08 -08:00

157 lines
5.3 KiB
Java

/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.development;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.hardware.usb.UsbManager;
import android.os.Bundle;
import android.support.annotation.VisibleForTesting;
import android.support.v7.preference.ListPreference;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settings.Utils;
import com.android.settings.core.PreferenceControllerMixin;
import com.android.settings.connecteddevice.usb.UsbBackend;
import com.android.settingslib.core.lifecycle.Lifecycle;
import com.android.settingslib.core.lifecycle.LifecycleObserver;
import com.android.settingslib.core.lifecycle.events.OnCreate;
import com.android.settingslib.core.lifecycle.events.OnDestroy;
import com.android.settingslib.development.DeveloperOptionsPreferenceController;
public class SelectUsbConfigPreferenceController extends
DeveloperOptionsPreferenceController implements
Preference.OnPreferenceChangeListener, LifecycleObserver, OnCreate, OnDestroy,
PreferenceControllerMixin {
private static final String USB_CONFIGURATION_KEY = "select_usb_configuration";
private final String[] mListValues;
private final String[] mListSummaries;
private final UsbManager mUsbManager;
@VisibleForTesting
UsbBackend.UsbManagerPassThrough mUsbManagerPassThrough;
private BroadcastReceiver mUsbReceiver;
private ListPreference mPreference;
public SelectUsbConfigPreferenceController(Context context, Lifecycle lifecycle) {
super(context);
mListValues = context.getResources().getStringArray(R.array.usb_configuration_values);
mListSummaries = context.getResources().getStringArray(R.array.usb_configuration_titles);
mUsbManager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
mUsbManagerPassThrough = new UsbBackend.UsbManagerPassThrough(mUsbManager);
mUsbReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (mPreference != null) {
updateUsbConfigurationValues();
}
}
};
if (lifecycle != null) {
lifecycle.addObserver(this);
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
IntentFilter filter = new IntentFilter();
filter.addAction(UsbManager.ACTION_USB_STATE);
mContext.registerReceiver(mUsbReceiver, filter);
}
@Override
public String getPreferenceKey() {
return USB_CONFIGURATION_KEY;
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
mPreference = (ListPreference) screen.findPreference(getPreferenceKey());
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
if (Utils.isMonkeyRunning()) {
return false;
}
writeUsbConfigurationOption(mUsbManagerPassThrough
.usbFunctionsFromString(newValue.toString()));
updateUsbConfigurationValues();
return true;
}
@Override
public void updateState(Preference preference) {
updateUsbConfigurationValues();
}
@Override
public void onDestroy() {
mContext.unregisterReceiver(mUsbReceiver);
}
@Override
public boolean isAvailable() {
final PackageManager packageManager = mContext.getPackageManager();
return packageManager.hasSystemFeature(PackageManager.FEATURE_USB_HOST)
|| packageManager.hasSystemFeature(PackageManager.FEATURE_USB_ACCESSORY);
}
@Override
protected void onDeveloperOptionsSwitchEnabled() {
mPreference.setEnabled(true);
}
@Override
protected void onDeveloperOptionsSwitchDisabled() {
mPreference.setEnabled(false);
}
@VisibleForTesting
void setCurrentFunctions(long functions) {
mUsbManager.setCurrentFunctions(functions);
}
private void updateUsbConfigurationValues() {
long functions = mUsbManagerPassThrough.getCurrentFunctions();
int index = 0;
for (int i = 0; i < mListValues.length; i++) {
if (functions == mUsbManagerPassThrough.usbFunctionsFromString(mListValues[i])) {
index = i;
break;
}
}
mPreference.setValue(mListValues[index]);
mPreference.setSummary(mListSummaries[index]);
}
private void writeUsbConfigurationOption(long newValue) {
setCurrentFunctions(newValue);
}
}