Android 各版本 设置 USB 默认连接 MTP 模式 ( Android 6.0+ )
Android 6.0 以及之后的版本,google默认设计直接配置USB连接模式为 :仅充电;
项目开发需要配置USB的 默认连接方式为:MTP 模式;
废话不多说,直接给方案,不同平台源码有少许差异,请知悉!
Android 6.0 Before
modify /device/mediatek/common/device.mk
# default usb function ifeq ($(strip $(MTK_MASS_STORAGE)),yes)
ADDITIONAL_DEFAULT_PROPERTIES += persist.sys.usb.config=mass_storage
//修改上面成想要的功能,如 ADDITIONAL_DEFAULT_PROPERTIES += persist.sys.usb.config=mtp
Android 6.0 +
modify frameworks/base/services/usb/java/com/android/server/usb/UsbDeviceManager.java。
public class UsbDeviceManager {
···省略很多代码···
private boolean mConfigured;
//modified in 2017-05-22 start
//解锁数据,连接电脑,就能看到默认连接模式为MTP
private boolean mUsbDataUnlocked = true;
//modified in 2017-05-22 end
private String mCurrentFunctions;
private String mDefaultFunctions;
······
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MSG_UPDATE_STATE:
mConnected = (msg.arg1 == 1);
mConfigured = (msg.arg2 == 1);
mUsbConfigured = mConfigured;
if (!mConnected) {
//modified in 2017-05-22 start
//default usb connect mode as mtp
// When a disconnect occurs, relock access to sensitive user data
//断开连接的时候,保持当前连接模式,下次连接的时候还是MTP
mUsbDataUnlocked = true;
//modified in 2017-05-22 end
}
updateUsbNotification();
updateAdbNotification();
if (UsbManager.containsFunction(mCurrentFunctions,
UsbManager.USB_FUNCTION_ACCESSORY)) {
updateCurrentAccessory();
} else if (!mConnected) {
//modified in 2017-05-22 start
//change default usb connect mode as mtp,do not restore
// 这里不恢复默认连接方式,保持保持当前的连接模式
setEnabledFunctions(null, true);
//modified in 2017-05-22 end
}
if (mBootCompleted) {
updateUsbStateBroadcastIfNeeded();
updateUsbFunctions();
}
······
}
Android 7 +
Modify
1.修改frameworks/base/services/usb/java/com/android/server/usb/UsbService.java
public class UsbService extends IUsbManager.Stub {
······
final IntentFilter filter = new IntentFilter();
filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
filter.addAction(Intent.ACTION_USER_SWITCHED);
filter.addAction(Intent.ACTION_USER_STOPPED);
//add by xx in 2017-06-12 for bug 169853 start
//注册三个广播
filter.addAction(Intent.ACTION_USER_PRESENT);
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_SCREEN_ON);
//add by xx in 2017-06-12 for bug 169853 end
filter.addAction(DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED);
mContext.registerReceiver(mReceiver, filter, null, null);
······
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1);
final String action = intent.getAction();
if (Intent.ACTION_USER_SWITCHED.equals(action)) {
setCurrentUser(userId);
} else if (Intent.ACTION_USER_STOPPED.equals(action)) {
synchronized (mLock) {
mSettingsByUser.remove(userId);
}
} else if (DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED
.equals(action)) {
if (mDeviceManager != null) {
mDeviceManager.updateUserRestrictions();
}
//add by xx in 2017-06-12 for bug 169853 start
//处理三个广播
}else if (Intent.ACTION_USER_PRESENT.equals(action)) {
if (mDeviceManager != null) {
mDeviceManager.usbDataUnlocked(true);//这个方法系统有的
mDeviceManager.setUserPresent();//这个方法是添加的
}
} else if (Intent.ACTION_SCREEN_OFF.equals(action)) {
if (mDeviceManager != null) {
mDeviceManager.updateScreentSate(true);//这个方法是添加的,为了更新屏幕状态
}
} else if (Intent.ACTION_SCREEN_ON.equals(action)) {
if (mDeviceManager != null) {
mDeviceManager.updateScreentSate(false);//这个方法是添加的,为了更新屏幕状态
}
//add by xx in 2017-06-12 for bug 169853 end
}
}
};
}
2.接下来修改frameworks/base/services/usb/java/com/android/server/usb/UsbDeviceManager.java
public class UsbDeviceManager {
·····
//添加两个全局变量,作为标识
//modified by xx in 2017-06-12 for bug 169853 start
private boolean mUserPresent = false;//用户是否结果到前台
private boolean screenOff = false;//屏幕是否是熄屏
private boolean changeByUser = false;//切换USB模式的时候,是这里代码切换的,还是用户点击切换的。
//modified by xx in 2017-06-12 for bug 169853 end
·····
//添加这两个方法,在UsbService.java中用到,更新这边的状态
//modified by xx in 2017-06-12 for bug 169853 start
public void setUserPresent(){
mUserPresent = true;
}
public void updateScreentSate(boolean state){
screenOff = state;
if(!screenOff) {
mUserPresent = false;
}
if(mHandler != null){
mHandler.updateUsbMode();
}
}
public void usbDataUnlocked(){
changeByUser = false;
mHandler.sendMessage(MSG_SET_USB_DATA_UNLOCKED, true);
}
//modified by xx in 2017-06-12 for bug 169853 end
·····
//更新USB连接状态
public void updateState(String state) {
int connected, configured;
if ("HWDISCONNECTED".equals(state)) {
connected = 0;
configured = 0;
mHwDisconnected = true;
//add by xx in 2017-08-03 for swtich usb mode start
changeByUser = false;
//add by xx in 2017-08-03 for swtich usb mode end
} else if ("DISCONNECTED".equals(state)) {
connected = 0;
configured = 0;
mHwDisconnected = false;
//modified by xx in 2017-06-12 for bug 169853 start
//当熄屏的情况下,更新用户不在前台的标识
if(screenOff){
mUserPresent = false;
}
//modified by xxx in 2017-06-12 for bug 169853 end
} else if ("CONNECTED".equals(state)) {
connected = 1;
configured = 0;
·····
private final class UsbHandler extends Handler {
·····
//在收到更新USB状态的消息之后,更新USB模式,当然要根据用户是否在前台进行判断
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MSG_UPDATE_STATE:
mConnected = (msg.arg1 == 1);
mConfigured = (msg.arg2 == 1);
mUsbConfigured = mConfigured;
//modified by xx in 2017-06-12 for bug 169853 start
//update usb state first
updateUsbMode();
if (mUserPresent && !changeByUser) { //用户在前台的
mUsbDataUnlocked = true;//解锁数据,那么连接电脑就能看到连接模式为MTP了
}
//modified by xx in 2017-06-12 for bug 169853 end
if (!mConnected) {
// When a disconnect occurs, relock access to sensitive user data
mUsbDataUnlocked = false;
}
updateUsbNotification();
updateAdbNotification();
if (UsbManager.containsFunction(mCurrentFunctions,
UsbManager.USB_FUNCTION_ACCESSORY)) {
updateCurrentAccessory();
} else if (!mConnected) {
}
·····//省略代码
case MSG_SET_USB_DATA_UNLOCKED:
//add by xx in 2017-08-03 for swtich usb mode start
if(!changeByUser && mUsbDataUnlocked) return;
//add by xx in 2017-08-03 for swtich usb mode end
setUsbDataUnlocked(msg.arg1 == 1);
break;
}
·····
//在UsbHandler类中的方法,主要是因为用到USB状态值:mConnected
//add by xx in 2017-06-12 for bug 169853 start
private void updateUsbMode(){
if(!mConnected && screenOff){
mUserPresent = false;
}
}
//add by xx in 2017-06-12 for bug 169853 end
}
·····
}
//省略其他代码
public void setCurrentFunctions(String functions) {
if (DEBUG) Slog.d(TAG, "setCurrentFunctions(" + functions + ")");
//add by xx in 2017-08-03 for swtich usb mode start
changeByUser = true;
//add by xx in 2017-08-03 for swtich usb mode end
mHandler.sendMessage(MSG_SET_CURRENT_FUNCTIONS, functions);
}
//省略其他代码
}
Android 8.0 +
modify /frameworks/base/services/usb/java/com/android/server/usb/UsbDeviceManager.java
@Override
public void handleMessage(Message msg) {
...
case MSG_UPDATE_STATE:
...
if (!mConnected) {
// restore defaults when USB is disconnected
setEnabledFunctions(null, !mAdbEnabled, false);
setEnabledFunctions(UsbManager.USB_FUNCTION_MTP, false, true); //chc
add
}
...
case MSG_BOOT_COMPLETED:
mBootCompleted = true;
if (mPendingBootBroadcast) {
updateUsbStateBroadcastIfNeeded(false);
mPendingBootBroadcast = false;
}
//setEnabledFunctions(null, false, false);
setEnabledFunctions(UsbManager.USB_FUNCTION_MTP, false, true); //chc add
if (mCurrentAccessory != null) {
getCurrentSettings().accessoryAttached(mCurrentAccessory);
}
if (mDebuggingManager != null) {
mDebuggingManager.setAdbEnabled(mAdbEnabled);
}
break;
}
连接电脑,看电脑上MTP的图标显示之后,然后将延迟进行相应的调整
Android 9.0
modify
--- a/frameworks/base/services/usb/java/com/android/server/usb/UsbDeviceManager.java
+++ b/frameworks/base/services/usb/java/com/android/server/usb/UsbDeviceManager.java
@@ -824,7 +824,7 @@ public class UsbDeviceManager implements ActivityManagerInternal.ScreenObserver
&& mScreenUnlockedFunctions != UsbManager.FUNCTION_NONE) {
setScreenUnlockedFunctions();
} else {
- setEnabledFunctions(UsbManager.FUNCTION_NONE, false);
+ setEnabledFunctions(UsbManager.FUNCTION_MTP, false);
}
}
updateUsbFunctions();
@@ -1012,7 +1012,9 @@ public class UsbDeviceManager implements ActivityManagerInternal.ScreenObserver
&& mScreenUnlockedFunctions != UsbManager.FUNCTION_NONE) {
setScreenUnlockedFunctions();
} else {
- setEnabledFunctions(UsbManager.FUNCTION_NONE, false);
+ Slog.e(TAG, "finishBoot");
+ //setEnabledFunctions(UsbManager.FUNCTION_NONE, false);
+ setEnabledFunctions(UsbManager.FUNCTION_MTP, false);
}
if (mCurrentAccessory != null) {
mUsbDeviceManager.getCurrentSettings().accessoryAttached(mCurrentAccessory);
---------------------