Skip to content
This repository has been archived by the owner on Oct 6, 2022. It is now read-only.

Commit

Permalink
android lock screen controls fix
Browse files Browse the repository at this point in the history
  • Loading branch information
lufinkey authored and tanguyantoine committed Jul 4, 2019
1 parent 0969cbd commit 6fcabed
Showing 1 changed file with 37 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,36 @@ private void createChannel(ReactApplicationContext context) {
mNotificationManager.createNotificationChannel(mChannel);
}

private boolean hasControl(long control) {
if((controls & control) == control) {
return true;
}
return false;
}

private void updateNotificationMediaStyle() {
if (!(Build.MANUFACTURER.toLowerCase(Locale.getDefault()).contains("huawei") && Build.VERSION.SDK_INT < Build.VERSION_CODES.M)) {
MediaStyle style = new MediaStyle();
style.setMediaSession(session.getSessionToken());
int controlCount = 0;
if(hasControl(PlaybackStateCompat.ACTION_PLAY) || hasControl(PlaybackStateCompat.ACTION_PAUSE) || hasControl(PlaybackStateCompat.ACTION_PLAY_PAUSE)) {
controlCount += 1;
}
if(hasControl(PlaybackStateCompat.ACTION_SKIP_TO_NEXT)) {
controlCount += 1;
}
if(hasControl(PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS)) {
controlCount += 1;
}
int[] actions = new int[controlCount];
for(int i=0; i<actions.length; i++) {
actions[i] = i;
}
style.setShowActionsInCompactView(actions);
nb.setStyle(style);
}
}

public void init() {
if (init) return;

Expand Down Expand Up @@ -142,12 +172,7 @@ public void init() {
nb = new NotificationCompat.Builder(context, CHANNEL_ID);
nb.setVisibility(Notification.VISIBILITY_PUBLIC);

if (!(Build.MANUFACTURER.toLowerCase(Locale.getDefault()).contains("huawei") && Build.VERSION.SDK_INT < Build.VERSION_CODES.M)) {
MediaStyle style = new MediaStyle();
style.setMediaSession(session.getSessionToken());
style.setShowActionsInCompactView(new int[]{0,1,2});
nb.setStyle(style);
}
updateNotificationMediaStyle();

state = pb.build();

Expand Down Expand Up @@ -459,6 +484,12 @@ synchronized public void enableControl(String control, boolean enable, ReadableM

state = pb.build();
session.setPlaybackState(state);

updateNotificationMediaStyle();

if(session.isActive()) {
notification.show(nb, isPlaying);
}
}

private Bitmap loadArtwork(String url, boolean local) {
Expand Down

0 comments on commit 6fcabed

Please sign in to comment.