Skip to content

Commit 8ff4ec6

Browse files
Merge pull request #859 from nextcloud/feat/ecosystem-manager
feat: ecosystem manager
2 parents 0d7d3f4 + 30a17d4 commit 8ff4ec6

7 files changed

Lines changed: 370 additions & 0 deletions

File tree

‎core/build.gradle‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ plugins {
1212
}
1313

1414
android {
15+
namespace 'com.nextcloud.android.common.core'
1516
defaultConfig {
1617
minSdk = 21
1718
compileSdk = 36
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
/**
11+
* Callback interface for receiving account information from another Nextcloud app.
12+
*
13+
*/
14+
interface AccountReceiverCallback {
15+
fun onAccountReceived(accountName: String)
16+
17+
fun onAccountError(reason: String)
18+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
/**
11+
* Represents Nextcloud ecosystem apps that can communicate with each other.
12+
*
13+
* Each enum value corresponds to a specific app in the Nextcloud ecosystem,
14+
* and holds a list of package names for that app. Multiple package names
15+
* allow compatibility with different flavours (Play Store, F-Droid, QA, beta/dev versions, etc.).
16+
*
17+
*/
18+
enum class EcosystemApp(
19+
val packageNames: List<String>
20+
) {
21+
FILES(
22+
listOf(
23+
"com.nextcloud.client", // generic, gplay, huawei
24+
"com.nextcloud.android.beta", // versionDev
25+
"com.nextcloud.android.qa" // qa
26+
)
27+
),
28+
NOTES(
29+
listOf(
30+
"it.niedermann.owncloud.notes", // play, fdroid
31+
"it.niedermann.owncloud.notes.dev", // dev
32+
"it.niedermann.owncloud.notes.qa" // qa
33+
)
34+
),
35+
TALK(
36+
listOf(
37+
"com.nextcloud.talk2", // generic, gplay
38+
"com.nextcloud.talk2.qa" // qa
39+
)
40+
)
41+
}
Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
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+
}

‎core/src/main/res/values/strings.xml‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,13 @@
1515
<item quantity="one">%dh</item>
1616
<item quantity="other">%dh</item>
1717
</plurals>
18+
19+
20+
<string name="ecosystem_null_account">Account is missing. Cannot open the app.</string>
21+
<string name="ecosystem_null_intent">Received intent is null.</string>
22+
<string name="ecosystem_received_intent_action_not_matching">Received intent is not matching.</string>
23+
<string name="ecosystem_received_account_invalid">Received account is invalid.</string>
24+
<string name="ecosystem_invalid_account">Invalid account format. Must be an email.</string>
25+
<string name="ecosystem_app_not_found">App not installed. Redirecting to store…</string>
26+
<string name="ecosystem_store_open_failed">Cannot open app store. Please install manually.</string>
1827
</resources>
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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
9+
10+
import com.nextcloud.android.common.core.utils.ecosystem.EcosystemManager
11+
import org.junit.Assert.assertFalse
12+
import org.junit.Assert.assertTrue
13+
import org.junit.Test
14+
import java.util.regex.Pattern
15+
16+
/**
17+
* Unit tests for validating account name format using the same
18+
* regular expression as {@link EcosystemManager}.
19+
*
20+
* These tests verify that:
21+
* - Valid account names (e.g. "abc@example.cloud.com") are accepted
22+
* - Invalid or malformed account names are rejected
23+
* - Edge cases such as empty or blank strings do not match
24+
*
25+
* The account name is expected to follow an email-like format and is
26+
* used when passing account information between ecosystem apps.
27+
*/
28+
class AccountNamePatternTest {
29+
private val pattern = Pattern.compile(EcosystemManager.ACCOUNT_NAME_PATTERN_REGEX)
30+
31+
@Test
32+
fun `valid account names should match`() {
33+
assertTrue(pattern.matcher("abc@example.cloud.com").matches())
34+
assertTrue(pattern.matcher("user.name+test@sub.domain.org").matches())
35+
assertTrue(pattern.matcher("user_123@test.co").matches())
36+
}
37+
38+
@Test
39+
fun `invalid account names should not match`() {
40+
assertFalse(pattern.matcher("abc").matches())
41+
assertFalse(pattern.matcher("abc@").matches())
42+
assertFalse(pattern.matcher("abc@example").matches())
43+
assertFalse(pattern.matcher("abc@example.").matches())
44+
assertFalse(pattern.matcher("abc@.com").matches())
45+
assertFalse(pattern.matcher("@example.com").matches())
46+
assertFalse(pattern.matcher("abc@example.c").matches())
47+
assertFalse(pattern.matcher("abc example@test.com").matches())
48+
}
49+
50+
@Test
51+
fun `empty or blank account names should not match`() {
52+
assertFalse(pattern.matcher("").matches())
53+
assertFalse(pattern.matcher(" ").matches())
54+
}
55+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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
9+
10+
import com.nextcloud.android.common.core.utils.ecosystem.EcosystemApp
11+
import org.junit.Assert.assertEquals
12+
import org.junit.Test
13+
14+
/**
15+
* Unit tests for {@link EcosystemApp}.
16+
*
17+
* These tests ensure that the order of package names is preserved and that
18+
* the first entry for each ecosystem app always represents the production
19+
* package name.
20+
*
21+
* The order is important because the first package name is used when
22+
* redirecting users to the Play Store or resolving fallback behavior.
23+
*/
24+
class EcosystemAppTest {
25+
@Test
26+
fun `first package name must always be the production package`() {
27+
assertEquals(
28+
"com.nextcloud.client",
29+
EcosystemApp.FILES.packageNames.first()
30+
)
31+
32+
assertEquals(
33+
"it.niedermann.owncloud.notes",
34+
EcosystemApp.NOTES.packageNames.first()
35+
)
36+
37+
assertEquals(
38+
"com.nextcloud.talk2",
39+
EcosystemApp.TALK.packageNames.first()
40+
)
41+
}
42+
}

0 commit comments

Comments
 (0)