Last active
August 7, 2020 08:50
-
-
Save bengongon97/43df45f1d3bc8e9d984925ca112af077 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private List<HwAudioStatusListener> mTempListeners = new CopyOnWriteArrayList<>(); //a global variable | |
private void doListenersAndNotifications(final Context context) { | |
new Handler(Looper.getMainLooper()).post(new Runnable() { | |
@Override | |
public void run() { | |
for (HwAudioStatusListener listener : mTempListeners) { | |
try { | |
mHwAudioManager.addPlayerStatusListener(listener); | |
} catch (RemoteException e) { | |
Log.e("TAG", "TAG", e); | |
} | |
} | |
mHwAudioConfigManager.setSaveQueue(true); | |
mHwAudioConfigManager.setNotificationFactory(new INotificationFactory() { | |
@Override | |
public Notification createNotification(NotificationConfig notificationConfig) { | |
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) { | |
builder = new NotificationCompat.Builder(getApplication(), null); | |
RemoteViews remoteViews = new RemoteViews(getApplication().getPackageName(), R.layout.notification_player); | |
builder.setContent(remoteViews); | |
builder.setSmallIcon(R.drawable.icon_notifaction_music); | |
builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC); | |
builder.setCustomBigContentView(remoteViews); | |
NotificationUtils.addChannel(getApplication(), NotificationUtils.NOTIFY_CHANNEL_ID_PLAY, builder); | |
boolean isQueueEmpty = mHwAudioManager.getQueueManager().isQueueEmpty(); | |
Bitmap bitmap; | |
bitmap = notificationConfig.getBitmap(); | |
setBitmap(remoteViews, bitmap); | |
boolean isPlaying = mHwAudioManager.getPlayerManager().isPlaying() && !isQueueEmpty; | |
remoteViews.setImageViewResource(R.id.image_toggle, isPlaying ? R.drawable.ic_notification_stop : R.drawable.ic_notification_play); | |
HwAudioPlayItem playItem = mHwAudioManager.getQueueManager().getCurrentPlayItem(); | |
remoteViews.setTextViewText(R.id.text_song, playItem.getAudioTitle()); | |
remoteViews.setTextViewText(R.id.text_artist, playItem.getSinger()); | |
remoteViews.setImageViewResource(R.id.image_last, R.drawable.ic_notification_before); | |
remoteViews.setImageViewResource(R.id.image_next, R.drawable.ic_notification_next); | |
remoteViews.setOnClickPendingIntent(R.id.image_last, notificationConfig.getPrePendingIntent()); | |
remoteViews.setOnClickPendingIntent(R.id.image_toggle, notificationConfig.getPlayPendingIntent()); | |
remoteViews.setOnClickPendingIntent(R.id.image_next, notificationConfig.getNextPendingIntent()); | |
remoteViews.setOnClickPendingIntent(R.id.image_close, getCancelPendingIntent()); | |
remoteViews.setOnClickPendingIntent(R.id.layout_content, getMainIntent()); | |
return builder.build(); | |
} | |
else{ | |
NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplication(), null); | |
RemoteViews remoteViews = new RemoteViews(getApplication().getPackageName(), R.layout.statusbar); | |
builder.setContent(remoteViews); | |
builder.setSmallIcon(R.drawable.icon_notifaction_music); | |
builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC); | |
builder.setCustomBigContentView(remoteViews); | |
NotificationUtils.addChannel(getApplication(), NotificationUtils.NOTIFY_CHANNEL_ID_PLAY, builder); | |
boolean isQueueEmpty = mHwAudioManager.getQueueManager().isQueueEmpty(); | |
Bitmap bitmap; | |
bitmap = notificationConfig.getBitmap(); | |
setBitmap(remoteViews, bitmap); | |
boolean isPlaying = mHwAudioManager.getPlayerManager().isPlaying() && !isQueueEmpty; | |
remoteViews.setImageViewResource(R.id.widget_id_control_play, | |
isPlaying ? R.drawable.notify_btn_pause_selector : R.drawable.notify_btn_play_selector); | |
HwAudioPlayItem playItem = mHwAudioManager.getQueueManager().getCurrentPlayItem(); | |
remoteViews.setTextViewText(R.id.trackname, playItem.getAudioTitle()); | |
remoteViews.setTextViewText(R.id.artistalbum, playItem.getSinger()); | |
remoteViews.setImageViewResource(R.id.widget_id_control_prev, R.drawable.notify_btn_close_selector); | |
remoteViews.setImageViewResource(R.id.widget_id_control_next, R.drawable.notify_btn_next_selector); | |
remoteViews.setOnClickPendingIntent(R.id.widget_id_control_prev, getCancelPendingIntent()); | |
remoteViews.setOnClickPendingIntent(R.id.widget_id_control_play, notificationConfig.getPlayPendingIntent()); | |
remoteViews.setOnClickPendingIntent(R.id.widget_id_control_next, notificationConfig.getNextPendingIntent()); | |
remoteViews.setOnClickPendingIntent(R.id.statusbar_layout, getMainIntent()); | |
return builder.build(); | |
} | |
} | |
}); | |
} | |
}); | |
} | |
private void setBitmap(RemoteViews remoteViews, Bitmap bitmap) { | |
HwAudioPlayItem tmpItem = mHwAudioQueueManager.getCurrentPlayItem(); | |
Bitmap imageCoverOfMusic = getBitmapOfCover(tmpItem); | |
if(imageCoverOfMusic != null){ | |
Log.i("TAG", "Notification bitmap not empty"); | |
remoteViews.setImageViewBitmap(R.id.image_cover, imageCoverOfMusic); | |
} | |
else{ | |
if (bitmap != null) { | |
Log.i("TAG", "Notification bitmap not empty"); | |
remoteViews.setImageViewBitmap(R.id.image_cover, bitmap); | |
} else { | |
Log.w("TAG", "Notification bitmap is null"); | |
remoteViews.setImageViewResource(R.id.image_cover, R.drawable.icon_notifaction_default); | |
} | |
} | |
} | |
private Bitmap getAlbumImage(String path) { | |
try{ | |
android.media.MediaMetadataRetriever mmr = new MediaMetadataRetriever(); | |
mmr.setDataSource(path); | |
byte[] data = mmr.getEmbeddedPicture(); | |
if (data != null) | |
return BitmapFactory.decodeByteArray(data, 0, data.length); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
return null; | |
} | |
return null; | |
} | |
public Bitmap getBitmapOfCover(HwAudioPlayItem currItem){ | |
if(currItem != null) { | |
String currentSongPath = currItem.getFilePath(); | |
if (currentSongPath != null) { | |
Bitmap tmpMap = getAlbumImage(currentSongPath); | |
binding.albumPictureImageView.setImageBitmap(tmpMap); | |
return tmpMap; | |
} | |
} | |
return null; | |
} | |
private PendingIntent getCancelPendingIntent() { | |
Log.i("TAG", "getCancelPendingIntent"); | |
Intent closeIntent = new Intent("com.menes.audiokittryoutapp.cancel_notification"); | |
closeIntent.setPackage(getApplication().getPackageName()); | |
return PendingIntent.getBroadcast(getApplication(), 2, closeIntent, PendingIntent.FLAG_UPDATE_CURRENT); | |
} | |
private PendingIntent getMainIntent() { | |
Intent intent = new Intent("android.intent.action.MAIN"); | |
intent.addCategory("android.intent.category.LAUNCHER"); | |
intent.setClass(getApplication().getBaseContext(), MainActivity.class); | |
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); | |
return PendingIntent.getActivity(getApplication(), 0, intent, 0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment