|
3 | 3 | import android.annotation.SuppressLint; |
4 | 4 | import android.app.AlarmManager; |
5 | 5 | import android.app.PendingIntent; |
| 6 | +import android.app.ProgressDialog; |
6 | 7 | import android.content.Context; |
7 | 8 | import android.content.Intent; |
8 | 9 | import android.content.SharedPreferences; |
9 | 10 | import android.os.Build; |
10 | 11 | import android.os.Bundle; |
| 12 | +import android.text.InputType; |
| 13 | +import android.text.method.PasswordTransformationMethod; |
11 | 14 | import android.util.Log; |
12 | 15 | import android.widget.AutoCompleteTextView; |
13 | 16 | import android.widget.Button; |
|
19 | 22 | import androidx.fragment.app.Fragment; |
20 | 23 | import androidx.fragment.app.FragmentActivity; |
21 | 24 | import androidx.fragment.app.FragmentTransaction; |
| 25 | +import androidx.preference.EditTextPreference; |
22 | 26 | import androidx.preference.ListPreference; |
23 | 27 | import androidx.preference.Preference; |
24 | 28 | import androidx.preference.PreferenceFragmentCompat; |
|
51 | 55 | import com.topjohnwu.superuser.internal.UiThreadHandler; |
52 | 56 |
|
53 | 57 | import org.json.JSONException; |
| 58 | +import org.json.JSONObject; |
54 | 59 |
|
55 | 60 | import java.io.IOException; |
56 | 61 | import java.util.HashSet; |
57 | 62 | import java.util.Objects; |
58 | 63 | import java.util.Random; |
59 | 64 |
|
| 65 | +import okhttp3.OkHttpClient; |
| 66 | +import okhttp3.Request; |
| 67 | + |
60 | 68 | public class SettingsActivity extends FoxActivity implements LanguageActivity { |
61 | 69 | private static final int LANGUAGE_SUPPORT_LEVEL = 1; |
62 | 70 | private static final String TAG = "SettingsActivity"; |
@@ -270,11 +278,56 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { |
270 | 278 | "https://github.com/Fox2Code/FoxMagiskModuleManager"); |
271 | 279 | return true; |
272 | 280 | }); |
273 | | - // Add a listener to pref_androidacy_repo_api_key to update the api key in shared preferences |
274 | | - findPreference("pref_androidacy_repo_api_key").setOnPreferenceChangeListener((preference, newValue) -> { |
275 | | - MainApplication.getSharedPreferences().edit() |
276 | | - .putString("androidacy_api_token", String.valueOf(newValue)).apply(); |
277 | | - return true; |
| 281 | + // Create the pref_androidacy_repo_api_key text input |
| 282 | + EditTextPreference prefAndroidacyRepoApiKey = new EditTextPreference(this.requireContext()); |
| 283 | + prefAndroidacyRepoApiKey.setKey("pref_androidacy_repo_api_key"); |
| 284 | + prefAndroidacyRepoApiKey.setTitle(R.string.api_key); |
| 285 | + prefAndroidacyRepoApiKey.setDialogTitle(R.string.api_key); |
| 286 | + // Set the summary to the current androidacy_api_token |
| 287 | + prefAndroidacyRepoApiKey.setSummary(MainApplication.getSharedPreferences() |
| 288 | + .getString("androidacy_api_token", "")); |
| 289 | + prefAndroidacyRepoApiKey.setOnBindEditTextListener(editText -> { |
| 290 | + editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); |
| 291 | + editText.setTransformationMethod(PasswordTransformationMethod.getInstance()); |
| 292 | + }); |
| 293 | + // Bind ok button to save the new androidacy_api_token |
| 294 | + // On hitting OK, save the new androidacy_api_token after checking it. While checking, show a progress dialog |
| 295 | + prefAndroidacyRepoApiKey.setOnPreferenceChangeListener((preference, newValue) -> { |
| 296 | + String newToken = String.valueOf(newValue); |
| 297 | + if (newToken.isEmpty()) { |
| 298 | + MainApplication.getSharedPreferences().edit() |
| 299 | + .remove("androidacy_api_token").apply(); |
| 300 | + return true; |
| 301 | + } |
| 302 | + ProgressDialog progressDialog = new ProgressDialog(this.requireContext()); |
| 303 | + progressDialog.setMessage(getString(R.string.checking_api_key)); |
| 304 | + progressDialog.setCancelable(false); |
| 305 | + progressDialog.show(); |
| 306 | + new Thread(() -> { |
| 307 | + try { |
| 308 | + String response = new OkHttpClient().newCall(new Request.Builder() |
| 309 | + .url("https://production-api.androidacy.com/auth/me") |
| 310 | + .header("Authorization", "Bearer " + newToken) |
| 311 | + .build()).execute().body().string(); |
| 312 | + JSONObject jsonObject = new JSONObject(response); |
| 313 | + if (!jsonObject.has("role")) { |
| 314 | + throw new IOException("Invalid response"); |
| 315 | + } |
| 316 | + MainApplication.getSharedPreferences().edit() |
| 317 | + .putString("androidacy_api_token", newToken).apply(); |
| 318 | + progressDialog.dismiss(); |
| 319 | + this.requireActivity().runOnUiThread(() -> { |
| 320 | + prefAndroidacyRepoApiKey.setSummary(newToken); |
| 321 | + Toast.makeText(this.requireContext(), |
| 322 | + R.string.api_key_valid, Toast.LENGTH_SHORT).show(); |
| 323 | + }); |
| 324 | + } catch (IOException | JSONException e) { |
| 325 | + progressDialog.dismiss(); |
| 326 | + this.requireActivity().runOnUiThread(() -> Toast.makeText(this.requireContext(), |
| 327 | + R.string.api_key_invalid, Toast.LENGTH_SHORT).show()); |
| 328 | + } |
| 329 | + }).start(); |
| 330 | + return false; |
278 | 331 | }); |
279 | 332 | findPreference("pref_support").setOnPreferenceClickListener(p -> { |
280 | 333 | devModeStep = 0; |
|
0 commit comments