Merge "Fix problem with usb mode summary not updated correctly."
This commit is contained in:
committed by
Android (Google) Code Review
commit
76cf6f9a68
@@ -46,7 +46,7 @@ public class UsbBackend {
|
|||||||
private UsbPort mPort;
|
private UsbPort mPort;
|
||||||
private UsbPortStatus mPortStatus;
|
private UsbPortStatus mPortStatus;
|
||||||
|
|
||||||
private boolean mIsUnlocked;
|
private Context mContext;
|
||||||
|
|
||||||
public UsbBackend(Context context) {
|
public UsbBackend(Context context) {
|
||||||
this(context, new UserRestrictionUtil(context));
|
this(context, new UserRestrictionUtil(context));
|
||||||
@@ -54,11 +54,7 @@ public class UsbBackend {
|
|||||||
|
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
UsbBackend(Context context, UserRestrictionUtil userRestrictionUtil) {
|
UsbBackend(Context context, UserRestrictionUtil userRestrictionUtil) {
|
||||||
Intent intent = context.registerReceiver(null,
|
mContext = context;
|
||||||
new IntentFilter(UsbManager.ACTION_USB_STATE));
|
|
||||||
mIsUnlocked = intent == null ?
|
|
||||||
false : intent.getBooleanExtra(UsbManager.USB_DATA_UNLOCKED, false);
|
|
||||||
|
|
||||||
mUsbManager = context.getSystemService(UsbManager.class);
|
mUsbManager = context.getSystemService(UsbManager.class);
|
||||||
|
|
||||||
mRestricted = userRestrictionUtil.isUsbFileTransferRestricted();
|
mRestricted = userRestrictionUtil.isUsbFileTransferRestricted();
|
||||||
@@ -92,7 +88,7 @@ public class UsbBackend {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public int getUsbDataMode() {
|
public int getUsbDataMode() {
|
||||||
if (!mIsUnlocked) {
|
if (!isUsbDataUnlocked()) {
|
||||||
return MODE_DATA_NONE;
|
return MODE_DATA_NONE;
|
||||||
} else if (mUsbManager.isFunctionEnabled(UsbManager.USB_FUNCTION_MTP)) {
|
} else if (mUsbManager.isFunctionEnabled(UsbManager.USB_FUNCTION_MTP)) {
|
||||||
return MODE_DATA_MTP;
|
return MODE_DATA_MTP;
|
||||||
@@ -104,6 +100,13 @@ public class UsbBackend {
|
|||||||
return MODE_DATA_NONE; // ...
|
return MODE_DATA_NONE; // ...
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean isUsbDataUnlocked() {
|
||||||
|
Intent intent = mContext.registerReceiver(null,
|
||||||
|
new IntentFilter(UsbManager.ACTION_USB_STATE));
|
||||||
|
return intent == null ?
|
||||||
|
false : intent.getBooleanExtra(UsbManager.USB_DATA_UNLOCKED, false);
|
||||||
|
}
|
||||||
|
|
||||||
private void setUsbFunction(int mode) {
|
private void setUsbFunction(int mode) {
|
||||||
switch (mode) {
|
switch (mode) {
|
||||||
case MODE_DATA_MTP:
|
case MODE_DATA_MTP:
|
||||||
|
@@ -17,6 +17,7 @@
|
|||||||
package com.android.settings.deviceinfo;
|
package com.android.settings.deviceinfo;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.content.IntentFilter;
|
||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
import android.hardware.usb.UsbManager;
|
import android.hardware.usb.UsbManager;
|
||||||
|
|
||||||
@@ -26,11 +27,15 @@ import com.android.settings.TestConfig;
|
|||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
|
import org.mockito.ArgumentMatcher;
|
||||||
import org.mockito.Mock;
|
import org.mockito.Mock;
|
||||||
import org.mockito.MockitoAnnotations;
|
import org.mockito.MockitoAnnotations;
|
||||||
import org.robolectric.annotation.Config;
|
import org.robolectric.annotation.Config;
|
||||||
|
|
||||||
import static org.mockito.Answers.RETURNS_DEEP_STUBS;
|
import static org.mockito.Answers.RETURNS_DEEP_STUBS;
|
||||||
|
import static org.mockito.Matchers.argThat;
|
||||||
|
import static org.mockito.Mockito.eq;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
@RunWith(SettingsRobolectricTestRunner.class)
|
@RunWith(SettingsRobolectricTestRunner.class)
|
||||||
@@ -57,4 +62,21 @@ public class UsbBackendTest {
|
|||||||
UsbBackend usbBackend = new UsbBackend(mContext, mUserRestrictionUtil);
|
UsbBackend usbBackend = new UsbBackend(mContext, mUserRestrictionUtil);
|
||||||
// Should not crash
|
// Should not crash
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void getCurrentMode_shouldRegisterReceiverToGetUsbState() {
|
||||||
|
UsbBackend usbBackend = new UsbBackend(mContext, mUserRestrictionUtil);
|
||||||
|
|
||||||
|
usbBackend.getCurrentMode();
|
||||||
|
|
||||||
|
verify(mContext).registerReceiver(eq(null),
|
||||||
|
argThat(new ArgumentMatcher<IntentFilter>() {
|
||||||
|
@Override
|
||||||
|
public boolean matches(Object i) {
|
||||||
|
final IntentFilter intentFilter = (IntentFilter) i;
|
||||||
|
return intentFilter != null &&
|
||||||
|
UsbManager.ACTION_USB_STATE.equals(intentFilter.getAction(0));
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user