From e8d24c9867a5b3c34b61099faedbe168b893ec3f Mon Sep 17 00:00:00 2001 From: daTom24 Date: Tue, 3 Mar 2020 22:46:22 +0100 Subject: [PATCH] Added menu button to show a notification of the current pass for quick access --- .../passandroid/ui/PassViewActivityBase.kt | 52 +++++++++++++++++-- .../src/main/res/drawable/ic_announcement.xml | 5 ++ .../res/drawable/ic_theaters_notification.xml | 5 ++ .../main/res/layout/notification_expanded.xml | 12 +++++ .../src/main/res/menu/activity_pass_view.xml | 6 +++ android/src/main/res/values-de/strings.xml | 2 + android/src/main/res/values/strings.xml | 2 + 7 files changed, 81 insertions(+), 3 deletions(-) create mode 100644 android/src/main/res/drawable/ic_announcement.xml create mode 100644 android/src/main/res/drawable/ic_theaters_notification.xml create mode 100644 android/src/main/res/layout/notification_expanded.xml diff --git a/android/src/main/java/org/ligi/passandroid/ui/PassViewActivityBase.kt b/android/src/main/java/org/ligi/passandroid/ui/PassViewActivityBase.kt index 5b171d866..39976c29c 100644 --- a/android/src/main/java/org/ligi/passandroid/ui/PassViewActivityBase.kt +++ b/android/src/main/java/org/ligi/passandroid/ui/PassViewActivityBase.kt @@ -1,8 +1,8 @@ package org.ligi.passandroid.ui import android.annotation.SuppressLint -import android.app.Dialog -import android.app.ProgressDialog +import android.app.* +import android.content.Context import android.content.Intent import android.graphics.Bitmap import android.graphics.BitmapFactory @@ -12,7 +12,9 @@ import android.view.Menu import android.view.MenuItem import android.view.ViewConfiguration import android.view.WindowManager +import android.widget.RemoteViews import androidx.appcompat.app.AlertDialog +import androidx.core.app.NotificationCompat import androidx.core.content.pm.ShortcutInfoCompat import androidx.core.content.pm.ShortcutManagerCompat import androidx.core.graphics.drawable.IconCompat @@ -36,6 +38,8 @@ open class PassViewActivityBase : PassAndroidActivity() { lateinit var currentPass: Pass private var fullBrightnessSet = false + private val NOTIFICATION_CHANNEL_ID = "passnotifications" + val PASS_NOTIFICATION_ID = 5 override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -137,7 +141,10 @@ open class PassViewActivityBase : PassAndroidActivity() { Thread(UpdateAsync()).start() true } - + R.id.menu_notification -> { + createNotification() + true + } else -> super.onOptionsItemSelected(item) } @@ -168,6 +175,45 @@ open class PassViewActivityBase : PassAndroidActivity() { ShortcutManagerCompat.requestPinShortcut(this, shortcutInfo, null) } + private fun createNotification() { + val expandedView = RemoteViews(packageName, R.layout.notification_expanded) + val notifyManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager + if (Build.VERSION.SDK_INT > 25) { + val channel = NotificationChannel(NOTIFICATION_CHANNEL_ID, "PassAndroid Pass Notification", NotificationManager.IMPORTANCE_DEFAULT) + channel.description = "Notifications to quickly show and open PassAndroid passes" + notifyManager.createNotificationChannel(channel) + } + + val passNotificationBuilder = NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID) + passNotificationBuilder.setSmallIcon(R.drawable.ic_theaters_notification) + .setContentTitle(currentPass.description) + .setContentText(getString(R.string.notification_text)) + + val iconBitmap = currentPass.getBitmap(passStore, BITMAP_ICON) + if (iconBitmap != null) { + passNotificationBuilder.setLargeIcon(iconBitmap) + } + + val barCodeBitmap = currentPass.barCode?.getBitmap(this.resources)!!.bitmap + if (barCodeBitmap != null) { + val height = (240 * this.resources.displayMetrics.density).toInt() + val originalHeight = barCodeBitmap.height + val originalWidth = barCodeBitmap.width + val scale = height / originalHeight + val width = (originalWidth * scale).toInt() + val scaledBarCodeBitmap = Bitmap.createScaledBitmap(barCodeBitmap, height, width, false) + expandedView.setImageViewBitmap(R.id.image_view_expanded, scaledBarCodeBitmap) + passNotificationBuilder.setCustomBigContentView(expandedView) + .setStyle(NotificationCompat.DecoratedCustomViewStyle()) + } + + val intent = Intent(this, PassViewActivity::class.java) + val pendingIntent: PendingIntent = PendingIntent.getActivity(this, 0, intent, 0) + passNotificationBuilder.setContentIntent(pendingIntent) + passNotificationBuilder.setAutoCancel(true) + notifyManager.notify(PASS_NOTIFICATION_ID, passNotificationBuilder.build()) + } + inner class UpdateAsync : Runnable { private lateinit var dlg: ProgressDialog diff --git a/android/src/main/res/drawable/ic_announcement.xml b/android/src/main/res/drawable/ic_announcement.xml new file mode 100644 index 000000000..6dcb796c3 --- /dev/null +++ b/android/src/main/res/drawable/ic_announcement.xml @@ -0,0 +1,5 @@ + + + diff --git a/android/src/main/res/drawable/ic_theaters_notification.xml b/android/src/main/res/drawable/ic_theaters_notification.xml new file mode 100644 index 000000000..349fbf310 --- /dev/null +++ b/android/src/main/res/drawable/ic_theaters_notification.xml @@ -0,0 +1,5 @@ + + + diff --git a/android/src/main/res/layout/notification_expanded.xml b/android/src/main/res/layout/notification_expanded.xml new file mode 100644 index 000000000..bad69dbad --- /dev/null +++ b/android/src/main/res/layout/notification_expanded.xml @@ -0,0 +1,12 @@ + + + + + \ No newline at end of file diff --git a/android/src/main/res/menu/activity_pass_view.xml b/android/src/main/res/menu/activity_pass_view.xml index 46a78a7c8..54cd69ad2 100644 --- a/android/src/main/res/menu/activity_pass_view.xml +++ b/android/src/main/res/menu/activity_pass_view.xml @@ -30,4 +30,10 @@ android:title="@string/menu_delete" android:icon="@drawable/ic_action_delete" custom:showAsAction="ifRoom" /> + + diff --git a/android/src/main/res/values-de/strings.xml b/android/src/main/res/values-de/strings.xml index afc252359..ecc438d4f 100644 --- a/android/src/main/res/values-de/strings.xml +++ b/android/src/main/res/values-de/strings.xml @@ -188,4 +188,6 @@ WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWIS Datei öffnen Berechtigung zum Importieren erforderlich Berechtigungsfehler + Klicken um den Pass zu öffnen + Benachrichtigung diff --git a/android/src/main/res/values/strings.xml b/android/src/main/res/values/strings.xml index af6f5e539..296e5be36 100644 --- a/android/src/main/res/values/strings.xml +++ b/android/src/main/res/values/strings.xml @@ -7,6 +7,7 @@ Share Print Edit + Notification "barcode" "Help" Need permission to import pass Permission Error Scanning for passes. Please wait. + Click to open pass