Skip to content

Commit 97b3d44

Browse files
committed
ecosystem link handle
Signed-off-by: alperozturk96 <alper_ozturk@proton.me>
1 parent 61ce6df commit 97b3d44

2 files changed

Lines changed: 30 additions & 87 deletions

File tree

app/src/main/java/it/niedermann/owncloud/notes/main/MainActivity.java

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@
6767
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
6868
import com.google.android.material.floatingactionbutton.FloatingActionButton;
6969
import com.google.android.material.snackbar.Snackbar;
70+
import com.nextcloud.android.common.core.utils.ecosystem.AccountReceiverCallback;
71+
import com.nextcloud.android.common.core.utils.ecosystem.EcosystemApp;
72+
import com.nextcloud.android.common.core.utils.ecosystem.EcosystemManager;
7073
import com.nextcloud.android.common.ui.theme.utils.ColorRole;
7174
import com.nextcloud.android.sso.AccountImporter;
7275
import com.nextcloud.android.sso.exceptions.AccountImportCancelledException;
@@ -77,10 +80,13 @@
7780
import com.nextcloud.android.sso.exceptions.UnknownErrorException;
7881
import com.nextcloud.android.sso.helper.SingleAccountHelper;
7982

83+
import org.jetbrains.annotations.NotNull;
84+
8085
import java.net.HttpURLConnection;
8186
import java.util.Arrays;
8287
import java.util.LinkedList;
8388
import java.util.List;
89+
import java.util.Objects;
8490
import java.util.concurrent.ExecutorService;
8591
import java.util.concurrent.Executors;
8692
import java.util.stream.Collectors;
@@ -133,6 +139,8 @@ public class MainActivity extends LockedActivity implements NoteClickListener, A
133139

134140
protected MainViewModel mainViewModel;
135141

142+
private EcosystemManager ecosystemManager;
143+
136144
private boolean gridView = true;
137145

138146
public static final String ADAPTER_KEY_RECENT = "recent";
@@ -173,6 +181,8 @@ protected void onCreate(Bundle savedInstanceState) {
173181

174182
setContentView(binding.getRoot());
175183

184+
ecosystemManager = new EcosystemManager(this);
185+
handleEcosystemIntent(getIntent());
176186
this.coordinatorLayout = binding.activityNotesListView.activityNotesListView;
177187
this.swipeRefreshLayout = binding.activityNotesListView.swiperefreshlayout;
178188
this.fabCreate = binding.activityNotesListView.fabCreate;
@@ -379,9 +389,11 @@ private void setupDrawerAppMenu() {
379389
}
380390

381391
private void setupDrawerAppMenuListener() {
392+
final var accountName = Objects.requireNonNull(mainViewModel.getCurrentAccount().getValue()).getAccountName();
393+
382394
// Add listeners to the ecosystem items to launch the app or app-store
383-
binding.drawerEcosystemFiles.setOnClickListener(v -> LinkHelper.INSTANCE.openAppOrStore(LinkHelper.APP_NEXTCLOUD_FILES, mainViewModel.getCurrentAccount().getValue().getAccountName(), this));
384-
binding.drawerEcosystemTalk.setOnClickListener(v -> LinkHelper.INSTANCE.openAppOrStore(LinkHelper.APP_NEXTCLOUD_TALK, mainViewModel.getCurrentAccount().getValue().getAccountName(), this));
395+
binding.drawerEcosystemFiles.setOnClickListener(v -> ecosystemManager.openApp(EcosystemApp.FILES, accountName));
396+
binding.drawerEcosystemTalk.setOnClickListener(v -> ecosystemManager.openApp(EcosystemApp.TALK, accountName));
385397
binding.drawerEcosystemMore.setOnClickListener(v -> LinkHelper.INSTANCE.openAppStore("Nextcloud", true, this));
386398
}
387399

@@ -482,6 +494,20 @@ protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
482494
mainViewModel.restoreInstanceState();
483495
}
484496

497+
private void handleEcosystemIntent(Intent intent) {
498+
ecosystemManager.receiveAccount(intent, new AccountReceiverCallback() {
499+
@Override
500+
public void onAccountReceived(@NotNull String accountName) {
501+
502+
}
503+
504+
@Override
505+
public void onAccountError(@NotNull String reason) {
506+
507+
}
508+
});
509+
}
510+
485511
private void setupToolbars() {
486512
setSupportActionBar(binding.activityNotesListView.searchToolbar);
487513
activityBinding.searchBar.homeToolbar.setOnClickListener((v) -> {
@@ -754,6 +780,7 @@ protected void onNewIntent(Intent intent) {
754780
activityBinding.searchView.setQuery(intent.getStringExtra(SearchManager.QUERY), true);
755781
}
756782
super.onNewIntent(intent);
783+
handleEcosystemIntent(intent);
757784
}
758785

759786
@Override

app/src/main/java/it/niedermann/owncloud/notes/util/LinkHelper.kt

Lines changed: 1 addition & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -10,46 +10,16 @@ package it.niedermann.owncloud.notes.util
1010
import android.content.ActivityNotFoundException
1111
import android.content.Context
1212
import android.content.Intent
13-
import android.net.Uri
1413
import androidx.core.net.toUri
15-
import com.owncloud.android.lib.common.utils.Log_OC
16-
import java.util.Locale
1714

1815
/**
1916
* Helper class for opening Nextcloud apps if present
2017
* or falling back to opening the app store
2118
* in case the app is not yet installed on the device.
2219
*/
2320
object LinkHelper {
24-
const val APP_NEXTCLOUD_FILES = "com.nextcloud.client"
25-
const val APP_NEXTCLOUD_NOTES = "it.niedermann.owncloud.notes"
26-
const val APP_NEXTCLOUD_TALK = "com.nextcloud.talk2"
27-
const val KEY_ACCOUNT: String = "KEY_ACCOUNT"
2821
private const val TAG = "LinkHelper"
2922

30-
/**
31-
* Open specified app and, if not installed redirect to corresponding download.
32-
*
33-
* @param packageName of app to be opened
34-
* @param userHash to pass in intent
35-
*/
36-
fun openAppOrStore(
37-
packageName: String,
38-
userHash: String?,
39-
context: Context,
40-
) {
41-
val intent = context.packageManager.getLaunchIntentForPackage(packageName)
42-
if (intent != null) {
43-
// app installed - open directly
44-
// TODO handle null user?
45-
intent.putExtra(KEY_ACCOUNT, userHash)
46-
context.startActivity(intent)
47-
} else {
48-
// app not found - open market (Google Play Store, F-Droid, etc.)
49-
openAppStore(packageName, false, context)
50-
}
51-
}
52-
5323
/**
5424
* Open app store page of specified app or search for specified string. Will attempt to open browser when no app
5525
* store is available.
@@ -66,7 +36,7 @@ object LinkHelper {
6636
val intent = Intent(Intent.ACTION_VIEW, "market://$suffix".toUri())
6737
try {
6838
context.startActivity(intent)
69-
} catch (activityNotFoundException1: ActivityNotFoundException) {
39+
} catch (_: ActivityNotFoundException) {
7040
// all is lost: open google play store web page for app
7141
if (!search) {
7242
suffix = "apps/$suffix"
@@ -75,58 +45,4 @@ object LinkHelper {
7545
context.startActivity(intent)
7646
}
7747
}
78-
79-
// region Validation
80-
private const val HTTP = "http"
81-
private const val HTTPS = "https"
82-
private const val FILE = "file"
83-
private const val CONTENT = "content"
84-
85-
/**
86-
* Validates if a string can be converted to a valid URI
87-
*/
88-
@Suppress("TooGenericExceptionCaught", "ReturnCount")
89-
fun validateAndGetURI(uriString: String?): Uri? {
90-
if (uriString.isNullOrBlank()) {
91-
Log_OC.w(TAG, "Given uriString is null or blank")
92-
return null
93-
}
94-
95-
return try {
96-
val uri = uriString.toUri()
97-
if (uri.scheme == null) {
98-
return null
99-
}
100-
101-
val validSchemes = listOf(HTTP, HTTPS, FILE, CONTENT)
102-
if (uri.scheme in validSchemes) uri else null
103-
} catch (e: Exception) {
104-
Log_OC.e(TAG, "Invalid URI string: $uriString -- $e")
105-
null
106-
}
107-
}
108-
109-
/**
110-
* Validates if a URL string is valid
111-
*/
112-
@Suppress("TooGenericExceptionCaught", "ReturnCount")
113-
fun validateAndGetURL(url: String?): String? {
114-
if (url.isNullOrBlank()) {
115-
Log_OC.w(TAG, "Given url is null or blank")
116-
return null
117-
}
118-
119-
return try {
120-
val uri = url.toUri()
121-
if (uri.scheme == null) {
122-
return null
123-
}
124-
val validSchemes = listOf(HTTP, HTTPS)
125-
if (uri.scheme in validSchemes) url else null
126-
} catch (e: Exception) {
127-
Log_OC.e(TAG, "Invalid URL: $url -- $e")
128-
null
129-
}
130-
}
131-
// endregion
13248
}

0 commit comments

Comments
 (0)