|
| 1 | +package app.grapheneos.camera.ui.dialog |
| 2 | + |
| 3 | +import android.app.Activity |
| 4 | +import androidx.annotation.StringRes |
| 5 | +import androidx.appcompat.app.AlertDialog |
| 6 | +import app.grapheneos.camera.R |
| 7 | +import app.grapheneos.camera.qr.data.GEO |
| 8 | +import app.grapheneos.camera.qr.data.Mail |
| 9 | +import app.grapheneos.camera.qr.data.MeCard |
| 10 | +import app.grapheneos.camera.qr.data.Phone |
| 11 | +import app.grapheneos.camera.qr.data.SMS |
| 12 | +import app.grapheneos.camera.qr.data.VCard |
| 13 | +import app.grapheneos.camera.qr.parser.parseGeo |
| 14 | +import app.grapheneos.camera.qr.parser.parseMail |
| 15 | +import app.grapheneos.camera.qr.parser.parseMeCard |
| 16 | +import app.grapheneos.camera.qr.parser.parsePhoneOrFacetime |
| 17 | +import app.grapheneos.camera.qr.parser.parseSMS |
| 18 | +import app.grapheneos.camera.qr.parser.parseVCard |
| 19 | + |
| 20 | +private data class DialogContent( |
| 21 | + @StringRes val title: Int, |
| 22 | + val message: String, |
| 23 | + @StringRes val action: Int |
| 24 | +) |
| 25 | + |
| 26 | +fun showActionableDialog(activity: Activity, rawContent: String, onDismiss: () -> Unit): Boolean { |
| 27 | + |
| 28 | + val card = parsePhoneOrFacetime(rawContent) |
| 29 | + ?: parseSMS(rawContent) |
| 30 | + ?: parseGeo(rawContent) |
| 31 | + ?: parseMeCard(rawContent) |
| 32 | + ?: parseMail(rawContent) |
| 33 | + ?: parseVCard(rawContent) |
| 34 | + ?: return false |
| 35 | + |
| 36 | + val (title, message, action) = when (card) { |
| 37 | + is GEO -> DialogContent( |
| 38 | + R.string.address, |
| 39 | + activity.getString(R.string.address_message, card.long, card.lat), |
| 40 | + R.string.open_in_maps |
| 41 | + ) |
| 42 | + |
| 43 | + is MeCard -> DialogContent( |
| 44 | + R.string.contact_card_me_card, |
| 45 | + activity.getString(R.string.mecard_message), |
| 46 | + R.string.add_to_contacts |
| 47 | + ) |
| 48 | + |
| 49 | + is Phone -> DialogContent( |
| 50 | + R.string.phone, |
| 51 | + activity.getString(R.string.call_message, "${card.number}"), |
| 52 | + R.string.call |
| 53 | + ) |
| 54 | + |
| 55 | + is SMS -> DialogContent( |
| 56 | + R.string.message, |
| 57 | + activity.getString(R.string.sms_message, card.number), |
| 58 | + R.string.message |
| 59 | + ) |
| 60 | + |
| 61 | + is Mail -> DialogContent( |
| 62 | + R.string.mail, |
| 63 | + activity.getString(R.string.mail_message, card.mailTo.to), |
| 64 | + R.string.mail, |
| 65 | + ) |
| 66 | + |
| 67 | + is VCard -> DialogContent( |
| 68 | + R.string.contact_card_vcard, |
| 69 | + activity.getString(R.string.vcard_message), |
| 70 | + R.string.add_to_contacts |
| 71 | + ) |
| 72 | + } |
| 73 | + |
| 74 | + activity.runOnUiThread { |
| 75 | + AlertDialog.Builder(activity) |
| 76 | + .setTitle(title) |
| 77 | + .setMessage(message) |
| 78 | + .setOnDismissListener { onDismiss() } |
| 79 | + .setNegativeButton(android.R.string.cancel, null) |
| 80 | + .setPositiveButton(action) { _, _ -> |
| 81 | + activity.startActivity(card.toActionIntent(activity)) |
| 82 | + }.show() |
| 83 | + } |
| 84 | + |
| 85 | + return true |
| 86 | +} |
0 commit comments