Skip to content

Commit

Permalink
Enforced servers
Browse files Browse the repository at this point in the history
Signed-off-by: tobiasKaminsky <[email protected]>
  • Loading branch information
tobiasKaminsky committed Aug 30, 2024
1 parent 45e3feb commit 84f55ce
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 1 deletion.
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,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<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
}
});
}
}

initServerPreFragment(savedInstanceState);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* Nextcloud - Android Client
*
* SPDX-FileCopyrightText: 2024 Your Name <[email protected]>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

package com.owncloud.android.authentication

data class EnforcedServer(val name: String, val url: String)
16 changes: 16 additions & 0 deletions app/src/main/res/layout-land/account_setup.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,22 @@
</com.google.android.material.textfield.TextInputLayout>
</FrameLayout>

<LinearLayout
android:id="@+id/multiple_servers_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone">

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

<TextView
android:id="@+id/host_url_input_helper_text"
android:layout_width="match_parent"
Expand Down
17 changes: 17 additions & 0 deletions app/src/main/res/layout/account_setup.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,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 +80,22 @@
</com.google.android.material.textfield.TextInputLayout>
</FrameLayout>

<LinearLayout
android:id="@+id/multiple_servers_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone">

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

<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: 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\":\"nameu","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 @@ -1236,4 +1236,5 @@
<string name="file_name_validator_error_forbidden_file_extensions">.%s is a forbidden file extension</string>
<string name="file_name_validator_error_ends_with_space_period">Name ends with a space or a period</string>
<string name="sync">Sync</string>
<string name="please_select_a_server">Please select a server…</string>
</resources>

0 comments on commit 84f55ce

Please sign in to comment.