From aad605cbd0c0ce2df9ca4060c7e8662045cc6df7 Mon Sep 17 00:00:00 2001 From: androidacy-user Date: Fri, 26 Aug 2022 21:22:09 -0400 Subject: [PATCH] Add custom API key support Signed-off-by: androidacy-user --- .../fox2code/mmm/settings/SettingsActivity.java | 16 ++++++++++++++-- app/src/main/res/values/strings.xml | 3 ++- app/src/main/res/xml/repo_preferences.xml | 1 + 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/com/fox2code/mmm/settings/SettingsActivity.java b/app/src/main/java/com/fox2code/mmm/settings/SettingsActivity.java index 8b1273b..1854ee5 100644 --- a/app/src/main/java/com/fox2code/mmm/settings/SettingsActivity.java +++ b/app/src/main/java/com/fox2code/mmm/settings/SettingsActivity.java @@ -12,6 +12,7 @@ import android.os.Bundle; import android.os.Handler; import android.os.Looper; import android.util.Log; +import android.view.inputmethod.EditorInfo; import android.widget.AutoCompleteTextView; import android.widget.Button; import android.widget.Toast; @@ -337,6 +338,7 @@ public class SettingsActivity extends FoxActivity implements LanguageActivity { public static class RepoFragment extends PreferenceFragmentCompat { private static final int CUSTOM_REPO_ENTRIES = 5; + @SuppressLint("RestrictedApi") @Override public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { getPreferenceManager().setSharedPreferencesName("mmm"); @@ -353,14 +355,16 @@ public class SettingsActivity extends FoxActivity implements LanguageActivity { .getString("pref_androidacy_api_token", ""); // Create the pref_androidacy_repo_api_key text input with validation EditTextPreference prefAndroidacyRepoApiKey = findPreference("pref_androidacy_repo_api_key"); - prefAndroidacyRepoApiKey.setKey("pref_androidacy_repo_api_key"); prefAndroidacyRepoApiKey.setOnBindEditTextListener(editText -> { editText.setSingleLine(); // Make the single line wrap editText.setHorizontallyScrolling(false); // Set the height to the height of 2 lines - editText.setHeight(editText.getLineHeight() * 2); + editText.setHeight(editText.getLineHeight() * 3); + // Make ok button say "Save" + editText.setImeOptions(EditorInfo.IME_ACTION_DONE); }); + prefAndroidacyRepoApiKey.setPositiveButtonText(R.string.save_api_key); prefAndroidacyRepoApiKey.setOnPreferenceChangeListener((preference, newValue) -> { // Curious if this actually works - so crash the app on purpose // throw new RuntimeException("This is a test crash"); @@ -387,6 +391,14 @@ public class SettingsActivity extends FoxActivity implements LanguageActivity { if (apiKey.length() < 64) { new Handler(Looper.getMainLooper()).post(() -> { progressDialog.dismiss(); + // Save the original key + MainApplication.getSharedPreferences().edit() + .putString("pref_androidacy_api_token", originalApiKey).apply(); + // Re-show the dialog with an error + prefAndroidacyRepoApiKey.performClick(); + // Show error + prefAndroidacyRepoApiKey.setDialogMessage(getString(R.string.api_key_invalid)); + // Set the error color Toast.makeText(getContext(), R.string.api_key_invalid, Toast.LENGTH_SHORT).show(); }); return; diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 67da3e4..fbbbc13 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -158,10 +158,11 @@ Use a custom API key for Androidacy. Useful for premium subscribers, to remove ads and more. Androidacy API key is empty Current Androidacy API key - Could not validate API key. Please try again. + Could not validate API key. Please check it and try again. API key is valid. Validating API key... Validating API key... Please wait Successfully reset API key + Validate diff --git a/app/src/main/res/xml/repo_preferences.xml b/app/src/main/res/xml/repo_preferences.xml index 444b40b..8cbb1a8 100644 --- a/app/src/main/res/xml/repo_preferences.xml +++ b/app/src/main/res/xml/repo_preferences.xml @@ -51,6 +51,7 @@ app:dialogIcon="@drawable/ic_baseline_vpn_key_24" app:dependency="pref_androidacy_repo_enabled" android:inputType="text" + android:selectAllOnFocus="true" android:imeOptions="actionDone" />