Skip to content

Commit

Permalink
Merge pull request #1 from EltonFaust/master
Browse files Browse the repository at this point in the history
Set FLAG_MUTABLE only on Android 12 (SDK 31)
  • Loading branch information
TheRockettek authored Aug 6, 2022
2 parents 1a8cbf3 + b28140e commit a973463
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
5 changes: 4 additions & 1 deletion src/android/MusicControls.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,10 @@ protected void onNotificationDestroyed() {
try {
this.mAudioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
Intent headsetIntent = new Intent("music-controls-media-button");
this.mediaButtonPendingIntent = PendingIntent.getBroadcast(context, 0, headsetIntent, PendingIntent.FLAG_UPDATE_CURRENT);
this.mediaButtonPendingIntent = PendingIntent.getBroadcast(
context, 0, headsetIntent,
Build.VERSION.SDK_INT >= Build.VERSION_CODES.S ? PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE : PendingIntent.FLAG_UPDATE_CURRENT
);
this.registerMediaButtonEvent();
} catch (Exception e) {
this.mediaButtonAccess=false;
Expand Down
14 changes: 7 additions & 7 deletions src/android/MusicControlsNotification.java
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ private void createBuilder(){
if (infos.dismissable){
builder.setOngoing(false);
Intent dismissIntent = new Intent("music-controls-destroy");
PendingIntent dismissPendingIntent = PendingIntent.getBroadcast(context, 1, dismissIntent, PendingIntent.FLAG_MUTABLE);
PendingIntent dismissPendingIntent = PendingIntent.getBroadcast(context, 1, dismissIntent, Build.VERSION.SDK_INT >= Build.VERSION_CODES.S ? PendingIntent.FLAG_MUTABLE : 0);
builder.setDeleteIntent(dismissPendingIntent);
} else {
builder.setOngoing(true);
Expand Down Expand Up @@ -214,7 +214,7 @@ private void createBuilder(){
Intent resultIntent = new Intent(context, cordovaActivity.getClass());
resultIntent.setAction(Intent.ACTION_MAIN);
resultIntent.addCategory(Intent.CATEGORY_LAUNCHER);
PendingIntent resultPendingIntent = PendingIntent.getActivity(context, 0, resultIntent, PendingIntent.FLAG_MUTABLE);
PendingIntent resultPendingIntent = PendingIntent.getActivity(context, 0, resultIntent, Build.VERSION.SDK_INT >= Build.VERSION_CODES.S ? PendingIntent.FLAG_MUTABLE : 0);
builder.setContentIntent(resultPendingIntent);

//Controls
Expand All @@ -224,35 +224,35 @@ private void createBuilder(){
/* Previous */
nbControls++;
Intent previousIntent = new Intent("music-controls-previous");
PendingIntent previousPendingIntent = PendingIntent.getBroadcast(context, 1, previousIntent, PendingIntent.FLAG_MUTABLE);
PendingIntent previousPendingIntent = PendingIntent.getBroadcast(context, 1, previousIntent, Build.VERSION.SDK_INT >= Build.VERSION_CODES.S ? PendingIntent.FLAG_MUTABLE : 0);
builder.addAction(this.getResourceId(infos.prevIcon, android.R.drawable.ic_media_previous), "", previousPendingIntent);
}
if (infos.isPlaying){
/* Pause */
nbControls++;
Intent pauseIntent = new Intent("music-controls-pause");
PendingIntent pausePendingIntent = PendingIntent.getBroadcast(context, 1, pauseIntent, PendingIntent.FLAG_MUTABLE);
PendingIntent pausePendingIntent = PendingIntent.getBroadcast(context, 1, pauseIntent, Build.VERSION.SDK_INT >= Build.VERSION_CODES.S ? PendingIntent.FLAG_MUTABLE : 0);
builder.addAction(this.getResourceId(infos.pauseIcon, android.R.drawable.ic_media_pause), "", pausePendingIntent);
} else {
/* Play */
nbControls++;
Intent playIntent = new Intent("music-controls-play");
PendingIntent playPendingIntent = PendingIntent.getBroadcast(context, 1, playIntent, PendingIntent.FLAG_MUTABLE);
PendingIntent playPendingIntent = PendingIntent.getBroadcast(context, 1, playIntent, Build.VERSION.SDK_INT >= Build.VERSION_CODES.S ? PendingIntent.FLAG_MUTABLE : 0);
builder.addAction(this.getResourceId(infos.playIcon, android.R.drawable.ic_media_play), "", playPendingIntent);
}

if (infos.hasNext){
/* Next */
nbControls++;
Intent nextIntent = new Intent("music-controls-next");
PendingIntent nextPendingIntent = PendingIntent.getBroadcast(context, 1, nextIntent, PendingIntent.FLAG_MUTABLE);
PendingIntent nextPendingIntent = PendingIntent.getBroadcast(context, 1, nextIntent, Build.VERSION.SDK_INT >= Build.VERSION_CODES.S ? PendingIntent.FLAG_MUTABLE : 0);
builder.addAction(this.getResourceId(infos.nextIcon, android.R.drawable.ic_media_next), "", nextPendingIntent);
}
if (infos.hasClose){
/* Close */
nbControls++;
Intent destroyIntent = new Intent("music-controls-destroy");
PendingIntent destroyPendingIntent = PendingIntent.getBroadcast(context, 1, destroyIntent, PendingIntent.FLAG_MUTABLE);
PendingIntent destroyPendingIntent = PendingIntent.getBroadcast(context, 1, destroyIntent, Build.VERSION.SDK_INT >= Build.VERSION_CODES.S ? PendingIntent.FLAG_MUTABLE : 0);
builder.addAction(this.getResourceId(infos.closeIcon, android.R.drawable.ic_menu_close_clear_cancel), "", destroyPendingIntent);
}

Expand Down

0 comments on commit a973463

Please sign in to comment.