Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added menu button to show a notification of the current pass for quick access #248

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -137,7 +141,10 @@ open class PassViewActivityBase : PassAndroidActivity() {
Thread(UpdateAsync()).start()
true
}

R.id.menu_notification -> {
createNotification()
true
}
else -> super.onOptionsItemSelected(item)
}

Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions android/src/main/res/drawable/ic_announcement.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#FFFFFF"
android:viewportHeight="24.0" android:viewportWidth="24.0"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#FF000000" android:pathData="M20,2L4,2c-1.1,0 -1.99,0.9 -1.99,2L2,22l4,-4h14c1.1,0 2,-0.9 2,-2L22,4c0,-1.1 -0.9,-2 -2,-2zM13,11h-2L11,5h2v6zM13,15h-2v-2h2v2z"/>
</vector>
5 changes: 5 additions & 0 deletions android/src/main/res/drawable/ic_theaters_notification.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#FFFFFF"
android:viewportHeight="24.0" android:viewportWidth="24.0"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#FF000000" android:pathData="M18,3v2h-2L16,3L8,3v2L6,5L6,3L4,3v18h2v-2h2v2h8v-2h2v2h2L20,3h-2zM8,17L6,17v-2h2v2zM8,13L6,13v-2h2v2zM8,9L6,9L6,7h2v2zM18,17h-2v-2h2v2zM18,13h-2v-2h2v2zM18,9h-2L16,7h2v2z"/>
</vector>
12 changes: 12 additions & 0 deletions android/src/main/res/layout/notification_expanded.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="256dp">

<ImageView
android:id="@+id/image_view_expanded"
android:layout_width="match_parent"
android:layout_height="240dp"
android:scaleType="fitCenter"
android:src="@drawable/ic_launcher" />
</LinearLayout>
6 changes: 6 additions & 0 deletions android/src/main/res/menu/activity_pass_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,10 @@
android:title="@string/menu_delete"
android:icon="@drawable/ic_action_delete"
custom:showAsAction="ifRoom" />

<item
android:id="@+id/menu_notification"
android:title="@string/menu_notification"
android:icon="@drawable/ic_announcement"
custom:showAsAction="ifRoom" />
</menu>
2 changes: 2 additions & 0 deletions android/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,6 @@ WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWIS
<string name="open_file">Datei öffnen</string>
<string name="error_no_permission_msg">Berechtigung zum Importieren erforderlich</string>
<string name="error_no_permission_title">Berechtigungsfehler</string>
<string name="notification_text">Klicken um den Pass zu öffnen</string>
<string name="menu_notification">Benachrichtigung</string>
</resources>
2 changes: 2 additions & 0 deletions android/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<string name="menu_share">Share</string>
<string name="menu_print">Print</string>
<string name="menu_edit">Edit</string>
<string name="menu_notification">Notification</string>
<string name="barcode">"barcode"</string>
<string name="help_label">"Help"</string>
<string name="help_content"><![CDATA[
Expand Down Expand Up @@ -217,6 +218,7 @@ WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWIS
<string name="error_no_permission_msg">Need permission to import pass</string>
<string name="error_no_permission_title">Permission Error</string>
<string name="scan_pass_message">Scanning for passes. Please wait.</string>
<string name="notification_text">Click to open pass</string>


</resources>