Use standard media style notifications

Use the standard media style notifications instead of the custom one
This commit is contained in:
Synced Synapse 2019-03-21 18:26:50 +00:00 committed by Martijn Brekhof
parent 5c61743b59
commit 46d4b5ffe1
3 changed files with 23 additions and 381 deletions

View File

@ -30,8 +30,6 @@ import android.os.Build;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder; import android.support.v4.app.TaskStackBuilder;
import android.view.View;
import android.widget.RemoteViews;
import com.squareup.picasso.Picasso; import com.squareup.picasso.Picasso;
import com.squareup.picasso.Target; import com.squareup.picasso.Target;
@ -58,7 +56,7 @@ public class NotificationObserver
public static final String TAG = LogUtils.makeLogTag(NotificationObserver.class); public static final String TAG = LogUtils.makeLogTag(NotificationObserver.class);
public static final int NOTIFICATION_ID = 1; public static final int NOTIFICATION_ID = 1;
public static final String NOTIFICATION_CHANNEL = "KORE"; private static final String NOTIFICATION_CHANNEL = "KORE";
private PendingIntent mRemoteStartPendingIntent; private PendingIntent mRemoteStartPendingIntent;
private Service mService; private Service mService;
@ -139,7 +137,8 @@ public class NotificationObserver
NotificationManager notificationManager = NotificationManager notificationManager =
(NotificationManager) mService.getSystemService(Context.NOTIFICATION_SERVICE); (NotificationManager) mService.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(channel); if (notificationManager != null)
notificationManager.createNotificationChannel(channel);
} }
// Picasso target that will be used to load images // Picasso target that will be used to load images
@ -252,48 +251,20 @@ public class NotificationObserver
ffIcon = R.drawable.ic_fast_forward_white_24dp; ffIcon = R.drawable.ic_fast_forward_white_24dp;
} }
// Setup the collpased and expanded notifications final NotificationCompat.Builder builder =
final RemoteViews collapsedRV = new RemoteViews(mService.getPackageName(), R.layout.notification_colapsed); new NotificationCompat.Builder(mService, NOTIFICATION_CHANNEL)
collapsedRV.setImageViewResource(R.id.rewind, rewindIcon); .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
collapsedRV.setOnClickPendingIntent(R.id.rewind, rewindPendingIntent);
collapsedRV.setImageViewResource(R.id.play, playPauseIcon);
collapsedRV.setOnClickPendingIntent(R.id.play, playPausePendingIntent);
collapsedRV.setImageViewResource(R.id.fast_forward, ffIcon);
collapsedRV.setOnClickPendingIntent(R.id.fast_forward, ffPendingIntent);
collapsedRV.setTextViewText(R.id.title, title);
collapsedRV.setTextViewText(R.id.text2, underTitle);
final RemoteViews expandedRV = new RemoteViews(mService.getPackageName(), R.layout.notification_expanded);
expandedRV.setImageViewResource(R.id.rewind, rewindIcon);
expandedRV.setOnClickPendingIntent(R.id.rewind, rewindPendingIntent);
expandedRV.setImageViewResource(R.id.play, playPauseIcon);
expandedRV.setOnClickPendingIntent(R.id.play, playPausePendingIntent);
expandedRV.setImageViewResource(R.id.fast_forward, ffIcon);
expandedRV.setOnClickPendingIntent(R.id.fast_forward, ffPendingIntent);
expandedRV.setTextViewText(R.id.title, title);
expandedRV.setTextViewText(R.id.text2, underTitle);
final int expandedIconResId;
if (isVideo) {
expandedIconResId = R.id.icon_slim;
expandedRV.setViewVisibility(R.id.icon_slim, View.VISIBLE);
expandedRV.setViewVisibility(R.id.icon_square, View.GONE);
} else {
expandedIconResId = R.id.icon_square;
expandedRV.setViewVisibility(R.id.icon_slim, View.GONE);
expandedRV.setViewVisibility(R.id.icon_square, View.VISIBLE);
}
// Build the notification
NotificationCompat.Builder builder = new NotificationCompat.Builder(mService, NOTIFICATION_CHANNEL);
final Notification notification = builder
.setSmallIcon(smallIcon) .setSmallIcon(smallIcon)
.setShowWhen(false) .setShowWhen(false)
.setOngoing(true) .setOngoing(true)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC) .addAction(rewindIcon, mService.getString(R.string.rewind), rewindPendingIntent) // #0
.setCategory(NotificationCompat.CATEGORY_TRANSPORT) .addAction(playPauseIcon, mService.getString(R.string.play), playPausePendingIntent) // #1
.addAction(ffIcon, mService.getString(R.string.fast_forward), ffPendingIntent) // #2
.setStyle(new android.support.v4.media.app.NotificationCompat.MediaStyle()
.setShowActionsInCompactView(0, 1, 2))
.setContentIntent(mRemoteStartPendingIntent) .setContentIntent(mRemoteStartPendingIntent)
.setContent(collapsedRV) .setContentTitle(title)
.build(); .setContentText(underTitle);
// This is a convoluted way of loading the image and showing the // This is a convoluted way of loading the image and showing the
// notification, but it's what works with Picasso and is efficient. // notification, but it's what works with Picasso and is efficient.
@ -336,15 +307,11 @@ public class NotificationObserver
public void onPrepareLoad(Drawable placeHolderDrawable) { } public void onPrepareLoad(Drawable placeHolderDrawable) { }
private void showNotification(Bitmap bitmap) { private void showNotification(Bitmap bitmap) {
collapsedRV.setImageViewBitmap(R.id.icon, bitmap); builder.setLargeIcon(bitmap);
if (Utils.isJellybeanOrLater()) {
notification.bigContentView = expandedRV;
expandedRV.setImageViewBitmap(expandedIconResId, bitmap);
}
NotificationManager notificationManager = NotificationManager notificationManager =
(NotificationManager) mService.getSystemService(Context.NOTIFICATION_SERVICE); (NotificationManager) mService.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, notification); if (notificationManager != null)
notificationManager.notify(NOTIFICATION_ID, builder.build());
picassoTarget = null; picassoTarget = null;
} }
}; };
@ -367,14 +334,16 @@ public class NotificationObserver
} }
private void removeNotification() { private void removeNotification() {
NotificationManager notificationManager = NotificationManager notificationManager =
(NotificationManager) mService.getSystemService(Context.NOTIFICATION_SERVICE); (NotificationManager)mService.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(NOTIFICATION_ID); if (notificationManager != null)
notificationManager.cancel(NOTIFICATION_ID);
} }
private void notifyNothingPlaying() { private void notifyNothingPlaying() {
NotificationManager notificationManager = NotificationManager notificationManager =
(NotificationManager) mService.getSystemService(Context.NOTIFICATION_SERVICE); (NotificationManager)mService.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, mNothingPlayingNotification); if (notificationManager != null)
notificationManager.notify(NOTIFICATION_ID, mNothingPlayingNotification);
} }
} }

View File

@ -1,100 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright 2015 Synced Synapse. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/status_bar_latest_event_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="@color/notification_backgroup">
<ImageView android:id="@+id/icon"
android:layout_width="@dimen/notification_art_default_width"
android:layout_height="@dimen/notification_art_default_height"
android:layout_weight="0"
android:padding="12dp"
android:scaleType="centerInside"
android:contentDescription="@string/poster"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="fill_vertical"
android:minHeight="@dimen/notification_height"
android:orientation="vertical">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="@dimen/small_padding"
android:textAppearance="@style/TextAppearance.Notification.Title"
android:singleLine="true"
android:ellipsize="marquee"
android:fadingEdge="horizontal"/>
<TextView
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="@style/TextAppearance.Notification.Details"
android:singleLine="true"
android:fadingEdge="horizontal"
android:ellipsize="marquee"/>
</LinearLayout>
<LinearLayout
android:id="@+id/media_actions"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginRight="6dp"
android:layout_marginEnd="6dp"
android:layout_gravity="center_vertical|end"
android:orientation="horizontal"
android:layoutDirection="ltr">
<ImageButton
android:id="@+id/rewind"
style="@style/Widget.Button.Notification"
android:layout_width="@dimen/default_icon_size"
android:layout_height="match_parent"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_weight="1"
android:gravity="center"
android:contentDescription="@string/rewind"/>
<ImageButton
android:id="@+id/play"
style="@style/Widget.Button.Notification"
android:layout_width="@dimen/default_icon_size"
android:layout_height="match_parent"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_weight="1"
android:gravity="center"
android:contentDescription="@string/play"/>
<ImageButton
android:id="@+id/fast_forward"
style="@style/Widget.Button.Notification"
android:layout_width="@dimen/default_icon_size"
android:layout_height="match_parent"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_weight="1"
android:gravity="center"
android:contentDescription="@string/fast_forward"/>
</LinearLayout>
</LinearLayout>

View File

@ -1,227 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright 2015 Synced Synapse. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!--<LinearLayout-->
<!--xmlns:android="http://schemas.android.com/apk/res/android"-->
<!--android:id="@+id/status_bar_latest_event_content"-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="wrap_content"-->
<!--android:orientation="horizontal"-->
<!--android:background="@color/background_floating_material_dark">-->
<!--<ImageView android:id="@+id/icon_square"-->
<!--android:layout_width="@dimen/notification_expanded_art_default_height"-->
<!--android:layout_height="@dimen/notification_expanded_art_default_height"-->
<!--android:scaleType="centerCrop"-->
<!--android:contentDescription="@string/poster"-->
<!--android:visibility="gone"/>-->
<!--<ImageView android:id="@+id/icon_slim"-->
<!--android:layout_width="@dimen/notification_expanded_art_slim_width"-->
<!--android:layout_height="@dimen/notification_expanded_art_default_height"-->
<!--android:scaleType="centerCrop"-->
<!--android:contentDescription="@string/poster"-->
<!--android:visibility="gone"/>-->
<!--<LinearLayout-->
<!--android:layout_width="0dp"-->
<!--android:layout_height="wrap_content"-->
<!--android:layout_weight="1"-->
<!--android:layout_gravity="fill_vertical"-->
<!--android:minHeight="@dimen/notification_expanded_height"-->
<!--android:orientation="vertical">-->
<!--<TextView-->
<!--android:id="@+id/title"-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:paddingTop="@dimen/small_padding"-->
<!--android:layout_marginLeft="12dp"-->
<!--android:layout_marginStart="12dp"-->
<!--android:layout_marginRight="12dp"-->
<!--android:layout_marginEnd="12dp"-->
<!--android:textAppearance="@style/TextAppearance.Notification.Title"-->
<!--android:singleLine="true"-->
<!--android:ellipsize="marquee"-->
<!--android:fadingEdge="horizontal"/>-->
<!--<TextView-->
<!--android:id="@+id/text2"-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="0dp"-->
<!--android:layout_weight="1"-->
<!--android:layout_marginLeft="12dp"-->
<!--android:layout_marginStart="12dp"-->
<!--android:layout_marginRight="12dp"-->
<!--android:layout_marginEnd="12dp"-->
<!--android:textAppearance="@style/TextAppearance.Notification.Details"-->
<!--android:maxLines="2"-->
<!--android:fadingEdge="horizontal"-->
<!--android:ellipsize="marquee"/>-->
<!--<ImageView-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="1dp"-->
<!--android:id="@+id/action_divider"-->
<!--android:background="#29ffffff"/>-->
<!--<LinearLayout-->
<!--android:id="@+id/media_actions"-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="@dimen/default_icon_size"-->
<!--android:layout_marginStart="12dp"-->
<!--android:layout_marginEnd="12dp"-->
<!--android:orientation="horizontal"-->
<!--android:layoutDirection="ltr">-->
<!--<ImageButton-->
<!--android:id="@+id/rewind"-->
<!--style="@style/Widget.Button.Borderless"-->
<!--android:layout_width="@dimen/default_icon_size"-->
<!--android:layout_height="match_parent"-->
<!--android:layout_marginLeft="2dp"-->
<!--android:layout_marginRight="2dp"-->
<!--android:layout_weight="1"-->
<!--android:gravity="center"-->
<!--android:contentDescription="@string/rewind"/>-->
<!--<ImageButton-->
<!--android:id="@+id/play"-->
<!--style="@style/Widget.Button.Borderless"-->
<!--android:layout_width="@dimen/default_icon_size"-->
<!--android:layout_height="match_parent"-->
<!--android:layout_marginLeft="2dp"-->
<!--android:layout_marginRight="2dp"-->
<!--android:layout_weight="1"-->
<!--android:gravity="center"-->
<!--android:contentDescription="@string/play"/>-->
<!--<ImageButton-->
<!--android:id="@+id/fast_forward"-->
<!--style="@style/Widget.Button.Borderless"-->
<!--android:layout_width="@dimen/default_icon_size"-->
<!--android:layout_height="match_parent"-->
<!--android:layout_marginLeft="2dp"-->
<!--android:layout_marginRight="2dp"-->
<!--android:layout_weight="1"-->
<!--android:gravity="center"-->
<!--android:contentDescription="@string/fast_forward"/>-->
<!--</LinearLayout>-->
<!--</LinearLayout>-->
<!--</LinearLayout>-->
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/status_bar_latest_event_content"
android:layout_width="match_parent"
android:layout_height="128dp"
android:background="@color/notification_backgroup">
<FrameLayout
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView android:id="@+id/icon_square"
android:layout_width="@dimen/notification_expanded_art_default_height"
android:layout_height="@dimen/notification_expanded_art_default_height"
android:scaleType="centerCrop"
android:contentDescription="@string/poster"
android:visibility="gone"/>
<ImageView android:id="@+id/icon_slim"
android:layout_width="@dimen/notification_expanded_art_slim_width"
android:layout_height="@dimen/notification_expanded_art_default_height"
android:scaleType="centerCrop"
android:contentDescription="@string/poster"
android:visibility="gone"/>
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_marginStart="12dp"
android:layout_marginRight="12dp"
android:layout_marginEnd="12dp"
android:layout_toRightOf="@id/icon"
android:layout_toEndOf="@id/icon"
android:orientation="vertical">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="@dimen/small_padding"
android:textAppearance="@style/TextAppearance.Notification.Title"
android:singleLine="true"
android:ellipsize="marquee"
android:fadingEdge="horizontal"/>
<TextView
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="@style/TextAppearance.Notification.Details"
android:maxLines="2"
android:fadingEdge="horizontal"
android:ellipsize="marquee"/>
</LinearLayout>
<LinearLayout
android:id="@+id/media_actions"
android:layout_width="match_parent"
android:layout_height="@dimen/default_icon_size"
android:layout_toRightOf="@id/icon"
android:layout_toEndOf="@id/icon"
android:layout_alignParentBottom="true"
android:layout_marginStart="12dp"
android:layout_marginEnd="12dp"
android:orientation="horizontal"
android:layoutDirection="ltr">
<ImageButton
android:id="@+id/rewind"
style="@style/Widget.Button.Notification"
android:layout_width="@dimen/default_icon_size"
android:layout_height="match_parent"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_weight="1"
android:gravity="center"
android:contentDescription="@string/rewind"/>
<ImageButton
android:id="@+id/play"
style="@style/Widget.Button.Notification"
android:layout_width="@dimen/default_icon_size"
android:layout_height="match_parent"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_weight="1"
android:gravity="center"
android:contentDescription="@string/play"/>
<ImageButton
android:id="@+id/fast_forward"
style="@style/Widget.Button.Notification"
android:layout_width="@dimen/default_icon_size"
android:layout_height="match_parent"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_weight="1"
android:gravity="center"
android:contentDescription="@string/fast_forward"/>
</LinearLayout>
<ImageView
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_toRightOf="@id/icon"
android:layout_toEndOf="@id/icon"
android:layout_above="@id/media_actions"
android:id="@+id/action_divider"
android:background="#29ffffff"/>
</RelativeLayout>