Skip to content

Commit

Permalink
Improve interop communication on Android
Browse files Browse the repository at this point in the history
  • Loading branch information
mup committed Nov 29, 2024
1 parent db6f60b commit f5a0a8d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import android.Manifest
import android.content.ActivityNotFoundException
import android.content.ClipData
import android.content.Intent
import android.content.Intent.EXTRA_REFERRER
import android.net.Uri
import android.provider.Settings
import android.util.Base64
Expand Down Expand Up @@ -37,6 +36,8 @@ class AndroidMobileSystemFacade(
companion object {
private const val TAG = "SystemFacade"
const val APP_LOCK_METHOD = "AppLockMethod"
const val TUTA_INTENT_ACTION = "TUTA_INTEROP"
const val TUTA_INTENT_INTEROP_DATA = "TUTA_INTEROP_DATA"
}

override suspend fun openLink(uri: String): Boolean {
Expand Down Expand Up @@ -188,11 +189,13 @@ class AndroidMobileSystemFacade(

override suspend fun openCalendarApp(query: String) {
val decodedQuery = Base64.decode(query.toByteArray(), Base64.DEFAULT).toString(Charset.defaultCharset())
val targetPackageId = activity.getString(R.string.package_name).replace("calendar", "tutanota")

val intent = Intent()
intent.setPackage(targetPackageId)
intent.setAction(Intent.ACTION_EDIT)
intent.putExtra(EXTRA_REFERRER, BuildConfig.APPLICATION_ID)
intent.setData(Uri.parse("tutacalendar://interop?${decodedQuery}"))
intent.putExtra(TUTA_INTENT_ACTION, "interop")
intent.setData(Uri.parse("tutacalendar://interop?$decodedQuery"))

try {
startActivityForResult(activity, intent, 0, null)
Expand Down
16 changes: 10 additions & 6 deletions app-android/app/src/main/java/de/tutao/tutanota/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import android.content.ClipData
import android.content.ClipboardManager
import android.content.ComponentName
import android.content.Intent
import android.content.Intent.EXTRA_REFERRER
import android.content.Intent.ACTION_EDIT
import android.content.pm.PackageManager
import android.content.res.Configuration
import android.graphics.Color
Expand Down Expand Up @@ -454,12 +454,14 @@ class MainActivity : FragmentActivity() {
return@launch
}

if (data != null && data.toString().startsWith("tutamail://") && data.host == "interop") {
val caller = intent.getStringExtra(EXTRA_REFERRER)
val isInteropCall = intent.action == ACTION_EDIT && intent.getStringExtra(TUTA_INTENT_ACTION) == "interop"
val isTrustedCaller = callingPackage == BuildConfig.APPLICATION_ID.replace(
"tutanota",
"calendar"
)

if (caller?.startsWith("de.tutao") == true) {
openContactEditor(data)
}
if (data != null && isInteropCall && isTrustedCaller) {
openContactEditor(data)
}

if (intent.action != null && !intent.getBooleanExtra(ALREADY_HANDLED_INTENT, false)) {
Expand Down Expand Up @@ -809,6 +811,8 @@ class MainActivity : FragmentActivity() {
const val OPEN_CONTACT_EDITOR_CONTACT_ID = "contactId"
const val OPEN_USER_MAILBOX_MAILID_KEY = "mailId"
const val ALREADY_HANDLED_INTENT = "alreadyHandledIntent"
const val TUTA_INTENT_ACTION = "TUTA_INTEROP"

private const val TAG = "MainActivity"
private var requestId = 0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import android.Manifest
import android.content.ActivityNotFoundException
import android.content.ClipData
import android.content.Intent
import android.content.Intent.EXTRA_REFERRER
import android.net.Uri
import android.provider.Settings
import android.util.Base64
Expand Down Expand Up @@ -33,12 +32,13 @@ class AndroidMobileSystemFacade(
private val activity: MainActivity,
private val db: AppDatabase,
) : MobileSystemFacade {

private val authenticationPrompt = AuthenticationPrompt()

companion object {
private const val TAG = "SystemFacade"
const val APP_LOCK_METHOD = "AppLockMethod"
const val TUTA_INTENT_ACTION = "TUTA_INTEROP"
const val TUTA_INTENT_INTEROP_DATA = "TUTA_INTEROP_DATA"
}

override suspend fun openLink(uri: String): Boolean {
Expand Down Expand Up @@ -186,11 +186,13 @@ class AndroidMobileSystemFacade(

override suspend fun openMailApp(query: String) {
val decodedQuery = Base64.decode(query.toByteArray(), Base64.DEFAULT).toString(Charset.defaultCharset())
val targetPackageId = activity.getString(R.string.package_name).replace("calendar", "tutanota")

val intent = Intent()
intent.setPackage(targetPackageId)
intent.setAction(Intent.ACTION_EDIT)
intent.putExtra(EXTRA_REFERRER, BuildConfig.APPLICATION_ID)
intent.setData(Uri.parse("tutamail://interop?${decodedQuery}"))
intent.putExtra(TUTA_INTENT_ACTION, "interop")
intent.setData(Uri.parse("tutamail://interop?$decodedQuery"))

try {
startActivityForResult(activity, intent, 0, null)
Expand Down

0 comments on commit f5a0a8d

Please sign in to comment.