private void updateUsbNotification() {
      if (mNotificationManager == null || !mUseUsbNotification) return;
      int id = 0;
      Resources r = mContext.getResources();
      if (mConnected || mHostConnected) {
        if (!mUsbDataUnlocked) {
          id = com.android.internal.R.string.usb_charging_notification_title;
        } else if (UsbManager.containsFunction(mCurrentFunctions, UsbManager.USB_FUNCTION_MTP)) {
          id = com.android.internal.R.string.usb_mtp_notification_title;
        } else if (UsbManager.containsFunction(mCurrentFunctions, UsbManager.USB_FUNCTION_PTP)) {
          id = com.android.internal.R.string.usb_ptp_notification_title;
        } else if (UsbManager.containsFunction(mCurrentFunctions, UsbManager.USB_FUNCTION_MIDI)) {
          id = com.android.internal.R.string.usb_midi_notification_title;
        } else if (UsbManager.containsFunction(
            mCurrentFunctions, UsbManager.USB_FUNCTION_ACCESSORY)) {
          id = com.android.internal.R.string.usb_accessory_notification_title;
        } else {
          id = com.android.internal.R.string.usb_charging_notification_title;
        }
      }
      if (id != mUsbNotificationId) {
        // clear notification if title needs changing
        if (mUsbNotificationId != 0) {
          mNotificationManager.cancelAsUser(null, mUsbNotificationId, UserHandle.ALL);
          mUsbNotificationId = 0;
        }
        if (id != 0) {
          CharSequence message = r.getText(com.android.internal.R.string.usb_notification_message);
          CharSequence title = r.getText(id);

          Intent intent =
              Intent.makeRestartActivityTask(
                  new ComponentName(
                      "com.android.settings",
                      "com.android.settings.deviceinfo.UsbModeChooserActivity"));
          PendingIntent pi =
              PendingIntent.getActivityAsUser(mContext, 0, intent, 0, null, UserHandle.CURRENT);

          Notification notification =
              new Notification.Builder(mContext)
                  .setSmallIcon(com.android.internal.R.drawable.stat_sys_adb)
                  .setWhen(0)
                  .setOngoing(true)
                  .setTicker(title)
                  .setDefaults(0) // please be quiet
                  .setPriority(Notification.PRIORITY_MIN)
                  .setColor(
                      mContext.getColor(
                          com.android.internal.R.color.system_notification_accent_color))
                  .setContentTitle(title)
                  .setContentText(message)
                  .setContentIntent(pi)
                  .setVisibility(Notification.VISIBILITY_PUBLIC)
                  .build();
          mNotificationManager.notifyAsUser(null, id, notification, UserHandle.ALL);
          mUsbNotificationId = id;
        }
      }
    }
    private void updateAudioSourceFunction() {
      boolean enabled =
          UsbManager.containsFunction(mCurrentFunctions, UsbManager.USB_FUNCTION_AUDIO_SOURCE);
      if (enabled != mAudioSourceEnabled) {
        int card = -1;
        int device = -1;

        if (enabled) {
          Scanner scanner = null;
          try {
            scanner = new Scanner(new File(AUDIO_SOURCE_PCM_PATH));
            card = scanner.nextInt();
            device = scanner.nextInt();
          } catch (FileNotFoundException e) {
            Slog.e(TAG, "could not open audio source PCM file", e);
          } finally {
            if (scanner != null) {
              scanner.close();
            }
          }
        }
        mUsbAlsaManager.setAccessoryAudioState(enabled, card, device);
        mAudioSourceEnabled = enabled;
      }
    }
    public UsbHandler(Looper looper) {
      super(looper);
      try {
        // Restore default functions.
        mCurrentFunctions = SystemProperties.get(USB_CONFIG_PROPERTY, UsbManager.USB_FUNCTION_NONE);
        if (UsbManager.USB_FUNCTION_NONE.equals(mCurrentFunctions)) {
          mCurrentFunctions = UsbManager.USB_FUNCTION_MTP;
        }
        mCurrentFunctionsApplied =
            mCurrentFunctions.equals(SystemProperties.get(USB_STATE_PROPERTY));
        mAdbEnabled =
            UsbManager.containsFunction(getDefaultFunctions(), UsbManager.USB_FUNCTION_ADB);
        setEnabledFunctions(null, false);

        String state = FileUtils.readTextFile(new File(STATE_PATH), 0, null).trim();
        updateState(state);

        // register observer to listen for settings changes
        mContentResolver.registerContentObserver(
            Settings.Global.getUriFor(Settings.Global.ADB_ENABLED),
            false,
            new AdbSettingsObserver());
        mContentResolver.registerContentObserver(
            Settings.Secure.getUriFor(Settings.Secure.ADB_NOTIFY),
            false,
            new ContentObserver(null) {
              public void onChange(boolean selfChange) {
                updateAdbNotification();
              }
            });

        // Watch for USB configuration changes
        mUEventObserver.startObserving(USB_STATE_MATCH);
        mUEventObserver.startObserving(ACCESSORY_START_MATCH);
      } catch (Exception e) {
        Slog.e(TAG, "Error initializing UsbHandler", e);
      }
    }
 private void updateMidiFunction() {
   boolean enabled =
       UsbManager.containsFunction(mCurrentFunctions, UsbManager.USB_FUNCTION_MIDI);
   if (enabled != mMidiEnabled) {
     if (enabled) {
       Scanner scanner = null;
       try {
         scanner = new Scanner(new File(MIDI_ALSA_PATH));
         mMidiCard = scanner.nextInt();
         mMidiDevice = scanner.nextInt();
       } catch (FileNotFoundException e) {
         Slog.e(TAG, "could not open MIDI PCM file", e);
         enabled = false;
       } finally {
         if (scanner != null) {
           scanner.close();
         }
       }
     }
     mMidiEnabled = enabled;
   }
   mUsbAlsaManager.setPeripheralMidiState(mMidiEnabled && mConfigured, mMidiCard, mMidiDevice);
 }
 public boolean isFunctionEnabled(String function) {
   return UsbManager.containsFunction(SystemProperties.get(USB_CONFIG_PROPERTY), function);
 }
 @Override
 public void handleMessage(Message msg) {
   switch (msg.what) {
     case MSG_UPDATE_STATE:
       mConnected = (msg.arg1 == 1);
       mConfigured = (msg.arg2 == 1);
       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) {
         // restore defaults when USB is disconnected
         setEnabledFunctions(null, false);
       }
       if (mBootCompleted) {
         updateUsbStateBroadcast();
         updateUsbFunctions();
       }
       break;
     case MSG_UPDATE_HOST_STATE:
       mHostConnected = (msg.arg1 == 1);
       updateUsbNotification();
       break;
     case MSG_ENABLE_ADB:
       setAdbEnabled(msg.arg1 == 1);
       break;
     case MSG_SET_CURRENT_FUNCTIONS:
       String functions = (String) msg.obj;
       setEnabledFunctions(functions, false);
       break;
     case MSG_UPDATE_USER_RESTRICTIONS:
       setEnabledFunctions(mCurrentFunctions, false);
       break;
     case MSG_SET_USB_DATA_UNLOCKED:
       setUsbDataUnlocked(msg.arg1 == 1);
       break;
     case MSG_SYSTEM_READY:
       updateUsbNotification();
       updateAdbNotification();
       updateUsbStateBroadcast();
       updateUsbFunctions();
       break;
     case MSG_BOOT_COMPLETED:
       mBootCompleted = true;
       if (mCurrentAccessory != null) {
         getCurrentSettings().accessoryAttached(mCurrentAccessory);
       }
       if (mDebuggingManager != null) {
         mDebuggingManager.setAdbEnabled(mAdbEnabled);
       }
       break;
     case MSG_USER_SWITCHED:
       {
         if (mCurrentUser != msg.arg1) {
           // Restart the USB stack and re-apply user restrictions for MTP or PTP.
           final boolean active =
               UsbManager.containsFunction(mCurrentFunctions, UsbManager.USB_FUNCTION_MTP)
                   || UsbManager.containsFunction(
                       mCurrentFunctions, UsbManager.USB_FUNCTION_PTP);
           if (active && mCurrentUser != UserHandle.USER_NULL) {
             Slog.v(
                 TAG,
                 "Current user switched to "
                     + mCurrentUser
                     + "; resetting USB host stack for MTP or PTP");
             setEnabledFunctions(mCurrentFunctions, true);
           }
           mCurrentUser = msg.arg1;
         }
         break;
       }
   }
 }