2015-11-24 00:40:36 +00:00
|
|
|
package org.schabi.newpipe;
|
|
|
|
|
|
|
|
import android.app.Notification;
|
2015-11-25 17:18:01 +00:00
|
|
|
import android.app.NotificationManager;
|
2015-11-29 17:05:44 +00:00
|
|
|
import android.app.PendingIntent;
|
2015-11-25 15:19:50 +00:00
|
|
|
import android.app.Service;
|
2015-12-17 17:49:12 +00:00
|
|
|
import android.content.BroadcastReceiver;
|
2015-11-25 15:19:50 +00:00
|
|
|
import android.content.Context;
|
2015-11-24 00:40:36 +00:00
|
|
|
import android.content.Intent;
|
2015-12-17 17:49:12 +00:00
|
|
|
import android.content.IntentFilter;
|
2015-12-24 23:09:35 +00:00
|
|
|
import android.content.res.Resources;
|
|
|
|
import android.graphics.Bitmap;
|
2015-11-24 00:40:36 +00:00
|
|
|
import android.media.AudioManager;
|
|
|
|
import android.media.MediaPlayer;
|
2015-11-25 15:19:50 +00:00
|
|
|
import android.net.wifi.WifiManager;
|
|
|
|
import android.os.IBinder;
|
2015-11-24 00:40:36 +00:00
|
|
|
import android.os.PowerManager;
|
2015-11-25 15:19:50 +00:00
|
|
|
import android.support.v7.app.NotificationCompat;
|
2015-11-25 17:18:01 +00:00
|
|
|
import android.util.Log;
|
2016-01-02 15:08:18 +00:00
|
|
|
import android.widget.RemoteViews;
|
2015-11-25 15:19:50 +00:00
|
|
|
import android.widget.Toast;
|
2015-11-24 00:40:36 +00:00
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by Adam Howard on 08/11/15.
|
|
|
|
* Copyright (c) Adam Howard <achdisposable1@gmail.com> 2015
|
|
|
|
*
|
|
|
|
* BackgroundPlayer.java is part of NewPipe.
|
|
|
|
*
|
|
|
|
* NewPipe 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.
|
|
|
|
*
|
|
|
|
* NewPipe 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 NewPipe. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2015-11-25 15:19:50 +00:00
|
|
|
|
2015-12-17 17:49:12 +00:00
|
|
|
/**Plays the audio stream of videos in the background.*/
|
2015-11-25 15:19:50 +00:00
|
|
|
public class BackgroundPlayer extends Service /*implements MediaPlayer.OnPreparedListener*/ {
|
2015-11-24 00:40:36 +00:00
|
|
|
|
|
|
|
private static final String TAG = BackgroundPlayer.class.toString();
|
2015-12-24 23:09:35 +00:00
|
|
|
private static final String ACTION_STOP = TAG + ".STOP";
|
|
|
|
private static final String ACTION_PLAYPAUSE = TAG + ".PLAYPAUSE";
|
|
|
|
|
|
|
|
// Extra intent arguments
|
|
|
|
public static final String TITLE = "title";
|
|
|
|
public static final String WEB_URL = "web_url";
|
|
|
|
public static final String SERVICE_ID = "service_id";
|
|
|
|
public static final String CHANNEL_NAME="channel_name";
|
|
|
|
|
|
|
|
private volatile String webUrl = "";
|
|
|
|
private volatile int serviceId = -1;
|
|
|
|
private volatile String channelName = "";
|
|
|
|
|
|
|
|
// Determines if the service is already running.
|
|
|
|
// Prevents launching the service twice.
|
|
|
|
public static volatile boolean isRunning = false;
|
2015-11-25 15:19:50 +00:00
|
|
|
|
2015-11-24 00:40:36 +00:00
|
|
|
public BackgroundPlayer() {
|
2015-11-25 15:19:50 +00:00
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCreate() {
|
2015-12-17 17:49:12 +00:00
|
|
|
/*PendingIntent pi = PendingIntent.getActivity(this, 0,
|
|
|
|
new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);*/
|
2015-11-25 15:19:50 +00:00
|
|
|
super.onCreate();
|
|
|
|
}
|
|
|
|
@Override
|
|
|
|
public int onStartCommand(Intent intent, int flags, int startId) {
|
2016-01-05 19:56:40 +00:00
|
|
|
Toast.makeText(this, R.string.background_player_playing_toast,
|
2015-12-24 23:09:35 +00:00
|
|
|
Toast.LENGTH_SHORT).show();
|
2015-11-25 15:19:50 +00:00
|
|
|
|
2015-11-25 17:18:01 +00:00
|
|
|
String source = intent.getDataString();
|
2015-12-17 17:49:12 +00:00
|
|
|
//Log.i(TAG, "backgroundPLayer source:"+source);
|
2015-12-24 23:09:35 +00:00
|
|
|
String videoTitle = intent.getStringExtra(TITLE);
|
|
|
|
webUrl = intent.getStringExtra(WEB_URL);
|
|
|
|
serviceId = intent.getIntExtra(SERVICE_ID, -1);
|
|
|
|
channelName = intent.getStringExtra(CHANNEL_NAME);
|
2015-11-25 17:18:01 +00:00
|
|
|
|
2015-12-17 17:49:12 +00:00
|
|
|
//do nearly everything in a separate thread
|
2015-11-25 17:18:01 +00:00
|
|
|
PlayerThread player = new PlayerThread(source, videoTitle, this);
|
|
|
|
player.start();
|
2015-11-25 15:19:50 +00:00
|
|
|
|
2015-12-24 23:09:35 +00:00
|
|
|
isRunning = true;
|
|
|
|
|
2015-11-25 17:18:01 +00:00
|
|
|
// If we get killed after returning here, don't restart
|
2015-11-25 15:19:50 +00:00
|
|
|
return START_NOT_STICKY;
|
2015-11-24 00:40:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-11-25 15:19:50 +00:00
|
|
|
public IBinder onBind(Intent intent) {
|
2015-11-25 17:18:01 +00:00
|
|
|
// We don't provide binding (yet?), so return null
|
2015-11-25 15:19:50 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDestroy() {
|
|
|
|
//Toast.makeText(this, "service done", Toast.LENGTH_SHORT).show();
|
2015-12-24 23:09:35 +00:00
|
|
|
isRunning = false;
|
2015-11-25 15:19:50 +00:00
|
|
|
}
|
|
|
|
|
2015-11-25 17:18:01 +00:00
|
|
|
private class PlayerThread extends Thread {
|
2015-12-17 17:49:12 +00:00
|
|
|
MediaPlayer mediaPlayer;
|
2015-11-25 17:18:01 +00:00
|
|
|
private String source;
|
|
|
|
private String title;
|
2015-12-17 17:49:12 +00:00
|
|
|
private int noteID = TAG.hashCode();
|
2015-11-25 17:18:01 +00:00
|
|
|
private BackgroundPlayer owner;
|
2015-12-17 17:49:12 +00:00
|
|
|
private NotificationManager noteMgr;
|
|
|
|
private WifiManager.WifiLock wifiLock;
|
2015-12-24 23:09:35 +00:00
|
|
|
private Bitmap videoThumbnail = null;
|
2016-01-02 15:08:18 +00:00
|
|
|
private NotificationCompat.Builder noteBuilder;
|
2015-12-17 17:49:12 +00:00
|
|
|
|
2015-11-25 17:18:01 +00:00
|
|
|
public PlayerThread(String src, String title, BackgroundPlayer owner) {
|
|
|
|
this.source = src;
|
|
|
|
this.title = title;
|
|
|
|
this.owner = owner;
|
|
|
|
mediaPlayer = new MediaPlayer();
|
|
|
|
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
|
2015-11-25 15:19:50 +00:00
|
|
|
}
|
2016-01-02 15:08:18 +00:00
|
|
|
|
2015-11-25 17:18:01 +00:00
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
mediaPlayer.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);//cpu lock
|
|
|
|
try {
|
|
|
|
mediaPlayer.setDataSource(source);
|
2015-12-24 23:09:35 +00:00
|
|
|
//We are already in a separate worker thread,
|
2015-11-25 17:18:01 +00:00
|
|
|
//so calling the blocking prepare() method should be ok
|
2015-12-24 23:09:35 +00:00
|
|
|
mediaPlayer.prepare();
|
2015-11-25 17:18:01 +00:00
|
|
|
|
|
|
|
} catch (IOException ioe) {
|
|
|
|
ioe.printStackTrace();
|
|
|
|
Log.e(TAG, "video source:" + source);
|
|
|
|
Log.e(TAG, "video title:" + title);
|
|
|
|
//can't do anything useful without a file to play; exit early
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-12-24 23:09:35 +00:00
|
|
|
try {
|
|
|
|
videoThumbnail = ActivityCommunicator.getCommunicator().backgroundPlayerThumbnail;
|
|
|
|
} catch (Exception e) {
|
|
|
|
Log.e(TAG, "Could not get video thumbnail from ActivityCommunicator");
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
2015-11-25 17:18:01 +00:00
|
|
|
WifiManager wifiMgr = ((WifiManager)getSystemService(Context.WIFI_SERVICE));
|
2015-12-17 17:49:12 +00:00
|
|
|
wifiLock = wifiMgr.createWifiLock(WifiManager.WIFI_MODE_FULL, TAG);
|
2015-11-25 17:18:01 +00:00
|
|
|
|
2015-12-24 23:09:35 +00:00
|
|
|
//listen for end of video
|
|
|
|
mediaPlayer.setOnCompletionListener(new EndListener(wifiLock));
|
2015-11-25 17:18:01 +00:00
|
|
|
|
|
|
|
//get audio focus
|
|
|
|
/*
|
|
|
|
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
|
|
|
|
int result = audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC,
|
|
|
|
AudioManager.AUDIOFOCUS_GAIN);
|
|
|
|
|
|
|
|
if (result != AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
|
|
|
|
// could not get audio focus.
|
|
|
|
}*/
|
|
|
|
wifiLock.acquire();
|
|
|
|
mediaPlayer.start();
|
2015-12-17 17:49:12 +00:00
|
|
|
|
2015-12-20 00:08:12 +00:00
|
|
|
IntentFilter filter = new IntentFilter();
|
|
|
|
filter.setPriority(Integer.MAX_VALUE);
|
|
|
|
filter.addAction(ACTION_PLAYPAUSE);
|
|
|
|
filter.addAction(ACTION_STOP);
|
|
|
|
registerReceiver(broadcastReceiver, filter);
|
2015-12-17 17:49:12 +00:00
|
|
|
|
2016-01-02 15:08:18 +00:00
|
|
|
Notification note = buildNotification();
|
2015-12-17 17:49:12 +00:00
|
|
|
|
2015-12-24 23:09:35 +00:00
|
|
|
startForeground(noteID, note);
|
2015-12-17 17:49:12 +00:00
|
|
|
|
|
|
|
//currently decommissioned progressbar looping update code - works, but doesn't fit inside
|
|
|
|
//Notification.MediaStyle Notification layout.
|
|
|
|
noteMgr = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
|
|
|
|
/*
|
|
|
|
//update every 2s or 4 times in the video, whichever is shorter
|
|
|
|
int sleepTime = Math.min(2000, (int)((double)vidLength/4));
|
2015-11-25 17:18:01 +00:00
|
|
|
while(mediaPlayer.isPlaying()) {
|
2015-12-17 17:49:12 +00:00
|
|
|
noteBuilder.setProgress(vidLength, mediaPlayer.getCurrentPosition(), false);
|
|
|
|
noteMgr.notify(noteID, noteBuilder.build());
|
2015-11-25 17:18:01 +00:00
|
|
|
try {
|
|
|
|
Thread.sleep(sleepTime);
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
Log.d(TAG, "sleep failure");
|
|
|
|
}
|
2015-12-17 17:49:12 +00:00
|
|
|
}*/
|
|
|
|
}
|
|
|
|
|
2016-01-04 01:15:13 +00:00
|
|
|
/**Handles button presses from the notification. */
|
2015-12-17 17:49:12 +00:00
|
|
|
private final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
|
|
|
|
@Override
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
|
|
|
String action = intent.getAction();
|
2015-12-24 23:09:35 +00:00
|
|
|
//Log.i(TAG, "received broadcast action:"+action);
|
2015-12-17 17:49:12 +00:00
|
|
|
if(action.equals(ACTION_PLAYPAUSE)) {
|
|
|
|
if(mediaPlayer.isPlaying()) {
|
|
|
|
mediaPlayer.pause();
|
|
|
|
}
|
|
|
|
else {
|
2016-01-04 01:15:13 +00:00
|
|
|
//reacquire CPU lock after auto-releasing it on pause
|
2015-12-17 18:27:35 +00:00
|
|
|
mediaPlayer.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
|
2015-12-17 17:49:12 +00:00
|
|
|
mediaPlayer.start();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(action.equals(ACTION_STOP)) {
|
2015-12-24 23:09:35 +00:00
|
|
|
//this auto-releases CPU lock
|
|
|
|
mediaPlayer.stop();
|
2015-12-17 17:49:12 +00:00
|
|
|
afterPlayCleanup();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
private void afterPlayCleanup() {
|
2015-12-20 20:28:07 +00:00
|
|
|
//remove progress bar
|
2016-01-04 01:15:13 +00:00
|
|
|
//noteBuilder.setProgress(0, 0, false);
|
|
|
|
|
2015-12-20 20:28:07 +00:00
|
|
|
//remove notification
|
|
|
|
noteMgr.cancel(noteID);
|
2015-12-17 17:49:12 +00:00
|
|
|
unregisterReceiver(broadcastReceiver);
|
2015-12-20 20:28:07 +00:00
|
|
|
//release mediaPlayer's system resources
|
|
|
|
mediaPlayer.release();
|
2015-12-17 17:49:12 +00:00
|
|
|
|
2015-12-20 20:28:07 +00:00
|
|
|
//release wifilock
|
|
|
|
wifiLock.release();
|
2016-01-04 01:15:13 +00:00
|
|
|
//remove foreground status of service; make BackgroundPlayer killable
|
2015-12-20 20:28:07 +00:00
|
|
|
stopForeground(true);
|
2015-12-17 17:49:12 +00:00
|
|
|
|
|
|
|
stopSelf();
|
|
|
|
}
|
|
|
|
|
|
|
|
private class EndListener implements MediaPlayer.OnCompletionListener {
|
|
|
|
private WifiManager.WifiLock wl;
|
|
|
|
public EndListener(WifiManager.WifiLock wifiLock) {
|
|
|
|
this.wl = wifiLock;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCompletion(MediaPlayer mp) {
|
|
|
|
afterPlayCleanup();
|
2015-11-25 17:18:01 +00:00
|
|
|
}
|
2015-11-25 15:19:50 +00:00
|
|
|
}
|
2016-01-02 15:08:18 +00:00
|
|
|
|
|
|
|
private Notification buildNotification() {
|
|
|
|
Notification note;
|
|
|
|
Resources res = getApplicationContext().getResources();
|
|
|
|
noteBuilder = new NotificationCompat.Builder(owner);
|
|
|
|
|
|
|
|
PendingIntent playPI = PendingIntent.getBroadcast(owner, noteID,
|
|
|
|
new Intent(ACTION_PLAYPAUSE), PendingIntent.FLAG_UPDATE_CURRENT);
|
|
|
|
PendingIntent stopPI = PendingIntent.getBroadcast(owner, noteID,
|
|
|
|
new Intent(ACTION_STOP), PendingIntent.FLAG_UPDATE_CURRENT);
|
|
|
|
/*
|
|
|
|
NotificationCompat.Action pauseButton = new NotificationCompat.Action.Builder
|
|
|
|
(R.drawable.ic_pause_white_24dp, "Pause", playPI).build();
|
|
|
|
*/
|
|
|
|
|
2016-01-04 01:51:24 +00:00
|
|
|
//build intent to return to video, on tapping notification
|
|
|
|
Intent openDetailView = new Intent(getApplicationContext(),
|
|
|
|
VideoItemDetailActivity.class);
|
|
|
|
openDetailView.putExtra(VideoItemDetailFragment.STREAMING_SERVICE, serviceId);
|
|
|
|
openDetailView.putExtra(VideoItemDetailFragment.VIDEO_URL, webUrl);
|
|
|
|
openDetailView.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
|
|
|
2016-01-02 15:08:18 +00:00
|
|
|
noteBuilder
|
|
|
|
.setOngoing(true)
|
|
|
|
.setDeleteIntent(stopPI)
|
|
|
|
//doesn't fit with Notification.MediaStyle
|
|
|
|
//.setProgress(vidLength, 0, false)
|
|
|
|
.setSmallIcon(R.drawable.ic_play_circle_filled_white_24dp)
|
|
|
|
.setTicker(
|
|
|
|
String.format(res.getString(
|
2016-01-05 19:56:40 +00:00
|
|
|
R.string.background_player_time_text), title))
|
2016-01-04 01:51:24 +00:00
|
|
|
.setContentIntent(PendingIntent.getActivity(getApplicationContext(),
|
|
|
|
noteID, openDetailView,
|
|
|
|
PendingIntent.FLAG_UPDATE_CURRENT));
|
|
|
|
|
2016-01-02 15:08:18 +00:00
|
|
|
|
|
|
|
if (android.os.Build.VERSION.SDK_INT < 21) {
|
|
|
|
|
|
|
|
NotificationCompat.Action playButton = new NotificationCompat.Action.Builder
|
|
|
|
(R.drawable.ic_play_arrow_white_48dp,
|
2016-01-05 19:56:40 +00:00
|
|
|
res.getString(R.string.play_btn_text), playPI).build();
|
2016-01-02 15:08:18 +00:00
|
|
|
|
|
|
|
noteBuilder
|
|
|
|
.setContentTitle(title)
|
|
|
|
//really? Id like to put something more helpful here.
|
2016-01-04 01:15:13 +00:00
|
|
|
//was more of a placeholder than anything else. -medavox
|
2016-01-02 15:08:18 +00:00
|
|
|
//.setContentText("NewPipe is playing in the background")
|
|
|
|
.setContentText(channelName)
|
|
|
|
//.setAutoCancel(!mediaPlayer.isPlaying())
|
|
|
|
.setDeleteIntent(stopPI)
|
|
|
|
//doesn't fit with Notification.MediaStyle
|
|
|
|
//.setProgress(vidLength, 0, false)
|
|
|
|
.setLargeIcon(videoThumbnail)
|
|
|
|
.addAction(playButton);
|
|
|
|
//.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
|
|
|
|
//.setLargeIcon(cover)
|
|
|
|
|
2016-01-04 01:15:13 +00:00
|
|
|
//is wrapping this in an SDK version check really necessary,
|
2016-01-04 01:51:24 +00:00
|
|
|
// if we're using NotificationCompat?
|
|
|
|
// the compat libraries should handle this, right? -medavox
|
2016-01-02 15:08:18 +00:00
|
|
|
if (android.os.Build.VERSION.SDK_INT >= 16)
|
|
|
|
noteBuilder.setPriority(Notification.PRIORITY_LOW);
|
|
|
|
|
|
|
|
noteBuilder.setStyle(new NotificationCompat.MediaStyle()
|
|
|
|
//.setMediaSession(mMediaSession.getSessionToken())
|
|
|
|
.setShowActionsInCompactView(new int[]{0})
|
|
|
|
.setShowCancelButton(true)
|
|
|
|
.setCancelButtonIntent(stopPI));
|
|
|
|
if (videoThumbnail != null) {
|
|
|
|
noteBuilder.setLargeIcon(videoThumbnail);
|
|
|
|
}
|
|
|
|
note = noteBuilder.build();
|
|
|
|
} else {
|
|
|
|
RemoteViews view =
|
|
|
|
new RemoteViews(BuildConfig.APPLICATION_ID, R.layout.player_notification);
|
|
|
|
view.setImageViewBitmap(R.id.backgroundCover, videoThumbnail);
|
|
|
|
view.setTextViewText(R.id.backgroundSongName, title);
|
|
|
|
view.setTextViewText(R.id.backgroundArtist, channelName);
|
|
|
|
view.setOnClickPendingIntent(R.id.backgroundStop, stopPI);
|
|
|
|
view.setOnClickPendingIntent(R.id.backgroundPlayPause, playPI);
|
|
|
|
|
2016-01-04 01:15:13 +00:00
|
|
|
//possibly found the expandedView problem,
|
|
|
|
//but can't test it as I don't have a 5.0 device. -medavox
|
2016-01-02 15:08:18 +00:00
|
|
|
RemoteViews expandedView =
|
2016-01-04 01:15:13 +00:00
|
|
|
new RemoteViews(BuildConfig.APPLICATION_ID, R.layout.player_notification_expanded);
|
2016-01-02 15:08:18 +00:00
|
|
|
expandedView.setImageViewBitmap(R.id.backgroundCover, videoThumbnail);
|
|
|
|
expandedView.setTextViewText(R.id.backgroundSongName, title);
|
|
|
|
expandedView.setTextViewText(R.id.backgroundArtist, channelName);
|
|
|
|
expandedView.setOnClickPendingIntent(R.id.backgroundStop, stopPI);
|
|
|
|
expandedView.setOnClickPendingIntent(R.id.backgroundPlayPause, playPI);
|
|
|
|
|
|
|
|
noteBuilder.setCategory(Notification.CATEGORY_TRANSPORT);
|
|
|
|
|
|
|
|
//Make notification appear on lockscreen
|
|
|
|
noteBuilder.setVisibility(Notification.VISIBILITY_PUBLIC);
|
|
|
|
|
|
|
|
note = noteBuilder.build();
|
|
|
|
note.contentView = view;
|
|
|
|
|
|
|
|
//todo: This never shows up. I was not able to figure out why:
|
|
|
|
note.bigContentView = expandedView;
|
|
|
|
}
|
|
|
|
|
|
|
|
return note;
|
|
|
|
}
|
2015-11-25 15:19:50 +00:00
|
|
|
}
|
2015-11-24 00:40:36 +00:00
|
|
|
}
|