Pebble: Address the missing remarks of #807

- Separate the getAudioPlayer method
- Replace the println calls with proper LOG statements
master
Daniele Gobbetti 2017-09-25 11:09:45 +02:00
parent 4d2cc8cfcb
commit c74ea64b70
1 changed files with 20 additions and 16 deletions

View File

@ -76,22 +76,7 @@ public class GBMusicControlReceiver extends BroadcastReceiver {
}
if (keyCode != -1) {
Prefs prefs = GBApplication.getPrefs();
String audioPlayer = prefs.getString("audio_player", "default");
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
MediaSessionManager mediaSessionManager =
(MediaSessionManager) context.getSystemService(Context.MEDIA_SESSION_SERVICE);
List<MediaController> controllers = mediaSessionManager.getActiveSessions(
new ComponentName(context, NotificationListener.class));
try {
MediaController controller = controllers.get(0);
audioPlayer = controller.getPackageName();
} catch (IndexOutOfBoundsException e) {
System.err.println("IndexOutOfBoundsException: " + e.getMessage());
}
}
String audioPlayer = getAudioPlayer(context);
LOG.debug("keypress: " + musicCmd.toString() + " sent to: " + audioPlayer);
@ -114,4 +99,23 @@ public class GBMusicControlReceiver extends BroadcastReceiver {
context.sendOrderedBroadcast(upIntent, null);
}
}
private String getAudioPlayer(Context context) {
Prefs prefs = GBApplication.getPrefs();
String audioPlayer = prefs.getString("audio_player", "default");
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
MediaSessionManager mediaSessionManager =
(MediaSessionManager) context.getSystemService(Context.MEDIA_SESSION_SERVICE);
List<MediaController> controllers = mediaSessionManager.getActiveSessions(
new ComponentName(context, NotificationListener.class));
try {
MediaController controller = controllers.get(0);
audioPlayer = controller.getPackageName();
} catch (IndexOutOfBoundsException e) {
LOG.error("IndexOutOfBoundsException: " + e.getMessage());
}
}
return audioPlayer;
}
}