Skip to content

Commit c6edf44

Browse files
committed
MiniPlayer now updates when MainActivity opened from notification
1 parent 93146b1 commit c6edf44

File tree

6 files changed

+152
-31
lines changed

6 files changed

+152
-31
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
//package uk.arcalder.Kanta;
2+
//
3+
//import android.support.v7.widget.RecyclerView;
4+
//import android.util.Log;
5+
//import android.view.LayoutInflater;
6+
//import android.view.View;
7+
//import android.view.ViewGroup;
8+
//import android.widget.ImageView;
9+
//import android.widget.TextView;
10+
//
11+
//import com.squareup.picasso.Picasso;
12+
//
13+
//import java.util.ArrayList;
14+
//
15+
///**
16+
// * Created by Zynch on 22/04/2018.
17+
// */
18+
//
19+
//public class ArtistListAdapter extends RecyclerView.Adapter<ArtistListAdapter.ViewHolder> {
20+
// private static final String TAG = ArtistListAdapter.class.getSimpleName();
21+
// // As per https://developer.android.com/guide/topics/ui/layout/recyclerview.html#java
22+
//
23+
// public static final int PLAYTYPE_FROM_SONGS = 0;
24+
// public static final int PLAYTYPE_FROM_ALBUM = 1;
25+
// public static final int PLAYTYPE_FROM_ARTIST = 2;
26+
//
27+
// private ArrayList<Artist> adapterArtists;
28+
//
29+
// public static int addedItems = 0;
30+
//
31+
// public int addItem(){
32+
// return addedItems++;
33+
// }
34+
//
35+
// public class ViewHolder extends RecyclerView.ViewHolder {
36+
//
37+
// public ImageView albumListArtView;
38+
// public TextView albumListArtistView;
39+
//
40+
// public ViewHolder(View itemView) {
41+
// super(itemView);
42+
// this.albumListArtView = (ImageView) itemView.findViewById(R.id.artist_item_art);
43+
// this.albumListArtistView = (TextView) itemView.findViewById(R.id.artist_item_name);
44+
// }
45+
// }
46+
//
47+
// public ArtistListAdapter(ArrayList<Artist> album){
48+
// Log.d(TAG, "SongListAdapter created");
49+
// this.adapterArtists = album;
50+
// }
51+
//
52+
// // Create new views (invoked by the layout manager)
53+
// @Override
54+
// public ArtistListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
55+
// // create a new view
56+
// Log.d(TAG, "onCreateViewHolder");
57+
// View mView = LayoutInflater.from(parent.getContext()).inflate(R.layout.artist_list_item, parent, false);
58+
// return new ArtistListAdapter.ViewHolder(mView);
59+
// }
60+
//
61+
// // Replace the contents of a view (invoked by the layout manager)
62+
// @Override
63+
// public void onBindViewHolder(ViewHolder holder, int position) {
64+
// Artist artist;
65+
// try {
66+
// artist = adapterArtists.get(position);
67+
// } catch (Exception e){
68+
// Log.e(TAG, "onBindViewHolder: ", e);
69+
// return;
70+
// }
71+
//
72+
// // Set art
73+
// if (artist.getName() != null){
74+
// // Picasso doesn't need context, glide does
75+
// // Glide is apparently better, but I can't be bothered adapting
76+
// // all the code just for one lib
77+
// // As per http://square.github.io/picasso/
78+
// try {
79+
// Picasso.get().load(artist.getAlbumArt()).fit().centerCrop().into(holder.albumListArtView);
80+
// } catch (IllegalArgumentException iae){
81+
// Log.d(TAG, "Picasso tried to load albums.getArt = " +artist.getAlbumArt());
82+
// }
83+
// }
84+
//
85+
// // Set Title
86+
// holder.albumListTitleView.setText(artist.getName());
87+
// // Set Artist/Album
88+
// holder.albumListArtistView.setText(String.format("%s", artist.getArtist()));
89+
//
90+
// }
91+
//
92+
// // Return the size of your dataset (invoked by the layout manager)
93+
// @Override
94+
// public int getItemCount() {
95+
// return adapterArtists.size();
96+
// }
97+
//
98+
//}

app/src/main/java/uk/arcalder/Kanta/MainActivity.java

+11-2
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ public void onConnectionFailed() {
8787
}
8888
};
8989

90+
private static int currentPlaybackState;
91+
9092
// Controller callback
9193
private MediaControllerCompat.Callback mMediaControllerCompatCallback = new MediaControllerCompat.Callback() {
9294

@@ -101,24 +103,29 @@ public void onPlaybackStateChanged(PlaybackStateCompat state) {
101103
switch( state.getState() ) {
102104
case PlaybackStateCompat.STATE_PLAYING: {
103105
Log.d(TAG, "onPlaybackStateChanged to: STATE_PLAYING");
106+
currentPlaybackState = PlaybackStateCompat.STATE_PLAYING;
104107
miniPlayerFragment(true, true);
105108
break;
106109
}
107110
case PlaybackStateCompat.STATE_PAUSED: {
108-
Log.d(TAG, "onPlaybackStateChanged to: STATE_PLAYING");
111+
Log.d(TAG, "onPlaybackStateChanged to: STATE_PAUSED");
112+
currentPlaybackState = PlaybackStateCompat.STATE_PAUSED;
109113
miniPlayerFragment(false, true);
110114
break;
111115
}
112116
case PlaybackStateCompat.STATE_SKIPPING_TO_NEXT:
113117
Log.d(TAG, "onPlaybackStateChanged to: STATE_SKIPPING_TO_NEXT");
118+
currentPlaybackState = PlaybackStateCompat.STATE_SKIPPING_TO_NEXT;
114119
miniPlayerFragment(true, true);
115120
break;
116121
case PlaybackStateCompat.STATE_SKIPPING_TO_PREVIOUS:
117122
Log.d(TAG, "onPlaybackStateChanged to: STATE_SKIPPING_TO_PREVIOUS");
123+
currentPlaybackState = PlaybackStateCompat.STATE_SKIPPING_TO_PREVIOUS;
118124
miniPlayerFragment(true, true);
119125
break;
120126
default:
121127
Log.d(TAG, "onPlaybackStateChanged to: *STATE_NOT_CARE_ABOUT");
128+
currentPlaybackState = PlaybackStateCompat.STATE_NONE;
122129
miniPlayerFragment(false, false);
123130
}
124131
}
@@ -226,6 +233,9 @@ protected void onResume() {
226233
super.onResume();
227234
Log.d(TAG, "onResume Called");
228235
setVolumeControlStream(AudioManager.STREAM_MUSIC);
236+
miniPlayerFragment((currentPlaybackState == PlaybackStateCompat.STATE_PLAYING),
237+
(currentPlaybackState != PlaybackStateCompat.STATE_NONE &&
238+
currentPlaybackState != PlaybackStateCompat.STATE_STOPPED));
229239
}
230240

231241
@Override
@@ -462,6 +472,5 @@ public void clickMiniPlayerPlayPause(boolean state) {
462472
Log.d(TAG, "clickMiniPlayerPlayPause: play");
463473
mMediaControllerCompat.getTransportControls().play();
464474
}
465-
466475
}
467476
}

app/src/main/java/uk/arcalder/Kanta/MusicLibrary.java

+1-9
Original file line numberDiff line numberDiff line change
@@ -364,11 +364,6 @@ public Song getNextSongFromAny(){
364364
return playSet.get(current_position);
365365
}
366366
Log.d(TAG, "getCurrent: getting playSet song");
367-
368-
} else if (!allSongs.isEmpty()){
369-
// get a random song
370-
Log.d(TAG, "getCurrent: getting random song");
371-
allSongs.get(ThreadLocalRandom.current().nextInt(0, allSongs.size()));
372367
}
373368

374369
// Literally nothing
@@ -400,10 +395,7 @@ public int getSizeOfPlaySet(){
400395
// get next queued song
401396
public Song getNextQueueSong(){
402397
Log.d(TAG, "getNextQueueSong");
403-
if (!songQueue.isEmpty()) {
404-
return songQueue.remove(0);
405-
}
406-
return null;
398+
return songQueue.remove(0);
407399
}
408400

409401
// add a song to the queue

app/src/main/java/uk/arcalder/Kanta/MusicPlayerService.java

+39-17
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,25 @@ public void onReceive(Context context, Intent intent) {
134134

135135
@Override
136136
public void onPlay() {
137-
super.onPlay();
138137

138+
if( !mMediaPlayer.isPlaying() ) {
139+
mMediaPlayer.start();
140+
//Doesn't update itself
141+
setMediaPlaybackState(PlaybackStateCompat.STATE_PLAYING);
142+
}
143+
144+
// Try to get focus
145+
if(!getAudioFocus()){
146+
Log.d(TAG, "onPlayFromMediaId: Couldn't get audio focus");
147+
}
148+
149+
//Should be started but sometimes not :s
150+
if(!isServiceStarted){
151+
Log.d(TAG, "onPlay: Starting Service");
152+
startService(new Intent(getApplicationContext(), MusicPlayerService.class));
153+
}
154+
155+
super.onPlay();
139156
// TODO NOTE: onPlay is really onResume/onNowPlaying, don't try to play songs here
140157
// //Might have been playing something else
141158
// mMediaPlayer.reset();
@@ -175,7 +192,6 @@ public void onPause() {
175192
if( mMediaPlayer.isPlaying() ) {
176193
mMediaPlayer.pause();
177194
removeNoisyReceiver();
178-
setMediaPlaybackState(PlaybackState.STATE_PAUSED);
179195
//TODO fix?
180196
//Notifications
181197
setMediaPlaybackState(PlaybackStateCompat.STATE_PAUSED);
@@ -253,14 +269,16 @@ public void onStop() {
253269

254270
@Override
255271
public void onSkipToNext() {
256-
Song nextSong = mMusicLibrary.getCurrentSong(); // TODO GET NEXT SONG
257-
if (null != nextSong){
272+
Song nextSong = mMusicLibrary.getNextSongFromAny(); // TODO GET NEXT SONG
273+
if (null != nextSong) {
274+
Log.d(TAG, "onSkipToNext: nextSong was null");
258275
mMusicLibrary.setCurrentSong(nextSong);
259-
276+
} else {
277+
Log.d(TAG, "onSkipToNext: nextSong was null");
260278
}
261-
super.onSkipToNext();
262279
// TODO REMOVE - JUST FOR TESTING
263280
onPlay();
281+
super.onSkipToNext();
264282
}
265283

266284
@Override
@@ -409,17 +427,19 @@ private void removeNoisyReceiver(){
409427
}
410428

411429
private void initMediaPlayer() {
412-
mAudioManager = (AudioManager) getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
413-
mMediaPlayer = (mMediaPlayer == null) ? new MediaPlayer() : mMediaPlayer;
414-
mMediaPlayer.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
415-
mMediaPlayer.setAudioAttributes(new AudioAttributes.Builder()
416-
.setUsage(AudioAttributes.USAGE_MEDIA)
417-
.setFlags(AudioAttributes.CONTENT_TYPE_MUSIC)
418-
.build());
419-
mMediaPlayer.setVolume(1.0f, 1.0f);
420-
421-
mMediaPlayer.setOnPreparedListener(this);
422-
mMediaPlayer.setOnCompletionListener(this);
430+
mAudioManager = (mAudioManager == null) ? (AudioManager) getApplicationContext().getSystemService(Context.AUDIO_SERVICE) : mAudioManager;
431+
if ( mMediaPlayer == null) {
432+
mMediaPlayer = new MediaPlayer();
433+
mMediaPlayer.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
434+
mMediaPlayer.setAudioAttributes(new AudioAttributes.Builder()
435+
.setUsage(AudioAttributes.USAGE_MEDIA)
436+
.setFlags(AudioAttributes.CONTENT_TYPE_MUSIC)
437+
.build());
438+
mMediaPlayer.setVolume(1.0f, 1.0f);
439+
440+
mMediaPlayer.setOnPreparedListener(this);
441+
mMediaPlayer.setOnCompletionListener(this);
442+
}
423443
}
424444

425445
private void initMediaSession() {
@@ -436,6 +456,8 @@ private void initMediaSession() {
436456
mMediaSession.setPlaybackState( new PlaybackStateCompat.Builder()
437457
.setActions(
438458
PlaybackState.ACTION_PLAY |
459+
PlaybackState.ACTION_SKIP_TO_NEXT |
460+
PlaybackState.ACTION_SKIP_TO_PREVIOUS |
439461
PlaybackState.ACTION_PLAY_PAUSE).build());
440462

441463
Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);

build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ buildscript {
77
jcenter()
88
}
99
dependencies {
10-
classpath 'com.android.tools.build:gradle:3.0.1'
10+
classpath 'com.android.tools.build:gradle:3.1.2'
1111

1212

1313
// NOTE: Do not place your application dependencies here; they belong
+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#Tue Mar 13 22:30:30 GMT 2018
1+
#Tue Apr 24 14:25:42 BST 2018
22
distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip

0 commit comments

Comments
 (0)