Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable-3.30] Enforced servers #13598

Merged
merged 4 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -627,4 +629,21 @@ private void hideCursors(ViewGroup viewGroup) {
}
}
}

@Test
public void testGson() {
ArrayList<EnforcedServer> t = new ArrayList<>();
t.add(new EnforcedServer("name", "url"));
t.add(new EnforcedServer("name2", "url1"));

String s = new Gson().toJson(t);

ArrayList<EnforcedServer> t2 = new Gson().fromJson(s, new TypeToken<ArrayList<EnforcedServer>>() {
}.getType());

ArrayList<String> temp = new ArrayList<>();
for (EnforcedServer p : t2) {
temp.add(p.getName());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -356,14 +362,63 @@ 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 {
showEnforcedServers();
}

initServerPreFragment(savedInstanceState);
ProcessLifecycleOwner.get().getLifecycle().addObserver(lifecycleEventObserver);

// webViewUtil.checkWebViewVersion();
}
}

private void showEnforcedServers() {

initServerPreFragment(savedInstanceState);
ProcessLifecycleOwner.get().getLifecycle().addObserver(lifecycleEventObserver);
showAuthStatus();
accountSetupBinding.hostUrlFrame.setVisibility(View.GONE);
accountSetupBinding.hostUrlInputHelperText.setVisibility(View.GONE);
accountSetupBinding.scanQr.setVisibility(View.GONE);
accountSetupBinding.serversSpinner.setVisibility(View.VISIBLE);

// webViewUtil.checkWebViewVersion();
}
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, R.layout.enforced_servers_spinner);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

ArrayList<String> servers = new ArrayList<>();
servers.add("");
adapter.add(getString(R.string.please_select_a_server));

ArrayList<EnforcedServer> t = new Gson().fromJson(getString(R.string.enforce_servers),
new TypeToken<ArrayList<EnforcedServer>>() {
}
.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
}
});
}

private final LifecycleEventObserver lifecycleEventObserver = ((lifecycleOwner, event) -> {
if (event == Lifecycle.Event.ON_START && token != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* Nextcloud - Android Client
*
* SPDX-FileCopyrightText: 2024 Tobias Kaminsky <tobias.kaminsky@nextcloud.com>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

package com.owncloud.android.authentication

data class EnforcedServer(val name: String, val url: String)
13 changes: 11 additions & 2 deletions app/src/main/res/layout-land/account_setup.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
<?xml version="1.0" encoding="utf-8"?><!--
~ Nextcloud - Android Client
~
~ SPDX-FileCopyrightText: 2019 Tobias Kaminsky <tobias@kaminsky.me>
Expand Down Expand Up @@ -80,6 +79,16 @@
</com.google.android.material.textfield.TextInputLayout>
</FrameLayout>

<Spinner
android:id="@+id/servers_spinner"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:foregroundTint="#FFFFFF"
android:spinnerMode="dialog"
android:textColor="@color/white_helper_text"
android:visibility="gone" />

<TextView
android:id="@+id/host_url_input_helper_text"
android:layout_width="match_parent"
Expand Down
14 changes: 12 additions & 2 deletions app/src/main/res/layout/account_setup.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
<?xml version="1.0" encoding="utf-8"?><!--
~ Nextcloud - Android Client
~
~ SPDX-FileCopyrightText: 2019 Tobias Kaminsky <tobias@kaminsky.me>
Expand All @@ -15,6 +14,7 @@
android:layout_height="match_parent"
android:layout_gravity="center"
android:fillViewport="true"
android:foregroundTint="@color/login_text_color"
android:orientation="vertical">

<LinearLayout
Expand Down Expand Up @@ -79,6 +79,16 @@
</com.google.android.material.textfield.TextInputLayout>
</FrameLayout>

<Spinner
android:id="@+id/servers_spinner"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:foregroundTint="#FFFFFF"
android:spinnerMode="dialog"
android:textColor="@color/white_helper_text"
android:visibility="gone" />

<TextView
android:id="@+id/host_url_input_helper_text"
android:layout_width="match_parent"
Expand Down
16 changes: 16 additions & 0 deletions app/src/main/res/layout/enforced_servers_spinner.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Nextcloud - Android Client
~
~ SPDX-FileCopyrightText: 2006 The Android Open Source Project
~ SPDX-License-Identifier: Apache-2.0
-->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
style="?android:attr/spinnerItemStyle"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/login_text_color"
android:ellipsize="marquee"
android:textAlignment="inherit"/>
4 changes: 4 additions & 0 deletions app/src/main/res/values/setup.xml
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@
If set, will replace all other login methods available -->
<string name="webview_login_url" translatable="false"></string>

<!-- [{\"name\":\"name1\",\"url\":\"url\"},{\"name\":\"name2\",\"url\":\"url1\"}]-->
<string name="enforce_servers" translatable="false"></string>


<!-- Files becomes Home -->
<bool name="use_home">false</bool>

Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1235,4 +1235,5 @@
<string name="file_name_validator_error_reserved_names">%s is a forbidden name</string>
<string name="file_name_validator_error_forbidden_file_extensions">%s is a forbidden file extension</string>
<string name="sync">Sync</string>
<string name="please_select_a_server">Please select a server…</string>
</resources>
Loading