Merge "UsbModeChooserActivity: Ensure MIDI available with DISALLOW_USB_FILE_TRANSFER" into mnc-dev

This commit is contained in:
Nick Kralevich
2015-08-04 19:45:58 +00:00
committed by Android (Google) Code Review
2 changed files with 32 additions and 45 deletions

View File

@@ -1389,12 +1389,4 @@
<item>0</item> <item>0</item>
</string-array> </string-array>
<!-- These values must match up with the code in UsbModeChooserActivity.java. -->
<string-array name="usb_available_functions">
<item>@string/usb_use_charging_only</item>
<item>@string/usb_use_file_transfers</item>
<item>@string/usb_use_photo_transfers</item>
<item>@string/usb_use_MIDI</item>
</string-array>
</resources> </resources>

View File

@@ -22,6 +22,8 @@ import android.app.ActivityManager;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.usb.UsbManager; import android.hardware.usb.UsbManager;
import android.os.Bundle; import android.os.Bundle;
import android.os.UserManager; import android.os.UserManager;
@@ -35,23 +37,34 @@ import com.android.settings.R;
public class UsbModeChooserActivity extends Activity { public class UsbModeChooserActivity extends Activity {
private UsbManager mUsbManager; private UsbManager mUsbManager;
private String[] mFunctions;
private boolean mIsUnlocked;
@Override @Override
protected void onCreate(@Nullable Bundle savedInstanceState) { protected void onCreate(@Nullable Bundle savedInstanceState) {
Intent i = getBaseContext().registerReceiver(null, new IntentFilter(UsbManager.ACTION_USB_STATE));
mIsUnlocked = i.getBooleanExtra(UsbManager.USB_DATA_UNLOCKED, false);
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE); mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
boolean isFileTransferRestricted = ((UserManager) getSystemService(Context.USER_SERVICE))
.hasUserRestriction(UserManager.DISALLOW_USB_FILE_TRANSFER);
CharSequence[] items; CharSequence[] items;
UserManager userManager = if (isFileTransferRestricted) {
(UserManager) getSystemService(Context.USER_SERVICE); items = new CharSequence[] { getText(R.string.usb_use_charging_only), getText(R.string.usb_use_MIDI)};
if (userManager.hasUserRestriction(UserManager.DISALLOW_USB_FILE_TRANSFER)) { mFunctions = new String[] { null, UsbManager.USB_FUNCTION_MIDI };
items = new CharSequence[] { getText(R.string.usb_use_charging_only) };
} else { } else {
items = getResources().getTextArray(R.array.usb_available_functions); items = new CharSequence[] {
getText(R.string.usb_use_charging_only), getText(R.string.usb_use_file_transfers),
getText(R.string.usb_use_photo_transfers), getText(R.string.usb_use_MIDI)};
mFunctions = new String[] { null, UsbManager.USB_FUNCTION_MTP,
UsbManager.USB_FUNCTION_PTP, UsbManager.USB_FUNCTION_MIDI };
} }
final AlertDialog levelDialog; final AlertDialog levelDialog;
AlertDialog.Builder builder = new AlertDialog.Builder(this); AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.usb_use); builder.setTitle(R.string.usb_use);
builder.setSingleChoiceItems(items, getCurrentFunction(), builder.setSingleChoiceItems(items, getCurrentFunction(),
new DialogInterface.OnClickListener() { new DialogInterface.OnClickListener() {
@Override @Override
@@ -79,45 +92,27 @@ public class UsbModeChooserActivity extends Activity {
levelDialog.show(); levelDialog.show();
} }
/*
* If you change the numbers here, you also need to change R.array.usb_available_functions
* so that everything matches.
*/
private int getCurrentFunction() { private int getCurrentFunction() {
if (!mUsbManager.isUsbDataUnlocked()) { if (!mIsUnlocked) {
return 0; return 0;
} else if (mUsbManager.isFunctionEnabled(UsbManager.USB_FUNCTION_MTP)) { }
return 1;
} else if (mUsbManager.isFunctionEnabled(UsbManager.USB_FUNCTION_PTP)) { for (int i = 1; i < mFunctions.length; i++) {
return 2; if (mUsbManager.isFunctionEnabled(mFunctions[i])) {
} else if (mUsbManager.isFunctionEnabled(UsbManager.USB_FUNCTION_MIDI)) { return i;
return 3; }
} }
return 0; return 0;
} }
/*
* If you change the numbers here, you also need to change R.array.usb_available_functions
* so that everything matches.
*/
private void setCurrentFunction(int which) { private void setCurrentFunction(int which) {
switch (which) { if (which == 0) {
case 0: mUsbManager.setCurrentFunction(null);
mUsbManager.setCurrentFunction(null); mUsbManager.setUsbDataUnlocked(false);
mUsbManager.setUsbDataUnlocked(false); return;
break;
case 1:
mUsbManager.setCurrentFunction(UsbManager.USB_FUNCTION_MTP);
mUsbManager.setUsbDataUnlocked(true);
break;
case 2:
mUsbManager.setCurrentFunction(UsbManager.USB_FUNCTION_PTP);
mUsbManager.setUsbDataUnlocked(true);
break;
case 3:
mUsbManager.setCurrentFunction(UsbManager.USB_FUNCTION_MIDI);
mUsbManager.setUsbDataUnlocked(true);
break;
} }
mUsbManager.setCurrentFunction(mFunctions[which]);
mUsbManager.setUsbDataUnlocked(true);
} }
} }