1
0
mirror of https://github.com/TeamNewPipe/NewPipe synced 2024-12-23 16:40:32 +00:00

fixed close notification problem

This commit is contained in:
Christian Schabesberger 2016-01-02 16:08:18 +01:00
parent 8aa5f87a1c
commit 7dadb2b26c
10 changed files with 240 additions and 52 deletions

View File

@ -17,6 +17,7 @@ import android.os.IBinder;
import android.os.PowerManager; import android.os.PowerManager;
import android.support.v7.app.NotificationCompat; import android.support.v7.app.NotificationCompat;
import android.util.Log; import android.util.Log;
import android.widget.RemoteViews;
import android.widget.Toast; import android.widget.Toast;
import java.io.IOException; import java.io.IOException;
@ -113,9 +114,9 @@ public class BackgroundPlayer extends Service /*implements MediaPlayer.OnPrepare
private int noteID = TAG.hashCode(); private int noteID = TAG.hashCode();
private BackgroundPlayer owner; private BackgroundPlayer owner;
private NotificationManager noteMgr; private NotificationManager noteMgr;
private NotificationCompat.Builder noteBuilder;
private WifiManager.WifiLock wifiLock; private WifiManager.WifiLock wifiLock;
private Bitmap videoThumbnail = null; private Bitmap videoThumbnail = null;
private NotificationCompat.Builder noteBuilder;
public PlayerThread(String src, String title, BackgroundPlayer owner) { public PlayerThread(String src, String title, BackgroundPlayer owner) {
this.source = src; this.source = src;
@ -124,10 +125,9 @@ public class BackgroundPlayer extends Service /*implements MediaPlayer.OnPrepare
mediaPlayer = new MediaPlayer(); mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
} }
@Override @Override
public void run() { public void run() {
Resources res = getApplicationContext().getResources();
mediaPlayer.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);//cpu lock mediaPlayer.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);//cpu lock
try { try {
mediaPlayer.setDataSource(source); mediaPlayer.setDataSource(source);
@ -177,54 +177,7 @@ public class BackgroundPlayer extends Service /*implements MediaPlayer.OnPrepare
filter.addAction(ACTION_STOP); filter.addAction(ACTION_STOP);
registerReceiver(broadcastReceiver, filter); registerReceiver(broadcastReceiver, filter);
PendingIntent playPI = PendingIntent.getBroadcast(owner, noteID, Notification note = buildNotification();
new Intent(ACTION_PLAYPAUSE), PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Action playButton = new NotificationCompat.Action.Builder
(R.drawable.ic_play_arrow_white_48dp, "Play", playPI).build();
/*
NotificationCompat.Action pauseButton = new NotificationCompat.Action.Builder
(R.drawable.ic_pause_white_24dp, "Pause", playPI).build();
*/
PendingIntent stopPI = PendingIntent.getBroadcast(owner, noteID,
new Intent(ACTION_STOP), PendingIntent.FLAG_UPDATE_CURRENT);
noteBuilder = new NotificationCompat.Builder(owner);
noteBuilder
.setContentTitle(title)
//really? Id like to put something more helpful here.
//.setContentText("NewPipe is playing in the background")
.setContentText(channelName)
//.setAutoCancel(!mediaPlayer.isPlaying())
.setOngoing(true)
.setDeleteIntent(stopPI)
//doesn't fit with Notification.MediaStyle
//.setProgress(vidLength, 0, false)
.setSmallIcon(R.drawable.ic_play_circle_filled_white_24dp)
.setLargeIcon(videoThumbnail)
.setTicker(
String.format(res.getString(
R.string.backgroundPlayerTickerText), title))
.addAction(playButton);
//.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
//.setLargeIcon(cover)
if(android.os.Build.VERSION.SDK_INT >= 16)
noteBuilder.setPriority(Notification.PRIORITY_LOW);
if(android.os.Build.VERSION.SDK_INT >= 21)
noteBuilder.setCategory(Notification.CATEGORY_TRANSPORT);
noteBuilder.setStyle(new NotificationCompat.MediaStyle()
//.setMediaSession(mMediaSession.getSessionToken())
.setShowActionsInCompactView(new int[]{0})
.setShowCancelButton(true)
.setCancelButtonIntent(stopPI));
if(videoThumbnail != null) {
noteBuilder.setLargeIcon(videoThumbnail);
}
Notification note = noteBuilder.build();
Intent openDetailView = new Intent(getApplicationContext(), Intent openDetailView = new Intent(getApplicationContext(),
VideoItemDetailActivity.class); VideoItemDetailActivity.class);
@ -252,7 +205,6 @@ public class BackgroundPlayer extends Service /*implements MediaPlayer.OnPrepare
Log.d(TAG, "sleep failure"); Log.d(TAG, "sleep failure");
} }
}*/ }*/
} }
private final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() { private final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@ -306,5 +258,93 @@ public class BackgroundPlayer extends Service /*implements MediaPlayer.OnPrepare
afterPlayCleanup(); afterPlayCleanup();
} }
} }
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();
*/
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(
R.string.backgroundPlayerTickerText), title));
if (android.os.Build.VERSION.SDK_INT < 21) {
NotificationCompat.Action playButton = new NotificationCompat.Action.Builder
(R.drawable.ic_play_arrow_white_48dp,
res.getString(R.string.play), playPI).build();
noteBuilder
.setContentTitle(title)
//really? Id like to put something more helpful here.
//.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)
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);
RemoteViews expandedView =
new RemoteViews(BuildConfig.APPLICATION_ID, R.layout.player_notification);
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;
}
} }
} }

Binary file not shown.

After

Width:  |  Height:  |  Size: 221 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 175 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 257 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 347 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 436 B

View File

@ -0,0 +1,66 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/content"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:gravity="center_vertical"
android:orientation="horizontal"
android:background="@color/background_notification_color"
tools:targetApi="jelly_bean">
<ImageView
android:id="@+id/backgroundCover"
android:layout_width="64dp"
android:layout_height="64dp"
android:src="@drawable/dummy_thumbnail"
android:scaleType="centerCrop"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:id="@+id/backgroundSongName"
style="@android:style/TextAppearance.StatusBar.EventContent.Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:singleLine="true"
android:text="title" />
<TextView
android:id="@+id/backgroundArtist"
style="@android:style/TextAppearance.StatusBar.EventContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:singleLine="true"
android:text="artist" />
</LinearLayout>
<ImageButton
android:id="@+id/backgroundPlayPause"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_margin="5dp"
android:background="#00ffffff"
android:clickable="true"
android:scaleType="fitXY"
android:src="@drawable/ic_play_arrow_white_48dp" />
<ImageButton
android:id="@+id/backgroundStop"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_margin="5dp"
android:background="#00ffffff"
android:clickable="true"
android:scaleType="fitXY"
android:src="@drawable/ic_close_white_24dp" />
</LinearLayout>

View File

@ -0,0 +1,80 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/content"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:background="@color/background_notification_color"
tools:targetApi="jelly_bean" >
<ImageView
android:id="@+id/backgroundCover"
android:layout_width="128dp"
android:layout_height="128dp"
android:layout_marginRight="8dp"
android:src="@drawable/dummy_thumbnail"
android:scaleType="centerCrop"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@+id/backgroundButtons"
android:layout_toRightOf="@+id/backgroundCover"
android:gravity="center_vertical"
android:orientation="vertical" >
<TextView
android:id="@+id/backgroundSongName"
style="@android:style/TextAppearance.StatusBar.EventContent.Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="40dp"
android:ellipsize="marquee"
android:singleLine="true"
android:text="title" />
<TextView
android:id="@+id/backgroundArtist"
style="@android:style/TextAppearance.StatusBar.EventContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:singleLine="true"
android:text="artist" />
</LinearLayout>
<ImageButton
android:id="@+id/backgroundStop"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:layout_margin="5dp"
android:background="#00ffffff"
android:clickable="true"
android:scaleType="fitXY"
android:src="@drawable/ic_close_white_24dp" />
<LinearLayout
android:id="@+id/backgroundButtons"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignBottom="@id/backgroundCover"
android:layout_alignParentRight="true"
android:layout_toRightOf="@+id/backgroundCover"
android:orientation="horizontal" >
<ImageButton
android:id="@+id/backgroundPlayPause"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_weight="1"
android:background="#00ffffff"
android:clickable="true"
android:scaleType="centerInside"
android:src="@drawable/ic_play_arrow_white_48dp" />
</LinearLayout>
</RelativeLayout>

View File

@ -7,4 +7,5 @@
<color name="durationText">#efff</color> <color name="durationText">#efff</color>
<color name="dark_overlay">#6000</color> <color name="dark_overlay">#6000</color>
<color name="background_gray">#EEEEEE</color> <color name="background_gray">#EEEEEE</color>
<color name="background_notification_color">#323232</color>
</resources> </resources>

View File

@ -53,6 +53,7 @@
<string name="backgroundPlayerTickerText" translatable="false">%1$s - NewPipe</string> <string name="backgroundPlayerTickerText" translatable="false">%1$s - NewPipe</string>
<string name="backgroundPlayerStartPlayingToast">Playing in background</string> <string name="backgroundPlayerStartPlayingToast">Playing in background</string>
<string name="c3sUrl" translatable="false">https://www.c3s.cc/</string> <string name="c3sUrl" translatable="false">https://www.c3s.cc/</string>
<string name="play">Play</string>
<!-- Content descriptions (for better accessibility) --> <!-- Content descriptions (for better accessibility) -->
<string name="itemThumbnailViewDescription">Video preview thumbnail</string> <string name="itemThumbnailViewDescription">Video preview thumbnail</string>