1212import android .os .Handler ;
1313import android .os .Looper ;
1414import android .util .Log ;
15+ import android .view .inputmethod .EditorInfo ;
1516import android .widget .AutoCompleteTextView ;
1617import android .widget .Button ;
1718import android .widget .Toast ;
@@ -337,6 +338,7 @@ private int currentLanguageLevel() {
337338 public static class RepoFragment extends PreferenceFragmentCompat {
338339 private static final int CUSTOM_REPO_ENTRIES = 5 ;
339340
341+ @ SuppressLint ("RestrictedApi" )
340342 @ Override
341343 public void onCreatePreferences (Bundle savedInstanceState , String rootKey ) {
342344 getPreferenceManager ().setSharedPreferencesName ("mmm" );
@@ -353,14 +355,16 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
353355 .getString ("pref_androidacy_api_token" , "" );
354356 // Create the pref_androidacy_repo_api_key text input with validation
355357 EditTextPreference prefAndroidacyRepoApiKey = findPreference ("pref_androidacy_repo_api_key" );
356- prefAndroidacyRepoApiKey .setKey ("pref_androidacy_repo_api_key" );
357358 prefAndroidacyRepoApiKey .setOnBindEditTextListener (editText -> {
358359 editText .setSingleLine ();
359360 // Make the single line wrap
360361 editText .setHorizontallyScrolling (false );
361362 // Set the height to the height of 2 lines
362- editText .setHeight (editText .getLineHeight () * 2 );
363+ editText .setHeight (editText .getLineHeight () * 3 );
364+ // Make ok button say "Save"
365+ editText .setImeOptions (EditorInfo .IME_ACTION_DONE );
363366 });
367+ prefAndroidacyRepoApiKey .setPositiveButtonText (R .string .save_api_key );
364368 prefAndroidacyRepoApiKey .setOnPreferenceChangeListener ((preference , newValue ) -> {
365369 // Curious if this actually works - so crash the app on purpose
366370 // throw new RuntimeException("This is a test crash");
@@ -387,6 +391,14 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
387391 if (apiKey .length () < 64 ) {
388392 new Handler (Looper .getMainLooper ()).post (() -> {
389393 progressDialog .dismiss ();
394+ // Save the original key
395+ MainApplication .getSharedPreferences ().edit ()
396+ .putString ("pref_androidacy_api_token" , originalApiKey ).apply ();
397+ // Re-show the dialog with an error
398+ prefAndroidacyRepoApiKey .performClick ();
399+ // Show error
400+ prefAndroidacyRepoApiKey .setDialogMessage (getString (R .string .api_key_invalid ));
401+ // Set the error color
390402 Toast .makeText (getContext (), R .string .api_key_invalid , Toast .LENGTH_SHORT ).show ();
391403 });
392404 return ;
0 commit comments