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
This commit is contained in:
@@ -24,8 +24,9 @@ import com.android.settings.R;
|
||||
import com.android.settings.bluetooth.BluetoothFilesPreferenceController;
|
||||
import com.android.settings.bluetooth.BluetoothMasterSwitchPreferenceController;
|
||||
import com.android.settings.bluetooth.BluetoothSwitchPreferenceController;
|
||||
import com.android.settings.connecteddevice.usb.UsbBackend;
|
||||
import com.android.settings.connecteddevice.usb.UsbModePreferenceController;
|
||||
import com.android.settings.dashboard.DashboardFragment;
|
||||
import com.android.settings.deviceinfo.UsbBackend;
|
||||
import com.android.settings.nfc.NfcPreferenceController;
|
||||
import com.android.settings.overlay.FeatureFactory;
|
||||
import com.android.settings.search.BaseSearchIndexProvider;
|
||||
|
@@ -26,9 +26,10 @@ import com.android.settings.R;
|
||||
import com.android.settings.SettingsActivity;
|
||||
import com.android.settings.bluetooth.BluetoothMasterSwitchPreferenceController;
|
||||
import com.android.settings.bluetooth.Utils;
|
||||
import com.android.settings.connecteddevice.usb.UsbBackend;
|
||||
import com.android.settings.connecteddevice.usb.UsbModePreferenceController;
|
||||
import com.android.settings.dashboard.DashboardFragment;
|
||||
import com.android.settings.dashboard.SummaryLoader;
|
||||
import com.android.settings.deviceinfo.UsbBackend;
|
||||
import com.android.settings.nfc.NfcPreferenceController;
|
||||
import com.android.settings.overlay.FeatureFactory;
|
||||
import com.android.settings.search.BaseSearchIndexProvider;
|
||||
|
@@ -20,6 +20,7 @@ import android.support.v7.preference.Preference;
|
||||
import android.support.v7.preference.PreferenceGroup;
|
||||
import android.support.v7.preference.PreferenceScreen;
|
||||
|
||||
import com.android.settings.connecteddevice.usb.ConnectedUsbDeviceUpdater;
|
||||
import com.android.settings.core.PreferenceControllerMixin;
|
||||
import com.android.settings.bluetooth.BluetoothDeviceUpdater;
|
||||
import com.android.settings.bluetooth.ConnectedBluetoothDeviceUpdater;
|
||||
@@ -48,7 +49,7 @@ public class ConnectedDeviceGroupController extends AbstractPreferenceController
|
||||
public ConnectedDeviceGroupController(DashboardFragment fragment, Lifecycle lifecycle) {
|
||||
super(fragment.getContext());
|
||||
init(lifecycle, new ConnectedBluetoothDeviceUpdater(fragment, this),
|
||||
new ConnectedUsbDeviceUpdater(fragment.getContext(), this));
|
||||
new ConnectedUsbDeviceUpdater(fragment, this));
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
|
@@ -1,76 +0,0 @@
|
||||
/*
|
||||
* 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.connecteddevice;
|
||||
|
||||
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.hardware.usb.UsbManager;
|
||||
|
||||
/**
|
||||
* Receiver to receive usb update and use {@link UsbConnectionListener} to invoke callback
|
||||
*/
|
||||
public class UsbConnectionBroadcastReceiver extends BroadcastReceiver {
|
||||
private Context mContext;
|
||||
private UsbConnectionListener mUsbConnectionListener;
|
||||
private boolean mListeningToUsbEvents;
|
||||
private boolean mConnected;
|
||||
|
||||
public UsbConnectionBroadcastReceiver(Context context,
|
||||
UsbConnectionListener usbConnectionListener) {
|
||||
mContext = context;
|
||||
mUsbConnectionListener = usbConnectionListener;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
mConnected = intent != null
|
||||
&& intent.getExtras().getBoolean(UsbManager.USB_CONNECTED);
|
||||
if (mUsbConnectionListener != null) {
|
||||
mUsbConnectionListener.onUsbConnectionChanged(mConnected);
|
||||
}
|
||||
}
|
||||
|
||||
public void register() {
|
||||
if (!mListeningToUsbEvents) {
|
||||
final IntentFilter intentFilter = new IntentFilter(UsbManager.ACTION_USB_STATE);
|
||||
final Intent intent = mContext.registerReceiver(this, intentFilter);
|
||||
mConnected = intent != null
|
||||
&& intent.getExtras().getBoolean(UsbManager.USB_CONNECTED);
|
||||
mListeningToUsbEvents = true;
|
||||
}
|
||||
}
|
||||
|
||||
public void unregister() {
|
||||
if (mListeningToUsbEvents) {
|
||||
mContext.unregisterReceiver(this);
|
||||
mListeningToUsbEvents = false;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isConnected() {
|
||||
return mConnected;
|
||||
}
|
||||
|
||||
/**
|
||||
* Interface definition for a callback to be invoked when usb connection is changed.
|
||||
*/
|
||||
interface UsbConnectionListener {
|
||||
void onUsbConnectionChanged(boolean connected);
|
||||
}
|
||||
}
|
@@ -13,22 +13,24 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.android.settings.connecteddevice;
|
||||
package com.android.settings.connecteddevice.usb;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.VisibleForTesting;
|
||||
import android.support.v14.preference.PreferenceFragment;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.deviceinfo.UsbBackend;
|
||||
import com.android.settings.deviceinfo.UsbModeChooserActivity;
|
||||
import com.android.settings.SettingsActivity;
|
||||
import com.android.settings.connecteddevice.DevicePreferenceCallback;
|
||||
import com.android.settings.dashboard.DashboardFragment;
|
||||
import com.android.settings.widget.GearPreference;
|
||||
|
||||
/**
|
||||
* Controller to maintain connected usb device
|
||||
*/
|
||||
public class ConnectedUsbDeviceUpdater {
|
||||
private Context mContext;
|
||||
private PreferenceFragment mFragment;
|
||||
private UsbBackend mUsbBackend;
|
||||
private DevicePreferenceCallback mDevicePreferenceCallback;
|
||||
@VisibleForTesting
|
||||
@@ -36,8 +38,9 @@ public class ConnectedUsbDeviceUpdater {
|
||||
@VisibleForTesting
|
||||
UsbConnectionBroadcastReceiver mUsbReceiver;
|
||||
|
||||
private UsbConnectionBroadcastReceiver.UsbConnectionListener mUsbConnectionListener =
|
||||
(connected) -> {
|
||||
@VisibleForTesting
|
||||
UsbConnectionBroadcastReceiver.UsbConnectionListener mUsbConnectionListener =
|
||||
(connected, newMode) -> {
|
||||
if (connected) {
|
||||
mUsbPreference.setSummary(
|
||||
UsbModePreferenceController.getSummary(mUsbBackend.getCurrentMode()));
|
||||
@@ -47,18 +50,19 @@ public class ConnectedUsbDeviceUpdater {
|
||||
}
|
||||
};
|
||||
|
||||
public ConnectedUsbDeviceUpdater(Context context,
|
||||
public ConnectedUsbDeviceUpdater(DashboardFragment fragment,
|
||||
DevicePreferenceCallback devicePreferenceCallback) {
|
||||
this(context, devicePreferenceCallback, new UsbBackend(context));
|
||||
this(fragment, devicePreferenceCallback, new UsbBackend(fragment.getContext()));
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
ConnectedUsbDeviceUpdater(Context context, DevicePreferenceCallback devicePreferenceCallback,
|
||||
UsbBackend usbBackend) {
|
||||
mContext = context;
|
||||
ConnectedUsbDeviceUpdater(DashboardFragment fragment,
|
||||
DevicePreferenceCallback devicePreferenceCallback, UsbBackend usbBackend) {
|
||||
mFragment = fragment;
|
||||
mDevicePreferenceCallback = devicePreferenceCallback;
|
||||
mUsbBackend = usbBackend;
|
||||
mUsbReceiver = new UsbConnectionBroadcastReceiver(context, mUsbConnectionListener);
|
||||
mUsbReceiver = new UsbConnectionBroadcastReceiver(fragment.getContext(),
|
||||
mUsbConnectionListener, mUsbBackend);
|
||||
}
|
||||
|
||||
public void registerCallback() {
|
||||
@@ -76,8 +80,12 @@ public class ConnectedUsbDeviceUpdater {
|
||||
mUsbPreference.setIcon(R.drawable.ic_usb);
|
||||
mUsbPreference.setSelectable(false);
|
||||
mUsbPreference.setOnGearClickListener((GearPreference p) -> {
|
||||
final Intent intent = new Intent(mContext, UsbModeChooserActivity.class);
|
||||
mContext.startActivity(intent);
|
||||
// New version - uses a separate screen.
|
||||
final Bundle args = new Bundle();
|
||||
final SettingsActivity activity = (SettingsActivity) mFragment.getContext();
|
||||
activity.startPreferencePanel(mFragment,
|
||||
UsbDetailsFragment.class.getName(), args,
|
||||
R.string.device_details_title, null /* titleText */, null /* resultTo */, 0);
|
||||
});
|
||||
|
||||
forceUpdate();
|
||||
@@ -87,6 +95,5 @@ public class ConnectedUsbDeviceUpdater {
|
||||
// Register so we can get the connection state from sticky intent.
|
||||
//TODO(b/70336520): Use an API to get data instead of sticky intent
|
||||
mUsbReceiver.register();
|
||||
mUsbConnectionListener.onUsbConnectionChanged(mUsbReceiver.isConnected());
|
||||
}
|
||||
}
|
3
src/com/android/settings/connecteddevice/usb/OWNERS
Normal file
3
src/com/android/settings/connecteddevice/usb/OWNERS
Normal file
@@ -0,0 +1,3 @@
|
||||
# Default reviewers for this and subdirectories.
|
||||
zhangjerry@google.com
|
||||
badhri@google.com
|
247
src/com/android/settings/connecteddevice/usb/UsbBackend.java
Normal file
247
src/com/android/settings/connecteddevice/usb/UsbBackend.java
Normal file
@@ -0,0 +1,247 @@
|
||||
/*
|
||||
* Copyright (C) 2015 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.connecteddevice.usb;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.hardware.usb.UsbManager;
|
||||
import android.hardware.usb.UsbPort;
|
||||
import android.hardware.usb.UsbPortStatus;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.os.UserHandle;
|
||||
import android.os.UserManager;
|
||||
import android.support.annotation.VisibleForTesting;
|
||||
|
||||
public class UsbBackend {
|
||||
|
||||
public static final int MODE_POWER_MASK = 0x01;
|
||||
public static final int MODE_POWER_SINK = 0x00;
|
||||
public static final int MODE_POWER_SOURCE = 0x01;
|
||||
|
||||
public static final int MODE_DATA_MASK = 0x0f << 1;
|
||||
public static final int MODE_DATA_NONE = 0;
|
||||
public static final int MODE_DATA_MTP = 0x01 << 1;
|
||||
public static final int MODE_DATA_PTP = 0x01 << 2;
|
||||
public static final int MODE_DATA_MIDI = 0x01 << 3;
|
||||
public static final int MODE_DATA_TETHER = 0x01 << 4;
|
||||
|
||||
private final boolean mFileTransferRestricted;
|
||||
private final boolean mFileTransferRestrictedBySystem;
|
||||
private final boolean mTetheringRestricted;
|
||||
private final boolean mTetheringRestrictedBySystem;
|
||||
private final boolean mMidiSupported;
|
||||
private final boolean mTetheringSupported;
|
||||
|
||||
private UsbManager mUsbManager;
|
||||
@VisibleForTesting
|
||||
UsbManagerPassThrough mUsbManagerPassThrough;
|
||||
private UsbPort mPort;
|
||||
private UsbPortStatus mPortStatus;
|
||||
|
||||
private Context mContext;
|
||||
|
||||
public UsbBackend(Context context) {
|
||||
this(context, new UserRestrictionUtil(context), null);
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
public UsbBackend(Context context, UserRestrictionUtil userRestrictionUtil,
|
||||
UsbManagerPassThrough usbManagerPassThrough) {
|
||||
mContext = context;
|
||||
mUsbManager = context.getSystemService(UsbManager.class);
|
||||
|
||||
mUsbManagerPassThrough = usbManagerPassThrough;
|
||||
if (mUsbManagerPassThrough == null) {
|
||||
mUsbManagerPassThrough = new UsbManagerPassThrough(mUsbManager);
|
||||
}
|
||||
|
||||
mFileTransferRestricted = userRestrictionUtil.isUsbFileTransferRestricted();
|
||||
mFileTransferRestrictedBySystem = userRestrictionUtil.isUsbFileTransferRestrictedBySystem();
|
||||
mTetheringRestricted = userRestrictionUtil.isUsbTetheringRestricted();
|
||||
mTetheringRestrictedBySystem = userRestrictionUtil.isUsbTetheringRestrictedBySystem();
|
||||
|
||||
mMidiSupported = context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_MIDI);
|
||||
ConnectivityManager cm =
|
||||
(ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
mTetheringSupported = cm.isTetheringSupported();
|
||||
|
||||
UsbPort[] ports = mUsbManager.getPorts();
|
||||
if (ports == null) {
|
||||
return;
|
||||
}
|
||||
// For now look for a connected port, in the future we should identify port in the
|
||||
// notification and pick based on that.
|
||||
final int N = ports.length;
|
||||
for (int i = 0; i < N; i++) {
|
||||
UsbPortStatus status = mUsbManager.getPortStatus(ports[i]);
|
||||
if (status.isConnected()) {
|
||||
mPort = ports[i];
|
||||
mPortStatus = status;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public int getCurrentMode() {
|
||||
if (mPort != null) {
|
||||
int power = mPortStatus.getCurrentPowerRole() == UsbPort.POWER_ROLE_SOURCE
|
||||
&& mPortStatus.isConnected()
|
||||
? MODE_POWER_SOURCE : MODE_POWER_SINK;
|
||||
return power | getUsbDataMode();
|
||||
}
|
||||
return MODE_POWER_SINK | getUsbDataMode();
|
||||
}
|
||||
|
||||
public int getUsbDataMode() {
|
||||
long functions = mUsbManagerPassThrough.getCurrentFunctions();
|
||||
if (functions == UsbManager.FUNCTION_MTP) {
|
||||
return MODE_DATA_MTP;
|
||||
} else if (functions == UsbManager.FUNCTION_PTP) {
|
||||
return MODE_DATA_PTP;
|
||||
} else if (functions == UsbManager.FUNCTION_MIDI) {
|
||||
return MODE_DATA_MIDI;
|
||||
} else if (functions == UsbManager.FUNCTION_RNDIS) {
|
||||
return MODE_DATA_TETHER;
|
||||
}
|
||||
return MODE_DATA_NONE;
|
||||
}
|
||||
|
||||
private void setUsbFunction(int mode) {
|
||||
switch (mode) {
|
||||
case MODE_DATA_MTP:
|
||||
mUsbManager.setCurrentFunctions(UsbManager.FUNCTION_MTP);
|
||||
break;
|
||||
case MODE_DATA_PTP:
|
||||
mUsbManager.setCurrentFunctions(UsbManager.FUNCTION_PTP);
|
||||
break;
|
||||
case MODE_DATA_MIDI:
|
||||
mUsbManager.setCurrentFunctions(UsbManager.FUNCTION_MIDI);
|
||||
break;
|
||||
case MODE_DATA_TETHER:
|
||||
mUsbManager.setCurrentFunctions(UsbManager.FUNCTION_RNDIS);
|
||||
break;
|
||||
default:
|
||||
mUsbManager.setCurrentFunctions(UsbManager.FUNCTION_NONE);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public void setMode(int mode) {
|
||||
if (mPort != null) {
|
||||
int powerRole = modeToPower(mode);
|
||||
// If we aren't using any data modes and we support host mode, then go to host mode
|
||||
// so maybe? the other device can provide data if it wants, otherwise go into device
|
||||
// mode because we have no choice.
|
||||
int dataRole = (mode & MODE_DATA_MASK) == MODE_DATA_NONE
|
||||
&& mPortStatus.isRoleCombinationSupported(powerRole, UsbPort.DATA_ROLE_HOST)
|
||||
? UsbPort.DATA_ROLE_HOST : UsbPort.DATA_ROLE_DEVICE;
|
||||
mUsbManager.setPortRoles(mPort, powerRole, dataRole);
|
||||
}
|
||||
setUsbFunction(mode & MODE_DATA_MASK);
|
||||
}
|
||||
|
||||
private int modeToPower(int mode) {
|
||||
return (mode & MODE_POWER_MASK) == MODE_POWER_SOURCE
|
||||
? UsbPort.POWER_ROLE_SOURCE : UsbPort.POWER_ROLE_SINK;
|
||||
}
|
||||
|
||||
public boolean isModeDisallowed(int mode) {
|
||||
if (mFileTransferRestricted && ((mode & MODE_DATA_MASK) == MODE_DATA_MTP
|
||||
|| (mode & MODE_DATA_MASK) == MODE_DATA_PTP)) {
|
||||
return true;
|
||||
} else if (mTetheringRestricted && ((mode & MODE_DATA_MASK) == MODE_DATA_TETHER)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean isModeDisallowedBySystem(int mode) {
|
||||
if (mFileTransferRestrictedBySystem && ((mode & MODE_DATA_MASK) == MODE_DATA_MTP
|
||||
|| (mode & MODE_DATA_MASK) == MODE_DATA_PTP)) {
|
||||
return true;
|
||||
} else if (mTetheringRestrictedBySystem && ((mode & MODE_DATA_MASK) == MODE_DATA_TETHER)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean isModeSupported(int mode) {
|
||||
if (!mMidiSupported && (mode & MODE_DATA_MASK) == MODE_DATA_MIDI) {
|
||||
return false;
|
||||
}
|
||||
if (!mTetheringSupported && (mode & MODE_DATA_MASK) == MODE_DATA_TETHER) {
|
||||
return false;
|
||||
}
|
||||
if (mPort != null) {
|
||||
int power = modeToPower(mode);
|
||||
if ((mode & MODE_DATA_MASK) != 0) {
|
||||
// We have a port and data, need to be in device mode.
|
||||
return mPortStatus.isRoleCombinationSupported(power,
|
||||
UsbPort.DATA_ROLE_DEVICE);
|
||||
} else {
|
||||
// No data needed, we can do this power mode in either device or host.
|
||||
return mPortStatus.isRoleCombinationSupported(power, UsbPort.DATA_ROLE_DEVICE)
|
||||
|| mPortStatus.isRoleCombinationSupported(power, UsbPort.DATA_ROLE_HOST);
|
||||
}
|
||||
}
|
||||
// No port, support sink modes only.
|
||||
return (mode & MODE_POWER_MASK) != MODE_POWER_SOURCE;
|
||||
}
|
||||
|
||||
// Wrapper class to enable testing with UserManager APIs
|
||||
public static class UserRestrictionUtil {
|
||||
private UserManager mUserManager;
|
||||
|
||||
public UserRestrictionUtil(Context context) {
|
||||
mUserManager = UserManager.get(context);
|
||||
}
|
||||
|
||||
public boolean isUsbFileTransferRestricted() {
|
||||
return mUserManager.hasUserRestriction(UserManager.DISALLOW_USB_FILE_TRANSFER);
|
||||
}
|
||||
|
||||
public boolean isUsbTetheringRestricted() {
|
||||
return mUserManager.hasUserRestriction(UserManager.DISALLOW_CONFIG_TETHERING);
|
||||
}
|
||||
|
||||
public boolean isUsbFileTransferRestrictedBySystem() {
|
||||
return mUserManager.hasBaseUserRestriction(
|
||||
UserManager.DISALLOW_USB_FILE_TRANSFER, UserHandle.of(UserHandle.myUserId()));
|
||||
}
|
||||
|
||||
public boolean isUsbTetheringRestrictedBySystem() {
|
||||
return mUserManager.hasBaseUserRestriction(
|
||||
UserManager.DISALLOW_CONFIG_TETHERING, UserHandle.of(UserHandle.myUserId()));
|
||||
}
|
||||
}
|
||||
|
||||
// Temporary pass-through to allow roboelectric to use getCurrentFunctions()
|
||||
public static class UsbManagerPassThrough {
|
||||
private UsbManager mUsbManager;
|
||||
|
||||
public UsbManagerPassThrough(UsbManager manager) {
|
||||
mUsbManager = manager;
|
||||
}
|
||||
|
||||
public long getCurrentFunctions() {
|
||||
return mUsbManager.getCurrentFunctions();
|
||||
}
|
||||
|
||||
public long usbFunctionsFromString(String str) {
|
||||
return UsbManager.usbFunctionsFromString(str);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,129 @@
|
||||
/*
|
||||
* 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.connecteddevice.usb;
|
||||
|
||||
|
||||
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.hardware.usb.UsbManager;
|
||||
import android.hardware.usb.UsbPort;
|
||||
import android.hardware.usb.UsbPortStatus;
|
||||
|
||||
import com.android.settingslib.core.lifecycle.LifecycleObserver;
|
||||
import com.android.settingslib.core.lifecycle.events.OnResume;
|
||||
import com.android.settingslib.core.lifecycle.events.OnPause;
|
||||
|
||||
/**
|
||||
* Receiver to receive usb update and use {@link UsbConnectionListener} to invoke callback
|
||||
*/
|
||||
public class UsbConnectionBroadcastReceiver extends BroadcastReceiver implements LifecycleObserver,
|
||||
OnResume, OnPause {
|
||||
private Context mContext;
|
||||
private UsbConnectionListener mUsbConnectionListener;
|
||||
private boolean mListeningToUsbEvents;
|
||||
private int mMode;
|
||||
private boolean mConnected;
|
||||
private UsbBackend mUsbBackend;
|
||||
|
||||
public UsbConnectionBroadcastReceiver(Context context,
|
||||
UsbConnectionListener usbConnectionListener, UsbBackend backend) {
|
||||
mContext = context;
|
||||
mUsbConnectionListener = usbConnectionListener;
|
||||
mUsbBackend = backend;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
if (UsbManager.ACTION_USB_STATE.equals(intent.getAction())) {
|
||||
mConnected = intent.getExtras().getBoolean(UsbManager.USB_CONNECTED)
|
||||
|| intent.getExtras().getBoolean(UsbManager.USB_HOST_CONNECTED);
|
||||
if (mConnected) {
|
||||
mMode &= UsbBackend.MODE_POWER_MASK;
|
||||
if (intent.getExtras().getBoolean(UsbManager.USB_FUNCTION_MTP)
|
||||
&& intent.getExtras().getBoolean(UsbManager.USB_DATA_UNLOCKED, false)) {
|
||||
mMode |= UsbBackend.MODE_DATA_MTP;
|
||||
}
|
||||
if (intent.getExtras().getBoolean(UsbManager.USB_FUNCTION_PTP)
|
||||
&& intent.getExtras().getBoolean(UsbManager.USB_DATA_UNLOCKED, false)) {
|
||||
mMode |= UsbBackend.MODE_DATA_PTP;
|
||||
}
|
||||
if (intent.getExtras().getBoolean(UsbManager.USB_FUNCTION_MIDI)) {
|
||||
mMode |= UsbBackend.MODE_DATA_MIDI;
|
||||
}
|
||||
if (intent.getExtras().getBoolean(UsbManager.USB_FUNCTION_RNDIS)) {
|
||||
mMode |= UsbBackend.MODE_DATA_TETHER;
|
||||
}
|
||||
}
|
||||
} else if (UsbManager.ACTION_USB_PORT_CHANGED.equals(intent.getAction())) {
|
||||
mMode &= UsbBackend.MODE_DATA_MASK;
|
||||
UsbPortStatus portStatus = intent.getExtras()
|
||||
.getParcelable(UsbManager.EXTRA_PORT_STATUS);
|
||||
if (portStatus != null) {
|
||||
mConnected = portStatus.isConnected();
|
||||
if (mConnected) {
|
||||
mMode |= portStatus.getCurrentPowerRole() == UsbPort.POWER_ROLE_SOURCE
|
||||
? UsbBackend.MODE_POWER_SOURCE : UsbBackend.MODE_POWER_SINK;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (mUsbConnectionListener != null) {
|
||||
mUsbConnectionListener.onUsbConnectionChanged(mConnected, mMode);
|
||||
}
|
||||
}
|
||||
|
||||
public void register() {
|
||||
if (!mListeningToUsbEvents) {
|
||||
mMode = mUsbBackend.getCurrentMode();
|
||||
mConnected = false;
|
||||
final IntentFilter intentFilter = new IntentFilter();
|
||||
intentFilter.addAction(UsbManager.ACTION_USB_STATE);
|
||||
intentFilter.addAction(UsbManager.ACTION_USB_PORT_CHANGED);
|
||||
mContext.registerReceiver(this, intentFilter);
|
||||
mListeningToUsbEvents = true;
|
||||
}
|
||||
}
|
||||
|
||||
public void unregister() {
|
||||
if (mListeningToUsbEvents) {
|
||||
mContext.unregisterReceiver(this);
|
||||
mListeningToUsbEvents = false;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isConnected() {
|
||||
return mConnected;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
register();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPause() {
|
||||
unregister();
|
||||
}
|
||||
|
||||
/**
|
||||
* Interface definition for a callback to be invoked when usb connection is changed.
|
||||
*/
|
||||
interface UsbConnectionListener {
|
||||
void onUsbConnectionChanged(boolean connected, int newMode);
|
||||
}
|
||||
}
|
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Copyright (C) 2018 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.connecteddevice.usb;
|
||||
|
||||
import android.content.Context;
|
||||
import android.support.annotation.UiThread;
|
||||
import android.support.v14.preference.PreferenceFragment;
|
||||
|
||||
import com.android.settings.core.PreferenceControllerMixin;
|
||||
import com.android.settingslib.core.AbstractPreferenceController;
|
||||
|
||||
/**
|
||||
* This class provides common members and refresh functionality for usb controllers.
|
||||
*/
|
||||
public abstract class UsbDetailsController extends AbstractPreferenceController
|
||||
implements PreferenceControllerMixin {
|
||||
|
||||
protected final Context mContext;
|
||||
protected final PreferenceFragment mFragment;
|
||||
protected final UsbBackend mUsbBackend;
|
||||
|
||||
public UsbDetailsController(Context context, PreferenceFragment fragment, UsbBackend backend) {
|
||||
super(context);
|
||||
mContext = context;
|
||||
mFragment = fragment;
|
||||
mUsbBackend = backend;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAvailable() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method is called when the USB mode has changed and the controller needs to update.
|
||||
* @param newMode the new mode, made up of OR'd values from UsbBackend
|
||||
*/
|
||||
@UiThread
|
||||
protected abstract void refresh(int newMode);
|
||||
}
|
@@ -0,0 +1,131 @@
|
||||
/*
|
||||
* Copyright (C) 2018 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.connecteddevice.usb;
|
||||
|
||||
import android.content.Context;
|
||||
import android.hardware.usb.UsbManager;
|
||||
import android.os.Bundle;
|
||||
import android.provider.SearchIndexableResource;
|
||||
import android.support.annotation.VisibleForTesting;
|
||||
|
||||
import com.android.internal.logging.nano.MetricsProto;
|
||||
import com.android.settings.R;
|
||||
|
||||
import com.android.settings.dashboard.DashboardFragment;
|
||||
import com.android.settings.search.BaseSearchIndexProvider;
|
||||
import com.android.settings.search.Indexable;
|
||||
import com.android.settingslib.core.AbstractPreferenceController;
|
||||
|
||||
import com.google.android.collect.Lists;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Controls the USB device details and provides updates to individual controllers.
|
||||
*/
|
||||
public class UsbDetailsFragment extends DashboardFragment {
|
||||
private static final String TAG = UsbDetailsFragment.class.getSimpleName();
|
||||
|
||||
private List<UsbDetailsController> mControllers;
|
||||
private UsbBackend mUsbBackend;
|
||||
|
||||
@VisibleForTesting
|
||||
UsbConnectionBroadcastReceiver mUsbReceiver;
|
||||
|
||||
private UsbConnectionBroadcastReceiver.UsbConnectionListener mUsbConnectionListener =
|
||||
(connected, newMode) -> {
|
||||
if (!connected) {
|
||||
this.finish();
|
||||
} else {
|
||||
for (UsbDetailsController controller : mControllers) {
|
||||
controller.refresh(newMode);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@Override
|
||||
public int getMetricsCategory() {
|
||||
return MetricsProto.MetricsEvent.USB_DEVICE_DETAILS;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getLogTag() {
|
||||
return TAG;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int getPreferenceScreenResId() {
|
||||
return R.xml.usb_details_fragment;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
|
||||
super.onCreatePreferences(savedInstanceState, rootKey);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected List<AbstractPreferenceController> getPreferenceControllers(Context context) {
|
||||
mUsbBackend = new UsbBackend(context);
|
||||
mControllers = createControllerList(context, mUsbBackend, this);
|
||||
mUsbReceiver = new UsbConnectionBroadcastReceiver(context, mUsbConnectionListener,
|
||||
mUsbBackend);
|
||||
this.getLifecycle().addObserver(mUsbReceiver);
|
||||
|
||||
List<AbstractPreferenceController> ret = new ArrayList<>();
|
||||
ret.addAll(mControllers);
|
||||
return ret;
|
||||
}
|
||||
|
||||
private static List<UsbDetailsController> createControllerList(Context context,
|
||||
UsbBackend usbBackend, DashboardFragment fragment) {
|
||||
List<UsbDetailsController> ret = new ArrayList<>();
|
||||
ret.add(new UsbDetailsHeaderController(context, fragment, usbBackend));
|
||||
ret.add(new UsbDetailsProfilesController(context, fragment,
|
||||
usbBackend, Lists.newArrayList(UsbManager.USB_FUNCTION_MTP), "usb_main_options"));
|
||||
ret.add(new UsbDetailsProfilesController(context, fragment,
|
||||
usbBackend, Lists.newArrayList(UsbDetailsProfilesController.KEY_POWER,
|
||||
UsbManager.USB_FUNCTION_RNDIS, UsbManager.USB_FUNCTION_MIDI,
|
||||
UsbManager.USB_FUNCTION_PTP), "usb_secondary_options"));
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* For Search.
|
||||
*/
|
||||
public static final Indexable.SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
|
||||
new BaseSearchIndexProvider() {
|
||||
@Override
|
||||
public List<SearchIndexableResource> getXmlResourcesToIndex(
|
||||
Context context, boolean enabled) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getNonIndexableKeys(Context context) {
|
||||
return super.getNonIndexableKeys(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<AbstractPreferenceController> getPreferenceControllers(
|
||||
Context context) {
|
||||
List<AbstractPreferenceController> ret = new ArrayList<>();
|
||||
ret.addAll(createControllerList(context, new UsbBackend(context), null));
|
||||
return ret;
|
||||
}
|
||||
};
|
||||
}
|
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
* Copyright (C) 2018 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.connecteddevice.usb;
|
||||
|
||||
import android.content.Context;
|
||||
import android.support.v14.preference.PreferenceFragment;
|
||||
import android.support.v7.preference.PreferenceScreen;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.applications.LayoutPreference;
|
||||
import com.android.settings.widget.EntityHeaderController;
|
||||
|
||||
/**
|
||||
* This class adds a header with device name and current function.
|
||||
*/
|
||||
public class UsbDetailsHeaderController extends UsbDetailsController {
|
||||
private static final String KEY_DEVICE_HEADER = "usb_device_header";
|
||||
|
||||
private EntityHeaderController mHeaderController;
|
||||
|
||||
public UsbDetailsHeaderController(Context context, PreferenceFragment fragment,
|
||||
UsbBackend backend) {
|
||||
super(context, fragment, backend);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void displayPreference(PreferenceScreen screen) {
|
||||
super.displayPreference(screen);
|
||||
final LayoutPreference headerPreference =
|
||||
(LayoutPreference) screen.findPreference(KEY_DEVICE_HEADER);
|
||||
mHeaderController = EntityHeaderController.newInstance(mFragment.getActivity(), mFragment,
|
||||
headerPreference.findViewById(R.id.entity_header));
|
||||
screen.addPreference(headerPreference);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void refresh(int newMode) {
|
||||
mHeaderController.setLabel(mContext.getString(R.string.usb_pref));
|
||||
mHeaderController.setIcon(mContext.getDrawable(R.drawable.ic_usb));
|
||||
mHeaderController.setSummary(
|
||||
mContext.getString(UsbModePreferenceController.getSummary(newMode)));
|
||||
mHeaderController.done(mFragment.getActivity(), true /* rebindActions */);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPreferenceKey() {
|
||||
return KEY_DEVICE_HEADER;
|
||||
}
|
||||
}
|
@@ -0,0 +1,147 @@
|
||||
/*
|
||||
* Copyright (C) 2018 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.connecteddevice.usb;
|
||||
|
||||
import com.android.settings.R;
|
||||
import android.content.Context;
|
||||
import android.hardware.usb.UsbManager;
|
||||
import android.support.v14.preference.PreferenceFragment;
|
||||
import android.support.v14.preference.SwitchPreference;
|
||||
import android.support.v7.preference.Preference;
|
||||
import android.support.v7.preference.PreferenceCategory;
|
||||
import android.support.v7.preference.PreferenceScreen;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* This class adds switches for toggling individual USB options, such as "transfer files",
|
||||
* "supply power", "usb tethering", etc.
|
||||
*/
|
||||
public class UsbDetailsProfilesController extends UsbDetailsController
|
||||
implements Preference.OnPreferenceClickListener {
|
||||
|
||||
static final String KEY_POWER = "power";
|
||||
|
||||
private PreferenceCategory mProfilesContainer;
|
||||
private List<String> mOptions;
|
||||
private String mKey;
|
||||
|
||||
public UsbDetailsProfilesController(Context context, PreferenceFragment fragment,
|
||||
UsbBackend backend, List<String> options, String key) {
|
||||
super(context, fragment, backend);
|
||||
mOptions = options;
|
||||
mKey = key;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void displayPreference(PreferenceScreen screen) {
|
||||
super.displayPreference(screen);
|
||||
mProfilesContainer = (PreferenceCategory) screen.findPreference(getPreferenceKey());
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a switch preference for the particular option, creating it if needed.
|
||||
*/
|
||||
private SwitchPreference getProfilePreference(String key, int titleId) {
|
||||
SwitchPreference pref = (SwitchPreference) mProfilesContainer.findPreference(key);
|
||||
if (pref == null) {
|
||||
pref = new SwitchPreference(mProfilesContainer.getContext());
|
||||
pref.setKey(key);
|
||||
pref.setTitle(titleId);
|
||||
pref.setOnPreferenceClickListener(this);
|
||||
mProfilesContainer.addPreference(pref);
|
||||
}
|
||||
return pref;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void refresh(int mode) {
|
||||
SwitchPreference pref;
|
||||
for (String option : mOptions) {
|
||||
int newMode;
|
||||
int summary = -1;
|
||||
int title;
|
||||
if (option.equals(UsbManager.USB_FUNCTION_MTP)) {
|
||||
newMode = UsbBackend.MODE_DATA_MTP;
|
||||
title = R.string.usb_use_file_transfers;
|
||||
} else if (option.equals(KEY_POWER)) {
|
||||
newMode = UsbBackend.MODE_POWER_SOURCE;
|
||||
title = R.string.usb_use_power_only;
|
||||
summary = R.string.usb_use_power_only_desc;
|
||||
} else if (option.equals(UsbManager.USB_FUNCTION_PTP)) {
|
||||
newMode = UsbBackend.MODE_DATA_PTP;
|
||||
title = R.string.usb_use_photo_transfers;
|
||||
} else if (option.equals(UsbManager.USB_FUNCTION_MIDI)) {
|
||||
newMode = UsbBackend.MODE_DATA_MIDI;
|
||||
title = R.string.usb_use_MIDI;
|
||||
} else if (option.equals(UsbManager.USB_FUNCTION_RNDIS)) {
|
||||
newMode = UsbBackend.MODE_DATA_TETHER;
|
||||
title = R.string.usb_use_tethering;
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
|
||||
pref = getProfilePreference(option, title);
|
||||
// Only show supported and allowed options
|
||||
if (mUsbBackend.isModeSupported(newMode)
|
||||
&& !mUsbBackend.isModeDisallowedBySystem(newMode)
|
||||
&& !mUsbBackend.isModeDisallowed(newMode)) {
|
||||
if (summary != -1) {
|
||||
pref.setSummary(summary);
|
||||
}
|
||||
pref.setChecked((mode & newMode) != 0);
|
||||
} else {
|
||||
mProfilesContainer.removePreference(pref);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onPreferenceClick(Preference preference) {
|
||||
SwitchPreference profilePref = (SwitchPreference) preference;
|
||||
String key = profilePref.getKey();
|
||||
int mode = mUsbBackend.getCurrentMode();
|
||||
int thisMode = 0;
|
||||
if (key.equals(KEY_POWER)) {
|
||||
thisMode = UsbBackend.MODE_POWER_SOURCE;
|
||||
} else if (key.equals(UsbManager.USB_FUNCTION_MTP)) {
|
||||
thisMode = UsbBackend.MODE_DATA_MTP;
|
||||
} else if (key.equals(UsbManager.USB_FUNCTION_PTP)) {
|
||||
thisMode = UsbBackend.MODE_DATA_PTP;
|
||||
} else if (key.equals(UsbManager.USB_FUNCTION_RNDIS)) {
|
||||
thisMode = UsbBackend.MODE_DATA_TETHER;
|
||||
} else if (key.equals(UsbManager.USB_FUNCTION_MIDI)) {
|
||||
thisMode = UsbBackend.MODE_DATA_MIDI;
|
||||
}
|
||||
if (profilePref.isChecked()) {
|
||||
if (!key.equals(KEY_POWER)) {
|
||||
// Only one non power mode can currently be set at once.
|
||||
mode &= UsbBackend.MODE_POWER_MASK;
|
||||
}
|
||||
mode |= thisMode;
|
||||
} else {
|
||||
mode &= ~thisMode;
|
||||
}
|
||||
mUsbBackend.setMode(mode);
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPreferenceKey() {
|
||||
return mKey;
|
||||
}
|
||||
}
|
@@ -0,0 +1,204 @@
|
||||
/*
|
||||
* Copyright (C) 2015 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.connecteddevice.usb;
|
||||
|
||||
import android.annotation.Nullable;
|
||||
import android.app.Activity;
|
||||
import android.app.ActivityManager;
|
||||
import android.app.AlertDialog;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.graphics.PorterDuff;
|
||||
import android.hardware.usb.UsbManager;
|
||||
import android.os.Bundle;
|
||||
import android.os.UserHandle;
|
||||
import android.os.UserManager;
|
||||
import android.support.annotation.VisibleForTesting;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.View.OnClickListener;
|
||||
import android.widget.Checkable;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settingslib.RestrictedLockUtils;
|
||||
|
||||
import static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
|
||||
|
||||
/**
|
||||
* UI for the USB chooser dialog.
|
||||
*
|
||||
*/
|
||||
public class UsbModeChooserActivity extends Activity {
|
||||
|
||||
public static final int[] DEFAULT_MODES = {
|
||||
UsbBackend.MODE_POWER_SINK | UsbBackend.MODE_DATA_NONE,
|
||||
UsbBackend.MODE_POWER_SOURCE | UsbBackend.MODE_DATA_NONE,
|
||||
UsbBackend.MODE_POWER_SINK | UsbBackend.MODE_DATA_MTP,
|
||||
UsbBackend.MODE_POWER_SINK | UsbBackend.MODE_DATA_PTP,
|
||||
UsbBackend.MODE_POWER_SINK | UsbBackend.MODE_DATA_MIDI
|
||||
};
|
||||
|
||||
private UsbBackend mBackend;
|
||||
private AlertDialog mDialog;
|
||||
private LayoutInflater mLayoutInflater;
|
||||
private EnforcedAdmin mEnforcedAdmin;
|
||||
|
||||
private BroadcastReceiver mDisconnectedReceiver = new BroadcastReceiver() {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
String action = intent.getAction();
|
||||
if (UsbManager.ACTION_USB_STATE.equals(action)) {
|
||||
boolean connected = intent.getBooleanExtra(UsbManager.USB_CONNECTED, false);
|
||||
boolean hostConnected =
|
||||
intent.getBooleanExtra(UsbManager.USB_HOST_CONNECTED, false);
|
||||
if (!connected && !hostConnected) {
|
||||
mDialog.dismiss();
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@Override
|
||||
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
||||
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
mLayoutInflater = LayoutInflater.from(this);
|
||||
|
||||
mDialog = new AlertDialog.Builder(this)
|
||||
.setTitle(R.string.usb_use)
|
||||
.setView(R.layout.usb_dialog_container)
|
||||
.setOnDismissListener(new DialogInterface.OnDismissListener() {
|
||||
@Override
|
||||
public void onDismiss(DialogInterface dialog) {
|
||||
finish();
|
||||
}
|
||||
})
|
||||
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
finish();
|
||||
}
|
||||
}).create();
|
||||
mDialog.show();
|
||||
|
||||
LinearLayout container = (LinearLayout) mDialog.findViewById(R.id.container);
|
||||
|
||||
mEnforcedAdmin = RestrictedLockUtils.checkIfRestrictionEnforced(this,
|
||||
UserManager.DISALLOW_USB_FILE_TRANSFER, UserHandle.myUserId());
|
||||
mBackend = new UsbBackend(this);
|
||||
int current = mBackend.getCurrentMode();
|
||||
for (int i = 0; i < DEFAULT_MODES.length; i++) {
|
||||
if (mBackend.isModeSupported(DEFAULT_MODES[i])
|
||||
&& !mBackend.isModeDisallowedBySystem(DEFAULT_MODES[i])) {
|
||||
inflateOption(DEFAULT_MODES[i], current == DEFAULT_MODES[i], container,
|
||||
mBackend.isModeDisallowed(DEFAULT_MODES[i]));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStart() {
|
||||
super.onStart();
|
||||
|
||||
IntentFilter filter = new IntentFilter(UsbManager.ACTION_USB_STATE);
|
||||
registerReceiver(mDisconnectedReceiver, filter);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onStop() {
|
||||
unregisterReceiver(mDisconnectedReceiver);
|
||||
super.onStop();
|
||||
}
|
||||
|
||||
private void inflateOption(final int mode, boolean selected, LinearLayout container,
|
||||
final boolean disallowedByAdmin) {
|
||||
View v = mLayoutInflater.inflate(R.layout.restricted_radio_with_summary, container, false);
|
||||
|
||||
TextView titleView = (TextView) v.findViewById(android.R.id.title);
|
||||
titleView.setText(getTitle(mode));
|
||||
TextView summaryView = (TextView) v.findViewById(android.R.id.summary);
|
||||
updateSummary(summaryView, mode);
|
||||
|
||||
if (disallowedByAdmin) {
|
||||
if (mEnforcedAdmin != null) {
|
||||
setDisabledByAdmin(v, titleView, summaryView);
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
v.setOnClickListener(new OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
if (disallowedByAdmin && mEnforcedAdmin != null) {
|
||||
RestrictedLockUtils.sendShowAdminSupportDetailsIntent(
|
||||
UsbModeChooserActivity.this, mEnforcedAdmin);
|
||||
return;
|
||||
}
|
||||
if (!ActivityManager.isUserAMonkey()) {
|
||||
mBackend.setMode(mode);
|
||||
}
|
||||
mDialog.dismiss();
|
||||
finish();
|
||||
}
|
||||
});
|
||||
((Checkable) v).setChecked(selected);
|
||||
container.addView(v);
|
||||
}
|
||||
|
||||
private void setDisabledByAdmin(View rootView, TextView titleView, TextView summaryView) {
|
||||
if (mEnforcedAdmin != null) {
|
||||
titleView.setEnabled(false);
|
||||
summaryView.setEnabled(false);
|
||||
rootView.findViewById(R.id.restricted_icon).setVisibility(View.VISIBLE);
|
||||
Drawable[] compoundDrawables = titleView.getCompoundDrawablesRelative();
|
||||
compoundDrawables[0 /* start */].mutate().setColorFilter(
|
||||
getColor(R.color.disabled_text_color), PorterDuff.Mode.MULTIPLY);
|
||||
}
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
static void updateSummary(TextView summaryView, int mode) {
|
||||
if (mode == (UsbBackend.MODE_POWER_SOURCE | UsbBackend.MODE_DATA_NONE)) {
|
||||
summaryView.setText(R.string.usb_use_power_only_desc);
|
||||
}
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
static int getTitle(int mode) {
|
||||
switch (mode) {
|
||||
case UsbBackend.MODE_POWER_SINK | UsbBackend.MODE_DATA_NONE:
|
||||
return R.string.usb_use_charging_only;
|
||||
case UsbBackend.MODE_POWER_SOURCE | UsbBackend.MODE_DATA_NONE:
|
||||
return R.string.usb_use_power_only;
|
||||
case UsbBackend.MODE_POWER_SINK | UsbBackend.MODE_DATA_MTP:
|
||||
return R.string.usb_use_file_transfers;
|
||||
case UsbBackend.MODE_POWER_SINK | UsbBackend.MODE_DATA_PTP:
|
||||
return R.string.usb_use_photo_transfers;
|
||||
case UsbBackend.MODE_POWER_SINK | UsbBackend.MODE_DATA_MIDI:
|
||||
return R.string.usb_use_MIDI;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
@@ -13,15 +13,15 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.android.settings.connecteddevice;
|
||||
package com.android.settings.connecteddevice.usb;
|
||||
|
||||
import android.content.Context;
|
||||
import android.support.v7.preference.Preference;
|
||||
import android.support.v7.preference.PreferenceScreen;
|
||||
|
||||
import com.android.internal.annotations.VisibleForTesting;
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.core.PreferenceControllerMixin;
|
||||
import com.android.settings.deviceinfo.UsbBackend;
|
||||
import com.android.settingslib.core.AbstractPreferenceController;
|
||||
import com.android.settingslib.core.lifecycle.LifecycleObserver;
|
||||
import com.android.settingslib.core.lifecycle.events.OnPause;
|
||||
@@ -33,27 +33,27 @@ public class UsbModePreferenceController extends AbstractPreferenceController
|
||||
private static final String KEY_USB_MODE = "usb_mode";
|
||||
|
||||
private UsbBackend mUsbBackend;
|
||||
private UsbConnectionBroadcastReceiver mUsbReceiver;
|
||||
@VisibleForTesting
|
||||
UsbConnectionBroadcastReceiver mUsbReceiver;
|
||||
private Preference mUsbPreference;
|
||||
|
||||
public UsbModePreferenceController(Context context, UsbBackend usbBackend) {
|
||||
super(context);
|
||||
mUsbBackend = usbBackend;
|
||||
mUsbReceiver = new UsbConnectionBroadcastReceiver(mContext, (connected) -> {
|
||||
updateSummary(mUsbPreference);
|
||||
});
|
||||
mUsbReceiver = new UsbConnectionBroadcastReceiver(mContext, (connected, newMode) -> {
|
||||
updateSummary(mUsbPreference, connected, newMode);
|
||||
}, mUsbBackend);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void displayPreference(PreferenceScreen screen) {
|
||||
super.displayPreference(screen);
|
||||
mUsbPreference = screen.findPreference(KEY_USB_MODE);
|
||||
updateSummary(mUsbPreference);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateState(Preference preference) {
|
||||
updateSummary(preference);
|
||||
updateSummary(preference, mUsbReceiver.isConnected(), mUsbBackend.getCurrentMode());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -88,17 +88,24 @@ public class UsbModePreferenceController extends AbstractPreferenceController
|
||||
return R.string.usb_summary_photo_transfers;
|
||||
case UsbBackend.MODE_POWER_SINK | UsbBackend.MODE_DATA_MIDI:
|
||||
return R.string.usb_summary_MIDI;
|
||||
case UsbBackend.MODE_POWER_SINK | UsbBackend.MODE_DATA_TETHER:
|
||||
return R.string.usb_summary_tether;
|
||||
case UsbBackend.MODE_POWER_SOURCE | UsbBackend.MODE_DATA_MTP:
|
||||
return R.string.usb_summary_file_transfers_power;
|
||||
case UsbBackend.MODE_POWER_SOURCE | UsbBackend.MODE_DATA_PTP:
|
||||
return R.string.usb_summary_photo_transfers_power;
|
||||
case UsbBackend.MODE_POWER_SOURCE | UsbBackend.MODE_DATA_MIDI:
|
||||
return R.string.usb_summary_MIDI_power;
|
||||
case UsbBackend.MODE_POWER_SOURCE | UsbBackend.MODE_DATA_TETHER:
|
||||
return R.string.usb_summary_tether_power;
|
||||
default:
|
||||
return R.string.usb_summary_charging_only;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
private void updateSummary(Preference preference) {
|
||||
updateSummary(preference, mUsbBackend.getCurrentMode());
|
||||
}
|
||||
|
||||
private void updateSummary(Preference preference, int mode) {
|
||||
private void updateSummary(Preference preference, boolean connected, int mode) {
|
||||
if (preference != null) {
|
||||
if (mUsbReceiver.isConnected()) {
|
||||
if (connected) {
|
||||
preference.setEnabled(true);
|
||||
preference.setSummary(getSummary(mode));
|
||||
} else {
|
||||
@@ -107,5 +114,4 @@ public class UsbModePreferenceController extends AbstractPreferenceController
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user