Search in sources :

Example 1 with SleepTimer

use of com.simplecity.amp_library.utils.SleepTimer in project Shuttle by timusus.

the class MusicService method onCreate.

@SuppressLint("InlinedApi")
@Override
public void onCreate() {
    super.onCreate();
    notificationHelper = new MusicNotificationHelper(this);
    servicePrefs = getSharedPreferences("Service", 0);
    prefs = PreferenceManager.getDefaultSharedPreferences(this);
    // Start up the thread running the service. Note that we create a
    // separate thread because the service normally runs in the process's
    // main thread, which we don't want to block. We also make it
    // background priority so CPU-intensive work will not disrupt the UI.
    handlerThread = new HandlerThread("MusicPlayerHandler", android.os.Process.THREAD_PRIORITY_BACKGROUND);
    handlerThread.start();
    mainHandler = new Handler(Looper.getMainLooper());
    // Initialize the handlers
    playerHandler = new MediaPlayerHandler(this, handlerThread.getLooper());
    notificationStateHandler = new NotificationStateHandler(this);
    registerHeadsetPlugReceiver();
    registerBluetoothReceiver();
    audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    mediaButtonReceiverComponent = new ComponentName(getPackageName(), MediaButtonIntentReceiver.class.getName());
    setupMediaSession();
    playbackLocation = LOCAL;
    if (ShuttleUtils.isUpgraded()) {
        castManager = VideoCastManager.getInstance();
        setupCastListener();
        castManager.addVideoCastConsumer(castConsumer);
    }
    if (castManager != null && castManager.isConnected()) {
        updatePlaybackLocation(REMOTE);
    } else {
        updatePlaybackLocation(LOCAL);
    }
    playbackState = STOPPED;
    registerExternalStorageListener();
    registerA2dpServiceListener();
    player = new MultiPlayer(this);
    player.setHandler(playerHandler);
    equalizer = new Equalizer(this);
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(ServiceCommand.SERVICE_COMMAND);
    intentFilter.addAction(ServiceCommand.TOGGLE_PAUSE_ACTION);
    intentFilter.addAction(ServiceCommand.PAUSE_ACTION);
    intentFilter.addAction(ServiceCommand.NEXT_ACTION);
    intentFilter.addAction(ServiceCommand.PREV_ACTION);
    intentFilter.addAction(ServiceCommand.STOP_ACTION);
    intentFilter.addAction(ServiceCommand.SHUFFLE_ACTION);
    intentFilter.addAction(ServiceCommand.REPEAT_ACTION);
    intentFilter.addAction(ExternalIntents.PLAY_STATUS_REQUEST);
    registerReceiver(mIntentReceiver, intentFilter);
    final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, getClass().getName());
    wakeLock.setReferenceCounted(false);
    // Initialize the delayed shutdown intent
    final Intent shutdownIntent = new Intent(this, MusicService.class);
    shutdownIntent.setAction(ServiceCommand.SHUTDOWN);
    alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    this.shutdownIntent = PendingIntent.getService(this, 0, shutdownIntent, 0);
    // Listen for the idle state
    scheduleDelayedShutdown();
    reloadQueue();
    disposables.add(SleepTimer.getInstance().getCurrentTimeObservable().subscribe(remainingTime -> {
        if (remainingTime == 0) {
            if (SleepTimer.getInstance().playToEnd) {
                pauseOnTrackFinish = true;
            } else {
                playerHandler.sendEmptyMessage(MusicService.PlayerHandler.FADE_DOWN_STOP);
            }
        }
    }, throwable -> LogUtils.logException(TAG, "Error consuming SleepTimer observable", throwable)));
}
Also used : PowerManager(android.os.PowerManager) R(com.simplecity.amp_library.R) WidgetProviderSmall(com.simplecity.amp_library.ui.widgets.WidgetProviderSmall) Bundle(android.os.Bundle) WebImage(com.google.android.gms.common.images.WebImage) PackageManager(android.content.pm.PackageManager) CrashlyticsCore(com.crashlytics.android.core.CrashlyticsCore) PlaylistUtils(com.simplecity.amp_library.utils.PlaylistUtils) Completable(io.reactivex.Completable) Uri(android.net.Uri) Random(java.util.Random) PendingIntent(android.app.PendingIntent) MediaSessionCompat(android.support.v4.media.session.MediaSessionCompat) Drawable(android.graphics.drawable.Drawable) AndroidSchedulers(io.reactivex.android.schedulers.AndroidSchedulers) IBinder(android.os.IBinder) WakeLock(android.os.PowerManager.WakeLock) Song(com.simplecity.amp_library.model.Song) Manifest(android.Manifest) SQLiteException(android.database.sqlite.SQLiteException) PowerManager(android.os.PowerManager) MediaMetadataCompat(android.support.v4.media.MediaMetadataCompat) Handler(android.os.Handler) Looper(android.os.Looper) MediaStore(android.provider.MediaStore) Map(java.util.Map) Schedulers(io.reactivex.schedulers.Schedulers) PreferenceManager(android.preference.PreferenceManager) PlaybackStateCompat(android.support.v4.media.session.PlaybackStateCompat) UnsafeAction(com.simplecity.amp_library.rx.UnsafeAction) Log(android.util.Log) WidgetProviderLarge(com.simplecity.amp_library.ui.widgets.WidgetProviderLarge) SimpleTarget(com.bumptech.glide.request.target.SimpleTarget) LogUtils(com.simplecity.amp_library.utils.LogUtils) Service(android.app.Service) IntentFilter(android.content.IntentFilter) ContextCompat(android.support.v4.content.ContextCompat) MediaStatus(com.google.android.gms.cast.MediaStatus) MediaButtonIntentReceiver(com.simplecity.amp_library.utils.MediaButtonIntentReceiver) NoConnectionException(com.google.android.libraries.cast.companionlibrary.cast.exceptions.NoConnectionException) AudioFocusRequest(android.media.AudioFocusRequest) VideoCastManager(com.google.android.libraries.cast.companionlibrary.cast.VideoCastManager) MusicNotificationHelper(com.simplecity.amp_library.notifications.MusicNotificationHelper) BroadcastReceiver(android.content.BroadcastReceiver) SettingsManager(com.simplecity.amp_library.utils.SettingsManager) List(java.util.List) CompositeDisposable(io.reactivex.disposables.CompositeDisposable) WidgetProviderMedium(com.simplecity.amp_library.ui.widgets.WidgetProviderMedium) GlideUtils(com.simplecity.amp_library.glide.utils.GlideUtils) ContentValues(android.content.ContentValues) Nullable(android.support.annotation.Nullable) ConcurrentModificationException(java.util.ConcurrentModificationException) Context(android.content.Context) Album(com.simplecity.amp_library.model.Album) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Intent(android.content.Intent) SystemClock(android.os.SystemClock) NonNull(android.support.annotation.NonNull) Single(io.reactivex.Single) AudioManager(android.media.AudioManager) ApplicationMetadata(com.google.android.gms.cast.ApplicationMetadata) HttpServer(com.simplecity.amp_library.http.HttpServer) ArrayList(java.util.ArrayList) GlideAnimation(com.bumptech.glide.request.animation.GlideAnimation) SuppressLint(android.annotation.SuppressLint) BluetoothHeadset(android.bluetooth.BluetoothHeadset) SleepTimer(com.simplecity.amp_library.utils.SleepTimer) Toast(android.widget.Toast) AudioAttributes(android.media.AudioAttributes) BluetoothA2dp(android.bluetooth.BluetoothA2dp) Equalizer(com.simplecity.amp_library.services.Equalizer) VideoCastConsumerImpl(com.google.android.libraries.cast.companionlibrary.cast.callbacks.VideoCastConsumerImpl) UnsafeConsumer(com.simplecity.amp_library.rx.UnsafeConsumer) PlaceholderProvider(com.simplecity.amp_library.utils.PlaceholderProvider) ShuttleUtils(com.simplecity.amp_library.utils.ShuttleUtils) TransientNetworkDisconnectionException(com.google.android.libraries.cast.companionlibrary.cast.exceptions.TransientNetworkDisconnectionException) AlarmManager(android.app.AlarmManager) WidgetProviderExtraLarge(com.simplecity.amp_library.ui.widgets.WidgetProviderExtraLarge) Action(io.reactivex.functions.Action) ComponentName(android.content.ComponentName) MediaInfo(com.google.android.gms.cast.MediaInfo) TextUtils(android.text.TextUtils) CastException(com.google.android.libraries.cast.companionlibrary.cast.exceptions.CastException) AppWidgetManager(android.appwidget.AppWidgetManager) RemoteControlClient(android.media.RemoteControlClient) Glide(com.bumptech.glide.Glide) SharedPreferences(android.content.SharedPreferences) TreeMap(java.util.TreeMap) HandlerThread(android.os.HandlerThread) Bitmap(android.graphics.Bitmap) Crashlytics(com.crashlytics.android.Crashlytics) DataManager(com.simplecity.amp_library.utils.DataManager) OnAudioFocusChangeListener(android.media.AudioManager.OnAudioFocusChangeListener) Predicate(com.annimon.stream.function.Predicate) MediaMetadata(com.google.android.gms.cast.MediaMetadata) Collections(java.util.Collections) ContentUris(android.content.ContentUris) IntentFilter(android.content.IntentFilter) HandlerThread(android.os.HandlerThread) Handler(android.os.Handler) MusicNotificationHelper(com.simplecity.amp_library.notifications.MusicNotificationHelper) ComponentName(android.content.ComponentName) PendingIntent(android.app.PendingIntent) Intent(android.content.Intent) Equalizer(com.simplecity.amp_library.services.Equalizer) SuppressLint(android.annotation.SuppressLint)

Aggregations

Manifest (android.Manifest)1 SuppressLint (android.annotation.SuppressLint)1 AlarmManager (android.app.AlarmManager)1 PendingIntent (android.app.PendingIntent)1 Service (android.app.Service)1 AppWidgetManager (android.appwidget.AppWidgetManager)1 BluetoothA2dp (android.bluetooth.BluetoothA2dp)1 BluetoothHeadset (android.bluetooth.BluetoothHeadset)1 BroadcastReceiver (android.content.BroadcastReceiver)1 ComponentName (android.content.ComponentName)1 ContentUris (android.content.ContentUris)1 ContentValues (android.content.ContentValues)1 Context (android.content.Context)1 Intent (android.content.Intent)1 IntentFilter (android.content.IntentFilter)1 SharedPreferences (android.content.SharedPreferences)1 PackageManager (android.content.pm.PackageManager)1 SQLiteException (android.database.sqlite.SQLiteException)1 Bitmap (android.graphics.Bitmap)1 Drawable (android.graphics.drawable.Drawable)1