Null safety

This commit is contained in:
h4h13 2019-12-30 10:02:33 +05:30
parent 1a10984307
commit 9b1e078c20

View file

@ -36,7 +36,6 @@ import android.os.HandlerThread;
import android.os.IBinder; import android.os.IBinder;
import android.os.PowerManager; import android.os.PowerManager;
import android.os.Process; import android.os.Process;
import android.preference.PreferenceManager;
import android.provider.MediaStore; import android.provider.MediaStore;
import android.support.v4.media.MediaMetadataCompat; import android.support.v4.media.MediaMetadataCompat;
import android.support.v4.media.session.MediaSessionCompat; import android.support.v4.media.session.MediaSessionCompat;
@ -48,6 +47,7 @@ import android.widget.Toast;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.preference.PreferenceManager;
import com.bumptech.glide.BitmapRequestBuilder; import com.bumptech.glide.BitmapRequestBuilder;
import com.bumptech.glide.Glide; import com.bumptech.glide.Glide;
@ -56,6 +56,7 @@ import com.bumptech.glide.request.target.SimpleTarget;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Objects;
import java.util.Random; import java.util.Random;
import code.name.monkey.retromusic.R; import code.name.monkey.retromusic.R;
@ -153,6 +154,8 @@ public class MusicService extends Service implements
| PlaybackStateCompat.ACTION_SEEK_TO; | PlaybackStateCompat.ACTION_SEEK_TO;
private final IBinder musicBind = new MusicBinder(); private final IBinder musicBind = new MusicBinder();
public boolean pendingQuit = false; public boolean pendingQuit = false;
@Nullable
public Playback playback; public Playback playback;
public int position = -1; public int position = -1;
public int nextPosition = -1; public int nextPosition = -1;
@ -165,28 +168,29 @@ public class MusicService extends Service implements
@Override @Override
public void onReceive(final Context context, final Intent intent) { public void onReceive(final Context context, final Intent intent) {
final String command = intent.getStringExtra(EXTRA_APP_WIDGET_NAME); final String command = intent.getStringExtra(EXTRA_APP_WIDGET_NAME);
final int[] ids = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS); final int[] ids = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);
switch (command) { if (command != null) {
case AppWidgetClassic.NAME: { switch (command) {
appWidgetClassic.performUpdate(MusicService.this, ids); case AppWidgetClassic.NAME: {
break; appWidgetClassic.performUpdate(MusicService.this, ids);
} break;
case AppWidgetSmall.NAME: { }
appWidgetSmall.performUpdate(MusicService.this, ids); case AppWidgetSmall.NAME: {
break; appWidgetSmall.performUpdate(MusicService.this, ids);
} break;
case AppWidgetBig.NAME: { }
appWidgetBig.performUpdate(MusicService.this, ids); case AppWidgetBig.NAME: {
break; appWidgetBig.performUpdate(MusicService.this, ids);
} break;
case AppWidgetCard.NAME: { }
appWidgetCard.performUpdate(MusicService.this, ids); case AppWidgetCard.NAME: {
break; appWidgetCard.performUpdate(MusicService.this, ids);
} break;
case AppWidgetText.NAME: { }
appWidgetText.performUpdate(MusicService.this, ids); case AppWidgetText.NAME: {
break; appWidgetText.performUpdate(MusicService.this, ids);
break;
}
} }
} }
@ -435,10 +439,7 @@ public class MusicService extends Service implements
ArrayList<Song> playlistSongs = playlist.getSongs(getApplicationContext()); ArrayList<Song> playlistSongs = playlist.getSongs(getApplicationContext());
if (!playlistSongs.isEmpty()) { if (!playlistSongs.isEmpty()) {
if (shuffleMode == SHUFFLE_MODE_SHUFFLE) { if (shuffleMode == SHUFFLE_MODE_SHUFFLE) {
int startPosition = 0; int startPosition = new Random().nextInt(playlistSongs.size());
if (!playlistSongs.isEmpty()) {
startPosition = new Random().nextInt(playlistSongs.size());
}
openQueue(playlistSongs, startPosition, true); openQueue(playlistSongs, startPosition, true);
setShuffleMode(shuffleMode); setShuffleMode(shuffleMode);
} else { } else {
@ -474,16 +475,12 @@ public class MusicService extends Service implements
sendBroadcast(new Intent("code.name.monkey.retromusic.RETRO_MUSIC_SERVICE_DESTROYED")); sendBroadcast(new Intent("code.name.monkey.retromusic.RETRO_MUSIC_SERVICE_DESTROYED"));
} }
@NonNull
@Override @Override
public IBinder onBind(Intent intent) { public IBinder onBind(Intent intent) {
return musicBind; return musicBind;
} }
@Override
public void onRebind(Intent intent) {
}
@Override @Override
public boolean onUnbind(Intent intent) { public boolean onUnbind(Intent intent) {
if (!isPlaying()) { if (!isPlaying()) {
@ -565,7 +562,9 @@ public class MusicService extends Service implements
musicPlayerHandlerThread.quitSafely(); musicPlayerHandlerThread.quitSafely();
queueSaveHandler.removeCallbacksAndMessages(null); queueSaveHandler.removeCallbacksAndMessages(null);
queueSaveHandlerThread.quitSafely(); queueSaveHandlerThread.quitSafely();
playback.release(); if (playback != null) {
playback.release();
}
playback = null; playback = null;
mediaSession.release(); mediaSession.release();
} }
@ -602,11 +601,14 @@ public class MusicService extends Service implements
private boolean openCurrent() { private boolean openCurrent() {
synchronized (this) { synchronized (this) {
try { try {
return playback.setDataSource(getTrackUri(getCurrentSong())); if (playback != null) {
return playback.setDataSource(getTrackUri(Objects.requireNonNull(getCurrentSong())));
}
} catch (Exception e) { } catch (Exception e) {
return false; return false;
} }
} }
return false;
} }
private void prepareNext() { private void prepareNext() {
@ -618,7 +620,9 @@ public class MusicService extends Service implements
synchronized (this) { synchronized (this) {
try { try {
int nextPosition = getNextPosition(false); int nextPosition = getNextPosition(false);
playback.setNextDataSource(getTrackUri(getSongAt(nextPosition))); if (playback != null) {
playback.setNextDataSource(getTrackUri(Objects.requireNonNull(getSongAt(nextPosition))));
}
this.nextPosition = nextPosition; this.nextPosition = nextPosition;
return true; return true;
} catch (Exception e) { } catch (Exception e) {
@ -629,7 +633,9 @@ public class MusicService extends Service implements
private void closeAudioEffectSession() { private void closeAudioEffectSession() {
final Intent audioEffectsIntent = new Intent(AudioEffect.ACTION_CLOSE_AUDIO_EFFECT_CONTROL_SESSION); final Intent audioEffectsIntent = new Intent(AudioEffect.ACTION_CLOSE_AUDIO_EFFECT_CONTROL_SESSION);
audioEffectsIntent.putExtra(AudioEffect.EXTRA_AUDIO_SESSION, playback.getAudioSessionId()); if (playback != null) {
audioEffectsIntent.putExtra(AudioEffect.EXTRA_AUDIO_SESSION, playback.getAudioSessionId());
}
audioEffectsIntent.putExtra(AudioEffect.EXTRA_PACKAGE_NAME, getPackageName()); audioEffectsIntent.putExtra(AudioEffect.EXTRA_PACKAGE_NAME, getPackageName());
sendBroadcast(audioEffectsIntent); sendBroadcast(audioEffectsIntent);
} }
@ -686,7 +692,7 @@ public class MusicService extends Service implements
.build()); .build());
} }
void updateMediaSessionMetaData() { void updateMediaSessionMetaData() {
final Song song = getCurrentSong(); final Song song = getCurrentSong();
if (song.getId() == -1) { if (song.getId() == -1) {
@ -741,12 +747,14 @@ public class MusicService extends Service implements
uiThreadHandler.post(runnable); uiThreadHandler.post(runnable);
} }
@Nullable
public Song getCurrentSong() { public Song getCurrentSong() {
return getSongAt(getPosition()); return getSongAt(getPosition());
} }
@Nullable
public Song getSongAt(int position) { public Song getSongAt(int position) {
if (position >= 0 && position < getPlayingQueue().size()) { if (position >= 0 && getPlayingQueue() != null && position < getPlayingQueue().size()) {
return getPlayingQueue().get(position); return getPlayingQueue().get(position);
} else { } else {
return Song.Companion.getEmptySong(); return Song.Companion.getEmptySong();
@ -781,9 +789,13 @@ public class MusicService extends Service implements
} }
public boolean isLastTrack() { public boolean isLastTrack() {
return getPosition() == getPlayingQueue().size() - 1; if (getPlayingQueue() != null) {
return getPosition() == getPlayingQueue().size() - 1;
}
return false;
} }
@Nullable
public ArrayList<Song> getPlayingQueue() { public ArrayList<Song> getPlayingQueue() {
return playingQueue; return playingQueue;
} }
@ -935,7 +947,7 @@ public class MusicService extends Service implements
public void pause() { public void pause() {
pausedByTransientLossOfFocus = false; pausedByTransientLossOfFocus = false;
if (playback.isPlaying()) { if (playback != null && playback.isPlaying()) {
playback.pause(); playback.pause();
notifyChange(PLAY_STATE_CHANGED); notifyChange(PLAY_STATE_CHANGED);
} }
@ -944,7 +956,7 @@ public class MusicService extends Service implements
public void play() { public void play() {
synchronized (this) { synchronized (this) {
if (requestFocus()) { if (requestFocus()) {
if (!playback.isPlaying()) { if (playback != null && !playback.isPlaying()) {
if (!playback.isInitialized()) { if (!playback.isInitialized()) {
playSongAt(getPosition()); playSongAt(getPosition());
} else { } else {
@ -973,10 +985,7 @@ public class MusicService extends Service implements
public void playSongs(ArrayList<Song> songs, int shuffleMode) { public void playSongs(ArrayList<Song> songs, int shuffleMode) {
if (songs != null && !songs.isEmpty()) { if (songs != null && !songs.isEmpty()) {
if (shuffleMode == SHUFFLE_MODE_SHUFFLE) { if (shuffleMode == SHUFFLE_MODE_SHUFFLE) {
int startPosition = 0; int startPosition = new Random().nextInt(songs.size());
if (!songs.isEmpty()) {
startPosition = new Random().nextInt(songs.size());
}
openQueue(songs, startPosition, false); openQueue(songs, startPosition, false);
setShuffleMode(shuffleMode); setShuffleMode(shuffleMode);
} else { } else {
@ -1005,13 +1014,17 @@ public class MusicService extends Service implements
switch (repeatMode) { switch (repeatMode) {
case REPEAT_MODE_ALL: case REPEAT_MODE_ALL:
if (newPosition < 0) { if (newPosition < 0) {
newPosition = getPlayingQueue().size() - 1; if (getPlayingQueue() != null) {
newPosition = getPlayingQueue().size() - 1;
}
} }
break; break;
case REPEAT_MODE_THIS: case REPEAT_MODE_THIS:
if (force) { if (force) {
if (newPosition < 0) { if (newPosition < 0) {
newPosition = getPlayingQueue().size() - 1; if (getPlayingQueue() != null) {
newPosition = getPlayingQueue().size() - 1;
}
} }
} else { } else {
newPosition = getPosition(); newPosition = getPosition();
@ -1028,11 +1041,17 @@ public class MusicService extends Service implements
} }
public int getSongProgressMillis() { public int getSongProgressMillis() {
return playback.position(); if (playback != null) {
return playback.position();
}
return -1;
} }
public int getSongDurationMillis() { public int getSongDurationMillis() {
return playback.duration(); if (playback != null) {
return playback.duration();
}
return -1;
} }
public long getQueueDurationMillis(int position) { public long getQueueDurationMillis(int position) {
@ -1045,7 +1064,10 @@ public class MusicService extends Service implements
public int seek(int millis) { public int seek(int millis) {
synchronized (this) { synchronized (this) {
try { try {
int newPosition = playback.seek(millis); int newPosition = 0;
if (playback != null) {
newPosition = playback.seek(millis);
}
throttledSeekHandler.notifySeek(); throttledSeekHandler.notifySeek();
return newPosition; return newPosition;
} catch (Exception e) { } catch (Exception e) {
@ -1087,17 +1109,21 @@ public class MusicService extends Service implements
switch (shuffleMode) { switch (shuffleMode) {
case SHUFFLE_MODE_SHUFFLE: case SHUFFLE_MODE_SHUFFLE:
this.shuffleMode = shuffleMode; this.shuffleMode = shuffleMode;
ShuffleHelper.INSTANCE.makeShuffleList(this.getPlayingQueue(), getPosition()); if (this.getPlayingQueue() != null) {
ShuffleHelper.INSTANCE.makeShuffleList(this.getPlayingQueue(), getPosition());
}
position = 0; position = 0;
break; break;
case SHUFFLE_MODE_NONE: case SHUFFLE_MODE_NONE:
this.shuffleMode = shuffleMode; this.shuffleMode = shuffleMode;
int currentSongId = getCurrentSong().getId(); int currentSongId = Objects.requireNonNull(getCurrentSong()).getId();
playingQueue = new ArrayList<>(originalPlayingQueue); playingQueue = new ArrayList<>(originalPlayingQueue);
int newPosition = 0; int newPosition = 0;
for (Song song : getPlayingQueue()) { if (getPlayingQueue() != null) {
if (song.getId() == currentSongId) { for (Song song : getPlayingQueue()) {
newPosition = getPlayingQueue().indexOf(song); if (song.getId() == currentSongId) {
newPosition = getPlayingQueue().indexOf(song);
}
} }
} }
position = newPosition; position = newPosition;
@ -1123,17 +1149,17 @@ public class MusicService extends Service implements
final Song song = getCurrentSong(); final Song song = getCurrentSong();
intent.putExtra("id", song.getId()); if (song != null) {
intent.putExtra("artist", song.getArtistName()); intent.putExtra("id", song.getId());
intent.putExtra("album", song.getAlbumName()); intent.putExtra("artist", song.getArtistName());
intent.putExtra("track", song.getTitle()); intent.putExtra("album", song.getAlbumName());
intent.putExtra("duration", song.getDuration()); intent.putExtra("track", song.getTitle());
intent.putExtra("position", (long) getSongProgressMillis()); intent.putExtra("duration", song.getDuration());
intent.putExtra("playing", isPlaying()); intent.putExtra("position", (long) getSongProgressMillis());
intent.putExtra("scrobbling_source", RETRO_MUSIC_PACKAGE_NAME); intent.putExtra("playing", isPlaying());
intent.putExtra("scrobbling_source", RETRO_MUSIC_PACKAGE_NAME);
sendStickyBroadcast(intent); sendStickyBroadcast(intent);
}
} }
private void sendChangeInternal(final String what) { private void sendChangeInternal(final String what) {
@ -1163,11 +1189,15 @@ public class MusicService extends Service implements
savePosition(); savePosition();
savePositionInTrack(); savePositionInTrack();
final Song currentSong = getCurrentSong(); final Song currentSong = getCurrentSong();
HistoryStore.getInstance(this).addSongId(currentSong.getId()); if (currentSong != null) {
HistoryStore.getInstance(this).addSongId(currentSong.getId());
}
if (songPlayCountHelper.shouldBumpPlayCount()) { if (songPlayCountHelper.shouldBumpPlayCount()) {
SongPlayCountStore.getInstance(this).bumpPlayCount(songPlayCountHelper.getSong().getId()); SongPlayCountStore.getInstance(this).bumpPlayCount(songPlayCountHelper.getSong().getId());
} }
songPlayCountHelper.notifySongChanged(currentSong); if (currentSong != null) {
songPlayCountHelper.notifySongChanged(currentSong);
}
break; break;
case QUEUE_CHANGED: case QUEUE_CHANGED:
updateMediaSessionMetaData(); // because playing queue size might have changed updateMediaSessionMetaData(); // because playing queue size might have changed
@ -1182,7 +1212,10 @@ public class MusicService extends Service implements
} }
public int getAudioSessionId() { public int getAudioSessionId() {
return playback.getAudioSessionId(); if (playback != null) {
return playback.getAudioSessionId();
}
return -1;
} }
@NonNull @NonNull
@ -1207,7 +1240,9 @@ public class MusicService extends Service implements
if (sharedPreferences.getBoolean(key, false)) { if (sharedPreferences.getBoolean(key, false)) {
prepareNext(); prepareNext();
} else { } else {
playback.setNextDataSource(null); if (playback != null) {
playback.setNextDataSource(null);
}
} }
break; break;
case PreferenceUtil.ALBUM_ART_ON_LOCKSCREEN: case PreferenceUtil.ALBUM_ART_ON_LOCKSCREEN: