1
0
mirror of https://github.com/TeamNewPipe/NewPipe synced 2024-06-30 00:53:19 +00:00
NewPipe/app/src/main/java/org/schabi/newpipe/player/PlayerService.java

148 lines
4.2 KiB
Java
Raw Normal View History

/*
* Copyright 2017 Mauricio Colli <mauriciocolli@outlook.com>
* Part of NewPipe
*
* License: GPL-3.0+
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.schabi.newpipe.player;
2022-04-08 07:35:14 +00:00
import static org.schabi.newpipe.util.Localization.assureCorrectAppLanguage;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
2022-07-21 16:00:43 +00:00
import org.schabi.newpipe.player.mediasession.MediaSessionPlayerUi;
import org.schabi.newpipe.util.ThemeHelper;
/**
2020-07-14 17:21:32 +00:00
* One service for all players.
*/
2022-04-08 07:35:14 +00:00
public final class PlayerService extends Service {
private static final String TAG = PlayerService.class.getSimpleName();
2021-01-08 17:35:33 +00:00
private static final boolean DEBUG = Player.DEBUG;
2021-01-08 17:35:33 +00:00
private Player player;
2022-04-08 07:35:14 +00:00
private final IBinder mBinder = new PlayerService.LocalBinder();
/*//////////////////////////////////////////////////////////////////////////
// Service's LifeCycle
//////////////////////////////////////////////////////////////////////////*/
@Override
public void onCreate() {
2020-07-14 17:21:32 +00:00
if (DEBUG) {
Log.d(TAG, "onCreate() called");
}
2020-07-13 01:17:21 +00:00
assureCorrectAppLanguage(this);
ThemeHelper.setTheme(this);
2021-01-08 17:35:33 +00:00
player = new Player(this);
}
@Override
2020-07-14 17:21:32 +00:00
public int onStartCommand(final Intent intent, final int flags, final int startId) {
if (DEBUG) {
Log.d(TAG, "onStartCommand() called with: intent = [" + intent
+ "], flags = [" + flags + "], startId = [" + startId + "]");
}
if (Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction())
&& player.getPlayQueue() == null) {
// No need to process media button's actions if the player is not working, otherwise the
// player service would strangely start with nothing to play
return START_NOT_STICKY;
}
2022-04-16 14:01:23 +00:00
player.handleIntent(intent);
2022-07-21 16:00:43 +00:00
player.UIs().get(MediaSessionPlayerUi.class)
.ifPresent(ui -> ui.handleMediaButtonIntent(intent));
2022-04-16 14:01:23 +00:00
return START_NOT_STICKY;
}
public void stopForImmediateReusing() {
2020-07-14 17:21:32 +00:00
if (DEBUG) {
Log.d(TAG, "stopForImmediateReusing() called");
2020-07-14 17:21:32 +00:00
}
2021-01-08 17:35:33 +00:00
if (!player.exoPlayerIsNull()) {
// Releases wifi & cpu, disables keepScreenOn, etc.
2020-07-14 17:21:32 +00:00
// We can't just pause the player here because it will make transition
// from one stream to a new stream not smooth
2022-04-08 07:35:14 +00:00
player.smoothStopForImmediateReusing();
}
}
@Override
2020-07-14 17:21:32 +00:00
public void onTaskRemoved(final Intent rootIntent) {
super.onTaskRemoved(rootIntent);
2021-01-08 17:35:33 +00:00
if (!player.videoPlayerSelected()) {
return;
}
onDestroy();
// Unload from memory completely
Runtime.getRuntime().halt(0);
}
@Override
public void onDestroy() {
2020-07-14 17:21:32 +00:00
if (DEBUG) {
Log.d(TAG, "destroy() called");
}
cleanup();
}
2021-01-08 17:35:33 +00:00
private void cleanup() {
2021-01-08 17:35:33 +00:00
if (player != null) {
player.destroy();
player = null;
2021-01-08 17:35:33 +00:00
}
}
2021-01-08 17:35:33 +00:00
public void stopService() {
cleanup();
2021-01-08 17:35:33 +00:00
stopSelf();
}
@Override
2020-07-14 17:21:32 +00:00
protected void attachBaseContext(final Context base) {
super.attachBaseContext(AudioServiceLeakFix.preventLeakOf(base));
}
@Override
2020-07-14 17:21:32 +00:00
public IBinder onBind(final Intent intent) {
return mBinder;
}
public class LocalBinder extends Binder {
2022-04-08 07:35:14 +00:00
public PlayerService getService() {
return PlayerService.this;
}
2021-01-08 17:35:33 +00:00
public Player getPlayer() {
2022-04-08 07:35:14 +00:00
return PlayerService.this.player;
}
}
}