diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 7bbfed6..9d9f71c 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -14,7 +14,8 @@ android:theme="@style/AppTheme"> + android:theme="@style/Theme.AppCompat.NoActionBar" + android:screenOrientation="portrait"> diff --git a/app/src/main/java/ninja/andrey/lyriclink/CurrentSongService.java b/app/src/main/java/ninja/andrey/lyriclink/CurrentSongService.java index 596da04..3d9bc34 100644 --- a/app/src/main/java/ninja/andrey/lyriclink/CurrentSongService.java +++ b/app/src/main/java/ninja/andrey/lyriclink/CurrentSongService.java @@ -72,6 +72,8 @@ public void onCreate() { } public void killService() { + Log.d(TAG, "Killing service!"); + unregisterReceiver(broadcastReceiver); CurrentSongService.INSTANCE = null; this.stopSelf(); } @@ -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); diff --git a/app/src/main/java/ninja/andrey/lyriclink/MainActivity.java b/app/src/main/java/ninja/andrey/lyriclink/MainActivity.java index 68fe41c..8c43319 100644 --- a/app/src/main/java/ninja/andrey/lyriclink/MainActivity.java +++ b/app/src/main/java/ninja/andrey/lyriclink/MainActivity.java @@ -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; @@ -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; @@ -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); @@ -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() { @@ -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) { @@ -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); } } diff --git a/app/src/main/java/ninja/andrey/lyriclink/Search.java b/app/src/main/java/ninja/andrey/lyriclink/Search.java index e2ab6ba..3eac7c2 100644 --- a/app/src/main/java/ninja/andrey/lyriclink/Search.java +++ b/app/src/main/java/ninja/andrey/lyriclink/Search.java @@ -113,5 +113,6 @@ private static void notifyListeners(String url) { for(SearchListener searchListener : searchListenerList) { searchListener.onSearchUrlLoaded(url); } + searchListenerList.clear(); } }