Skip to content
This repository was archived by the owner on May 4, 2023. It is now read-only.

Commit 9052703

Browse files
tweak custom API key support
Signed-off-by: androidacy-user <opensource@androidacy.com>
1 parent fff580a commit 9052703

File tree

3 files changed

+42
-40
lines changed

3 files changed

+42
-40
lines changed

app/src/main/java/com/fox2code/mmm/settings/SettingsActivity.java

Lines changed: 34 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@
33
import android.annotation.SuppressLint;
44
import android.app.AlarmManager;
55
import android.app.PendingIntent;
6-
import android.app.ProgressDialog;
76
import android.content.Context;
87
import android.content.Intent;
98
import android.content.SharedPreferences;
109
import android.os.Build;
1110
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;
1413
import android.util.Log;
1514
import android.widget.AutoCompleteTextView;
1615
import android.widget.Button;
@@ -54,16 +53,19 @@
5453
import com.mikepenz.aboutlibraries.LibsBuilder;
5554
import com.topjohnwu.superuser.internal.UiThreadHandler;
5655

56+
import org.jetbrains.annotations.NotNull;
5757
import org.json.JSONException;
58-
import org.json.JSONObject;
5958

6059
import java.io.IOException;
6160
import java.util.HashSet;
6261
import java.util.Objects;
6362
import java.util.Random;
6463

64+
import okhttp3.Call;
65+
import okhttp3.Callback;
6566
import okhttp3.OkHttpClient;
6667
import okhttp3.Request;
68+
import okhttp3.Response;
6769

6870
public class SettingsActivity extends FoxActivity implements LanguageActivity {
6971
private static final int LANGUAGE_SUPPORT_LEVEL = 1;
@@ -286,48 +288,42 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
286288
// Set the summary to the current androidacy_api_token
287289
prefAndroidacyRepoApiKey.setSummary(MainApplication.getSharedPreferences()
288290
.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
295292
prefAndroidacyRepoApiKey.setOnPreferenceChangeListener((preference, newValue) -> {
296293
String newToken = String.valueOf(newValue);
297-
if (newToken.isEmpty()) {
294+
if (newToken.length() == 0) {
298295
MainApplication.getSharedPreferences().edit()
299296
.remove("androidacy_api_token").apply();
300297
return true;
301298
}
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();
323312
});
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());
328313
}
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;
331327
});
332328
findPreference("pref_support").setOnPreferenceClickListener(p -> {
333329
devModeStep = 0;

app/src/main/res/values/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,4 +161,6 @@
161161
<string name="api_key_invalid">Could not validate API key. Please try again.</string>
162162
<string name="api_key_valid">API key is valid.</string>
163163
<string name="checking_api_key">Validating API key...</string>
164+
<string name="validating_api_key">Validating API key...</string>
165+
<string name="please_wait">Please wait</string>
164166
</resources>

app/src/main/res/xml/repo_preferences.xml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto">
2+
<PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto"
3+
xmlns:android="http://schemas.android.com/apk/res/android">
34
<PreferenceCategory
45
app:key="pref_magisk_alt_repo"
56
app:title="@string/loading">
@@ -65,7 +66,10 @@
6566
app:icon="@drawable/ic_baseline_security_24"
6667
app:title="@string/api_key"
6768
app:summary="@string/api_key_summary"
68-
app:singleLineTitle="false" />
69+
app:singleLineTitle="false"
70+
app:dialogTitle="@string/api_key"
71+
app:dialogMessage="@string/api_key_summary"
72+
android:imeOptions="actionDone" />
6973
<SwitchPreferenceCompat
7074
app:defaultValue="false"
7175
app:key="pref_androidacy_test_mode"

0 commit comments

Comments
 (0)