Skip to content

Commit

Permalink
show action dialog for supported actionable qr codes
Browse files Browse the repository at this point in the history
  • Loading branch information
empratyush committed Feb 20, 2024
1 parent 663f549 commit 0c9e9a1
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ import app.grapheneos.camera.ui.CustomGrid
import app.grapheneos.camera.ui.QROverlay
import app.grapheneos.camera.ui.QRToggle
import app.grapheneos.camera.ui.SettingsDialog
import app.grapheneos.camera.ui.dialog.showActionableDialog
import app.grapheneos.camera.ui.seekbar.ExposureBar
import app.grapheneos.camera.ui.seekbar.ZoomBar
import app.grapheneos.camera.util.CameraControl
Expand Down Expand Up @@ -1126,6 +1127,10 @@ open class MainActivity : AppCompatActivity(),

isQRDialogShowing = true

if (showActionableDialog(this, rawText) { isQRDialogShowing = false }) {
return
}

val hString = bytesToHex(
rawText.toByteArray(StandardCharsets.UTF_8)
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package app.grapheneos.camera.ui.dialog

import android.app.Activity
import androidx.annotation.StringRes
import androidx.appcompat.app.AlertDialog
import app.grapheneos.camera.R
import app.grapheneos.camera.qr.data.GEO
import app.grapheneos.camera.qr.data.Mail
import app.grapheneos.camera.qr.data.MeCard
import app.grapheneos.camera.qr.data.Phone
import app.grapheneos.camera.qr.data.SMS
import app.grapheneos.camera.qr.data.VCard
import app.grapheneos.camera.qr.parser.parseGeo
import app.grapheneos.camera.qr.parser.parseMail
import app.grapheneos.camera.qr.parser.parseMeCard
import app.grapheneos.camera.qr.parser.parsePhoneOrFacetime
import app.grapheneos.camera.qr.parser.parseSMS
import app.grapheneos.camera.qr.parser.parseVCard

private data class DialogContent(
@StringRes val title: Int,
val message: String,
@StringRes val action: Int
)

fun showActionableDialog(activity: Activity, rawContent: String, onDismiss: () -> Unit): Boolean {

val card = parsePhoneOrFacetime(rawContent)
?: parseSMS(rawContent)
?: parseGeo(rawContent)
?: parseMeCard(rawContent)
?: parseMail(rawContent)
?: parseVCard(rawContent)
?: return false

val (title, message, action) = when (card) {
is GEO -> DialogContent(
R.string.address,
activity.getString(R.string.address_message, card.long, card.lat),
R.string.open_in_maps
)

is MeCard -> DialogContent(
R.string.contact_card_me_card,
activity.getString(R.string.mecard_message),
R.string.add_to_contacts
)

is Phone -> DialogContent(
R.string.phone,
activity.getString(R.string.call_message, "${card.number}"),
R.string.call
)

is SMS -> DialogContent(
R.string.message,
activity.getString(R.string.sms_message, card.number),
R.string.message
)

is Mail -> DialogContent(
R.string.mail,
activity.getString(R.string.mail_message, card.mailTo.to),
R.string.mail,
)

is VCard -> DialogContent(
R.string.contact_card_vcard,
activity.getString(R.string.vcard_message),
R.string.add_to_contacts
)
}

activity.runOnUiThread {
AlertDialog.Builder(activity)
.setTitle(title)
.setMessage(message)
.setOnDismissListener { onDismiss() }
.setNegativeButton(android.R.string.cancel, null)
.setPositiveButton(action) { _, _ ->
activity.startActivity(card.toActionIntent(activity))
}.show()
}

return true
}
19 changes: 19 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,23 @@

<string name="zsl_setting_title">Use ZSL in Latency mode</string>
<string name="zsl_setting_desc">Uses Zero Shutter Lag (ZSL) in Latency mode for faster capture. Certain devices may have a buggy implementation for this.</string>

<string name="address">Address</string>
<string name="contact_card_me_card">Contact Card (MeCard)</string>
<string name="contact_card_vcard">Contact Card (vCard)</string>
<string name="phone">Phone</string>
<string name="message">Message</string>
<string name="mail">Email</string>

<string name="open_in_maps">Navigate</string>
<string name="call">Call</string>
<string name="add_to_contacts">Add to contacts</string>

<string name="address_message">Do you want to navigate to longitude %s and latitude %s?</string>
<string name="mecard_message">Do you want to save this contact info?</string>
<string name="vcard_message">Do you want to import this contact info?</string>
<string name="call_message">Do you want to call %s?</string>
<string name="sms_message">Do you want to chat with %s?</string>
<string name="mail_message">Do you want to send email to %s?</string>

</resources>

0 comments on commit 0c9e9a1

Please sign in to comment.