From db3490f61a6daf0c2f661a69115183fd5e71026f Mon Sep 17 00:00:00 2001 From: David Perez Date: Tue, 5 Nov 2024 11:36:10 -0600 Subject: [PATCH] PM-14480: Update IntentManager to be able to launch apps (#4233) --- .../manager/intent/IntentManagerImpl.kt | 39 ++++++++++++++++--- 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/com/x8bit/bitwarden/ui/platform/manager/intent/IntentManagerImpl.kt b/app/src/main/java/com/x8bit/bitwarden/ui/platform/manager/intent/IntentManagerImpl.kt index e53088032..3902676e6 100644 --- a/app/src/main/java/com/x8bit/bitwarden/ui/platform/manager/intent/IntentManagerImpl.kt +++ b/app/src/main/java/com/x8bit/bitwarden/ui/platform/manager/intent/IntentManagerImpl.kt @@ -6,6 +6,7 @@ import android.content.ActivityNotFoundException import android.content.ComponentName import android.content.Context import android.content.Intent +import android.content.IntentSender import android.content.pm.PackageManager import android.net.Uri import android.os.Build @@ -27,6 +28,7 @@ import com.x8bit.bitwarden.MainActivity import com.x8bit.bitwarden.R import com.x8bit.bitwarden.data.autofill.util.toPendingIntentMutabilityFlag import com.x8bit.bitwarden.data.platform.annotation.OmitFromCoverage +import com.x8bit.bitwarden.data.platform.util.isBuildVersionBelow import com.x8bit.bitwarden.ui.platform.util.toFormattedPattern import java.io.File import java.time.Clock @@ -82,7 +84,7 @@ class IntentManagerImpl( override fun startActivity(intent: Intent) { try { context.startActivity(intent) - } catch (e: ActivityNotFoundException) { + } catch (_: ActivityNotFoundException) { // no-op } } @@ -115,7 +117,7 @@ class IntentManagerImpl( } context.startActivity(intent) true - } catch (e: ActivityNotFoundException) { + } catch (_: ActivityNotFoundException) { false } @@ -132,12 +134,28 @@ class IntentManagerImpl( } override fun launchUri(uri: Uri) { - val newUri = if (uri.scheme == null) { - uri.buildUpon().scheme("https").build() + if (uri.scheme.equals(other = "androidapp", ignoreCase = true)) { + val packageName = uri.toString().removePrefix(prefix = "androidapp://") + if (isBuildVersionBelow(Build.VERSION_CODES.TIRAMISU)) { + startActivity(createPlayStoreIntent(packageName)) + } else { + try { + context + .packageManager + .getLaunchIntentSenderForPackage(packageName) + .sendIntent(context, Activity.RESULT_OK, null, null, null) + } catch (_: IntentSender.SendIntentException) { + startActivity(createPlayStoreIntent(packageName)) + } + } } else { - uri.normalizeScheme() + val newUri = if (uri.scheme == null) { + uri.buildUpon().scheme("https").build() + } else { + uri.normalizeScheme() + } + startActivity(Intent(Intent.ACTION_VIEW, newUri)) } - startActivity(Intent(Intent.ACTION_VIEW, newUri)) } override fun shareText(text: String) { @@ -301,6 +319,15 @@ class IntentManagerImpl( startActivity(intent) } + private fun createPlayStoreIntent(packageName: String): Intent { + val playStoreUri = "https://play.google.com/store/apps/details" + .toUri() + .buildUpon() + .appendQueryParameter("id", packageName) + .build() + return Intent(Intent.ACTION_VIEW, playStoreUri) + } + private fun getCameraFileData(): IntentManager.FileData { val tmpDir = File(context.filesDir, TEMP_CAMERA_IMAGE_DIR) val file = File(tmpDir, TEMP_CAMERA_IMAGE_NAME)