1
0
mirror of https://github.com/danog/termux-api.git synced 2024-12-02 09:17:50 +01:00

NotificationAPI - migrate to NotificationCompat

This commit is contained in:
Ben Moran 2020-01-09 14:28:19 +00:00 committed by Alessandro Caputo
parent dfbfb178ca
commit 47dc9d94a2
2 changed files with 27 additions and 18 deletions

View File

@ -40,6 +40,7 @@ android {
dependencies { dependencies {
implementation 'com.google.android.material:material:1.0.0' implementation 'com.google.android.material:material:1.0.0'
implementation 'androidx.biometric:biometric:1.0.0-alpha04' implementation 'androidx.biometric:biometric:1.0.0-alpha04'
implementation "androidx.media:media:1.1.0"
} }
task versionName { task versionName {

View File

@ -13,6 +13,9 @@ import android.os.Build;
import android.provider.Settings; import android.provider.Settings;
import android.text.TextUtils; import android.text.TextUtils;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import com.termux.api.util.ResultReturner; import com.termux.api.util.ResultReturner;
import com.termux.api.util.TermuxApiLogger; import com.termux.api.util.TermuxApiLogger;
@ -81,13 +84,12 @@ public class NotificationAPI {
String actionExtra = intent.getStringExtra("action"); String actionExtra = intent.getStringExtra("action");
String id = intent.getStringExtra("id"); final String notificationId = getNotificationId(intent);
if (id == null) id = UUID.randomUUID().toString();
final String notificationId = id;
String groupKey = intent.getStringExtra("group"); String groupKey = intent.getStringExtra("group");
final Notification.Builder notification = new Notification.Builder(context); final NotificationCompat.Builder notification = new NotificationCompat.Builder(context,
CHANNEL_ID);
notification.setSmallIcon(R.drawable.ic_event_note_black_24dp); notification.setSmallIcon(R.drawable.ic_event_note_black_24dp);
notification.setColor(0xFF000000); notification.setColor(0xFF000000);
notification.setContentTitle(title); notification.setContentTitle(title);
@ -99,19 +101,19 @@ public class NotificationAPI {
String ImagePath = intent.getStringExtra("image-path"); String ImagePath = intent.getStringExtra("image-path");
if(ImagePath != null){ if (ImagePath != null) {
File imgFile = new File(ImagePath); File imgFile = new File(ImagePath);
if(imgFile.exists()) { if (imgFile.exists()) {
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath()); Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
notification.setLargeIcon(myBitmap) notification.setLargeIcon(myBitmap)
.setStyle(new Notification.BigPictureStyle() .setStyle(new NotificationCompat.BigPictureStyle()
.bigPicture(myBitmap)); .bigPicture(myBitmap));
} }
} }
String styleType = intent.getStringExtra("type"); String styleType = intent.getStringExtra("type");
if(Objects.equals(styleType, "media")) { if (Objects.equals(styleType, "media")) {
String mediaPrevious = intent.getStringExtra("media-previous"); String mediaPrevious = intent.getStringExtra("media-previous");
String mediaPause = intent.getStringExtra("media-pause"); String mediaPause = intent.getStringExtra("media-pause");
String mediaPlay = intent.getStringExtra("media-play"); String mediaPlay = intent.getStringExtra("media-play");
@ -125,13 +127,13 @@ public class NotificationAPI {
PendingIntent playIntent = createAction(context, mediaPlay); PendingIntent playIntent = createAction(context, mediaPlay);
PendingIntent nextIntent = createAction(context, mediaNext); PendingIntent nextIntent = createAction(context, mediaNext);
notification.addAction(new Notification.Action(android.R.drawable.ic_media_previous, "previous", previousIntent)); notification.addAction(new NotificationCompat.Action(android.R.drawable.ic_media_previous, "previous", previousIntent));
notification.addAction(new Notification.Action(android.R.drawable.ic_media_pause, "pause", pauseIntent)); notification.addAction(new NotificationCompat.Action(android.R.drawable.ic_media_pause, "pause", pauseIntent));
notification.addAction(new Notification.Action(android.R.drawable.ic_media_play, "play", playIntent)); notification.addAction(new NotificationCompat.Action(android.R.drawable.ic_media_play, "play", playIntent));
notification.addAction(new Notification.Action(android.R.drawable.ic_media_next, "next", nextIntent)); notification.addAction(new NotificationCompat.Action(android.R.drawable.ic_media_next, "next", nextIntent));
notification.setStyle(new Notification.MediaStyle() notification.setStyle(new androidx.media.app.NotificationCompat.MediaStyle()
.setShowActionsInCompactView(0, 1, 3)); .setShowActionsInCompactView(0, 1, 3));
} }
} }
@ -167,7 +169,7 @@ public class NotificationAPI {
String buttonAction = intent.getStringExtra("button_action_" + button); String buttonAction = intent.getStringExtra("button_action_" + button);
if (buttonText != null && buttonAction != null) { if (buttonText != null && buttonAction != null) {
PendingIntent pi = createAction(context, buttonAction); PendingIntent pi = createAction(context, buttonAction);
notification.addAction(new Notification.Action(android.R.drawable.ic_input_add, buttonText, pi)); notification.addAction(new NotificationCompat.Action(android.R.drawable.ic_input_add, buttonText, pi));
} }
} }
@ -184,7 +186,7 @@ public class NotificationAPI {
if (!TextUtils.isEmpty(inputString)) { if (!TextUtils.isEmpty(inputString)) {
if (inputString.contains("\n")) { if (inputString.contains("\n")) {
Notification.BigTextStyle style = new Notification.BigTextStyle(); NotificationCompat.BigTextStyle style = new NotificationCompat.BigTextStyle();
style.bigText(inputString); style.bigText(inputString);
notification.setStyle(style); notification.setStyle(style);
} else { } else {
@ -204,6 +206,12 @@ public class NotificationAPI {
}); });
} }
private static String getNotificationId(Intent intent) {
String id = intent.getStringExtra("id");
if (id == null) id = UUID.randomUUID().toString();
return id;
}
static void onReceiveRemoveNotification(TermuxApiReceiver apiReceiver, final Context context, final Intent intent) { static void onReceiveRemoveNotification(TermuxApiReceiver apiReceiver, final Context context, final Intent intent) {
ResultReturner.noteDone(apiReceiver, intent); ResultReturner.noteDone(apiReceiver, intent);
String notificationId = intent.getStringExtra("id"); String notificationId = intent.getStringExtra("id");