|
| 1 | +/* |
| 2 | + * Nextcloud Android Common Library |
| 3 | + * |
| 4 | + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 5 | + * SPDX-License-Identifier: MIT |
| 6 | + */ |
| 7 | + |
| 8 | +package com.nextcloud.android.common.core.utils.ecosystem |
| 9 | + |
| 10 | +import android.app.Activity |
| 11 | +import android.content.ActivityNotFoundException |
| 12 | +import android.content.Context |
| 13 | +import android.content.Intent |
| 14 | +import android.content.pm.PackageManager |
| 15 | +import android.util.Log |
| 16 | +import android.view.View |
| 17 | +import androidx.core.net.toUri |
| 18 | +import com.google.android.material.snackbar.Snackbar |
| 19 | +import com.nextcloud.android.common.core.R |
| 20 | +import java.util.regex.Pattern |
| 21 | + |
| 22 | +/** |
| 23 | + * EcosystemManager: handles sending and receiving account info across apps |
| 24 | + * within the Nextcloud ecosystem (Notes, Files, Talk). |
| 25 | + * |
| 26 | + * Supports: |
| 27 | + * - Opening apps with account info |
| 28 | + * - Redirecting to Play Store if app not installed |
| 29 | + * - Receiving account info from intents with callback support |
| 30 | + */ |
| 31 | +class EcosystemManager( |
| 32 | + private val activity: Activity |
| 33 | +) { |
| 34 | + companion object { |
| 35 | + private const val TAG = "EcosystemManager" |
| 36 | + |
| 37 | + private const val ECOSYSTEM_INTENT_ACTION = "com.nextcloud.intent.OPEN_ECOSYSTEM_APP" |
| 38 | + private const val PLAY_STORE_LINK = "https://play.google.com/store/apps/details?id=" |
| 39 | + private const val PLAY_STORE_MARKET_LINK = "market://details?id=" |
| 40 | + private const val EXTRA_KEY_ACCOUNT = "KEY_ACCOUNT" |
| 41 | + |
| 42 | + const val ACCOUNT_NAME_PATTERN_REGEX = "[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-z]{2,}" |
| 43 | + } |
| 44 | + |
| 45 | + private val accountNamePattern = Pattern.compile(ACCOUNT_NAME_PATTERN_REGEX) |
| 46 | + |
| 47 | + /** |
| 48 | + * Opens an ecosystem app with the given account information. |
| 49 | + * |
| 50 | + * If the target app is installed, it will be launched using the |
| 51 | + * {@link #ECOSYSTEM_INTENT_ACTION} intent action and the account name |
| 52 | + * will be passed as an intent extra. |
| 53 | + * |
| 54 | + * If the app is not installed or cannot be launched, the user will be |
| 55 | + * redirected to the Google Play Store page for the app. |
| 56 | + * |
| 57 | + * @param app The ecosystem app to be opened (e.g. Notes, Files, Talk) |
| 58 | + * @param accountName The account name associated with the user, |
| 59 | + * e.g. "abc@example.cloud.com" |
| 60 | + */ |
| 61 | + @Suppress("TooGenericExceptionCaught", "ReturnCount") |
| 62 | + fun openApp( |
| 63 | + app: EcosystemApp, |
| 64 | + accountName: String? |
| 65 | + ) { |
| 66 | + Log.d(TAG, "open app, package name: ${app.packageNames}, account name: $accountName") |
| 67 | + |
| 68 | + // check account name emptiness |
| 69 | + if (accountName.isNullOrBlank()) { |
| 70 | + Log.w(TAG, "given account name is null") |
| 71 | + showSnackbar(R.string.ecosystem_null_account) |
| 72 | + openAppInStore(app) |
| 73 | + return |
| 74 | + } |
| 75 | + |
| 76 | + // validate account name |
| 77 | + if (!accountNamePattern.matcher(accountName).matches()) { |
| 78 | + showSnackbar(R.string.ecosystem_invalid_account) |
| 79 | + return |
| 80 | + } |
| 81 | + |
| 82 | + // validate package name |
| 83 | + val intent = activity.findLaunchIntentForInstalledPackage(app.packageNames) |
| 84 | + if (intent == null) { |
| 85 | + Log.w(TAG, "given package name cannot be found") |
| 86 | + showSnackbar(R.string.ecosystem_app_not_found) |
| 87 | + openAppInStore(app) |
| 88 | + return |
| 89 | + } |
| 90 | + |
| 91 | + try { |
| 92 | + Log.d(TAG, "launching app ${app.name} with account=$accountName") |
| 93 | + val launchIntent = |
| 94 | + Intent(ECOSYSTEM_INTENT_ACTION).apply { |
| 95 | + setPackage(intent.`package`) |
| 96 | + putExtra(EXTRA_KEY_ACCOUNT, accountName) |
| 97 | + addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_SINGLE_TOP) |
| 98 | + } |
| 99 | + activity.startActivity(launchIntent) |
| 100 | + } catch (e: Exception) { |
| 101 | + showSnackbar(R.string.ecosystem_store_open_failed) |
| 102 | + Log.e(TAG, "exception launching app ${app.packageNames}: $e") |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Finds the first launchable intent from a list of package names. |
| 108 | + * |
| 109 | + * @return launch Intent or null if none of the apps are installed |
| 110 | + */ |
| 111 | + private fun Context.findLaunchIntentForInstalledPackage(packageNames: List<String>): Intent? { |
| 112 | + val pm: PackageManager = packageManager |
| 113 | + return packageNames.firstNotNullOfOrNull { packageName -> |
| 114 | + pm.getLaunchIntentForPackage(packageName) |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + @Suppress("TooGenericExceptionCaught") |
| 119 | + private fun openAppInStore(app: EcosystemApp) { |
| 120 | + Log.d(TAG, "open app in store: $app") |
| 121 | + |
| 122 | + val firstPackageName = app.packageNames.firstOrNull() ?: return |
| 123 | + |
| 124 | + val marketUri = "$PLAY_STORE_MARKET_LINK$firstPackageName".toUri() |
| 125 | + val intent = Intent(Intent.ACTION_VIEW, marketUri) |
| 126 | + |
| 127 | + try { |
| 128 | + activity.startActivity(intent) |
| 129 | + } catch (_: ActivityNotFoundException) { |
| 130 | + val playStoreUri = "$PLAY_STORE_LINK$firstPackageName".toUri() |
| 131 | + val webIntent = Intent(Intent.ACTION_VIEW, playStoreUri) |
| 132 | + try { |
| 133 | + activity.startActivity(webIntent) |
| 134 | + } catch (e: Exception) { |
| 135 | + showSnackbar(R.string.ecosystem_store_open_failed) |
| 136 | + Log.e(TAG, "No browser available to open store for $firstPackageName, exception: ", e) |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Receives account from an intent and triggers the callback. |
| 143 | + * |
| 144 | + * This method should be called from your Activity's `onCreate()` and `onNewIntent()` |
| 145 | + * to handle incoming ecosystem intents from other ecosystem apps. |
| 146 | + * |
| 147 | + * Important: |
| 148 | + * 1. The sending app calls openApp. |
| 149 | + * 2. The receiving app must declare an intent-filter in its manifest to handle this action: |
| 150 | + * |
| 151 | + * <activity android:name=".ui.activity.MainActivity" |
| 152 | + * android:exported="true" |
| 153 | + * android:launchMode="singleTop"> |
| 154 | + * <intent-filter> |
| 155 | + * <action android:name="com.nextcloud.intent.OPEN_ECOSYSTEM_APP" /> |
| 156 | + * <category android:name="android.intent.category.DEFAULT" /> |
| 157 | + * </intent-filter> |
| 158 | + * </activity> |
| 159 | + * |
| 160 | + */ |
| 161 | + @Suppress("ReturnCount") |
| 162 | + fun receiveAccount( |
| 163 | + intent: Intent?, |
| 164 | + callback: AccountReceiverCallback |
| 165 | + ) { |
| 166 | + Log.d(TAG, "receive account started") |
| 167 | + |
| 168 | + if (intent == null) { |
| 169 | + Log.d(TAG, "received intent is null") |
| 170 | + val message = activity.getString(R.string.ecosystem_null_intent) |
| 171 | + callback.onAccountError(message) |
| 172 | + return |
| 173 | + } |
| 174 | + |
| 175 | + if (intent.action != ECOSYSTEM_INTENT_ACTION) { |
| 176 | + Log.d(TAG, "received intent action is not matching") |
| 177 | + val message = activity.getString(R.string.ecosystem_received_intent_action_not_matching) |
| 178 | + callback.onAccountError(message) |
| 179 | + return |
| 180 | + } |
| 181 | + |
| 182 | + val account = intent.getStringExtra(EXTRA_KEY_ACCOUNT) |
| 183 | + |
| 184 | + if (account.isNullOrBlank()) { |
| 185 | + val message = activity.getString(R.string.ecosystem_null_account) |
| 186 | + callback.onAccountError(message) |
| 187 | + return |
| 188 | + } |
| 189 | + |
| 190 | + if (!accountNamePattern.matcher(account).matches()) { |
| 191 | + val message = activity.getString(R.string.ecosystem_received_account_invalid) |
| 192 | + callback.onAccountError(message) |
| 193 | + return |
| 194 | + } |
| 195 | + |
| 196 | + Log.d(TAG, "Account received from intent: $account") |
| 197 | + callback.onAccountReceived(account) |
| 198 | + } |
| 199 | + |
| 200 | + private fun showSnackbar(messageRes: Int) { |
| 201 | + val rootContent = activity.findViewById<View>(android.R.id.content) |
| 202 | + Snackbar.make(rootContent, activity.getString(messageRes), Snackbar.LENGTH_LONG).show() |
| 203 | + } |
| 204 | +} |
0 commit comments