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

Commit fff580a

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

File tree

3 files changed

+88
-20
lines changed

3 files changed

+88
-20
lines changed

app/src/main/java/com/fox2code/mmm/androidacy/AndroidacyRepoData.java

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,28 @@ public static AndroidacyRepoData getInstance() {
6767
return RepoManager.getINSTANCE().getAndroidacyRepoData();
6868
}
6969

70+
public <string> boolean isValidToken(string token) {
71+
try {
72+
Http.doHttpGet("https://" + this.host + "/auth/me?token=" + token, false);
73+
} catch (Exception e) {
74+
if ("Received error code: 419".equals(e.getMessage()) ||
75+
"Received error code: 429".equals(e.getMessage())) {
76+
Log.e(TAG, "We are being rate limited!", e);
77+
long time = System.currentTimeMillis();
78+
this.androidacyBlockade = time + 3_600_000L;
79+
return false;
80+
}
81+
Log.w(TAG, "Invalid token, resetting...");
82+
// Remove saved preference
83+
SharedPreferences.Editor editor = this.cachedPreferences.edit();
84+
editor.remove("androidacy_api_token");
85+
editor.apply();
86+
return false;
87+
}
88+
// If status code is 200, we are good
89+
return true;
90+
}
91+
7092
@Override
7193
protected boolean prepare() {
7294
// Implementation details discussed on telegram
@@ -77,21 +99,9 @@ protected boolean prepare() {
7799
// Get token from androidacy_api_token shared preference
78100
String token = this.cachedPreferences.getString("androidacy_api_token", null);
79101
if (token != null) {
80-
try {
81-
Http.doHttpGet("https://" + this.host + "/auth/me?token=" + token, true);
82-
} catch (Exception e) {
83-
if ("Received error code: 419".equals(e.getMessage()) ||
84-
"Received error code: 429".equals(e.getMessage())) {
85-
Log.e(TAG, "We are being rate limited!", e);
86-
this.androidacyBlockade = time + 3_600_000L;
87-
return false;
88-
}
89-
Log.w(TAG, "Invalid token, resetting...");
90-
// Remove saved preference
91-
SharedPreferences.Editor editor = this.cachedPreferences.edit();
92-
editor.remove("androidacy_api_token");
93-
editor.apply();
94-
this.token = token = null;
102+
this.token = token;
103+
if (!isValidToken(token)) {
104+
token = null;
95105
}
96106
}
97107
if (token == null) {

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

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
import android.annotation.SuppressLint;
44
import android.app.AlarmManager;
55
import android.app.PendingIntent;
6+
import android.app.ProgressDialog;
67
import android.content.Context;
78
import android.content.Intent;
89
import android.content.SharedPreferences;
910
import android.os.Build;
1011
import android.os.Bundle;
12+
import android.text.InputType;
13+
import android.text.method.PasswordTransformationMethod;
1114
import android.util.Log;
1215
import android.widget.AutoCompleteTextView;
1316
import android.widget.Button;
@@ -19,6 +22,7 @@
1922
import androidx.fragment.app.Fragment;
2023
import androidx.fragment.app.FragmentActivity;
2124
import androidx.fragment.app.FragmentTransaction;
25+
import androidx.preference.EditTextPreference;
2226
import androidx.preference.ListPreference;
2327
import androidx.preference.Preference;
2428
import androidx.preference.PreferenceFragmentCompat;
@@ -51,12 +55,16 @@
5155
import com.topjohnwu.superuser.internal.UiThreadHandler;
5256

5357
import org.json.JSONException;
58+
import org.json.JSONObject;
5459

5560
import java.io.IOException;
5661
import java.util.HashSet;
5762
import java.util.Objects;
5863
import java.util.Random;
5964

65+
import okhttp3.OkHttpClient;
66+
import okhttp3.Request;
67+
6068
public class SettingsActivity extends FoxActivity implements LanguageActivity {
6169
private static final int LANGUAGE_SUPPORT_LEVEL = 1;
6270
private static final String TAG = "SettingsActivity";
@@ -270,11 +278,56 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
270278
"https://github.com/Fox2Code/FoxMagiskModuleManager");
271279
return true;
272280
});
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;
278331
});
279332
findPreference("pref_support").setOnPreferenceClickListener(p -> {
280333
devModeStep = 0;

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,4 +156,9 @@
156156
<string name="crash_reporting_desc">If you disable this, the developer will not get automatic bug reports, and this may make troubleshooting harder</string>
157157
<string name="api_key">Androidacy API Key</string>
158158
<string name="api_key_summary">Use a custom API key for Androidacy</string>
159+
<string name="api_key_not_set">Androidacy API key is empty</string>
160+
<string name="api_key_set">Current Androidacy API key</string>
161+
<string name="api_key_invalid">Could not validate API key. Please try again.</string>
162+
<string name="api_key_valid">API key is valid.</string>
163+
<string name="checking_api_key">Validating API key...</string>
159164
</resources>

0 commit comments

Comments
 (0)