The options of MTP, PTP, and ums are defined in usbsettings. java. The following code is executed when one of the options is selected.
Packages/apps/settings/src/COM/Android/settings/deviceinfo/usbsettings. Java
if (preference == mMtp) { mUsbManager.setCurrentFunction(UsbManager.USB_FUNCTION_MTP, true); updateToggles(UsbManager.USB_FUNCTION_MTP); } else if (preference == mPtp) { mUsbManager.setCurrentFunction(UsbManager.USB_FUNCTION_PTP, true); updateToggles(UsbManager.USB_FUNCTION_PTP); } else if(preference == mUms) { mUsbManager.setCurrentFunction(UsbManager.USB_FUNCTION_MASS_STORAGE, true); updateToggles(UsbManager.USB_FUNCTION_MASS_STORAGE); }
Updatetoggle allows you to select and cancel some options.
Most importantlyMusbmanager. setcurrentfunction (), Musbmanger is obtained as follows:
@Override public void onCreate(Bundle icicle) { super.onCreate(icicle); mUsbManager = (UsbManager)getSystemService(Context.USB_SERVICE); }
It is implemented in frameworks/base/CORE/Java/Android/hardware/USB Manager. java.
Its interface is implemented in frameworks/base/CORE/Java/Android/hardware/USB/iusbmanager. aidl
Frameworks/base/services/Java/COM/Android/Server/USB service. Java
/** * UsbService manages all USB related state, including both host and device support. * Host related events and calls are delegated to UsbHostManager, and device related * support is delegated to UsbDeviceManager. */ public class UsbService extends IUsbManager.Stub { private final Context mContext; private UsbDeviceManager mDeviceManager; private UsbHostManager mHostManager; private final UsbSettingsManager mSettingsManager;
This interface is "com. Android. settings. usbsettings" activity. It is called in frameworks/base/services/Java/COM/Android/Server/USB/usbdevicemanager. java.
If you select the USB debugging function, the "com. Android. settings. developmentsettings" activity will pop up, which is also called in usbdevicemanager. java. Let's take a closer look at usbdevciemanager.
If you select ums, the "com. Android. systemui. USB. usbstorageactivity" activity will pop up, which is the "open USB storage device" interface. It is defined:
Frameworks/base/packages/systemui/src/COM/Android/systemui/USB/usbstorageactivity. java.
These interfaces are displayed only when the USB is inserted. If no USB is inserted, some interfaces do not have an entrance. Let's take usbstorageactivity as an example to introduce how these notifications pop up.
How did the notification pop up?
Frameworks/base/packages/systemui/src/COM/Android/systemui/statusbar/phone/phonestatusbarpolicy. Java
// storage mStorageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE); mStorageManager.registerListener( new com.android.systemui.usb.StorageNotification(context));
The listening function com. Android. systemui. USB. storagenoication ication is registered here. This function is only for phone. For tablets, there are tablet/tabletstatusbarpolicy. Java
Let's take a look at the registeredCom. Android. systemui. USB. storagenotification
Frameworks/base/packages/systemui/src/COM/Android/systemui/USB/storagenoication ication. Java
public class StorageNotification extends StorageEventListener { private static final String TAG = "StorageNotification"; public StorageNotification(Context context) { mContext = context; mStorageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE); final boolean connected = mStorageManager.isUsbMassStorageConnected(); Slog.d(TAG, String.format( "Startup with UMS connection %s (media state %s)", mUmsAvailable, Environment.getExternalStorageState())); HandlerThread thr = new HandlerThread("SystemUI StorageNotification"); thr.start(); mAsyncEventHandler = new Handler(thr.getLooper()); onUsbMassStorageConnectionChanged(connected); }
The onusbmassstorageconnectionchanged function is called in the constructor. It is eventually transferred to updateusbmassstoragenotification (),
/** * Update the state of the USB mass storage notification */ void updateUsbMassStorageNotification(boolean available) { if (available) { Intent intent = new Intent(); intent.setClass(mContext, com.android.systemui.usb.UsbStorageActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); PendingIntent pi = PendingIntent.getActivity(mContext, 0, intent, 0); setUsbStorageNotification( com.android.internal.R.string.usb_storage_notification_title, com.android.internal.R.string.usb_storage_notification_message, com.android.internal.R.drawable.stat_sys_data_usb, false, true, pi); } else { setUsbStorageNotification(0, 0, 0, false, false, null); } }
That is to say, usbstorageactivity and storagenotification are bound together. When will this notification pop up, you can enter this activity.
If the USB is connected, the USB option notification is displayed in the notification bar. If no connection exists, the notification will be canceled. Let's take a look at how the notification was popped up.
Storagemanager
Frameworks/base/CORE/Java/Android/OS/storage/storagemanager. Java
public void registerListener(StorageEventListener listener) { if (listener == null) { return; } synchronized (mListeners) { mListeners.add(new ListenerDelegate(listener)); } }
Mlisteners is used by the Private member mountservicebinderlistener, and mountservicebinderlistener is inherited from imountservicelistener. stub and is registered in the constructor.
public StorageManager(Looper tgtLooper) throws RemoteException { mMountService = IMountService.Stub.asInterface(ServiceManager.getService("mount")); if (mMountService == null) { Log.e(TAG, "Unable to connect to mount service! - is it running yet?"); return; } mTgtLooper = tgtLooper; mBinderListener = new MountServiceBinderListener(); mMountService.registerListener(mBinderListener); }
Therefore, the key lies in mmountservice.