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

Commit 3bfa064

Browse files
committed
Add Androidacy test mode.
1 parent da70e11 commit 3bfa064

File tree

7 files changed

+56
-16
lines changed

7 files changed

+56
-16
lines changed

app/src/main/java/com/fox2code/mmm/MainApplication.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,11 @@ public static boolean isBackgroundUpdateCheckEnabled() {
158158
return getSharedPreferences().getBoolean("pref_background_update_check", true);
159159
}
160160

161+
public static boolean isAndroidacyTestMode() {
162+
return isDeveloper() &&
163+
getSharedPreferences().getBoolean("pref_androidacy_test_mode", false);
164+
}
165+
161166
public static boolean isFirstBoot() {
162167
return firstBoot;
163168
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
7676
this.forceBackPressed();
7777
return;
7878
}
79-
if (!url.endsWith(AndroidacyUtil.REFERRER)) {
79+
if (!url.contains(AndroidacyUtil.REFERRER)) {
8080
if (url.lastIndexOf('/') < url.lastIndexOf('?')) {
8181
url = url + '&' + AndroidacyUtil.REFERRER;
8282
} else {

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

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import android.util.Log;
55
import android.webkit.CookieManager;
66

7+
import androidx.annotation.NonNull;
8+
79
import com.fox2code.mmm.R;
810
import com.fox2code.mmm.manager.ModuleInfo;
911
import com.fox2code.mmm.repo.RepoData;
@@ -40,11 +42,14 @@ public class AndroidacyRepoData extends RepoData {
4042
// Avoid spamming requests to Androidacy
4143
private long androidacyBlockade = 0;
4244
private String token = null;
45+
private final boolean testMode;
46+
private final String host;
4347

44-
public AndroidacyRepoData(String url, File cacheRoot,
45-
SharedPreferences cachedPreferences) {
46-
super(url, cacheRoot, cachedPreferences);
47-
if (this.metaDataCache.exists()) {
48+
public AndroidacyRepoData(File cacheRoot, SharedPreferences cachedPreferences,
49+
boolean testMode) {
50+
super(testMode ? RepoManager.ANDROIDACY_TEST_MAGISK_REPO_ENDPOINT :
51+
RepoManager.ANDROIDACY_MAGISK_REPO_ENDPOINT, cacheRoot, cachedPreferences);
52+
if (this.metaDataCache.exists() && !testMode) {
4853
this.androidacyBlockade = this.metaDataCache.lastModified() + 30_000L;
4954
if (this.androidacyBlockade - 60_000L > System.currentTimeMillis()) {
5055
this.androidacyBlockade = 0; // Don't allow time travel. Well why not???
@@ -55,6 +60,8 @@ public AndroidacyRepoData(String url, File cacheRoot,
5560
this.defaultSupport = "https://t.me/androidacy_discussions";
5661
this.defaultDonate = "https://patreon.com/androidacy";
5762
this.defaultSubmitModule = "https://www.androidacy.com/module-repository-applications/";
63+
this.host = testMode ? "staging-api.androidacy.com" : "api.androidacy.com";
64+
this.testMode = testMode;
5865
}
5966

6067
private static String getCookies() {
@@ -89,7 +96,7 @@ protected boolean prepare() {
8996
}
9097
if (token != null) {
9198
try {
92-
Http.doHttpGet("https://api.androidacy.com/auth/me?token=" + token, true);
99+
Http.doHttpGet("https://" + this.host + "/auth/me?token=" + token, true);
93100
} catch (Exception e) {
94101
if ("Received error code: 419".equals(e.getMessage()) ||
95102
"Received error code: 429".equals(e.getMessage())) {
@@ -113,7 +120,7 @@ protected boolean prepare() {
113120
try {
114121
Log.i(TAG, "Refreshing token...");
115122
token = new String(Http.doHttpPost(
116-
"https://api.androidacy.com/auth/register",
123+
"https://" + this.host + "/auth/register",
117124
"",true), StandardCharsets.UTF_8);
118125
if (Http.hasWebView()) {
119126
CookieManager.getInstance().setCookie("https://.androidacy.com/",
@@ -184,11 +191,11 @@ protected List<RepoModule> populate(JSONObject jsonObject) throws JSONException
184191
jsonObject.optString("notesUrl", ""));
185192
if (repoModule.zipUrl == null) {
186193
repoModule.zipUrl = // Fallback url in case the API doesn't have zipUrl
187-
"https://api.androidacy.com/magisk/info/?module=" + moduleId;
194+
"https://" + this.host + "/magisk/info/?module=" + moduleId;
188195
}
189196
if (repoModule.notesUrl == null) {
190197
repoModule.notesUrl = // Fallback url in case the API doesn't have notesUrl
191-
"https://api.androidacy.com/magisk/readme/?module=" + moduleId;
198+
"https://" + this.host + "/magisk/readme/?module=" + moduleId;
192199
}
193200
repoModule.zipUrl = this.injectToken(repoModule.zipUrl);
194201
repoModule.notesUrl = this.injectToken(repoModule.notesUrl);
@@ -286,4 +293,10 @@ private String injectToken(String url) {
286293
}
287294
return url;
288295
}
296+
297+
@NonNull
298+
@Override
299+
public String getName() {
300+
return this.testMode ? super.getName() + " (Test Mode)" : super.getName();
301+
}
289302
}

app/src/main/java/com/fox2code/mmm/repo/RepoManager.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import android.content.SharedPreferences;
55
import android.util.Log;
66

7+
import com.fox2code.mmm.MainActivity;
78
import com.fox2code.mmm.MainApplication;
89
import com.fox2code.mmm.XHooks;
910
import com.fox2code.mmm.androidacy.AndroidacyRepoData;
@@ -37,6 +38,8 @@ public final class RepoManager {
3738

3839
public static final String ANDROIDACY_MAGISK_REPO_ENDPOINT =
3940
"https://api.androidacy.com/magisk/repo";
41+
public static final String ANDROIDACY_TEST_MAGISK_REPO_ENDPOINT =
42+
"https://staging-api.androidacy.com/magisk/repo";
4043
public static final String ANDROIDACY_MAGISK_REPO_HOMEPAGE =
4144
"https://www.androidacy.com/modules-repo";
4245

@@ -125,11 +128,18 @@ public RepoData addOrGet(String url) {
125128
}
126129

127130
public RepoData addOrGet(String url, String fallBackName) {
131+
if (MAGISK_ALT_REPO_JSDELIVR.equals(url))
132+
url = MAGISK_ALT_REPO;
133+
if (DG_MAGISK_REPO.equals(url))
134+
url = DG_MAGISK_REPO_GITHUB;
128135
RepoData repoData;
129136
synchronized (this.repoUpdateLock) {
130137
repoData = this.repoData.get(url);
131138
if (repoData == null) {
132-
if (ANDROIDACY_MAGISK_REPO_ENDPOINT.equals(url)) {
139+
if (ANDROIDACY_TEST_MAGISK_REPO_ENDPOINT.equals(url) ||
140+
ANDROIDACY_MAGISK_REPO_ENDPOINT.equals(url)) {
141+
if (this.androidacyRepoData != null)
142+
return this.androidacyRepoData;
133143
return this.addAndroidacyRepoData();
134144
} else {
135145
return this.addRepoData(url, fallBackName);
@@ -276,6 +286,7 @@ public static String internalIdOfUrl(String url) {
276286
case MAGISK_ALT_REPO_JSDELIVR:
277287
return "magisk_alt_repo";
278288
case ANDROIDACY_MAGISK_REPO_ENDPOINT:
289+
case ANDROIDACY_TEST_MAGISK_REPO_ENDPOINT:
279290
return "androidacy_repo";
280291
case DG_MAGISK_REPO:
281292
case DG_MAGISK_REPO_GITHUB:
@@ -291,6 +302,7 @@ static boolean isBuiltInRepo(String repo) {
291302
case RepoManager.MAGISK_ALT_REPO:
292303
case RepoManager.MAGISK_ALT_REPO_JSDELIVR:
293304
case RepoManager.ANDROIDACY_MAGISK_REPO_ENDPOINT:
305+
case RepoManager.ANDROIDACY_TEST_MAGISK_REPO_ENDPOINT:
294306
case RepoManager.DG_MAGISK_REPO:
295307
case RepoManager.DG_MAGISK_REPO_GITHUB:
296308
return true;
@@ -299,10 +311,6 @@ static boolean isBuiltInRepo(String repo) {
299311
}
300312

301313
private RepoData addRepoData(String url, String fallBackName) {
302-
if (MAGISK_ALT_REPO_JSDELIVR.equals(url))
303-
url = MAGISK_ALT_REPO;
304-
if (DG_MAGISK_REPO.equals(url))
305-
url = DG_MAGISK_REPO_GITHUB;
306314
String id = internalIdOfUrl(url);
307315
File cacheRoot = new File(this.mainApplication.getCacheDir(), id);
308316
SharedPreferences sharedPreferences = this.mainApplication
@@ -335,9 +343,10 @@ private AndroidacyRepoData addAndroidacyRepoData() {
335343
File cacheRoot = new File(this.mainApplication.getCacheDir(), "androidacy_repo");
336344
SharedPreferences sharedPreferences = this.mainApplication
337345
.getSharedPreferences("mmm_androidacy_repo", Context.MODE_PRIVATE);
338-
AndroidacyRepoData repoData = new AndroidacyRepoData(
339-
ANDROIDACY_MAGISK_REPO_ENDPOINT, cacheRoot, sharedPreferences);
346+
AndroidacyRepoData repoData = new AndroidacyRepoData(cacheRoot,
347+
sharedPreferences, MainApplication.isAndroidacyTestMode());
340348
this.repoData.put(ANDROIDACY_MAGISK_REPO_ENDPOINT, repoData);
349+
this.repoData.put(ANDROIDACY_TEST_MAGISK_REPO_ENDPOINT, repoData);
341350
return repoData;
342351
}
343352

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,10 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
325325
setRepoData(RepoManager.ANDROIDACY_MAGISK_REPO_ENDPOINT);
326326
setRepoData(RepoManager.DG_MAGISK_REPO_GITHUB);
327327
updateCustomRepoList(true);
328+
if (!MainApplication.isDeveloper()) {
329+
Objects.requireNonNull((Preference) findPreference(
330+
"pref_androidacy_test_mode")).setVisible(false);
331+
}
328332
}
329333

330334
@SuppressLint("RestrictedApi")

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@
133133
<string name="backup_module_list">Backup modules</string>
134134
<string name="restore_module_list">Restore modules</string>
135135
<string name="require_internet">This operation require an internet connection</string>
136+
<string name="androidacy_test_mode_pref">Androidacy test mode</string>
137+
<string name="androidacy_test_mode_desc">Use staging Androidacy Endpoint instead of release endpoint. (Require app process restart)</string>
136138

137139
<!-- Background Notification translation -->
138140
<string name="notification_update_title">Found %i module updates</string>

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@
5959
app:icon="@drawable/ic_baseline_upload_file_24"
6060
app:title="@string/submit_modules"
6161
app:singleLineTitle="false" />
62+
<SwitchPreferenceCompat
63+
app:defaultValue="false"
64+
app:key="pref_androidacy_test_mode"
65+
app:icon="@drawable/ic_baseline_bug_report_24"
66+
app:title="@string/androidacy_test_mode_pref"
67+
app:summary="@string/androidacy_test_mode_desc"
68+
app:singleLineTitle="false" />
6269
<Preference
6370
app:key="pref_androidacy_ads_disclaimer"
6471
app:icon="@drawable/ic_baseline_info_24"

0 commit comments

Comments
 (0)