Imports optimization and entries rearranged

This commit is contained in:
CappielloAntonio 2021-04-27 11:01:02 +02:00
parent 3c2837e096
commit 65e47f61ef
63 changed files with 987 additions and 1087 deletions

View file

@ -28,7 +28,6 @@ import java.util.List;
import java.util.concurrent.CopyOnWriteArraySet;
import static com.google.android.exoplayer2.util.Assertions.checkNotNull;
import static com.google.android.exoplayer2.util.Assertions.checkStateNotNull;
public class DownloadTracker {
@ -40,7 +39,7 @@ public class DownloadTracker {
private final DownloadIndex downloadIndex;
private final DefaultTrackSelector.Parameters trackSelectorParameters;
public DownloadTracker(Context context,HttpDataSource.Factory httpDataSourceFactory,DownloadManager downloadManager) {
public DownloadTracker(Context context, HttpDataSource.Factory httpDataSourceFactory, DownloadManager downloadManager) {
this.context = context.getApplicationContext();
this.httpDataSourceFactory = httpDataSourceFactory;
listeners = new CopyOnWriteArraySet<>();
@ -74,7 +73,7 @@ public class DownloadTracker {
public void toggleDownload(List<Song> songs) {
SongRepository songRepository = new SongRepository(App.getInstance());
for(Song song: songs) {
for (Song song : songs) {
MediaItem mediaItem = MusicUtil.getMediaItemFromSong(song);
@Nullable Download download = downloads.get(checkNotNull(mediaItem.playbackProperties).uri);
@ -84,7 +83,7 @@ public class DownloadTracker {
DownloadService.sendRemoveDownload(context, DownloaderService.class, download.request.id, false);
} else {
song.setOffline(true);
DownloadService.sendAddDownload(context, DownloaderService.class, getDownloadRequest(mediaItem.playbackProperties.uri),false);
DownloadService.sendAddDownload(context, DownloaderService.class, getDownloadRequest(mediaItem.playbackProperties.uri), false);
}
songRepository.setOfflineStatus(song);

View file

@ -44,7 +44,7 @@ public class DownloaderService extends DownloadService {
@Override
@NonNull
protected Notification getForegroundNotification(@NonNull List<Download> downloads) {
return DownloadUtil.getDownloadNotificationHelper(this).buildProgressNotification(this, R.drawable.ic_downloading,null, null, downloads);
return DownloadUtil.getDownloadNotificationHelper(this).buildProgressNotification(this, R.drawable.ic_downloading, null, null, downloads);
}
private static final class TerminalStateNotificationHelper implements DownloadManager.Listener {
@ -65,7 +65,7 @@ public class DownloaderService extends DownloadService {
Notification notification;
if (download.state == Download.STATE_COMPLETED) {
notification = notificationHelper.buildDownloadCompletedNotification(context, R.drawable.ic_done,null, Util.fromUtf8Bytes(download.request.data));
notification = notificationHelper.buildDownloadCompletedNotification(context, R.drawable.ic_done, null, Util.fromUtf8Bytes(download.request.data));
} else if (download.state == Download.STATE_FAILED) {
notification = notificationHelper.buildDownloadFailedNotification(context, R.drawable.ic_error, null, Util.fromUtf8Bytes(download.request.data));
} else {

View file

@ -6,15 +6,15 @@ import android.util.Log;
import android.widget.Toast;
import com.cappielloantonio.play.R;
import com.cappielloantonio.play.model.Song;
import com.cappielloantonio.play.interfaces.Playback;
import com.cappielloantonio.play.model.Song;
import com.cappielloantonio.play.util.DownloadUtil;
import com.cappielloantonio.play.util.MusicUtil;
import com.cappielloantonio.play.util.PreferenceUtil;
import com.google.android.exoplayer2.ExoPlaybackException;
import com.google.android.exoplayer2.ExoPlayer;
import com.google.android.exoplayer2.Player;
import com.google.android.exoplayer2.MediaItem;
import com.google.android.exoplayer2.Player;
import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.database.ExoDatabaseProvider;
import com.google.android.exoplayer2.source.DefaultMediaSourceFactory;
@ -185,23 +185,23 @@ public class MultiPlayer implements Playback {
return (int) exoPlayer.getCurrentPosition();
}
@Override
public int getDuration() {
return (int) exoPlayer.getDuration();
}
@Override
public void setProgress(int progress) {
exoPlayer.seekTo(progress);
}
@Override
public void setVolume(int volume) {
exoPlayer.setVolume(volume / 100f);
public int getDuration() {
return (int) exoPlayer.getDuration();
}
@Override
public int getVolume() {
return (int) (exoPlayer.getVolume() * 100);
}
@Override
public void setVolume(int volume) {
exoPlayer.setVolume(volume / 100f);
}
}

View file

@ -15,7 +15,6 @@ import androidx.annotation.Nullable;
import com.cappielloantonio.play.App;
import com.cappielloantonio.play.model.Song;
import com.cappielloantonio.play.repository.QueueRepository;
import com.cappielloantonio.play.service.MusicService;
import java.util.ArrayList;
import java.util.List;
@ -23,10 +22,8 @@ import java.util.WeakHashMap;
public class MusicPlayerRemote {
private static final String TAG = "MusicPlayerRemote";
public static MusicService musicService;
private static final WeakHashMap<Context, ServiceBinder> mConnectionMap = new WeakHashMap<>();
public static MusicService musicService;
public static ServiceToken bindToService(@NonNull final Context context, final ServiceConnection callback) {
Activity realActivity = ((Activity) context).getParent();
@ -64,52 +61,12 @@ public class MusicPlayerRemote {
}
}
public static final class ServiceBinder implements ServiceConnection {
private final ServiceConnection mCallback;
public ServiceBinder(final ServiceConnection callback) {
mCallback = callback;
}
@Override
public void onServiceConnected(final ComponentName className, final IBinder service) {
MusicService.MusicBinder binder = (MusicService.MusicBinder) service;
musicService = binder.getService();
if (mCallback != null) {
mCallback.onServiceConnected(className, service);
}
}
@Override
public void onServiceDisconnected(final ComponentName className) {
if (mCallback != null) {
mCallback.onServiceDisconnected(className);
}
musicService = null;
}
}
public static final class ServiceToken {
public ContextWrapper mWrappedContext;
public ServiceToken(final ContextWrapper context) {
mWrappedContext = context;
}
}
public static void playSongAt(final int position) {
if (musicService != null) {
musicService.playSongAt(position);
}
}
public static void setPosition(final int position) {
if (musicService != null) {
musicService.setPosition(position);
}
}
public static void pauseSong() {
if (musicService != null) {
musicService.pause();
@ -192,6 +149,12 @@ public class MusicPlayerRemote {
return -1;
}
public static void setPosition(final int position) {
if (musicService != null) {
musicService.setPosition(position);
}
}
public static List<Song> getPlayingQueue() {
if (musicService != null) {
return musicService.getPlayingQueue();
@ -328,4 +291,38 @@ public class MusicPlayerRemote {
return false;
}
public static final class ServiceBinder implements ServiceConnection {
private final ServiceConnection mCallback;
public ServiceBinder(final ServiceConnection callback) {
mCallback = callback;
}
@Override
public void onServiceConnected(final ComponentName className, final IBinder service) {
MusicService.MusicBinder binder = (MusicService.MusicBinder) service;
musicService = binder.getService();
if (mCallback != null) {
mCallback.onServiceConnected(className, service);
}
}
@Override
public void onServiceDisconnected(final ComponentName className) {
if (mCallback != null) {
mCallback.onServiceDisconnected(className);
}
musicService = null;
}
}
public static final class ServiceToken {
public ContextWrapper mWrappedContext;
public ServiceToken(final ContextWrapper context) {
mWrappedContext = context;
}
}
}

View file

@ -28,12 +28,12 @@ import androidx.annotation.Nullable;
import com.cappielloantonio.play.App;
import com.cappielloantonio.play.R;
import com.cappielloantonio.play.broadcast.receiver.MediaButtonIntentReceiver;
import com.cappielloantonio.play.interfaces.Playback;
import com.cappielloantonio.play.model.Playlist;
import com.cappielloantonio.play.model.Song;
import com.cappielloantonio.play.repository.QueueRepository;
import com.cappielloantonio.play.repository.SongRepository;
import com.cappielloantonio.play.ui.notification.PlayingNotification;
import com.cappielloantonio.play.interfaces.Playback;
import com.cappielloantonio.play.util.PreferenceUtil;
import org.jellyfin.apiclient.interaction.EmptyResponse;
@ -57,8 +57,6 @@ import static com.google.android.exoplayer2.Player.PLAY_WHEN_READY_CHANGE_REASON
public class MusicService extends Service implements Playback.PlaybackCallbacks {
public static final String PACKAGE_NAME = "com.antoniocappiello.play";
private static final String TAG = "MusicService";
public static final String ACTION_TOGGLE = PACKAGE_NAME + ".toggle";
public static final String ACTION_PLAY = PACKAGE_NAME + ".play";
public static final String ACTION_PLAY_PLAYLIST = PACKAGE_NAME + ".play.playlist";
@ -68,17 +66,13 @@ public class MusicService extends Service implements Playback.PlaybackCallbacks
public static final String ACTION_REWIND = PACKAGE_NAME + ".rewind";
public static final String ACTION_QUIT = PACKAGE_NAME + ".quit";
public static final String ACTION_PENDING_QUIT = PACKAGE_NAME + ".quit.pending";
public static final String INTENT_EXTRA_PLAYLIST = PACKAGE_NAME + ".extra.playlist";
public static final String STATE_CHANGED = PACKAGE_NAME + ".state.changed";
public static final String META_CHANGED = PACKAGE_NAME + ".meta.changed";
public static final String QUEUE_CHANGED = PACKAGE_NAME + ".queue.changed";
public static final int TRACK_STARTED = 9;
public static final int TRACK_CHANGED = 1;
public static final int TRACK_ENDED = 2;
public static final int RELEASE_WAKELOCK = 0;
public static final int PLAY_SONG = 3;
public static final int PREPARE_NEXT = 4;
@ -86,39 +80,25 @@ public class MusicService extends Service implements Playback.PlaybackCallbacks
public static final int FOCUS_CHANGE = 6;
public static final int DUCK = 7;
public static final int UNDUCK = 8;
public static final int LOAD_QUEUE = 9;
private static final String TAG = "MusicService";
private static final long MEDIA_SESSION_ACTIONS = PlaybackStateCompat.ACTION_PLAY
| PlaybackStateCompat.ACTION_PAUSE
| PlaybackStateCompat.ACTION_PLAY_PAUSE
| PlaybackStateCompat.ACTION_SKIP_TO_NEXT
| PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS
| PlaybackStateCompat.ACTION_STOP
| PlaybackStateCompat.ACTION_SEEK_TO;
private final IBinder musicBinder = new MusicBinder();
public boolean pendingQuit = false;
private Playback playback;
private List<Song> playingQueue = new ArrayList<>();
private int position = -1;
private int nextPosition = -1;
private boolean notHandledMetaChangedForCurrentTrack;
private boolean queuesRestored;
private boolean pausedByTransientLossOfFocus;
private PlayingNotification playingNotification;
private AudioManager audioManager;
private MediaSessionCompat mediaSession;
private PowerManager.WakeLock wakeLock;
private PlaybackHandler playerHandler;
private Handler uiThreadHandler;
private ThrottledSeekHandler throttledSeekHandler;
private QueueHandler queueHandler;
private ProgressHandler progressHandler;
private HandlerThread playerHandlerThread;
private HandlerThread progressHandlerThread;
private HandlerThread queueHandlerThread;
private final BroadcastReceiver becomingNoisyReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, @NonNull Intent intent) {
@ -127,21 +107,23 @@ public class MusicService extends Service implements Playback.PlaybackCallbacks
}
}
};
private AudioManager audioManager;
private MediaSessionCompat mediaSession;
private PowerManager.WakeLock wakeLock;
private PlaybackHandler playerHandler;
private final AudioManager.OnAudioFocusChangeListener audioFocusListener = new AudioManager.OnAudioFocusChangeListener() {
@Override
public void onAudioFocusChange(final int focusChange) {
playerHandler.obtainMessage(FOCUS_CHANGE, focusChange, 0).sendToTarget();
}
};
private static final long MEDIA_SESSION_ACTIONS = PlaybackStateCompat.ACTION_PLAY
| PlaybackStateCompat.ACTION_PAUSE
| PlaybackStateCompat.ACTION_PLAY_PAUSE
| PlaybackStateCompat.ACTION_SKIP_TO_NEXT
| PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS
| PlaybackStateCompat.ACTION_STOP
| PlaybackStateCompat.ACTION_SEEK_TO;
private Handler uiThreadHandler;
private ThrottledSeekHandler throttledSeekHandler;
private QueueHandler queueHandler;
private ProgressHandler progressHandler;
private HandlerThread playerHandlerThread;
private HandlerThread progressHandlerThread;
private HandlerThread queueHandlerThread;
@Override
public void onCreate() {
@ -304,26 +286,6 @@ public class MusicService extends Service implements Playback.PlaybackCallbacks
return musicBinder;
}
private static final class QueueHandler extends Handler {
@NonNull
private final WeakReference<MusicService> mService;
public QueueHandler(final MusicService service, @NonNull final Looper looper) {
super(looper);
mService = new WeakReference<>(service);
}
@Override
public void handleMessage(@NonNull Message msg) {
final MusicService service = mService.get();
switch (msg.what) {
case LOAD_QUEUE:
service.restoreQueuesAndPositionIfNecessary();
break;
}
}
}
public void saveState() {
savePosition();
saveProgress();
@ -405,6 +367,12 @@ public class MusicService extends Service implements Playback.PlaybackCallbacks
return position;
}
public void setPosition(final int position) {
// handle this on the handlers thread to avoid blocking the ui thread
playerHandler.removeMessages(SET_POSITION);
playerHandler.obtainMessage(SET_POSITION, position, 0).sendToTarget();
}
public void playNextSong() {
playSongAt(getNextPosition());
}
@ -609,12 +577,6 @@ public class MusicService extends Service implements Playback.PlaybackCallbacks
playerHandler.obtainMessage(PLAY_SONG, position, 0).sendToTarget();
}
public void setPosition(final int position) {
// handle this on the handlers thread to avoid blocking the ui thread
playerHandler.removeMessages(SET_POSITION);
playerHandler.obtainMessage(SET_POSITION, position, 0).sendToTarget();
}
private void playSongAtImpl(int position) {
openTrackAndPrepareNextAt(position);
}
@ -774,6 +736,26 @@ public class MusicService extends Service implements Playback.PlaybackCallbacks
}
}
private static final class QueueHandler extends Handler {
@NonNull
private final WeakReference<MusicService> mService;
public QueueHandler(final MusicService service, @NonNull final Looper looper) {
super(looper);
mService = new WeakReference<>(service);
}
@Override
public void handleMessage(@NonNull Message msg) {
final MusicService service = mService.get();
switch (msg.what) {
case LOAD_QUEUE:
service.restoreQueuesAndPositionIfNecessary();
break;
}
}
}
private static final class PlaybackHandler extends Handler {
private final WeakReference<MusicService> mService;
private int currentDuckVolume = 100;
@ -907,33 +889,6 @@ public class MusicService extends Service implements Playback.PlaybackCallbacks
}
}
public class MusicBinder extends Binder {
@NonNull
public MusicService getService() {
return MusicService.this;
}
}
private class ThrottledSeekHandler implements Runnable {
// milliseconds to throttle before calling run to aggregate events
private static final long THROTTLE = 500;
private final Handler mHandler;
public ThrottledSeekHandler(Handler handler) {
mHandler = handler;
}
public void notifySeek() {
mHandler.removeCallbacks(this);
mHandler.postDelayed(this, THROTTLE);
}
@Override
public void run() {
notifyChange(STATE_CHANGED);
}
}
private static final class ProgressHandler extends Handler {
private final WeakReference<MusicService> mService;
@ -1012,4 +967,31 @@ public class MusicService extends Service implements Playback.PlaybackCallbacks
if (executorService != null) executorService.shutdownNow();
}
}
public class MusicBinder extends Binder {
@NonNull
public MusicService getService() {
return MusicService.this;
}
}
private class ThrottledSeekHandler implements Runnable {
// milliseconds to throttle before calling run to aggregate events
private static final long THROTTLE = 500;
private final Handler mHandler;
public ThrottledSeekHandler(Handler handler) {
mHandler = handler;
}
public void notifySeek() {
mHandler.removeCallbacks(this);
mHandler.postDelayed(this, THROTTLE);
}
@Override
public void run() {
notifyChange(STATE_CHANGED);
}
}
}