4949import com .android .server .FgThread ;
5050
5151import java .io .File ;
52- import java .io .FileDescriptor ;
5352import java .io .FileNotFoundException ;
5453import java .io .IOException ;
55- import java .io .PrintWriter ;
5654import java .util .HashMap ;
5755import java .util .LinkedList ;
5856import java .util .List ;
@@ -316,6 +314,9 @@ public UsbHandler(Looper looper) {
316314 // Restore default functions.
317315 mCurrentFunctions = SystemProperties .get (USB_CONFIG_PROPERTY ,
318316 UsbManager .USB_FUNCTION_NONE );
317+ if (UsbManager .USB_FUNCTION_NONE .equals (mCurrentFunctions )) {
318+ mCurrentFunctions = UsbManager .USB_FUNCTION_MTP ;
319+ }
319320 mCurrentFunctionsApplied = mCurrentFunctions .equals (
320321 SystemProperties .get (USB_STATE_PROPERTY ));
321322 mAdbEnabled = UsbManager .containsFunction (getDefaultFunctions (),
@@ -400,6 +401,14 @@ private boolean setUsbConfig(String config) {
400401 return waitForState (config );
401402 }
402403
404+ private void setUsbDataUnlocked (boolean enable ) {
405+ if (DEBUG ) Slog .d (TAG , "setUsbDataUnlocked: " + enable );
406+ mUsbDataUnlocked = enable ;
407+ updateUsbNotification ();
408+ updateUsbStateBroadcast ();
409+ setEnabledFunctions (mCurrentFunctions , true );
410+ }
411+
403412 private void setAdbEnabled (boolean enable ) {
404413 if (DEBUG ) Slog .d (TAG , "setAdbEnabled: " + enable );
405414 if (enable != mAdbEnabled ) {
@@ -471,7 +480,6 @@ private boolean trySetEnabledFunctions(String functions, boolean forceRestart) {
471480 }
472481 functions = applyAdbFunction (functions );
473482 functions = applyOemOverrideFunction (functions );
474- functions = applyUserRestrictions (functions );
475483
476484 if (!mCurrentFunctions .equals (functions ) || !mCurrentFunctionsApplied
477485 || forceRestart ) {
@@ -502,13 +510,9 @@ private String applyAdbFunction(String functions) {
502510 return functions ;
503511 }
504512
505- private String applyUserRestrictions ( String functions ) {
513+ private boolean isUsbTransferAllowed ( ) {
506514 UserManager userManager = (UserManager ) mContext .getSystemService (Context .USER_SERVICE );
507- if (userManager .hasUserRestriction (UserManager .DISALLOW_USB_FILE_TRANSFER )) {
508- functions = UsbManager .removeFunction (functions , UsbManager .USB_FUNCTION_MTP );
509- functions = UsbManager .removeFunction (functions , UsbManager .USB_FUNCTION_PTP );
510- }
511- return functions ;
515+ return !userManager .hasUserRestriction (UserManager .DISALLOW_USB_FILE_TRANSFER );
512516 }
513517
514518 private void updateCurrentAccessory () {
@@ -555,7 +559,7 @@ private void updateUsbStateBroadcast() {
555559 | Intent .FLAG_RECEIVER_FOREGROUND );
556560 intent .putExtra (UsbManager .USB_CONNECTED , mConnected );
557561 intent .putExtra (UsbManager .USB_CONFIGURED , mConfigured );
558- intent .putExtra (UsbManager .USB_DATA_UNLOCKED , mUsbDataUnlocked );
562+ intent .putExtra (UsbManager .USB_DATA_UNLOCKED , isUsbTransferAllowed () && mUsbDataUnlocked );
559563
560564 if (mCurrentFunctions != null ) {
561565 String [] functions = mCurrentFunctions .split ("," );
@@ -659,10 +663,7 @@ public void handleMessage(Message msg) {
659663 setEnabledFunctions (mCurrentFunctions , false );
660664 break ;
661665 case MSG_SET_USB_DATA_UNLOCKED :
662- mUsbDataUnlocked = (msg .arg1 == 1 );
663- updateUsbNotification ();
664- updateUsbStateBroadcast ();
665- setEnabledFunctions (mCurrentFunctions , true );
666+ setUsbDataUnlocked (msg .arg1 == 1 );
666667 break ;
667668 case MSG_SYSTEM_READY :
668669 updateUsbNotification ();
@@ -807,8 +808,12 @@ private void updateAdbNotification() {
807808 }
808809
809810 private String getDefaultFunctions () {
810- return SystemProperties .get (USB_PERSISTENT_CONFIG_PROPERTY ,
811- UsbManager .USB_FUNCTION_ADB );
811+ String func = SystemProperties .get (USB_PERSISTENT_CONFIG_PROPERTY ,
812+ UsbManager .USB_FUNCTION_NONE );
813+ if (UsbManager .USB_FUNCTION_NONE .equals (func )) {
814+ func = UsbManager .USB_FUNCTION_MTP ;
815+ }
816+ return func ;
812817 }
813818
814819 public void dump (IndentingPrintWriter pw ) {
@@ -817,6 +822,7 @@ public void dump(IndentingPrintWriter pw) {
817822 pw .println (" mCurrentFunctionsApplied: " + mCurrentFunctionsApplied );
818823 pw .println (" mConnected: " + mConnected );
819824 pw .println (" mConfigured: " + mConfigured );
825+ pw .println (" mUsbDataUnlocked: " + mUsbDataUnlocked );
820826 pw .println (" mCurrentAccessory: " + mCurrentAccessory );
821827 try {
822828 pw .println (" Kernel state: "
@@ -864,11 +870,6 @@ public void setUsbDataUnlocked(boolean unlocked) {
864870 mHandler .sendMessage (MSG_SET_USB_DATA_UNLOCKED , unlocked );
865871 }
866872
867- public boolean isUsbDataUnlocked () {
868- if (DEBUG ) Slog .d (TAG , "isUsbDataUnlocked() -> " + mHandler .mUsbDataUnlocked );
869- return mHandler .mUsbDataUnlocked ;
870- }
871-
872873 private void readOemUsbOverrideConfig () {
873874 String [] configList = mContext .getResources ().getStringArray (
874875 com .android .internal .R .array .config_oemUsbModeOverride );
0 commit comments