|
3 | 3 | import android.annotation.SuppressLint; |
4 | 4 | import android.app.AlarmManager; |
5 | 5 | import android.app.PendingIntent; |
6 | | -import android.app.ProgressDialog; |
7 | 6 | import android.content.Context; |
8 | 7 | import android.content.Intent; |
9 | 8 | import android.content.SharedPreferences; |
10 | 9 | import android.os.Build; |
11 | 10 | import android.os.Bundle; |
12 | | -import android.text.InputType; |
13 | | -import android.text.method.PasswordTransformationMethod; |
| 11 | +import android.os.Handler; |
| 12 | +import android.os.Looper; |
14 | 13 | import android.util.Log; |
15 | 14 | import android.widget.AutoCompleteTextView; |
16 | 15 | import android.widget.Button; |
|
54 | 53 | import com.mikepenz.aboutlibraries.LibsBuilder; |
55 | 54 | import com.topjohnwu.superuser.internal.UiThreadHandler; |
56 | 55 |
|
| 56 | +import org.jetbrains.annotations.NotNull; |
57 | 57 | import org.json.JSONException; |
58 | | -import org.json.JSONObject; |
59 | 58 |
|
60 | 59 | import java.io.IOException; |
61 | 60 | import java.util.HashSet; |
62 | 61 | import java.util.Objects; |
63 | 62 | import java.util.Random; |
64 | 63 |
|
| 64 | +import okhttp3.Call; |
| 65 | +import okhttp3.Callback; |
65 | 66 | import okhttp3.OkHttpClient; |
66 | 67 | import okhttp3.Request; |
| 68 | +import okhttp3.Response; |
67 | 69 |
|
68 | 70 | public class SettingsActivity extends FoxActivity implements LanguageActivity { |
69 | 71 | private static final int LANGUAGE_SUPPORT_LEVEL = 1; |
@@ -286,48 +288,42 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { |
286 | 288 | // Set the summary to the current androidacy_api_token |
287 | 289 | prefAndroidacyRepoApiKey.setSummary(MainApplication.getSharedPreferences() |
288 | 290 | .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 |
| 291 | + // On user input, save the new androidacy_api_token after validating it |
295 | 292 | prefAndroidacyRepoApiKey.setOnPreferenceChangeListener((preference, newValue) -> { |
296 | 293 | String newToken = String.valueOf(newValue); |
297 | | - if (newToken.isEmpty()) { |
| 294 | + if (newToken.length() == 0) { |
298 | 295 | MainApplication.getSharedPreferences().edit() |
299 | 296 | .remove("androidacy_api_token").apply(); |
300 | 297 | return true; |
301 | 298 | } |
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(); |
| 299 | + // Call the androidacy api to validate the token |
| 300 | + OkHttpClient client = new OkHttpClient(); |
| 301 | + Request request = new Request.Builder() |
| 302 | + .url("https://production-api.androidacy.com/auth/me") |
| 303 | + .header("Authorization", "Bearer " + newToken) |
| 304 | + .build(); |
| 305 | + client.newCall(request).enqueue(new Callback() { |
| 306 | + @Override |
| 307 | + public void onFailure(@NotNull Call call, @NotNull IOException e) { |
| 308 | + // If the request failed, show an error message |
| 309 | + new Handler(Looper.getMainLooper()).post(() -> { |
| 310 | + Toast.makeText(getContext(), R.string.api_key_invalid, |
| 311 | + Toast.LENGTH_SHORT).show(); |
323 | 312 | }); |
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 | 313 | } |
329 | | - }).start(); |
330 | | - return false; |
| 314 | + |
| 315 | + @Override |
| 316 | + public void onResponse(@NotNull Call call, @NotNull Response response) { |
| 317 | + // If the request succeeded, save the token |
| 318 | + new Handler(Looper.getMainLooper()).post(() -> { |
| 319 | + MainApplication.getSharedPreferences().edit() |
| 320 | + .putString("androidacy_api_token", newToken).apply(); |
| 321 | + Toast.makeText(getContext(), R.string.api_key_valid, |
| 322 | + Toast.LENGTH_SHORT).show(); |
| 323 | + }); |
| 324 | + } |
| 325 | + }); |
| 326 | + return true; |
331 | 327 | }); |
332 | 328 | findPreference("pref_support").setOnPreferenceClickListener(p -> { |
333 | 329 | devModeStep = 0; |
|
0 commit comments