New UI for controlling USB
Create a new UI for managing USB access control. This UI is implemented as a transparent popup triggered by a persistent notification. Delete the old UI for controlling this. It is no longer used. Bug: 18905620 Change-Id: I54186961414447a9d37f68e946e1c167506170ae
This commit is contained in:
@@ -1274,7 +1274,7 @@ public class DevelopmentSettings extends SettingsPreferenceFragment
|
||||
|
||||
private void writeUsbConfigurationOption(Object newValue) {
|
||||
UsbManager manager = (UsbManager)getActivity().getSystemService(Context.USB_SERVICE);
|
||||
manager.setCurrentFunction(newValue.toString(), false);
|
||||
manager.setCurrentFunction(newValue.toString());
|
||||
}
|
||||
|
||||
private void updateCpuUsageOptions() {
|
||||
|
@@ -87,7 +87,6 @@ import com.android.settings.deviceinfo.PrivateVolumeForget;
|
||||
import com.android.settings.deviceinfo.PrivateVolumeSettings;
|
||||
import com.android.settings.deviceinfo.PublicVolumeSettings;
|
||||
import com.android.settings.deviceinfo.StorageSettings;
|
||||
import com.android.settings.deviceinfo.UsbSettings;
|
||||
import com.android.settings.fuelgauge.BatterySaverSettings;
|
||||
import com.android.settings.fuelgauge.PowerUsageDetail;
|
||||
import com.android.settings.fuelgauge.PowerUsageSummary;
|
||||
@@ -318,7 +317,6 @@ public class SettingsActivity extends Activity
|
||||
PrivateVolumeSettings.class.getName(),
|
||||
PublicVolumeSettings.class.getName(),
|
||||
DevelopmentSettings.class.getName(),
|
||||
UsbSettings.class.getName(),
|
||||
AndroidBeam.class.getName(),
|
||||
WifiDisplaySettings.class.getName(),
|
||||
PowerUsageSummary.class.getName(),
|
||||
|
@@ -273,7 +273,6 @@ public class PrivateVolumeSettings extends SettingsPreferenceFragment {
|
||||
final MenuItem unmount = menu.findItem(R.id.storage_unmount);
|
||||
final MenuItem format = menu.findItem(R.id.storage_format);
|
||||
final MenuItem migrate = menu.findItem(R.id.storage_migrate);
|
||||
final MenuItem usb = menu.findItem(R.id.storage_usb);
|
||||
|
||||
// Actions live in menu for non-internal private volumes; they're shown
|
||||
// as preference items for public volumes.
|
||||
@@ -293,9 +292,6 @@ public class PrivateVolumeSettings extends SettingsPreferenceFragment {
|
||||
final VolumeInfo privateVol = getActivity().getPackageManager()
|
||||
.getPrimaryStorageCurrentVolume();
|
||||
migrate.setVisible(!Objects.equals(mVolume, privateVol));
|
||||
|
||||
// TODO: show usb if we jumped past first screen
|
||||
usb.setVisible(false);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -324,10 +320,6 @@ public class PrivateVolumeSettings extends SettingsPreferenceFragment {
|
||||
intent.putExtra(VolumeInfo.EXTRA_VOLUME_ID, mVolume.getId());
|
||||
startActivity(intent);
|
||||
return true;
|
||||
case R.id.storage_usb:
|
||||
startFragment(this, UsbSettings.class.getCanonicalName(),
|
||||
R.string.storage_title_usb, 0, null);
|
||||
return true;
|
||||
}
|
||||
return super.onOptionsItemSelected(item);
|
||||
}
|
||||
|
@@ -148,29 +148,6 @@ public class StorageSettings extends SettingsPreferenceFragment implements Index
|
||||
mStorageManager.unregisterListener(mStorageListener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
|
||||
inflater.inflate(R.menu.storage, menu);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPrepareOptionsMenu(Menu menu) {
|
||||
final MenuItem usb = menu.findItem(R.id.storage_usb);
|
||||
|
||||
usb.setVisible(!mUserManager.hasUserRestriction(UserManager.DISALLOW_USB_FILE_TRANSFER));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
switch (item.getItemId()) {
|
||||
case R.id.storage_usb:
|
||||
startFragment(this, UsbSettings.class.getCanonicalName(),
|
||||
R.string.storage_title_usb, 0, null);
|
||||
return true;
|
||||
}
|
||||
return super.onOptionsItemSelected(item);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference pref) {
|
||||
final String volId = pref.getKey();
|
||||
|
105
src/com/android/settings/deviceinfo/UsbModeChooserActivity.java
Normal file
105
src/com/android/settings/deviceinfo/UsbModeChooserActivity.java
Normal file
@@ -0,0 +1,105 @@
|
||||
/*
|
||||
* 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.deviceinfo;
|
||||
|
||||
import android.annotation.Nullable;
|
||||
import android.app.Activity;
|
||||
import android.app.AlertDialog;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.hardware.usb.UsbManager;
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
import com.android.settings.R;
|
||||
|
||||
/**
|
||||
* UI for the USB chooser dialog.
|
||||
*
|
||||
* TODO: Don't show some UI elements if UserManager.DISALLOW_USB_FILE_TRANSFER is disabled.
|
||||
*/
|
||||
public class UsbModeChooserActivity extends Activity {
|
||||
|
||||
private UsbManager mUsbManager;
|
||||
|
||||
@Override
|
||||
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
|
||||
final AlertDialog levelDialog;
|
||||
AlertDialog.Builder builder = new AlertDialog.Builder(this);
|
||||
builder.setTitle(R.string.usb_use);
|
||||
builder.setSingleChoiceItems(R.array.usb_available_functions, getCurrentFunction(),
|
||||
new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
setCurrentFunction(which);
|
||||
dialog.dismiss();
|
||||
UsbModeChooserActivity.this.finish();
|
||||
}
|
||||
});
|
||||
builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
|
||||
@Override
|
||||
public void onDismiss(DialogInterface dialog) {
|
||||
UsbModeChooserActivity.this.finish();
|
||||
}
|
||||
});
|
||||
builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
UsbModeChooserActivity.this.finish();
|
||||
}
|
||||
});
|
||||
levelDialog = builder.create();
|
||||
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() {
|
||||
if (mUsbManager.isFunctionEnabled(UsbManager.USB_FUNCTION_MTP)) {
|
||||
return 1;
|
||||
} else if (mUsbManager.isFunctionEnabled(UsbManager.USB_FUNCTION_PTP)) {
|
||||
return 2;
|
||||
} else if (mUsbManager.isFunctionEnabled(UsbManager.USB_FUNCTION_MIDI)) {
|
||||
return 3;
|
||||
}
|
||||
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) {
|
||||
switch (which) {
|
||||
case 0:
|
||||
mUsbManager.setCurrentFunction("none");
|
||||
break;
|
||||
case 1:
|
||||
mUsbManager.setCurrentFunction(UsbManager.USB_FUNCTION_MTP);
|
||||
break;
|
||||
case 2:
|
||||
mUsbManager.setCurrentFunction(UsbManager.USB_FUNCTION_PTP);
|
||||
break;
|
||||
case 3:
|
||||
mUsbManager.setCurrentFunction(UsbManager.USB_FUNCTION_MIDI);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,182 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2011 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.deviceinfo;
|
||||
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.hardware.usb.UsbManager;
|
||||
import android.os.Bundle;
|
||||
import android.os.UserManager;
|
||||
import android.preference.CheckBoxPreference;
|
||||
import android.preference.Preference;
|
||||
import android.preference.PreferenceScreen;
|
||||
import android.util.Log;
|
||||
|
||||
import com.android.internal.logging.MetricsLogger;
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.SettingsPreferenceFragment;
|
||||
import com.android.settings.Utils;
|
||||
|
||||
/**
|
||||
* USB storage settings.
|
||||
*/
|
||||
public class UsbSettings extends SettingsPreferenceFragment {
|
||||
|
||||
private static final String TAG = "UsbSettings";
|
||||
|
||||
private static final String KEY_MTP = "usb_mtp";
|
||||
private static final String KEY_PTP = "usb_ptp";
|
||||
private static final String KEY_MIDI = "usb_midi";
|
||||
|
||||
private UsbManager mUsbManager;
|
||||
private CheckBoxPreference mMtp;
|
||||
private CheckBoxPreference mPtp;
|
||||
private CheckBoxPreference mMidi;
|
||||
private boolean mUsbAccessoryMode;
|
||||
|
||||
private final BroadcastReceiver mStateReceiver = new BroadcastReceiver() {
|
||||
public void onReceive(Context content, Intent intent) {
|
||||
String action = intent.getAction();
|
||||
if (action.equals(UsbManager.ACTION_USB_STATE)) {
|
||||
mUsbAccessoryMode = intent.getBooleanExtra(UsbManager.USB_FUNCTION_ACCESSORY, false);
|
||||
Log.e(TAG, "UsbAccessoryMode " + mUsbAccessoryMode);
|
||||
}
|
||||
updateToggles(mUsbManager.getDefaultFunction());
|
||||
}
|
||||
};
|
||||
|
||||
private PreferenceScreen createPreferenceHierarchy() {
|
||||
PreferenceScreen root = getPreferenceScreen();
|
||||
if (root != null) {
|
||||
root.removeAll();
|
||||
}
|
||||
addPreferencesFromResource(R.xml.usb_settings);
|
||||
root = getPreferenceScreen();
|
||||
|
||||
mMtp = (CheckBoxPreference)root.findPreference(KEY_MTP);
|
||||
mPtp = (CheckBoxPreference)root.findPreference(KEY_PTP);
|
||||
mMidi = (CheckBoxPreference)root.findPreference(KEY_MIDI);
|
||||
|
||||
UserManager um = (UserManager) getActivity().getSystemService(Context.USER_SERVICE);
|
||||
if (um.hasUserRestriction(UserManager.DISALLOW_USB_FILE_TRANSFER)) {
|
||||
mMtp.setEnabled(false);
|
||||
mPtp.setEnabled(false);
|
||||
}
|
||||
|
||||
return root;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int getMetricsCategory() {
|
||||
return MetricsLogger.DEVICEINFO_USB;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle icicle) {
|
||||
super.onCreate(icicle);
|
||||
mUsbManager = (UsbManager)getSystemService(Context.USB_SERVICE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPause() {
|
||||
super.onPause();
|
||||
getActivity().unregisterReceiver(mStateReceiver);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
|
||||
// Make sure we reload the preference hierarchy since some of these settings
|
||||
// depend on others...
|
||||
createPreferenceHierarchy();
|
||||
|
||||
// ACTION_USB_STATE is sticky so this will call updateToggles
|
||||
getActivity().registerReceiver(mStateReceiver,
|
||||
new IntentFilter(UsbManager.ACTION_USB_STATE));
|
||||
}
|
||||
|
||||
private void updateToggles(String function) {
|
||||
if (UsbManager.USB_FUNCTION_MTP.equals(function)) {
|
||||
mMtp.setChecked(true);
|
||||
mPtp.setChecked(false);
|
||||
mMidi.setChecked(false);
|
||||
} else if (UsbManager.USB_FUNCTION_PTP.equals(function)) {
|
||||
mMtp.setChecked(false);
|
||||
mPtp.setChecked(true);
|
||||
mMidi.setChecked(false);
|
||||
} else if (UsbManager.USB_FUNCTION_MIDI.equals(function)) {
|
||||
mMtp.setChecked(false);
|
||||
mPtp.setChecked(false);
|
||||
mMidi.setChecked(true);
|
||||
} else {
|
||||
mMtp.setChecked(false);
|
||||
mPtp.setChecked(false);
|
||||
mMidi.setChecked(false);
|
||||
}
|
||||
UserManager um = (UserManager) getActivity().getSystemService(Context.USER_SERVICE);
|
||||
if (um.hasUserRestriction(UserManager.DISALLOW_USB_FILE_TRANSFER)) {
|
||||
Log.e(TAG, "USB is locked down");
|
||||
mMtp.setEnabled(false);
|
||||
mPtp.setEnabled(false);
|
||||
mMidi.setEnabled(true);
|
||||
} else if (!mUsbAccessoryMode) {
|
||||
//Enable MTP and PTP switch while USB is not in Accessory Mode, otherwise disable it
|
||||
Log.e(TAG, "USB Normal Mode");
|
||||
mMtp.setEnabled(true);
|
||||
mPtp.setEnabled(true);
|
||||
mMidi.setEnabled(true);
|
||||
} else {
|
||||
Log.e(TAG, "USB Accessory Mode");
|
||||
mMtp.setEnabled(false);
|
||||
mPtp.setEnabled(false);
|
||||
mMidi.setEnabled(false);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
|
||||
|
||||
// Don't allow any changes to take effect as the USB host will be disconnected, killing
|
||||
// the monkeys
|
||||
if (Utils.isMonkeyRunning()) {
|
||||
return true;
|
||||
}
|
||||
// If this user is disallowed from using USB, don't handle their attempts to change the
|
||||
// setting.
|
||||
UserManager um = (UserManager) getActivity().getSystemService(Context.USER_SERVICE);
|
||||
if (um.hasUserRestriction(UserManager.DISALLOW_USB_FILE_TRANSFER)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
String function = "none";
|
||||
if (preference == mMtp && mMtp.isChecked()) {
|
||||
function = UsbManager.USB_FUNCTION_MTP;
|
||||
} else if (preference == mPtp && mPtp.isChecked()) {
|
||||
function = UsbManager.USB_FUNCTION_PTP;
|
||||
} else if (preference == mMidi && mMidi.isChecked()) {
|
||||
function = UsbManager.USB_FUNCTION_MIDI;
|
||||
}
|
||||
|
||||
mUsbManager.setCurrentFunction(function, true);
|
||||
updateToggles(function);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
@@ -34,7 +34,6 @@ import com.android.settings.applications.AdvancedAppSettings;
|
||||
import com.android.settings.applications.ManageDefaultApps;
|
||||
import com.android.settings.bluetooth.BluetoothSettings;
|
||||
import com.android.settings.deviceinfo.StorageSettings;
|
||||
import com.android.settings.deviceinfo.UsbSettings;
|
||||
import com.android.settings.fuelgauge.BatterySaverSettings;
|
||||
import com.android.settings.fuelgauge.PowerUsageSummary;
|
||||
import com.android.settings.inputmethod.InputMethodAndLanguageSettings;
|
||||
@@ -131,7 +130,6 @@ public final class Ranking {
|
||||
|
||||
// Storage
|
||||
sRankMap.put(StorageSettings.class.getName(), RANK_STORAGE);
|
||||
sRankMap.put(UsbSettings.class.getName(), RANK_STORAGE);
|
||||
|
||||
// Battery
|
||||
sRankMap.put(PowerUsageSummary.class.getName(), RANK_POWER_USAGE);
|
||||
|
@@ -36,7 +36,6 @@ import com.android.settings.applications.AdvancedAppSettings;
|
||||
import com.android.settings.applications.ManageDefaultApps;
|
||||
import com.android.settings.bluetooth.BluetoothSettings;
|
||||
import com.android.settings.deviceinfo.StorageSettings;
|
||||
import com.android.settings.deviceinfo.UsbSettings;
|
||||
import com.android.settings.fuelgauge.BatterySaverSettings;
|
||||
import com.android.settings.fuelgauge.PowerUsageSummary;
|
||||
import com.android.settings.inputmethod.InputMethodAndLanguageSettings;
|
||||
@@ -178,13 +177,6 @@ public final class SearchIndexableResources {
|
||||
StorageSettings.class.getName(),
|
||||
R.drawable.ic_settings_storage));
|
||||
|
||||
sResMap.put(UsbSettings.class.getName(),
|
||||
new SearchIndexableResource(
|
||||
Ranking.getRankForClassName(UsbSettings.class.getName()),
|
||||
R.xml.usb_settings,
|
||||
UsbSettings.class.getName(),
|
||||
R.drawable.ic_settings_storage));
|
||||
|
||||
sResMap.put(PowerUsageSummary.class.getName(),
|
||||
new SearchIndexableResource(
|
||||
Ranking.getRankForClassName(PowerUsageSummary.class.getName()),
|
||||
|
Reference in New Issue
Block a user