Skip to content

Commit

Permalink
only get new token when needed. remove mPreparedMediaPlayer
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobobryant committed Aug 3, 2017
1 parent ce01186 commit b6f0f5a
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 57 deletions.
1 change: 1 addition & 0 deletions src/com/jacobobryant/moody/Moody.java
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ public static void add_to_library(Context c, List<Metadata> songs) {
}
db.setTransactionSuccessful();
db.endTransaction();
db.close();
}

private static String match_clause(Metadata s) {
Expand Down
39 changes: 27 additions & 12 deletions src/com/jacobobryant/moody/vanilla/LibraryActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -239,13 +239,18 @@ public void onCreate(Bundle state)
}

// spotify stuff
AuthenticationRequest.Builder builder =
new AuthenticationRequest.Builder(C.CLIENT_ID,
AuthenticationResponse.Type.TOKEN, C.REDIRECT_URI);
builder.setScopes(new String[]{"user-read-private", "streaming",
"user-top-read"});
AuthenticationRequest request = builder.build();
AuthenticationClient.openLoginActivity(this, 666, request);
if (System.currentTimeMillis() / 1000 > settings.getLong(PrefKeys.SPOTIFY_TOKEN_EXPIRATION, 0)) {
Log.d(C.TAG, "refreshing spotify token");
AuthenticationRequest.Builder builder =
new AuthenticationRequest.Builder(C.CLIENT_ID,
AuthenticationResponse.Type.TOKEN, C.REDIRECT_URI);
builder.setScopes(new String[]{"user-read-private", "streaming",
"user-top-read"});
AuthenticationRequest request = builder.build();
AuthenticationClient.openLoginActivity(this, 666, request);
} else {
Log.d(C.TAG, "spotify token is still valid");
}

Log.d(C.TAG, "finished LibraryActivity.onCreate()");
}
Expand All @@ -256,11 +261,21 @@ protected void onActivityResult(int requestCode, int resultCode, Intent intent)
// Check if result comes from the correct activity
if (requestCode == 666) {
AuthenticationResponse response = AuthenticationClient.getResponse(resultCode, intent);
if (response.getType() == AuthenticationResponse.Type.TOKEN) {
Log.d(C.TAG, "spotify access token: " + response.getAccessToken());
SharedPreferences settings = PlaybackService.getSettings(this);
settings.edit().putString(PrefKeys.SPOTIFY_TOKEN,
response.getAccessToken()).commit();
switch (response.getType()) {
case TOKEN:
Log.d(C.TAG, "spotify access token: " + response.getAccessToken());
SharedPreferences settings = PlaybackService.getSettings(this);
settings.edit()
.putString(PrefKeys.SPOTIFY_TOKEN, response.getAccessToken())
.putLong(PrefKeys.SPOTIFY_TOKEN_EXPIRATION,
response.getExpiresIn() + System.currentTimeMillis() / 1000)
.commit();
break;
case ERROR:
Log.e(C.TAG, "Spotify auth error: " + response.getError());
break;
default:
break;
}
}
}
Expand Down
54 changes: 27 additions & 27 deletions src/com/jacobobryant/moody/vanilla/PlaybackService.java
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ public final class PlaybackService extends Service
private Looper mLooper;
private Handler mHandler;
VanillaMediaPlayer mMediaPlayer;
VanillaMediaPlayer mPreparedMediaPlayer;
//VanillaMediaPlayer mPreparedMediaPlayer;
private boolean mMediaPlayerInitialized;
private boolean mMediaPlayerAudioFxActive;
private PowerManager.WakeLock mWakeLock;
Expand Down Expand Up @@ -433,9 +433,9 @@ public void onCreate()
int state = loadState();

mMediaPlayer = getNewMediaPlayer();
mPreparedMediaPlayer = getNewMediaPlayer();
//mPreparedMediaPlayer = getNewMediaPlayer();
// We only have a single audio session
mPreparedMediaPlayer.setAudioSessionId(mMediaPlayer.getAudioSessionId());
//mPreparedMediaPlayer.setAudioSessionId(mMediaPlayer.getAudioSessionId());

mBastpUtil = new BastpUtil();
mReadahead = new ReadaheadThread();
Expand Down Expand Up @@ -589,10 +589,10 @@ public void onDestroy()
mMediaPlayer = null;
}

if (mPreparedMediaPlayer != null) {
mPreparedMediaPlayer.release();
mPreparedMediaPlayer = null;
}
//if (mPreparedMediaPlayer != null) {
// mPreparedMediaPlayer.release();
// mPreparedMediaPlayer = null;
//}

try {
unregisterReceiver(mReceiver);
Expand Down Expand Up @@ -654,13 +654,13 @@ private void preparePlayCountsUpdate(int delta) {
*/
private void refreshReplayGainValues() {
applyReplayGain(mMediaPlayer);
applyReplayGain(mPreparedMediaPlayer);
//applyReplayGain(mPreparedMediaPlayer);
}

private void refreshDuckingValues() {
float duckingFactor = ((float) mVolumeDuringDucking)/100f;
mMediaPlayer.setDuckingFactor(duckingFactor);
mPreparedMediaPlayer.setDuckingFactor(duckingFactor);
//mPreparedMediaPlayer.setDuckingFactor(duckingFactor);
}

/***
Expand Down Expand Up @@ -760,23 +760,23 @@ private void triggerGaplessUpdate() {

//if(doGapless == true) {
if (false) {
try {
if(nextSong.path.equals(mPreparedMediaPlayer.getDataSource()) == false) {
// Prepared MP has a different data source: We need to re-initalize
// it and set it as the next MP for the active media player
mPreparedMediaPlayer.reset();
prepareMediaPlayer(mPreparedMediaPlayer, nextSong.path);
mMediaPlayer.setNextMediaPlayer(mPreparedMediaPlayer);
}
if(mMediaPlayer.hasNextMediaPlayer() == false) {
// We can reuse the prepared MediaPlayer but the current instance lacks
// a link to it
mMediaPlayer.setNextMediaPlayer(mPreparedMediaPlayer);
}
} catch (IOException e) {
mMediaPlayer.setNextMediaPlayer(null);
mPreparedMediaPlayer.reset();
}
//try {
// //if(nextSong.path.equals(mPreparedMediaPlayer.getDataSource()) == false) {
// // // Prepared MP has a different data source: We need to re-initalize
// // // it and set it as the next MP for the active media player
// // mPreparedMediaPlayer.reset();
// // prepareMediaPlayer(mPreparedMediaPlayer, nextSong.path);
// // mMediaPlayer.setNextMediaPlayer(mPreparedMediaPlayer);
// //}
// //if(mMediaPlayer.hasNextMediaPlayer() == false) {
// // // We can reuse the prepared MediaPlayer but the current instance lacks
// // // a link to it
// // mMediaPlayer.setNextMediaPlayer(mPreparedMediaPlayer);
// //}
//} catch (IOException e) {
// mMediaPlayer.setNextMediaPlayer(null);
// //mPreparedMediaPlayer.reset();
//}
} else {
if(mMediaPlayer.hasNextMediaPlayer()) {
mMediaPlayer.setNextMediaPlayer(null);
Expand Down Expand Up @@ -1037,7 +1037,7 @@ private void processNewState(int oldState, int state)
if((toggled & FLAG_DUCKING) != 0) {
boolean isDucking = (state & FLAG_DUCKING) != 0;
mMediaPlayer.setIsDucking(isDucking);
mPreparedMediaPlayer.setIsDucking(isDucking);
//mPreparedMediaPlayer.setIsDucking(isDucking);
}
}

Expand Down
1 change: 1 addition & 0 deletions src/com/jacobobryant/moody/vanilla/PrefKeys.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,5 @@ public class PrefKeys {
public static final String MOOD = "mood";
public static final String USER_ID = "user_id";
public static final String SPOTIFY_TOKEN = "spotify_token";
public static final String SPOTIFY_TOKEN_EXPIRATION = "spotify_token_expiration";
}
3 changes: 2 additions & 1 deletion src/com/jacobobryant/moody/vanilla/SongTimeline.java
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ private void shiftCurrentSongInternal(int delta, boolean skipped)
Moody moody = Moody.getInstance(mContext);
try {
moody.update(mSongs.get(mCurrentPos), skipped);
} catch (ArrayIndexOutOfBoundsException e) {
} catch (IndexOutOfBoundsException e) {
Log.e(C.TAG, "couldn't call moody.update");
}
Metadata next;
Expand All @@ -686,6 +686,7 @@ private void shiftCurrentSongInternal(int delta, boolean skipped)
} else if (next.title.contains("spotify:track:")) {
try {
Song song = get_metadata(next.title);
Log.d(C.TAG, "duration: " + song.duration);
//Song song = new Song(-1, Song.FLAG_NO_COVER);
//song.title = next.title;
//song.path = next.title;
Expand Down
25 changes: 8 additions & 17 deletions src/com/jacobobryant/moody/vanilla/VanillaMediaPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.jacobobryant.moody.C;
import com.spotify.sdk.android.player.Config;
import com.spotify.sdk.android.player.ConnectionStateCallback;
import com.spotify.sdk.android.player.PlaybackState;
import com.spotify.sdk.android.player.Player;
import com.spotify.sdk.android.player.PlayerEvent;
import com.spotify.sdk.android.player.Spotify;
Expand Down Expand Up @@ -58,9 +59,12 @@ public class SpotPlayer implements SpotifyPlayer.NotificationCallback,
public SpotPlayer(Context context) {
SharedPreferences settings = PlaybackService.getSettings(context);
String token = settings.getString(PrefKeys.SPOTIFY_TOKEN, null);
if (token == null) {
throw new RuntimeException("spotify token is null");
if (token != null) {
init(context, token);
}
}

public void init(Context context, String token) {
Config playerConfig = new Config(context, token, C.CLIENT_ID);
Spotify.getPlayer(playerConfig, this, new SpotifyPlayer.InitializationObserver() {
@Override
Expand All @@ -82,23 +86,10 @@ public void onPlaybackEvent(PlayerEvent playerEvent) {
Log.d(C.TAG, "Playback event received: " + playerEvent.name());
switch (playerEvent) {
// Handle event type as necessary
//case kSpPlaybackNotifyAudioDeliveryDone:
case kSpPlaybackNotifyTrackDelivered:
Log.d(C.TAG, "calling onCompletion()");
mCompletionListener.onCompletion(VanillaMediaPlayer.this);
break;
//case kSpPlaybackNotifyMetadataChanged:
// try {
// Log.d(C.TAG, "timeline == null: " + (mTimeline == null));
// Song s = mTimeline.getSong(0);
// Metadata.Track mdata = mPlayer.getMetadata().currentTrack;
// s.title = mdata.name;
// s.album = mdata.albumName;
// s.duration = mdata.durationMs;
// Log.d(C.TAG, "set title: " + s.title);
// } catch (NullPointerException e) {
// Log.e(C.TAG, "nullpointer", e);
// }
// break;
default:
break;
}
Expand Down Expand Up @@ -141,7 +132,7 @@ public void onLoggedOut() {

@Override
public void onLoginFailed(com.spotify.sdk.android.player.Error i) {
Log.d(C.TAG, "Login failed");
Log.d(C.TAG, "Login failed: " + i);
}

@Override
Expand Down

0 comments on commit b6f0f5a

Please sign in to comment.