Merge "Use new UsbManager#getPorts API"
This commit is contained in:
committed by
Android (Google) Code Review
commit
adc1fbc37a
@@ -15,9 +15,12 @@
|
||||
*/
|
||||
package com.android.settings.connecteddevice.usb;
|
||||
|
||||
import static android.hardware.usb.UsbPortStatus.DATA_ROLE_DEVICE;
|
||||
import static android.hardware.usb.UsbPortStatus.POWER_ROLE_SINK;
|
||||
import static android.hardware.usb.UsbPortStatus.POWER_ROLE_SOURCE;
|
||||
|
||||
import android.content.Context;
|
||||
import android.hardware.usb.UsbManager;
|
||||
import android.hardware.usb.UsbPort;
|
||||
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
import androidx.preference.Preference;
|
||||
@@ -43,7 +46,7 @@ public class ConnectedUsbDeviceUpdater {
|
||||
UsbConnectionBroadcastReceiver.UsbConnectionListener mUsbConnectionListener =
|
||||
(connected, functions, powerRole, dataRole) -> {
|
||||
if (connected) {
|
||||
mUsbPreference.setSummary(getSummary(dataRole == UsbPort.DATA_ROLE_DEVICE
|
||||
mUsbPreference.setSummary(getSummary(dataRole == DATA_ROLE_DEVICE
|
||||
? functions : UsbManager.FUNCTION_NONE, powerRole));
|
||||
mDevicePreferenceCallback.onDeviceAdded(mUsbPreference);
|
||||
} else {
|
||||
@@ -100,7 +103,7 @@ public class ConnectedUsbDeviceUpdater {
|
||||
|
||||
public static int getSummary(long functions, int power) {
|
||||
switch (power) {
|
||||
case UsbPort.POWER_ROLE_SINK:
|
||||
case POWER_ROLE_SINK:
|
||||
if (functions == UsbManager.FUNCTION_MTP) {
|
||||
return R.string.usb_summary_file_transfers;
|
||||
} else if (functions == UsbManager.FUNCTION_RNDIS) {
|
||||
@@ -112,7 +115,7 @@ public class ConnectedUsbDeviceUpdater {
|
||||
} else {
|
||||
return R.string.usb_summary_charging_only;
|
||||
}
|
||||
case UsbPort.POWER_ROLE_SOURCE:
|
||||
case POWER_ROLE_SOURCE:
|
||||
if (functions == UsbManager.FUNCTION_MTP) {
|
||||
return R.string.usb_summary_file_transfers_power;
|
||||
} else if (functions == UsbManager.FUNCTION_RNDIS) {
|
||||
|
@@ -15,6 +15,13 @@
|
||||
*/
|
||||
package com.android.settings.connecteddevice.usb;
|
||||
|
||||
import static android.hardware.usb.UsbPortStatus.DATA_ROLE_DEVICE;
|
||||
import static android.hardware.usb.UsbPortStatus.POWER_ROLE_NONE;
|
||||
import static android.hardware.usb.UsbPortStatus.POWER_ROLE_SOURCE;
|
||||
import static android.service.usb.UsbPortStatusProto.DATA_ROLE_HOST;
|
||||
import static android.service.usb.UsbPortStatusProto.DATA_ROLE_NONE;
|
||||
import static android.service.usb.UsbPortStatusProto.POWER_ROLE_SINK;
|
||||
|
||||
import android.annotation.Nullable;
|
||||
import android.content.Context;
|
||||
import android.content.pm.PackageManager;
|
||||
@@ -27,6 +34,8 @@ import android.os.UserManager;
|
||||
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Provides access to underlying system USB functionality.
|
||||
*/
|
||||
@@ -96,30 +105,30 @@ public class UsbBackend {
|
||||
|
||||
public int getPowerRole() {
|
||||
updatePorts();
|
||||
return mPortStatus == null ? UsbPort.POWER_ROLE_NONE : mPortStatus.getCurrentPowerRole();
|
||||
return mPortStatus == null ? POWER_ROLE_NONE : mPortStatus.getCurrentPowerRole();
|
||||
}
|
||||
|
||||
public int getDataRole() {
|
||||
updatePorts();
|
||||
return mPortStatus == null ? UsbPort.DATA_ROLE_NONE : mPortStatus.getCurrentDataRole();
|
||||
return mPortStatus == null ? DATA_ROLE_NONE : mPortStatus.getCurrentDataRole();
|
||||
}
|
||||
|
||||
public void setPowerRole(int role) {
|
||||
int newDataRole = getDataRole();
|
||||
if (!areAllRolesSupported()) {
|
||||
switch (role) {
|
||||
case UsbPort.POWER_ROLE_SINK:
|
||||
newDataRole = UsbPort.DATA_ROLE_DEVICE;
|
||||
case POWER_ROLE_SINK:
|
||||
newDataRole = DATA_ROLE_DEVICE;
|
||||
break;
|
||||
case UsbPort.POWER_ROLE_SOURCE:
|
||||
newDataRole = UsbPort.DATA_ROLE_HOST;
|
||||
case POWER_ROLE_SOURCE:
|
||||
newDataRole = DATA_ROLE_HOST;
|
||||
break;
|
||||
default:
|
||||
newDataRole = UsbPort.DATA_ROLE_NONE;
|
||||
newDataRole = DATA_ROLE_NONE;
|
||||
}
|
||||
}
|
||||
if (mPort != null) {
|
||||
mUsbManager.setPortRoles(mPort, role, newDataRole);
|
||||
mPort.setRoles(role, newDataRole);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -127,31 +136,27 @@ public class UsbBackend {
|
||||
int newPowerRole = getPowerRole();
|
||||
if (!areAllRolesSupported()) {
|
||||
switch (role) {
|
||||
case UsbPort.DATA_ROLE_DEVICE:
|
||||
newPowerRole = UsbPort.POWER_ROLE_SINK;
|
||||
case DATA_ROLE_DEVICE:
|
||||
newPowerRole = POWER_ROLE_SINK;
|
||||
break;
|
||||
case UsbPort.DATA_ROLE_HOST:
|
||||
newPowerRole = UsbPort.POWER_ROLE_SOURCE;
|
||||
case DATA_ROLE_HOST:
|
||||
newPowerRole = POWER_ROLE_SOURCE;
|
||||
break;
|
||||
default:
|
||||
newPowerRole = UsbPort.POWER_ROLE_NONE;
|
||||
newPowerRole = POWER_ROLE_NONE;
|
||||
}
|
||||
}
|
||||
if (mPort != null) {
|
||||
mUsbManager.setPortRoles(mPort, newPowerRole, role);
|
||||
mPort.setRoles(newPowerRole, role);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean areAllRolesSupported() {
|
||||
return mPort != null && mPortStatus != null
|
||||
&& mPortStatus
|
||||
.isRoleCombinationSupported(UsbPort.POWER_ROLE_SINK, UsbPort.DATA_ROLE_DEVICE)
|
||||
&& mPortStatus
|
||||
.isRoleCombinationSupported(UsbPort.POWER_ROLE_SINK, UsbPort.DATA_ROLE_HOST)
|
||||
&& mPortStatus
|
||||
.isRoleCombinationSupported(UsbPort.POWER_ROLE_SOURCE, UsbPort.DATA_ROLE_DEVICE)
|
||||
&& mPortStatus
|
||||
.isRoleCombinationSupported(UsbPort.POWER_ROLE_SOURCE, UsbPort.DATA_ROLE_HOST);
|
||||
&& mPortStatus.isRoleCombinationSupported(POWER_ROLE_SINK, DATA_ROLE_DEVICE)
|
||||
&& mPortStatus.isRoleCombinationSupported(POWER_ROLE_SINK, DATA_ROLE_HOST)
|
||||
&& mPortStatus.isRoleCombinationSupported(POWER_ROLE_SOURCE, DATA_ROLE_DEVICE)
|
||||
&& mPortStatus.isRoleCombinationSupported(POWER_ROLE_SOURCE, DATA_ROLE_HOST);
|
||||
}
|
||||
|
||||
public static String usbFunctionsToString(long functions) {
|
||||
@@ -205,17 +210,14 @@ public class UsbBackend {
|
||||
private void updatePorts() {
|
||||
mPort = null;
|
||||
mPortStatus = null;
|
||||
UsbPort[] ports = mUsbManager.getPorts();
|
||||
if (ports == null) {
|
||||
return;
|
||||
}
|
||||
List<UsbPort> ports = mUsbManager.getPorts();
|
||||
// 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;
|
||||
final int N = ports.size();
|
||||
for (int i = 0; i < N; i++) {
|
||||
UsbPortStatus status = mUsbManager.getPortStatus(ports[i]);
|
||||
UsbPortStatus status = ports.get(i).getStatus();
|
||||
if (status.isConnected()) {
|
||||
mPort = ports[i];
|
||||
mPort = ports.get(i);
|
||||
mPortStatus = status;
|
||||
break;
|
||||
}
|
||||
|
@@ -49,8 +49,8 @@ public class UsbConnectionBroadcastReceiver extends BroadcastReceiver implements
|
||||
mUsbBackend = backend;
|
||||
|
||||
mFunctions = UsbManager.FUNCTION_NONE;
|
||||
mDataRole = UsbPort.DATA_ROLE_NONE;
|
||||
mPowerRole = UsbPort.POWER_ROLE_NONE;
|
||||
mDataRole = UsbPortStatus.DATA_ROLE_NONE;
|
||||
mPowerRole = UsbPortStatus.POWER_ROLE_NONE;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -16,6 +16,10 @@
|
||||
|
||||
package com.android.settings.connecteddevice.usb;
|
||||
|
||||
import static android.hardware.usb.UsbPortStatus.DATA_ROLE_DEVICE;
|
||||
import static android.hardware.usb.UsbPortStatus.DATA_ROLE_HOST;
|
||||
import static android.hardware.usb.UsbPortStatus.DATA_ROLE_NONE;
|
||||
|
||||
import android.content.Context;
|
||||
import android.hardware.usb.UsbPort;
|
||||
|
||||
@@ -55,23 +59,23 @@ public class UsbDetailsDataRoleController extends UsbDetailsController
|
||||
public void displayPreference(PreferenceScreen screen) {
|
||||
super.displayPreference(screen);
|
||||
mPreferenceCategory = (PreferenceCategory) screen.findPreference(getPreferenceKey());
|
||||
mHostPref = makeRadioPreference(UsbBackend.dataRoleToString(UsbPort.DATA_ROLE_HOST),
|
||||
mHostPref = makeRadioPreference(UsbBackend.dataRoleToString(DATA_ROLE_HOST),
|
||||
R.string.usb_control_host);
|
||||
mDevicePref = makeRadioPreference(UsbBackend.dataRoleToString(UsbPort.DATA_ROLE_DEVICE),
|
||||
mDevicePref = makeRadioPreference(UsbBackend.dataRoleToString(DATA_ROLE_DEVICE),
|
||||
R.string.usb_control_device);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void refresh(boolean connected, long functions, int powerRole, int dataRole) {
|
||||
if (dataRole == UsbPort.DATA_ROLE_DEVICE) {
|
||||
if (dataRole == DATA_ROLE_DEVICE) {
|
||||
mDevicePref.setChecked(true);
|
||||
mHostPref.setChecked(false);
|
||||
mPreferenceCategory.setEnabled(true);
|
||||
} else if (dataRole == UsbPort.DATA_ROLE_HOST) {
|
||||
} else if (dataRole == DATA_ROLE_HOST) {
|
||||
mDevicePref.setChecked(false);
|
||||
mHostPref.setChecked(true);
|
||||
mPreferenceCategory.setEnabled(true);
|
||||
} else if (!connected || dataRole == UsbPort.DATA_ROLE_NONE){
|
||||
} else if (!connected || dataRole == DATA_ROLE_NONE){
|
||||
mPreferenceCategory.setEnabled(false);
|
||||
if (mNextRolePref == null) {
|
||||
// Disconnected with no operation pending, so clear subtexts
|
||||
@@ -80,7 +84,7 @@ public class UsbDetailsDataRoleController extends UsbDetailsController
|
||||
}
|
||||
}
|
||||
|
||||
if (mNextRolePref != null && dataRole != UsbPort.DATA_ROLE_NONE) {
|
||||
if (mNextRolePref != null && dataRole != DATA_ROLE_NONE) {
|
||||
if (UsbBackend.dataRoleFromString(mNextRolePref.getKey()) == dataRole) {
|
||||
// Clear switching text if switch succeeded
|
||||
mNextRolePref.setSummary("");
|
||||
|
@@ -16,6 +16,7 @@
|
||||
|
||||
package com.android.settings.connecteddevice.usb;
|
||||
|
||||
import static android.hardware.usb.UsbPortStatus.DATA_ROLE_DEVICE;
|
||||
import static android.net.ConnectivityManager.TETHERING_USB;
|
||||
|
||||
import android.content.Context;
|
||||
@@ -88,7 +89,7 @@ public class UsbDetailsFunctionsController extends UsbDetailsController
|
||||
|
||||
@Override
|
||||
protected void refresh(boolean connected, long functions, int powerRole, int dataRole) {
|
||||
if (!connected || dataRole != UsbPort.DATA_ROLE_DEVICE) {
|
||||
if (!connected || dataRole != DATA_ROLE_DEVICE) {
|
||||
mProfilesContainer.setEnabled(false);
|
||||
} else {
|
||||
// Functions are only available in device mode
|
||||
|
@@ -16,8 +16,13 @@
|
||||
|
||||
package com.android.settings.connecteddevice.usb;
|
||||
|
||||
import static android.hardware.usb.UsbPortStatus.POWER_ROLE_NONE;
|
||||
import static android.hardware.usb.UsbPortStatus.POWER_ROLE_SINK;
|
||||
import static android.hardware.usb.UsbPortStatus.POWER_ROLE_SOURCE;
|
||||
|
||||
import android.content.Context;
|
||||
import android.hardware.usb.UsbPort;
|
||||
import android.hardware.usb.UsbPortStatus;
|
||||
|
||||
import androidx.preference.Preference;
|
||||
import androidx.preference.Preference.OnPreferenceClickListener;
|
||||
@@ -40,16 +45,16 @@ public class UsbDetailsPowerRoleController extends UsbDetailsController
|
||||
private int mNextPowerRole;
|
||||
|
||||
private final Runnable mFailureCallback = () -> {
|
||||
if (mNextPowerRole != UsbPort.POWER_ROLE_NONE) {
|
||||
if (mNextPowerRole != POWER_ROLE_NONE) {
|
||||
mSwitchPreference.setSummary(R.string.usb_switching_failed);
|
||||
mNextPowerRole = UsbPort.POWER_ROLE_NONE;
|
||||
mNextPowerRole = POWER_ROLE_NONE;
|
||||
}
|
||||
};
|
||||
|
||||
public UsbDetailsPowerRoleController(Context context, UsbDetailsFragment fragment,
|
||||
UsbBackend backend) {
|
||||
super(context, fragment, backend);
|
||||
mNextPowerRole = UsbPort.POWER_ROLE_NONE;
|
||||
mNextPowerRole = POWER_ROLE_NONE;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -70,20 +75,21 @@ public class UsbDetailsPowerRoleController extends UsbDetailsController
|
||||
} else if (connected && mUsbBackend.areAllRolesSupported()){
|
||||
mFragment.getPreferenceScreen().addPreference(mPreferenceCategory);
|
||||
}
|
||||
if (powerRole == UsbPort.POWER_ROLE_SOURCE) {
|
||||
if (powerRole == POWER_ROLE_SOURCE) {
|
||||
mSwitchPreference.setChecked(true);
|
||||
mPreferenceCategory.setEnabled(true);
|
||||
} else if (powerRole == UsbPort.POWER_ROLE_SINK) {
|
||||
} else if (powerRole == POWER_ROLE_SINK) {
|
||||
mSwitchPreference.setChecked(false);
|
||||
mPreferenceCategory.setEnabled(true);
|
||||
} else if (!connected || powerRole == UsbPort.POWER_ROLE_NONE){
|
||||
} else if (!connected || powerRole == POWER_ROLE_NONE){
|
||||
mPreferenceCategory.setEnabled(false);
|
||||
if (mNextPowerRole == UsbPort.POWER_ROLE_NONE) {
|
||||
if (mNextPowerRole == POWER_ROLE_NONE) {
|
||||
mSwitchPreference.setSummary("");
|
||||
}
|
||||
}
|
||||
|
||||
if (mNextPowerRole != UsbPort.POWER_ROLE_NONE && powerRole != UsbPort.POWER_ROLE_NONE) {
|
||||
if (mNextPowerRole != POWER_ROLE_NONE
|
||||
&& powerRole != POWER_ROLE_NONE) {
|
||||
if (mNextPowerRole == powerRole) {
|
||||
// Clear switching text if switch succeeded
|
||||
mSwitchPreference.setSummary("");
|
||||
@@ -91,16 +97,16 @@ public class UsbDetailsPowerRoleController extends UsbDetailsController
|
||||
// Set failure text if switch failed
|
||||
mSwitchPreference.setSummary(R.string.usb_switching_failed);
|
||||
}
|
||||
mNextPowerRole = UsbPort.POWER_ROLE_NONE;
|
||||
mNextPowerRole = POWER_ROLE_NONE;
|
||||
mHandler.removeCallbacks(mFailureCallback);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onPreferenceClick(Preference preference) {
|
||||
int newRole = mSwitchPreference.isChecked() ? UsbPort.POWER_ROLE_SOURCE
|
||||
: UsbPort.POWER_ROLE_SINK;
|
||||
if (mUsbBackend.getPowerRole() != newRole && mNextPowerRole == UsbPort.POWER_ROLE_NONE
|
||||
int newRole = mSwitchPreference.isChecked() ? POWER_ROLE_SOURCE
|
||||
: POWER_ROLE_SINK;
|
||||
if (mUsbBackend.getPowerRole() != newRole && mNextPowerRole == POWER_ROLE_NONE
|
||||
&& !Utils.isMonkeyRunning()) {
|
||||
mUsbBackend.setPowerRole(newRole);
|
||||
|
||||
|
Reference in New Issue
Block a user