Skip to content

Commit

Permalink
Various changes to fix various crashes related to listeners and servi…
Browse files Browse the repository at this point in the history
…ce start/stop
  • Loading branch information
andreybutenko committed Sep 15, 2017
1 parent a0a17a8 commit 41ec030
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 35 deletions.
3 changes: 2 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:theme="@style/Theme.AppCompat.NoActionBar">
android:theme="@style/Theme.AppCompat.NoActionBar"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ public void onCreate() {
}

public void killService() {
Log.d(TAG, "Killing service!");
unregisterReceiver(broadcastReceiver);
CurrentSongService.INSTANCE = null;
this.stopSelf();
}
Expand Down Expand Up @@ -137,6 +139,10 @@ public boolean hasListener(SongListener songListener) {
return songListenerList.contains(songListener);
}

public void triggerListeners() {
notifyListeners(currentTrack, currentAlbum, currentArtist);
}

private void notifyListeners(String track, String album, String artist) {
for(SongListener songListener : songListenerList) {
songListener.onSongChanged(track, album, artist);
Expand Down
78 changes: 44 additions & 34 deletions app/src/main/java/ninja/andrey/lyriclink/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.content.Intent;
import android.media.AudioManager;
import android.os.Build;
import android.os.Handler;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
Expand All @@ -21,9 +22,13 @@

public class MainActivity extends AppCompatActivity implements CurrentSongService.SongListener, Search.SearchListener {

private static final String TAG = "LyricLinkMain";

private static final long TIMER_RATE = 100; // how often to check if service started
private static final long INSTANT_LYRICS_COOLDOWN = 60 * 1000; // how long before instantly opening lyrics again

boolean loadingLyrics = false;

TextView musicTrack;
TextView musicAlbum;
TextView musicArtist;
Expand Down Expand Up @@ -64,8 +69,32 @@ public void onClick(View v) {
}
});

refreshCurrentSong();
}

@Override
protected void onResume() {
super.onResume();

refreshCurrentSong();
}

@Override
protected void onPause() {
super.onPause();
if(CurrentSongService.isStarted() && CurrentSongService.getInstance().hasListener(this)) {
CurrentSongService.getInstance().removeListener(this);
}

if(CurrentSongService.isStarted()) {
CurrentSongService.getInstance().killService();
}
}

private void refreshCurrentSong() {
// If music is playing, play/pause it to trigger the intent we need

Log.d(TAG, "Refreshing current song...");
AudioManager audioManager;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
audioManager = MainActivity.this.getSystemService(AudioManager.class);
Expand Down Expand Up @@ -94,33 +123,19 @@ public void run() {
if(CurrentSongService.isStarted()) {
timer.cancel();
CurrentSongService.getInstance().addListener(MainActivity.this);
runOnUiThread(new Runnable() {
@Override
public void run() {
updateCurrentlyPlaying();
}
});
}
}
};

timer.scheduleAtFixedRate(timerTask, TIMER_RATE, TIMER_RATE);
}
}

@Override
protected void onResume() {
super.onResume();

if(readyForInstantLyrics()) {
openCurrentSongLyrics();
}
}

@Override
protected void onDestroy() {
super.onDestroy();
if(CurrentSongService.isStarted() && CurrentSongService.getInstance().hasListener(this)) {
CurrentSongService.getInstance().removeListener(this);
}

if(CurrentSongService.isStarted()) {
CurrentSongService.getInstance().killService();
}
}

private boolean readyForInstantLyrics() {
Expand Down Expand Up @@ -152,10 +167,14 @@ private void updateCurrentlyPlaying() {
}

private void openCurrentSongLyrics() {
CurrentSongService currentSongService = CurrentSongService.getInstance();
showLoadingDialog(currentSongService.getCurrentTrack(), currentSongService.getCurrentArtist());
Search.addListener(MainActivity.this);
Search.loadLyricsUrl(currentSongService.getCurrentTrack(), currentSongService.getCurrentArtist());
Log.d(TAG, "Opening current song lyrics!");
if(!loadingLyrics) {
loadingLyrics = true;
CurrentSongService currentSongService = CurrentSongService.getInstance();
showLoadingDialog(currentSongService.getCurrentTrack(), currentSongService.getCurrentArtist());
Search.addListener(MainActivity.this);
Search.loadLyricsUrl(currentSongService.getCurrentTrack(), currentSongService.getCurrentArtist());
}
}

private void showLoadingDialog(String track, String artist) {
Expand All @@ -179,19 +198,10 @@ public void onSongChanged(String track, String album, String artist) {
public void onSearchUrlLoaded(String url) {
UserData userData = new UserData(this);
userData.setLatestLyricLookupTime(System.currentTimeMillis());
loadingLyrics = false;

dismissLoadingDialog();
Intent intent = Search.getLyricIntent(url);
startActivity(intent);

Timer timer = new Timer();
TimerTask timerTask = new TimerTask() {
@Override
public void run() {
Search.removeListener(MainActivity.this);
}
};

timer.schedule(timerTask, TIMER_RATE);
}
}
1 change: 1 addition & 0 deletions app/src/main/java/ninja/andrey/lyriclink/Search.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,5 +113,6 @@ private static void notifyListeners(String url) {
for(SearchListener searchListener : searchListenerList) {
searchListener.onSearchUrlLoaded(url);
}
searchListenerList.clear();
}
}

0 comments on commit 41ec030

Please sign in to comment.