From 167aecb696a62e0535614bc8861957f371ca0557 Mon Sep 17 00:00:00 2001 From: Bazsalanszky Date: Mon, 7 Aug 2023 13:56:20 +0200 Subject: [PATCH] Fix crashes related to incorrect instance URL --- .../activities/LoginActivity.java | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/activities/LoginActivity.java b/app/src/main/java/eu/toldi/infinityforlemmy/activities/LoginActivity.java index f904f945..15ceb6ab 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/activities/LoginActivity.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/activities/LoginActivity.java @@ -27,6 +27,8 @@ import org.json.JSONException; import org.json.JSONObject; import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; import java.util.concurrent.Executor; import javax.inject.Inject; @@ -146,11 +148,12 @@ public class LoginActivity extends BaseActivity { progressBar.setVisibility(ProgressBar.VISIBLE); String username = username_input.getText().toString().trim(); String instance = correctURL(instance_input.getText().toString().trim()); - if (!Patterns.WEB_URL.matcher(instance).matches()) { - instance_input.setError("Invalid instance URL"); + try { + URL urlObj = new URL(instance); + instance = urlObj.getProtocol() + "://" + urlObj.getHost() + "/"; + } catch (MalformedURLException e) { + instance_input.setError("Invalid URL"); Toast.makeText(LoginActivity.this, "Invalid instance URL", Toast.LENGTH_SHORT).show(); - loginButton.setEnabled(true); - progressBar.setVisibility(ProgressBar.GONE); return; } Log.i("LoginActivity", "Instance: " + instance); @@ -158,6 +161,7 @@ public class LoginActivity extends BaseActivity { mRetrofit.setBaseURL(instance); LemmyAPI api = mRetrofit.getRetrofit().create(LemmyAPI.class); Call accessTokenCall = api.userLogin(accountLoginDTO); + String finalInstance = instance; accessTokenCall.enqueue(new Callback() { @Override public void onResponse(@NonNull Call call, @NonNull Response response) { @@ -185,7 +189,7 @@ public class LoginActivity extends BaseActivity { @Override public void onFetchSiteInfoSuccess(SiteInfo siteInfo) { boolean canDownvote = siteInfo.isEnable_downvotes(); - ParseAndInsertNewAccount.parseAndInsertNewAccount(mExecutor, new Handler(), name,display_name, accessToken, profileImageUrl, bannerImageUrl, authCode,instance,canDownvote, mRedditDataRoomDatabase.accountDao(), + ParseAndInsertNewAccount.parseAndInsertNewAccount(mExecutor, new Handler(), name,display_name, accessToken, profileImageUrl, bannerImageUrl, authCode, finalInstance,canDownvote, mRedditDataRoomDatabase.accountDao(), () -> { Intent resultIntent = new Intent(); setResult(Activity.RESULT_OK, resultIntent); @@ -196,7 +200,7 @@ public class LoginActivity extends BaseActivity { @Override public void onFetchSiteInfoFailed() { - ParseAndInsertNewAccount.parseAndInsertNewAccount(mExecutor, new Handler(), name,display_name, accessToken, profileImageUrl, bannerImageUrl, authCode,instance,true, mRedditDataRoomDatabase.accountDao(), + ParseAndInsertNewAccount.parseAndInsertNewAccount(mExecutor, new Handler(), name,display_name, accessToken, profileImageUrl, bannerImageUrl, authCode, finalInstance,true, mRedditDataRoomDatabase.accountDao(), () -> { Intent resultIntent = new Intent(); setResult(Activity.RESULT_OK, resultIntent); @@ -208,7 +212,7 @@ public class LoginActivity extends BaseActivity { mCurrentAccountSharedPreferences.edit().putString(SharedPreferencesUtils.ACCESS_TOKEN, accessToken) .putString(SharedPreferencesUtils.ACCOUNT_NAME, display_name) .putString(SharedPreferencesUtils.ACCOUNT_QUALIFIED_NAME, name) - .putString(SharedPreferencesUtils.ACCOUNT_INSTANCE,instance) + .putString(SharedPreferencesUtils.ACCOUNT_INSTANCE,finalInstance) .putString(SharedPreferencesUtils.ACCOUNT_IMAGE_URL, profileImageUrl).apply(); }