Skip to content
This repository was archived by the owner on Jan 25, 2020. It is now read-only.

Commit 1952dd3

Browse files
committed
* googleassistant support(needed validator though) and mediastyle notification
1 parent 0fd2e7e commit 1952dd3

File tree

3 files changed

+39
-90
lines changed

3 files changed

+39
-90
lines changed

app/src/main/java/com/sahdeepsingh/Bop/notifications/MediaNotificationManager.java

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import android.content.Context;
99
import android.content.Intent;
1010
import android.content.IntentFilter;
11-
import android.graphics.Bitmap;
1211
import android.graphics.Color;
1312
import android.os.Build;
1413
import android.os.RemoteException;
@@ -18,9 +17,8 @@
1817
import android.support.v4.media.session.MediaSessionCompat;
1918
import android.support.v4.media.session.PlaybackStateCompat;
2019

21-
import com.sahdeepsingh.Bop.Activities.PlayingNowList;
20+
import com.sahdeepsingh.Bop.Activities.MainScreen;
2221
import com.sahdeepsingh.Bop.R;
23-
import com.sahdeepsingh.Bop.playerMain.Main;
2422
import com.sahdeepsingh.Bop.services.ServicePlayMusic;
2523
import com.sahdeepsingh.Bop.utils.utils;
2624

@@ -35,15 +33,14 @@
3533
* won't be killed during playback.
3634
*/
3735
public class MediaNotificationManager extends BroadcastReceiver {
38-
public static final String ACTION_PAUSE = "com.example.android.uamp.pause";
39-
public static final String ACTION_PLAY = "com.example.android.uamp.play";
40-
public static final String ACTION_PREV = "com.example.android.uamp.prev";
41-
public static final String ACTION_NEXT = "com.example.android.uamp.next";
42-
public static final String ACTION_STOP = "com.example.android.uamp.stop";
43-
public static final String ACTION_STOP_CASTING = "com.example.android.uamp.stop_cast";
36+
public static final String ACTION_PAUSE = "com.sahdeepsingh.Bop.pause";
37+
public static final String ACTION_PLAY = "com.sahdeepsingh.Bop.play";
38+
public static final String ACTION_PREV = "com.sahdeepsingh.Bop.prev";
39+
public static final String ACTION_NEXT = "com.sahdeepsingh.Bop.next";
40+
public static final String ACTION_STOP = "com.sahdeepsingh.Bop.stop";
4441
private static final String TAG = "Notification";
45-
private static final String CHANNEL_ID = "com.example.android.uamp.MUSIC_CHANNEL_ID";
46-
private static final int NOTIFICATION_ID = 412;
42+
private static final String CHANNEL_ID = "com.sahdeepsingh.Bop.BOP_MUSIC_CHANNEL_ID";
43+
private static final int NOTIFICATION_ID = 777;
4744
private static final int REQUEST_CODE = 100;
4845
private final ServicePlayMusic mService;
4946
private final NotificationManager mNotificationManager;
@@ -89,6 +86,7 @@ public void onSessionDestroyed() {
8986
try {
9087
updateSessionToken();
9188
} catch (RemoteException e) {
89+
e.printStackTrace();
9290
}
9391
}
9492
};
@@ -135,7 +133,7 @@ public void startNotification() {
135133
filter.addAction(ACTION_PAUSE);
136134
filter.addAction(ACTION_PLAY);
137135
filter.addAction(ACTION_PREV);
138-
filter.addAction(ACTION_STOP_CASTING);
136+
filter.addAction(ACTION_STOP);
139137
mService.registerReceiver(this, filter);
140138

141139
mService.startForeground(NOTIFICATION_ID, notification);
@@ -175,6 +173,8 @@ public void onReceive(Context context, Intent intent) {
175173
case ACTION_PREV:
176174
mTransportControls.skipToPrevious();
177175
break;
176+
case ACTION_STOP:
177+
mTransportControls.stop();
178178
default:
179179

180180
}
@@ -205,7 +205,7 @@ private void updateSessionToken() throws RemoteException {
205205
}
206206

207207
private PendingIntent createContentIntent(MediaDescriptionCompat description) {
208-
Intent openUI = new Intent(mService, PlayingNowList.class);
208+
Intent openUI = new Intent(mService, MainScreen.class);
209209
openUI.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
210210

211211
return PendingIntent.getActivity(mService, REQUEST_CODE, openUI,
@@ -219,14 +219,6 @@ private Notification createNotification() {
219219

220220
MediaDescriptionCompat description = mMetadata.getDescription();
221221

222-
Bitmap art = null;
223-
if (description.getIconUri() != null) {
224-
// This sample assumes the iconUri will be a valid URL formatted String, but
225-
// it can actually be any valid Android Uri formatted String.
226-
// async fetch the album art icon
227-
art = utils.getBitmapfromAlbumId(mService, Main.musicService.currentSong);
228-
}
229-
230222
// Notification channels are only supported on Android O+.
231223
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
232224
createNotificationChannel();
@@ -239,7 +231,7 @@ private Notification createNotification() {
239231
notificationBuilder
240232
.setStyle(new androidx.media.app.NotificationCompat.MediaStyle()
241233
// show only play/pause in compact view
242-
.setShowActionsInCompactView(playPauseButtonPosition)
234+
.setShowActionsInCompactView(0, playPauseButtonPosition, 2)
243235
.setShowCancelButton(true)
244236
.setCancelButtonIntent(mStopIntent)
245237
.setMediaSession(mSessionToken))
@@ -251,7 +243,7 @@ private Notification createNotification() {
251243
.setContentIntent(createContentIntent(description))
252244
.setContentTitle(description.getTitle())
253245
.setContentText(description.getSubtitle())
254-
.setLargeIcon(art);
246+
.setLargeIcon(description.getIconBitmap());
255247

256248

257249
setNotificationPlaybackState(notificationBuilder);
@@ -263,7 +255,8 @@ private int addActions(final NotificationCompat.Builder notificationBuilder) {
263255

264256
int playPauseButtonPosition = 0;
265257
// If skip to previous action is enabled
266-
if ((mPlaybackState.getActions() & PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS) != 0) {
258+
//left with "true" to provide customization for user ot change if s/he wants to
259+
if ((true)) {
267260
notificationBuilder.addAction(R.drawable.ic_previous, "previous", mPreviousIntent);
268261

269262
// If there is a "skip to previous" button, the play/pause button will
@@ -289,11 +282,13 @@ private int addActions(final NotificationCompat.Builder notificationBuilder) {
289282
notificationBuilder.addAction(new NotificationCompat.Action(icon, label, intent));
290283

291284
// If skip to next action is enabled
292-
if ((mPlaybackState.getActions() & PlaybackStateCompat.ACTION_SKIP_TO_NEXT) != 0) {
285+
if (true) {
293286
notificationBuilder.addAction(R.drawable.ic_skip, "next", mNextIntent);
294287
}
295288

289+
notificationBuilder.addAction(R.drawable.ic_cancel, "cancel", mStopIntent);
296290
return playPauseButtonPosition;
291+
297292
}
298293

299294
private void setNotificationPlaybackState(NotificationCompat.Builder builder) {

app/src/main/java/com/sahdeepsingh/Bop/services/ServicePlayMusic.java

Lines changed: 17 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import android.content.IntentFilter;
1010
import android.media.AudioManager;
1111
import android.media.MediaPlayer;
12-
import android.media.session.MediaController;
1312
import android.net.Uri;
1413
import android.os.Binder;
1514
import android.os.Bundle;
@@ -39,7 +38,6 @@
3938
import androidx.annotation.NonNull;
4039
import androidx.annotation.Nullable;
4140
import androidx.media.MediaBrowserServiceCompat;
42-
import androidx.media.MediaSessionManager;
4341
import androidx.media.session.MediaButtonReceiver;
4442

4543
public class ServicePlayMusic extends MediaBrowserServiceCompat
@@ -49,21 +47,16 @@ public class ServicePlayMusic extends MediaBrowserServiceCompat
4947
AudioManager.OnAudioFocusChangeListener {
5048

5149

52-
public static final String ACTION_CMD = "com.example.android.uamp.ACTION_CMD";
50+
public static final String ACTION_CMD = "com.sahdeepsingh.Bop.ACTION_CMD";
5351

54-
public static final String CMD_NAME = "CMD_NAME";
52+
public static final String CMD_NAME = "BopPlayer";
5553

5654
public static final String CMD_PAUSE = "CMD_PAUSE";
5755

5856
// Delay stopSelf by using a handler.
5957

6058
private static final int STOP_DELAY = 30000;
6159

62-
public static final String BROADCAST_ORDER_PLAY = "com.sahdeepsingh.Bop.action.PLAY";
63-
public static final String BROADCAST_ORDER_PAUSE = "com.sahdeepsingh.Bop.action.PAUSE";
64-
public static final String BROADCAST_ORDER_STOP = "com.sahdeepsingh.Bop.action.STOP";
65-
public static final String BROADCAST_ORDER_SKIP = "com.sahdeepsingh.Bop.action.SKIP";
66-
public static final String BROADCAST_ORDER_REWIND = "com.sahdeepsingh.Bop.action.REWIND";
6760

6861
// The tag we put on debug messages
6962
final static String TAG = "MusicService";
@@ -105,44 +98,9 @@ public class ServicePlayMusic extends MediaBrowserServiceCompat
10598
*/
10699

107100
public MediaSessionCompat mMediaSessionCompat;
108-
public MediaSessionManager mediaSessionManager;
109-
public MediaController mediaController;
110101

111102
AudioWidget audioWidget;
112-
BroadcastReceiver headsetBroadcastReceiver = new BroadcastReceiver() {
113-
@Override
114-
public void onReceive(Context context, Intent intent) {
115-
116-
String action = intent.getAction();
117-
118-
// Headphones just connected (or not)
119-
if (action != null && action.equals(Intent.ACTION_HEADSET_PLUG)) {
120-
121-
Log.w(TAG, "headset plug");
122-
boolean connectedHeadphones = (intent.getIntExtra("state", 0) == 1);
123-
boolean connectedMicrophone = (intent.getIntExtra("microphone", 0) == 1) && connectedHeadphones;
124-
125-
// User just connected headphone and the player was paused,
126-
// so we should restart the music.
127-
if (connectedMicrophone && (serviceState == ServiceState.Paused)) {
128103

129-
// Will only do it if it's Setting is enabled, of course
130-
if (Main.settings.get("play_headphone_on", true)) {
131-
132-
}
133-
}
134-
135-
// I wonder what's this for
136-
String headsetName = intent.getStringExtra("name");
137-
138-
if (connectedHeadphones) {
139-
String text = context.getString(R.string.headphone_connected) + headsetName;
140-
141-
Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
142-
}
143-
}
144-
}
145-
};
146104
/**
147105
* Android Media Player - we control it in here.
148106
*/
@@ -195,25 +153,30 @@ public void onPlayFromMediaId(String mediaId, Bundle extras) {
195153
@Override
196154
public void onPause() {
197155
super.onPause();
156+
Log.e("lol", "pause");
198157
pausePlayer();
199158
}
200159

201160
@Override
202161
public void onSkipToNext() {
203162
super.onSkipToNext();
204163
next(true);
164+
playSong();
165+
Log.e("lol", "next");
205166
}
206167

207168
@Override
208169
public void onSkipToPrevious() {
209170
super.onSkipToPrevious();
210171
previous(true);
172+
playSong();
211173
}
212174

213175
@Override
214176
public void onStop() {
215177
super.onStop();
216-
stopSelf();
178+
stopMusicPlayer();
179+
Log.e("lol", "stop");
217180
}
218181
};
219182

@@ -248,9 +211,6 @@ public void onCreate() {
248211
throw new IllegalStateException("Could not create a MediaNotificationManager", e);
249212
}
250213

251-
// to user plugging the headset.
252-
IntentFilter headsetFilter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
253-
registerReceiver(headsetBroadcastReceiver, headsetFilter);
254214

255215
Log.w(TAG, "onCreate");
256216

@@ -321,9 +281,11 @@ public void stopMusicPlayer() {
321281
if (player == null)
322282
return;
323283

284+
Main.mainMenuHasNowPlayingItem = false;
324285
player.stop();
325286
player.release();
326287
player = null;
288+
notificationManager.stopNotification();
327289

328290
Log.w(TAG, "stopMusicPlayer");
329291
}
@@ -531,13 +493,8 @@ public void onDestroy() {
531493
audioManager.abandonAudioFocus(this);
532494

533495
stopMusicPlayer();
496+
Log.w(TAG, "onDestroy");
534497

535-
if (player != null)
536-
//player.release();
537-
538-
Log.w(TAG, "onDestroy");
539-
540-
unregisterReceiver(headsetBroadcastReceiver);
541498
unregisterReceiver(mNoisyReceiver);
542499
super.onDestroy();
543500
}
@@ -798,6 +755,12 @@ public void notifyCurrentSong() {
798755
if (currentSong == null)
799756
return;
800757

758+
if (!requestAudioFocus()) {
759+
//Stop the service.
760+
stopSelf();
761+
Toast.makeText(getApplicationContext(), "FUCK", Toast.LENGTH_LONG).show();
762+
return;
763+
}
801764
/*if (notification == null)
802765
notification = new NotificationMusic();
803766

app/src/main/java/com/sahdeepsingh/Bop/utils/utils.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
import com.sahdeepsingh.Bop.SongData.Song;
2020
import com.sahdeepsingh.Bop.playerMain.Main;
2121

22-
import java.io.FileNotFoundException;
23-
import java.io.IOException;
2422
import java.util.Collections;
2523
import java.util.HashMap;
2624
import java.util.LinkedHashMap;
@@ -110,19 +108,12 @@ public static HashMap<String, Integer> sortMapByValue(HashMap<String, Integer> h
110108
}
111109

112110
public static Bitmap getBitmapfromAlbumId(Context context,Song localItem){
113-
Bitmap bitmap = null;
111+
Bitmap bitmap = BitmapFactory.decodeResource(Resources.getSystem(), R.mipmap.ic_launcher_foreground);
114112
Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
115113
Uri uri = ContentUris.withAppendedId(sArtworkUri,Long.parseLong(localItem.getAlbumid()));
116114
try {
117115
bitmap = MediaStore.Images.Media.getBitmap(context.getContentResolver(), uri);
118-
} catch (FileNotFoundException e) {
119-
bitmap = BitmapFactory.decodeResource(Resources.getSystem(),R.mipmap.ic_launcher);
120-
e.printStackTrace();
121-
} catch (IOException e) {
122-
bitmap = BitmapFactory.decodeResource(Resources.getSystem(),R.mipmap.ic_launcher);
123-
e.printStackTrace();
124-
} catch (Exception e){
125-
bitmap = BitmapFactory.decodeResource(Resources.getSystem(),R.mipmap.ic_launcher);
116+
} catch (Exception e) {
126117
e.printStackTrace();
127118
}
128119
return bitmap;

0 commit comments

Comments
 (0)