Android Framework 常见解决方案(14)修改默认usb连接模式为MTP模式

1 原理

Android P Q:找到UsbDeviceManager,这里在finishBoot(也就是启动后) 设置usb模式为 MTP模式,同时在处理消息时如果为默认模式NONE则将其更改为MTP模式。

2 修改方案(Android P Q)

修改文件为:frameworks/base/services/usb/java/com/android/server/usb/UsbDeviceManager.java,修改内容为:

//...
    //1 消息处理部分
    public void handleMessage(Message msg) {
        switch (msg.what) {
            case MSG_UPDATE_STATE:
                mConnected = (msg.arg1 == 1);
                mConfigured = (msg.arg2 == 1);
                updateUsbNotification(false);
                updateAdbNotification(false);
                if (mBootCompleted) {
                    updateUsbStateBroadcastIfNeeded(getAppliedFunctions(mCurrentFunctions));
                }
                if ((mCurrentFunctions & UsbManager.FUNCTION_ACCESSORY) != 0) {
                    updateCurrentAccessory();
                }
                if (mBootCompleted) {
                    if (!mConnected && !hasMessages(MSG_ACCESSORY_MODE_ENTER_TIMEOUT)
                            && !hasMessages(MSG_FUNCTION_SWITCH_TIMEOUT)) {
                        // restore defaults when USB is disconnected
                        if (!mScreenLocked
                                && mScreenUnlockedFunctions != UsbManager.FUNCTION_NONE) {
                            setScreenUnlockedFunctions();
                        } else {
                             //这里将默认值UsbManager.FUNCTION_NONE改为UsbManager.FUNCTION_MTP
                             //setEnabledFunctions(UsbManager.FUNCTION_NONE, false);
			                 setEnabledFunctions(UsbManager.FUNCTION_MTP, false);
                        }
                    }
                    updateUsbFunctions();
                } else {
                    mPendingBootBroadcast = true;
                }
                break;
	    //...
    }
//...
}
    //2 finishboot部分
    protected void finishBoot() {
        if (mBootCompleted && mCurrentUsbFunctionsReceived && mSystemReady) {
            if (mPendingBootBroadcast) {
                updateUsbStateBroadcastIfNeeded(getAppliedFunctions(mCurrentFunctions));
                mPendingBootBroadcast = false;
            }
            if (!mScreenLocked
                    && mScreenUnlockedFunctions != UsbManager.FUNCTION_NONE) {
                setScreenUnlockedFunctions();
            } else {
                 //这里将默认值UsbManager.FUNCTION_NONE改为UsbManager.FUNCTION_MTP
                 //setEnabledFunctions(UsbManager.FUNCTION_NONE, false);
      	         setEnabledFunctions(UsbManager.FUNCTION_MTP, false);
            }
            if (mCurrentAccessory != null) {
                mUsbDeviceManager.getCurrentSettings().accessoryAttached(mCurrentAccessory);
            }
            updateUsbNotification(false);
            updateAdbNotification(false);
            updateUsbFunctions();
        }
    }

 

你可能感兴趣的:(android,framework,常见解决方案)