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 May 20, 2024
1 parent f05b80e commit df1e960
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,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 @@ -1127,6 +1128,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,100 @@
package app.grapheneos.camera.ui.dialog

import android.app.Activity
import androidx.annotation.StringRes
import androidx.appcompat.view.ContextThemeWrapper
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.data.Wifi
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
import app.grapheneos.camera.qr.parser.parseWifi
import com.google.android.material.dialog.MaterialAlertDialogBuilder

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)
?: parseWifi(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
)

is Wifi -> DialogContent(
R.string.connect_to_wifi,
activity.getString(R.string.wifi_message, card.ssid),
R.string.connect
)
}

activity.runOnUiThread {
val dialogContext = ContextThemeWrapper(
activity,
com.google.android.material.R.style.Theme_MaterialComponents_DayNight
)
MaterialAlertDialogBuilder(dialogContext)
.setTitle(title)
.setMessage(message)
.setOnDismissListener { onDismiss() }
.setNegativeButton(R.string.cancel, null)
.setPositiveButton(action) { _, _ ->
card.startIntent(activity)
}.show()
}

return true
}
21 changes: 21 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,27 @@
<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="connect_to_wifi">Connect To Network (Wi-Fi)</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="connect">Connect</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="wifi_message">Do you want to connect to %s Wi-Fi network?</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>

<string name="wifi_dialog_title">Wifi Details</string>
<string name="wifi_dialog_message">You can connect to %s Wi-Fi network from settings.</string>
<string name="wifi_dialog_button_positive">Copy Password</string>
Expand Down

0 comments on commit df1e960

Please sign in to comment.