From 4d587d3bf4586b69f1388aa9417e4e2f86180ff9 Mon Sep 17 00:00:00 2001 From: tobiasKaminsky Date: Thu, 22 Aug 2024 14:36:30 +0200 Subject: [PATCH 1/4] Enforced servers Signed-off-by: tobiasKaminsky --- .../android/ui/dialog/DialogFragmentIT.java | 19 +++++++ .../authentication/AuthenticatorActivity.java | 52 ++++++++++++++++++- .../android/authentication/EnforcedServer.kt | 10 ++++ .../main/res/layout-land/account_setup.xml | 16 ++++++ app/src/main/res/layout/account_setup.xml | 17 ++++++ .../res/layout/enforced_servers_spinner.xml | 16 ++++++ app/src/main/res/values/setup.xml | 4 ++ app/src/main/res/values/strings.xml | 1 + 8 files changed, 134 insertions(+), 1 deletion(-) create mode 100644 app/src/main/java/com/owncloud/android/authentication/EnforcedServer.kt create mode 100644 app/src/main/res/layout/enforced_servers_spinner.xml diff --git a/app/src/androidTest/java/com/owncloud/android/ui/dialog/DialogFragmentIT.java b/app/src/androidTest/java/com/owncloud/android/ui/dialog/DialogFragmentIT.java index b75db5e27f08..eb14c5a7d3c9 100644 --- a/app/src/androidTest/java/com/owncloud/android/ui/dialog/DialogFragmentIT.java +++ b/app/src/androidTest/java/com/owncloud/android/ui/dialog/DialogFragmentIT.java @@ -20,6 +20,7 @@ import com.google.android.material.bottomsheet.BottomSheetBehavior; import com.google.gson.Gson; +import com.google.gson.reflect.TypeToken; import com.nextcloud.android.common.ui.color.ColorUtil; import com.nextcloud.android.common.ui.theme.MaterialSchemes; import com.nextcloud.android.common.ui.theme.MaterialSchemesImpl; @@ -37,6 +38,7 @@ import com.owncloud.android.AbstractIT; import com.owncloud.android.MainApp; import com.owncloud.android.R; +import com.owncloud.android.authentication.EnforcedServer; import com.owncloud.android.datamodel.ArbitraryDataProvider; import com.owncloud.android.datamodel.ArbitraryDataProviderImpl; import com.owncloud.android.datamodel.FileDataStorageManager; @@ -628,4 +630,21 @@ private void hideCursors(ViewGroup viewGroup) { } } } + + @Test + public void testGson() { + ArrayList t = new ArrayList<>(); + t.add(new EnforcedServer("name", "url")); + t.add(new EnforcedServer("name2", "url1")); + + String s = new Gson().toJson(t); + + ArrayList t2 = new Gson().fromJson(s, new TypeToken>() { + }.getType()); + + ArrayList temp = new ArrayList<>(); + for (EnforcedServer p : t2) { + temp.add(p.getName()); + } + } } diff --git a/app/src/main/java/com/owncloud/android/authentication/AuthenticatorActivity.java b/app/src/main/java/com/owncloud/android/authentication/AuthenticatorActivity.java index 727f514ca66d..4069b443747a 100644 --- a/app/src/main/java/com/owncloud/android/authentication/AuthenticatorActivity.java +++ b/app/src/main/java/com/owncloud/android/authentication/AuthenticatorActivity.java @@ -40,9 +40,12 @@ import android.view.inputmethod.EditorInfo; import android.webkit.CookieManager; import android.webkit.CookieSyncManager; +import android.webkit.URLUtil; import android.webkit.WebResourceRequest; import android.webkit.WebResourceResponse; import android.webkit.WebView; +import android.widget.AdapterView; +import android.widget.ArrayAdapter; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.TextView.OnEditorActionListener; @@ -51,8 +54,10 @@ import com.blikoon.qrcodescanner.QrCodeActivity; import com.google.android.material.button.MaterialButton; import com.google.android.material.snackbar.Snackbar; +import com.google.gson.Gson; import com.google.gson.JsonObject; import com.google.gson.JsonParser; +import com.google.gson.reflect.TypeToken; import com.nextcloud.android.common.ui.color.ColorUtil; import com.nextcloud.android.common.ui.theme.utils.ColorRole; import com.nextcloud.client.account.User; @@ -115,6 +120,7 @@ import java.io.InputStream; import java.net.URLDecoder; +import java.util.ArrayList; import java.util.HashMap; import java.util.Locale; import java.util.Map; @@ -356,7 +362,51 @@ protected void onCreate(Bundle savedInstanceState) { /// initialize block to be moved to single Fragment to check server and get info about it /// initialize block to be moved to single Fragment to retrieve and validate credentials - initAuthorizationPreFragment(savedInstanceState); + if (TextUtils.isEmpty(getString(R.string.enforce_servers))) { + initAuthorizationPreFragment(savedInstanceState); + } else { + showAuthStatus(); + accountSetupBinding.hostUrlFrame.setVisibility(View.GONE); + accountSetupBinding.hostUrlInputHelperText.setVisibility(View.GONE); + accountSetupBinding.scanQr.setVisibility(View.GONE); + accountSetupBinding.multipleServersLayout.setVisibility(View.VISIBLE); + + ArrayAdapter adapter = new ArrayAdapter<>(this, R.layout.enforced_servers_spinner); + adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); + + ArrayList servers = new ArrayList<>(); + servers.add(""); + adapter.add(getString(R.string.please_select_a_server)); + + ArrayList t = new Gson().fromJson(getString(R.string.enforce_servers), + new TypeToken>() { + } + .getType()); + + for (EnforcedServer e : t) { + adapter.add(e.getName()); + servers.add(e.getUrl()); + } + + accountSetupBinding.serversSpinner.setAdapter(adapter); + accountSetupBinding.serversSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){ + + @Override + public void onItemSelected(AdapterView parent, View view, int position, long id) { + String url = servers.get(position); + + if (URLUtil.isValidUrl(url)) { + accountSetupBinding.hostUrlInput.setText(url); + checkOcServer(); + } + } + + @Override + public void onNothingSelected(AdapterView parent) { + // do nothing + } + }); + } } initServerPreFragment(savedInstanceState); diff --git a/app/src/main/java/com/owncloud/android/authentication/EnforcedServer.kt b/app/src/main/java/com/owncloud/android/authentication/EnforcedServer.kt new file mode 100644 index 000000000000..abe2358be6b4 --- /dev/null +++ b/app/src/main/java/com/owncloud/android/authentication/EnforcedServer.kt @@ -0,0 +1,10 @@ +/* + * Nextcloud - Android Client + * + * SPDX-FileCopyrightText: 2024 Your Name + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +package com.owncloud.android.authentication + +data class EnforcedServer(val name: String, val url: String) diff --git a/app/src/main/res/layout-land/account_setup.xml b/app/src/main/res/layout-land/account_setup.xml index 0d11b30e816d..6a44696f4417 100644 --- a/app/src/main/res/layout-land/account_setup.xml +++ b/app/src/main/res/layout-land/account_setup.xml @@ -80,6 +80,22 @@ + + + + + + + + + + + + diff --git a/app/src/main/res/values/setup.xml b/app/src/main/res/values/setup.xml index 046976793481..47c4ffd9da87 100644 --- a/app/src/main/res/values/setup.xml +++ b/app/src/main/res/values/setup.xml @@ -123,6 +123,10 @@ If set, will replace all other login methods available --> + + + + false diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 4e4e775f1063..d9afd5132b8e 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1244,4 +1244,5 @@ .%s is a forbidden file extension Name ends with a space or a period Sync + Please select a server… From 60ec1481544b7a315ce7c9e019677f7a212b1128 Mon Sep 17 00:00:00 2001 From: tobiasKaminsky Date: Mon, 2 Sep 2024 09:34:31 +0200 Subject: [PATCH 2/4] - no need for linearlayout - correct license Signed-off-by: tobiasKaminsky --- .../authentication/AuthenticatorActivity.java | 2 +- .../android/authentication/EnforcedServer.kt | 2 +- .../main/res/layout-land/account_setup.xml | 27 +++++++------------ app/src/main/res/layout/account_setup.xml | 27 +++++++------------ app/src/main/res/values/setup.xml | 2 +- 5 files changed, 23 insertions(+), 37 deletions(-) diff --git a/app/src/main/java/com/owncloud/android/authentication/AuthenticatorActivity.java b/app/src/main/java/com/owncloud/android/authentication/AuthenticatorActivity.java index 4069b443747a..37b3eff37fad 100644 --- a/app/src/main/java/com/owncloud/android/authentication/AuthenticatorActivity.java +++ b/app/src/main/java/com/owncloud/android/authentication/AuthenticatorActivity.java @@ -369,7 +369,7 @@ protected void onCreate(Bundle savedInstanceState) { accountSetupBinding.hostUrlFrame.setVisibility(View.GONE); accountSetupBinding.hostUrlInputHelperText.setVisibility(View.GONE); accountSetupBinding.scanQr.setVisibility(View.GONE); - accountSetupBinding.multipleServersLayout.setVisibility(View.VISIBLE); + accountSetupBinding.serversSpinner.setVisibility(View.VISIBLE); ArrayAdapter adapter = new ArrayAdapter<>(this, R.layout.enforced_servers_spinner); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); diff --git a/app/src/main/java/com/owncloud/android/authentication/EnforcedServer.kt b/app/src/main/java/com/owncloud/android/authentication/EnforcedServer.kt index abe2358be6b4..b9483d2112c6 100644 --- a/app/src/main/java/com/owncloud/android/authentication/EnforcedServer.kt +++ b/app/src/main/java/com/owncloud/android/authentication/EnforcedServer.kt @@ -1,7 +1,7 @@ /* * Nextcloud - Android Client * - * SPDX-FileCopyrightText: 2024 Your Name + * SPDX-FileCopyrightText: 2024 Tobias Kaminsky * SPDX-License-Identifier: AGPL-3.0-or-later */ diff --git a/app/src/main/res/layout-land/account_setup.xml b/app/src/main/res/layout-land/account_setup.xml index 6a44696f4417..88f50287e975 100644 --- a/app/src/main/res/layout-land/account_setup.xml +++ b/app/src/main/res/layout-land/account_setup.xml @@ -1,5 +1,4 @@ - - - + From 8d0741062b5cc724498a3e810550ce9a5f453f0b Mon Sep 17 00:00:00 2001 From: tobiasKaminsky Date: Wed, 11 Sep 2024 08:47:02 +0200 Subject: [PATCH 3/4] fix license Signed-off-by: tobiasKaminsky --- app/src/main/res/layout/enforced_servers_spinner.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/res/layout/enforced_servers_spinner.xml b/app/src/main/res/layout/enforced_servers_spinner.xml index 411e80195c04..e1cae4c3939a 100644 --- a/app/src/main/res/layout/enforced_servers_spinner.xml +++ b/app/src/main/res/layout/enforced_servers_spinner.xml @@ -3,7 +3,7 @@ ~ Nextcloud - Android Client ~ ~ SPDX-FileCopyrightText: 2006 The Android Open Source Project - ~ SPDX-License-Identifier: SPDX-License-Identifier: Apache-2.0 + ~ SPDX-License-Identifier: Apache-2.0 --> Date: Wed, 11 Sep 2024 08:59:39 +0200 Subject: [PATCH 4/4] extract to function Signed-off-by: tobiasKaminsky --- .../authentication/AuthenticatorActivity.java | 91 ++++++++++--------- 1 file changed, 48 insertions(+), 43 deletions(-) diff --git a/app/src/main/java/com/owncloud/android/authentication/AuthenticatorActivity.java b/app/src/main/java/com/owncloud/android/authentication/AuthenticatorActivity.java index 37b3eff37fad..c4b1cc81d559 100644 --- a/app/src/main/java/com/owncloud/android/authentication/AuthenticatorActivity.java +++ b/app/src/main/java/com/owncloud/android/authentication/AuthenticatorActivity.java @@ -365,55 +365,60 @@ protected void onCreate(Bundle savedInstanceState) { if (TextUtils.isEmpty(getString(R.string.enforce_servers))) { initAuthorizationPreFragment(savedInstanceState); } else { - showAuthStatus(); - accountSetupBinding.hostUrlFrame.setVisibility(View.GONE); - accountSetupBinding.hostUrlInputHelperText.setVisibility(View.GONE); - accountSetupBinding.scanQr.setVisibility(View.GONE); - accountSetupBinding.serversSpinner.setVisibility(View.VISIBLE); - - ArrayAdapter adapter = new ArrayAdapter<>(this, R.layout.enforced_servers_spinner); - adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); - - ArrayList servers = new ArrayList<>(); - servers.add(""); - adapter.add(getString(R.string.please_select_a_server)); - - ArrayList t = new Gson().fromJson(getString(R.string.enforce_servers), - new TypeToken>() { - } - .getType()); - - for (EnforcedServer e : t) { - adapter.add(e.getName()); - servers.add(e.getUrl()); - } + showEnforcedServers(); + } + + initServerPreFragment(savedInstanceState); + ProcessLifecycleOwner.get().getLifecycle().addObserver(lifecycleEventObserver); - accountSetupBinding.serversSpinner.setAdapter(adapter); - accountSetupBinding.serversSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){ + // webViewUtil.checkWebViewVersion(); + } + } + + private void showEnforcedServers() { - @Override - public void onItemSelected(AdapterView parent, View view, int position, long id) { - String url = servers.get(position); - - if (URLUtil.isValidUrl(url)) { - accountSetupBinding.hostUrlInput.setText(url); - checkOcServer(); - } - } + showAuthStatus(); + accountSetupBinding.hostUrlFrame.setVisibility(View.GONE); + accountSetupBinding.hostUrlInputHelperText.setVisibility(View.GONE); + accountSetupBinding.scanQr.setVisibility(View.GONE); + accountSetupBinding.serversSpinner.setVisibility(View.VISIBLE); - @Override - public void onNothingSelected(AdapterView parent) { - // do nothing - } - }); + ArrayAdapter adapter = new ArrayAdapter<>(this, R.layout.enforced_servers_spinner); + adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); + + ArrayList servers = new ArrayList<>(); + servers.add(""); + adapter.add(getString(R.string.please_select_a_server)); + + ArrayList t = new Gson().fromJson(getString(R.string.enforce_servers), + new TypeToken>() { + } + .getType()); + + for (EnforcedServer e : t) { + adapter.add(e.getName()); + servers.add(e.getUrl()); } - } - initServerPreFragment(savedInstanceState); - ProcessLifecycleOwner.get().getLifecycle().addObserver(lifecycleEventObserver); + accountSetupBinding.serversSpinner.setAdapter(adapter); + accountSetupBinding.serversSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){ - // webViewUtil.checkWebViewVersion(); - } + @Override + public void onItemSelected(AdapterView parent, View view, int position, long id) { + String url = servers.get(position); + + if (URLUtil.isValidUrl(url)) { + accountSetupBinding.hostUrlInput.setText(url); + checkOcServer(); + } + } + + @Override + public void onNothingSelected(AdapterView parent) { + // do nothing + } + }); + } private final LifecycleEventObserver lifecycleEventObserver = ((lifecycleOwner, event) -> { if (event == Lifecycle.Event.ON_START && token != null) {