0
0
mirror of https://github.com/markusfisch/BinaryEye.git synced 2024-09-20 12:02:17 +02:00

Add Mail, SMS and Telephone action

* Added `SimpleIntentIAction` to simplify code
* Change actions registry Array to Set
This commit is contained in:
molikuner 2019-07-05 15:21:37 +02:00 committed by Markus Fisch
parent d0f0bedae2
commit a3ad6aa4c5
15 changed files with 163 additions and 4 deletions

View File

@ -1,10 +1,13 @@
package de.markusfisch.android.binaryeye.actions
import de.markusfisch.android.binaryeye.actions.mail.MailAction
import de.markusfisch.android.binaryeye.actions.sms.SmsAction
import de.markusfisch.android.binaryeye.actions.tel.TelAction
import de.markusfisch.android.binaryeye.actions.wifi.WifiAction
object ActionRegistry {
val REGISTRY : Array<IAction> = arrayOf(
WifiAction
val REGISTRY: Set<IAction> = setOf(
WifiAction, SmsAction, TelAction, MailAction
)
fun getAction(data: ByteArray): IAction? = REGISTRY.find { it.canExecuteOn(data) }

View File

@ -1,6 +1,9 @@
package de.markusfisch.android.binaryeye.actions
import android.content.Context
import android.content.Intent
import android.widget.Toast
import de.markusfisch.android.binaryeye.R
interface IAction {
val iconResId: Int
@ -10,6 +13,22 @@ interface IAction {
fun execute(context: Context, data: ByteArray)
}
abstract class SimpleIntentIAction : IAction {
abstract val errorMsg: Int
final override fun execute(context: Context, data: ByteArray) {
val intent = executeForIntent(context, data)
?: return Toast.makeText(context, errorMsg, Toast.LENGTH_LONG).show()
if (intent.resolveActivity(context.packageManager) != null) {
context.startActivity(intent)
} else {
Toast.makeText(context, R.string.cannot_resolve_action, Toast.LENGTH_LONG).show()
}
}
abstract fun executeForIntent(context: Context, data: ByteArray): Intent?
}
fun IAction?.validateOrGetNew(data: ByteArray): IAction? {
return this?.takeIf { canExecuteOn(data) } ?: ActionRegistry.getAction(data)
}

View File

@ -0,0 +1,25 @@
package de.markusfisch.android.binaryeye.actions.mail
import android.content.Context
import android.content.Intent
import android.net.Uri
import de.markusfisch.android.binaryeye.R
import de.markusfisch.android.binaryeye.actions.SimpleIntentIAction
object MailAction : SimpleIntentIAction() {
private val mailRegex = """^mail(?:to)?:([\w.%+-]+@[A-Za-z\d.-]+\.[A-Za-z]{2,6}(?:[?&](?:subject|body)=.*?){0,2}?)$""".toRegex()
override val iconResId: Int = R.drawable.ic_action_mail
override val titleResId: Int = R.string.mail_send
override val errorMsg: Int = R.string.mail_error
override fun canExecuteOn(data: ByteArray): Boolean {
return String(data).matches(mailRegex)
}
override fun executeForIntent(context: Context, data: ByteArray): Intent? {
val mailWithMessage = mailRegex.matchEntire(String(data))?.groupValues?.get(1) ?: return null
return Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:$mailWithMessage"))
}
}

View File

@ -0,0 +1,28 @@
package de.markusfisch.android.binaryeye.actions.sms
import android.content.Context
import android.content.Intent
import android.net.Uri
import de.markusfisch.android.binaryeye.R
import de.markusfisch.android.binaryeye.actions.SimpleIntentIAction
object SmsAction : SimpleIntentIAction() {
private val smsRegex = """^sms(?:to)?:(\+?[0-9]+)(?::((?:.*|\v)+))?$""".toRegex()
override val iconResId: Int = R.drawable.ic_action_sms
override val titleResId: Int = R.string.sms_send
override val errorMsg: Int = R.string.sms_error
override fun canExecuteOn(data: ByteArray): Boolean {
return String(data).matches(smsRegex)
}
override fun executeForIntent(context: Context, data: ByteArray): Intent? {
val (number: String, message: String) = smsRegex.matchEntire(String(data))?.let {
it.groupValues[1] to it.groupValues[2]
} ?: return null
return Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:$number")).apply {
if (message.isNotEmpty()) putExtra("sms_body", message)
}
}
}

View File

@ -0,0 +1,24 @@
package de.markusfisch.android.binaryeye.actions.tel
import android.content.Context
import android.content.Intent
import android.net.Uri
import de.markusfisch.android.binaryeye.R
import de.markusfisch.android.binaryeye.actions.SimpleIntentIAction
object TelAction : SimpleIntentIAction() {
private val telRegex = """^tel:(\+?[0-9]+)$""".toRegex()
override val iconResId: Int = R.drawable.ic_action_tel
override val titleResId: Int = R.string.tel_dial
override val errorMsg: Int = R.string.tel_error
override fun canExecuteOn(data: ByteArray): Boolean {
return String(data).matches(telRegex)
}
override fun executeForIntent(context: Context, data: ByteArray): Intent {
return Intent(Intent.ACTION_DIAL, Uri.parse(String(data)))
}
}

View File

@ -0,0 +1,6 @@
<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,4L4,4c-1.1,0 -1.99,0.9 -1.99,2L2,18c0,1.1 0.9,2 2,2h16c1.1,0 2,-0.9 2,-2L22,6c0,-1.1 -0.9,-2 -2,-2zM20,18L4,18L4,8l8,5 8,-5v10zM12,11L4,6h16l-8,5z"/>
</vector>

View File

@ -0,0 +1,6 @@
<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,-2zM9,11L7,11L7,9h2v2zM13,11h-2L11,9h2v2zM17,11h-2L15,9h2v2z"/>
</vector>

View File

@ -0,0 +1,6 @@
<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="M6.62,10.79c1.44,2.83 3.76,5.14 6.59,6.59l2.2,-2.2c0.27,-0.27 0.67,-0.36 1.02,-0.24 1.12,0.37 2.33,0.57 3.57,0.57 0.55,0 1,0.45 1,1V20c0,0.55 -0.45,1 -1,1 -9.39,0 -17,-7.61 -17,-17 0,-0.55 0.45,-1 1,-1h3.5c0.55,0 1,0.45 1,1 0,1.25 0.2,2.45 0.57,3.57 0.11,0.35 0.03,0.74 -0.25,1.02l-2.2,2.2z"/>
</vector>

View File

@ -43,4 +43,10 @@
<string name="connect_to_wifi">Mit WiFi verbinden</string>
<string name="wifi_config_failed">Konnte WiFi nicht konfigurieren</string>
<string name="wifi_added">WiFi hinzugefügt</string>
<string name="sms_send">SMS senden</string>
<string name="sms_error">SMS konnte nicht gesendet werden</string>
<string name="tel_dial">Telefonnummer wählen</string>
<string name="tel_error">Konnte Telefonnummer nicht wählen</string>
<string name="mail_send">E-Mail senden</string>
<string name="mail_error">Konnte E-Mail nicht senden</string>
</resources>

View File

@ -43,4 +43,10 @@
<string name="connect_to_wifi">Connect to WiFi</string>
<string name="wifi_config_failed">Could not configure WiFi</string>
<string name="wifi_added">WiFi added</string>
<string name="sms_send">Send SMS</string>
<string name="sms_error">Could not send SMS</string>
<string name="tel_dial">Dial number</string>
<string name="tel_error">Could not dial number</string>
<string name="mail_send">Send mail</string>
<string name="mail_error">Could not send mail</string>
</resources>

View File

@ -43,4 +43,10 @@
<string name="connect_to_wifi">Connect to WiFi</string>
<string name="wifi_config_failed">Could not configure WiFi</string>
<string name="wifi_added">WiFi added</string>
<string name="sms_send">Send SMS</string>
<string name="sms_error">Could not send SMS</string>
<string name="tel_dial">Dial number</string>
<string name="tel_error">Could not dial number</string>
<string name="mail_send">Send mail</string>
<string name="mail_error">Could not send mail</string>
</resources>

View File

@ -1,4 +1,4 @@
<resources>
<resources xmlns:tools="http://schemas.android.com/tools">
<string name="no_camera_no_fun">Aplikasi ini tidak akan bisa bekerja tanpa akses kamera. Selamat tinggal.</string>
<string name="camera_error">Tidak bisa mengakses kamera, silakan coba lagi</string>
<string name="scan_code">Pindai kode</string>
@ -6,7 +6,7 @@
<string name="decode_barcode">Dekode barcode</string>
<string name="content">Konten</string>
<string name="binary_data">(data binari)</string>
<plurals name="barcode_info">
<plurals name="barcode_info" tools:ignore="UnusedQuantity">
<item quantity="one">%1$s\n%2$d karakter</item>
<item quantity="other">%1$s\n%2$d karakter</item>
</plurals>
@ -43,4 +43,10 @@
<string name="connect_to_wifi">Connect to WiFi</string>
<string name="wifi_config_failed">Could not configure WiFi</string>
<string name="wifi_added">WiFi added</string>
<string name="sms_send">Send SMS</string>
<string name="sms_error">Could not send SMS</string>
<string name="tel_dial">Dial number</string>
<string name="tel_error">Could not dial number</string>
<string name="mail_send">Send mail</string>
<string name="mail_error">Could not send mail</string>
</resources>

View File

@ -43,4 +43,10 @@
<string name="connect_to_wifi">Connect to WiFi</string>
<string name="wifi_config_failed">Could not configure WiFi</string>
<string name="wifi_added">WiFi added</string>
<string name="sms_send">Send SMS</string>
<string name="sms_error">Could not send SMS</string>
<string name="tel_dial">Dial number</string>
<string name="tel_error">Could not dial number</string>
<string name="mail_send">Send mail</string>
<string name="mail_error">Could not send mail</string>
</resources>

View File

@ -43,4 +43,10 @@
<string name="connect_to_wifi">Connect to WiFi</string>
<string name="wifi_config_failed">Could not configure WiFi</string>
<string name="wifi_added">WiFi added</string>
<string name="sms_send">Send SMS</string>
<string name="sms_error">Could not send SMS</string>
<string name="tel_dial">Dial number</string>
<string name="tel_error">Could not dial number</string>
<string name="mail_send">Send mail</string>
<string name="mail_error">Could not send mail</string>
</resources>

View File

@ -43,4 +43,10 @@
<string name="connect_to_wifi">Connect to WiFi</string>
<string name="wifi_config_failed">Could not configure WiFi</string>
<string name="wifi_added">WiFi added</string>
<string name="sms_send">Send SMS</string>
<string name="sms_error">Could not send SMS</string>
<string name="tel_dial">Dial number</string>
<string name="tel_error">Could not dial number</string>
<string name="mail_send">Send mail</string>
<string name="mail_error">Could not send mail</string>
</resources>