mirror of
https://github.com/antebudimir/tempus.git
synced 2025-12-31 17:43:32 +00:00
Initializing the service from the base activity
This commit is contained in:
parent
03c9e89bf5
commit
4e968a8c4b
1 changed files with 18 additions and 187 deletions
|
|
@ -1,81 +1,30 @@
|
|||
package com.cappielloantonio.play.ui.activity.base;
|
||||
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.content.ServiceConnection;
|
||||
import android.os.Bundle;
|
||||
import android.os.IBinder;
|
||||
import android.os.PowerManager;
|
||||
import android.provider.Settings;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.appcompat.app.AlertDialog;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.media3.session.MediaController;
|
||||
import androidx.media3.session.SessionToken;
|
||||
|
||||
import com.cappielloantonio.play.R;
|
||||
import com.cappielloantonio.play.interfaces.MusicServiceEventListener;
|
||||
import com.cappielloantonio.play.service.DownloadTracker;
|
||||
import com.cappielloantonio.play.service.DownloaderService;
|
||||
import com.cappielloantonio.play.service.MusicPlayerRemote;
|
||||
import com.cappielloantonio.play.service.MusicService;
|
||||
import com.cappielloantonio.play.util.DownloadUtil;
|
||||
import com.google.android.exoplayer2.offline.DownloadService;
|
||||
import com.cappielloantonio.play.service.MediaService;
|
||||
import com.google.common.util.concurrent.ListenableFuture;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class BaseActivity extends AppCompatActivity implements MusicServiceEventListener, DownloadTracker.Listener {
|
||||
public class BaseActivity extends AppCompatActivity {
|
||||
private static final String TAG = "BaseActivity";
|
||||
|
||||
private final List<MusicServiceEventListener> mMusicServiceEventListeners = new ArrayList<>();
|
||||
|
||||
private MusicPlayerRemote.ServiceToken serviceToken;
|
||||
private MusicStateReceiver musicStateReceiver;
|
||||
|
||||
private DownloadTracker downloadTracker;
|
||||
|
||||
private boolean receiverRegistered;
|
||||
public SessionToken sessionToken;
|
||||
public ListenableFuture<MediaController> mediaControllerListenableFuture;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
Log.d(TAG, "onCreate");
|
||||
|
||||
serviceToken = MusicPlayerRemote.bindToService(this, new ServiceConnection() {
|
||||
@Override
|
||||
public void onServiceConnected(ComponentName name, IBinder service) {
|
||||
Log.d(TAG, "onServiceConnected");
|
||||
BaseActivity.this.onServiceConnected();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onServiceDisconnected(ComponentName name) {
|
||||
Log.d(TAG, "onServiceDisconnected");
|
||||
BaseActivity.this.onServiceDisconnected();
|
||||
}
|
||||
});
|
||||
|
||||
downloadTracker = DownloadUtil.getDownloadTracker(this);
|
||||
// Start the download service if it should be running but it's not currently.
|
||||
// Starting the service in the foreground causes notification flicker if there is no scheduled
|
||||
// action. Starting it in the background throws an exception if the app is in the background too
|
||||
// (e.g. if device screen is locked).
|
||||
try {
|
||||
DownloadService.start(this, DownloaderService.class);
|
||||
} catch (IllegalStateException e) {
|
||||
DownloadService.startForeground(this, DownloaderService.class);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStart() {
|
||||
protected void onStart() {
|
||||
super.onStart();
|
||||
downloadTracker.addListener(this);
|
||||
initializeMediaController();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -85,19 +34,9 @@ public class BaseActivity extends AppCompatActivity implements MusicServiceEvent
|
|||
}
|
||||
|
||||
@Override
|
||||
public void onStop() {
|
||||
downloadTracker.removeListener(this);
|
||||
protected void onStop() {
|
||||
super.onStop();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
super.onDestroy();
|
||||
MusicPlayerRemote.unbindFromService(serviceToken);
|
||||
if (receiverRegistered) {
|
||||
unregisterReceiver(musicStateReceiver);
|
||||
receiverRegistered = false;
|
||||
}
|
||||
releaseMediaController();
|
||||
}
|
||||
|
||||
private void checkBatteryOptimization() {
|
||||
|
|
@ -128,121 +67,13 @@ public class BaseActivity extends AppCompatActivity implements MusicServiceEvent
|
|||
startActivity(intent);
|
||||
}
|
||||
|
||||
public void addMusicServiceEventListener(final MusicServiceEventListener listener) {
|
||||
if (listener != null) {
|
||||
mMusicServiceEventListeners.add(listener);
|
||||
}
|
||||
@SuppressLint("UnsafeOptInUsageError")
|
||||
private void initializeMediaController() {
|
||||
sessionToken = new SessionToken(this, new ComponentName(this, MediaService.class));
|
||||
mediaControllerListenableFuture = new MediaController.Builder(this, sessionToken).buildAsync();
|
||||
}
|
||||
|
||||
public void removeMusicServiceEventListener(final MusicServiceEventListener listener) {
|
||||
if (listener != null) {
|
||||
mMusicServiceEventListeners.remove(listener);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onServiceConnected() {
|
||||
if (!receiverRegistered) {
|
||||
musicStateReceiver = new MusicStateReceiver(this);
|
||||
|
||||
final IntentFilter filter = new IntentFilter();
|
||||
filter.addAction(MusicService.STATE_CHANGED);
|
||||
filter.addAction(MusicService.META_CHANGED);
|
||||
filter.addAction(MusicService.QUEUE_CHANGED);
|
||||
|
||||
registerReceiver(musicStateReceiver, filter);
|
||||
|
||||
receiverRegistered = true;
|
||||
}
|
||||
|
||||
for (MusicServiceEventListener listener : mMusicServiceEventListeners) {
|
||||
if (listener != null) {
|
||||
listener.onServiceConnected();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onServiceDisconnected() {
|
||||
if (receiverRegistered) {
|
||||
unregisterReceiver(musicStateReceiver);
|
||||
receiverRegistered = false;
|
||||
}
|
||||
|
||||
for (MusicServiceEventListener listener : mMusicServiceEventListeners) {
|
||||
if (listener != null) {
|
||||
listener.onServiceDisconnected();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onQueueChanged() {
|
||||
for (MusicServiceEventListener listener : mMusicServiceEventListeners) {
|
||||
if (listener != null) {
|
||||
listener.onQueueChanged();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPlayMetadataChanged() {
|
||||
for (MusicServiceEventListener listener : mMusicServiceEventListeners) {
|
||||
if (listener != null) {
|
||||
listener.onPlayMetadataChanged();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPlayStateChanged() {
|
||||
for (MusicServiceEventListener listener : mMusicServiceEventListeners) {
|
||||
if (listener != null) {
|
||||
listener.onPlayStateChanged();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRepeatModeChanged() {
|
||||
for (MusicServiceEventListener listener : mMusicServiceEventListeners) {
|
||||
if (listener != null) {
|
||||
listener.onRepeatModeChanged();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDownloadsChanged() {
|
||||
// TODO Notificare all'item scaricato che lo stato di download è cambiato
|
||||
// sampleAdapter.notifyDataSetChanged();
|
||||
}
|
||||
|
||||
private static final class MusicStateReceiver extends BroadcastReceiver {
|
||||
|
||||
private final WeakReference<BaseActivity> reference;
|
||||
|
||||
public MusicStateReceiver(final BaseActivity activity) {
|
||||
reference = new WeakReference<>(activity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onReceive(final Context context, @NonNull final Intent intent) {
|
||||
final String action = intent.getAction();
|
||||
BaseActivity activity = reference.get();
|
||||
if (activity != null && action != null) {
|
||||
switch (action) {
|
||||
case MusicService.META_CHANGED:
|
||||
activity.onPlayMetadataChanged();
|
||||
break;
|
||||
case MusicService.QUEUE_CHANGED:
|
||||
activity.onQueueChanged();
|
||||
break;
|
||||
case MusicService.STATE_CHANGED:
|
||||
activity.onPlayStateChanged();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
private void releaseMediaController() {
|
||||
MediaController.releaseFuture(mediaControllerListenableFuture);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue