-
-
Notifications
You must be signed in to change notification settings - Fork 154
Expand file tree
/
Copy pathLinkHelper.kt
More file actions
48 lines (44 loc) · 1.51 KB
/
LinkHelper.kt
File metadata and controls
48 lines (44 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*
* Nextcloud Android Common Library
*
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: MIT
*/
package it.niedermann.owncloud.notes.util
import android.content.ActivityNotFoundException
import android.content.Context
import android.content.Intent
import androidx.core.net.toUri
/**
* Helper class for opening Nextcloud apps if present
* or falling back to opening the app store
* in case the app is not yet installed on the device.
*/
object LinkHelper {
private const val TAG = "LinkHelper"
/**
* Open app store page of specified app or search for specified string. Will attempt to open browser when no app
* store is available.
*
* @param string packageName or url-encoded search string
* @param search false -> show app corresponding to packageName; true -> open search for string
*/
fun openAppStore(
string: String,
search: Boolean = false,
context: Context,
) {
var suffix = (if (search) "search?q=" else "details?id=") + string
val intent = Intent(Intent.ACTION_VIEW, "market://$suffix".toUri())
try {
context.startActivity(intent)
} catch (_: ActivityNotFoundException) {
// all is lost: open google play store web page for app
if (!search) {
suffix = "apps/$suffix"
}
intent.setData("https://play.google.com/store/$suffix".toUri())
context.startActivity(intent)
}
}
}