@@ -322,8 +322,6 @@ public UsbHandler(Looper looper) {
322322 String state = FileUtils .readTextFile (new File (STATE_PATH ), 0 , null ).trim ();
323323 updateState (state );
324324 mAdbEnabled = containsFunction (mCurrentFunctions , UsbManager .USB_FUNCTION_ADB );
325- mAudioSourceEnabled = containsFunction (mCurrentFunctions ,
326- UsbManager .USB_FUNCTION_AUDIO_SOURCE );
327325
328326 // Upgrade step for previous versions that used persist.service.adb.enable
329327 String value = SystemProperties .get ("persist.service.adb.enable" , "" );
@@ -537,26 +535,29 @@ private void updateUsbState() {
537535 mContext .sendStickyBroadcast (intent );
538536 }
539537
540- private void updateAudioSourceFunction (boolean enabled ) {
541- // send a sticky broadcast containing current USB state
542- Intent intent = new Intent (Intent .ACTION_USB_AUDIO_ACCESSORY_PLUG );
543- intent .addFlags (Intent .FLAG_RECEIVER_REPLACE_PENDING );
544- intent .addFlags (Intent .FLAG_RECEIVER_REGISTERED_ONLY );
545- intent .putExtra ("state" , (enabled ? 1 : 0 ));
546- if (enabled ) {
547- try {
548- Scanner scanner = new Scanner (new File (AUDIO_SOURCE_PCM_PATH ));
549- int card = scanner .nextInt ();
550- int device = scanner .nextInt ();
551- intent .putExtra ("card" , card );
552- intent .putExtra ("device" , device );
553- } catch (FileNotFoundException e ) {
554- Slog .e (TAG , "could not open audio source PCM file" , e );
538+ private void updateAudioSourceFunction () {
539+ boolean enabled = containsFunction (mCurrentFunctions ,
540+ UsbManager .USB_FUNCTION_AUDIO_SOURCE );
541+ if (enabled != mAudioSourceEnabled ) {
542+ // send a sticky broadcast containing current USB state
543+ Intent intent = new Intent (Intent .ACTION_USB_AUDIO_ACCESSORY_PLUG );
544+ intent .addFlags (Intent .FLAG_RECEIVER_REPLACE_PENDING );
545+ intent .addFlags (Intent .FLAG_RECEIVER_REGISTERED_ONLY );
546+ intent .putExtra ("state" , (enabled ? 1 : 0 ));
547+ if (enabled ) {
548+ try {
549+ Scanner scanner = new Scanner (new File (AUDIO_SOURCE_PCM_PATH ));
550+ int card = scanner .nextInt ();
551+ int device = scanner .nextInt ();
552+ intent .putExtra ("card" , card );
553+ intent .putExtra ("device" , device );
554+ } catch (FileNotFoundException e ) {
555+ Slog .e (TAG , "could not open audio source PCM file" , e );
556+ }
555557 }
558+ mContext .sendStickyBroadcast (intent );
559+ mAudioSourceEnabled = enabled ;
556560 }
557-
558- mContext .sendStickyBroadcast (intent );
559- mAudioSourceEnabled = enabled ;
560561 }
561562
562563 @ Override
@@ -578,11 +579,7 @@ public void handleMessage(Message msg) {
578579 }
579580 if (mBootCompleted ) {
580581 updateUsbState ();
581- boolean audioSourceEnabled = containsFunction (mCurrentFunctions ,
582- UsbManager .USB_FUNCTION_AUDIO_SOURCE );
583- if (audioSourceEnabled != mAudioSourceEnabled ) {
584- updateAudioSourceFunction (audioSourceEnabled );
585- }
582+ updateAudioSourceFunction ();
586583 }
587584 break ;
588585 case MSG_ENABLE_ADB :
@@ -597,13 +594,13 @@ public void handleMessage(Message msg) {
597594 updateUsbNotification ();
598595 updateAdbNotification ();
599596 updateUsbState ();
597+ updateAudioSourceFunction ();
600598 break ;
601599 case MSG_BOOT_COMPLETED :
602600 mBootCompleted = true ;
603601 if (mCurrentAccessory != null ) {
604602 mSettingsManager .accessoryAttached (mCurrentAccessory );
605603 }
606- updateAudioSourceFunction (mAudioSourceEnabled );
607604 break ;
608605 }
609606 }
0 commit comments