(fix) fix adding and removing and showing custom repos

known issues: after adding and removing one, the add button may stop showing

custom repos and alt repo no longer use cache due to errors in storing prop values

etc

Signed-off-by: androidacy-user <opensource@androidacy.com>
This commit is contained in:
androidacy-user 2023-04-23 11:57:12 -04:00
parent 173422e9e1
commit 27fefbf3cf
4 changed files with 42 additions and 12 deletions

View File

@ -14,6 +14,7 @@ import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicBoolean;
import io.realm.Realm;
@ -45,13 +46,13 @@ public class RepoUpdater {
return 0;
}
// if we shouldn't update, get the values from the ModuleListCache realm
if (!this.repoData.shouldUpdate()) {
if (!this.repoData.shouldUpdate() && Objects.equals(this.repoData.id, "androidacy_repo")) { // for now, only enable cache reading for androidacy repo, until we handle storing module prop file values in cache
Timber.d("Fetching index from cache for %s", this.repoData.id);
File cacheRoot = MainApplication.getINSTANCE().getDataDirWithPath("realms/repos/" + this.repoData.id);
RealmConfiguration realmConfiguration = new RealmConfiguration.Builder().name("ModuleListCache.realm").encryptionKey(MainApplication.getINSTANCE().getKey()).schemaVersion(1).deleteRealmIfMigrationNeeded().allowWritesOnUiThread(true).allowQueriesOnUiThread(true).directory(cacheRoot).build();
Realm realm = Realm.getInstance(realmConfiguration);
RealmResults<ModuleListCache> results = realm.where(ModuleListCache.class).equalTo("repoId", this.repoData.id).findAll();
// reposlist realm
// repos-list realm
RealmConfiguration realmConfiguration2 = new RealmConfiguration.Builder().name("ReposList.realm").encryptionKey(MainApplication.getINSTANCE().getKey()).allowQueriesOnUiThread(true).allowWritesOnUiThread(true).directory(MainApplication.getINSTANCE().getDataDirWithPath("realms")).schemaVersion(1).build();
Realm realm2 = Realm.getInstance(realmConfiguration2);
this.toUpdate = Collections.emptyList();

View File

@ -1162,15 +1162,18 @@ public class SettingsActivity extends FoxActivity implements LanguageActivity {
ArrayList<String> customRepos = new ArrayList<>();
RealmResults<ReposList> customRepoDataDB = realm.where(ReposList.class).findAll();
for (ReposList repo : customRepoDataDB) {
if (!repo.getId().equals("androidacy") && !repo.getId().equals("magisk_alt_repo")) {
if (!repo.getId().equals("androidacy_repo") && !repo.getId().equals("magisk_alt_repo")) {
CUSTOM_REPO_ENTRIES++;
customRepos.add(repo.getId());
customRepos.add(repo.getUrl());
}
}
Timber.d("%d repos: %s", CUSTOM_REPO_ENTRIES, customRepos);
final CustomRepoManager customRepoManager = RepoManager.getINSTANCE().getCustomRepoManager();
for (int i = 0; i < CUSTOM_REPO_ENTRIES; i++) {
// get the id of the repo at current index in customRepos
CustomRepoData repoData = customRepoManager.getRepo(customRepos.get(i));
assert repoData != null;
Timber.d("RepoData for %d is %s", i, repoData.toString());
setRepoData(repoData, "pref_custom_repo_" + i);
if (initial) {
Preference preference = findPreference("pref_custom_repo_" + i + "_delete");
@ -1185,10 +1188,29 @@ public class SettingsActivity extends FoxActivity implements LanguageActivity {
realm.commitTransaction();
customRepoManager.removeRepo(index);
updateCustomRepoList(false);
preference1.setVisible(false);
return true;
});
}
}
// any custom repo prefs larger than the number of custom repos need to be hidden. max is 5
// loop up until 5, and for each that's greater than the number of custom repos, hide it. we start at 0
// if custom repos is zero, just hide them all
if (CUSTOM_REPO_ENTRIES == 0) {
for (int i = 0; i < 5; i++) {
Preference preference = findPreference("pref_custom_repo_" + i);
if (preference == null) continue;
preference.setVisible(false);
}
} else {
for (int i = 0; i < 5; i++) {
Preference preference = findPreference("pref_custom_repo_" + i);
if (preference == null) continue;
if (i >= CUSTOM_REPO_ENTRIES) {
preference.setVisible(false);
}
}
}
Preference preference = findPreference("pref_custom_add_repo");
if (preference == null) return;
preference.setVisible(customRepoManager.canAddRepo() && customRepoManager.getRepoCount() < CUSTOM_REPO_ENTRIES);

View File

@ -37,6 +37,7 @@ import java.util.zip.ZipOutputStream;
import timber.log.Timber;
/** @noinspection ResultOfMethodCallIgnored*/
public enum Files {
;
private static final boolean is64bit = Build.SUPPORTED_64_BIT_ABIS.length > 0;
@ -44,7 +45,7 @@ public enum Files {
// stolen from https://stackoverflow.com/a/25005243
public static @NonNull String getFileName(Context context, Uri uri) {
String result = null;
if (uri.getScheme().equals("content")) {
if (Objects.equals(uri.getScheme(), "content")) {
try (Cursor cursor = context.getContentResolver().query(uri, null, null, null, null)) {
if (cursor != null && cursor.moveToFirst()) {
int index = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
@ -56,7 +57,7 @@ public enum Files {
}
if (result == null) {
result = uri.getPath();
int cut = result.lastIndexOf('/');
int cut = Objects.requireNonNull(result).lastIndexOf('/');
if (cut != -1) {
result = result.substring(cut + 1);
}
@ -69,10 +70,10 @@ public enum Files {
Long result = null;
try {
String scheme = uri.getScheme();
if (scheme.equals("content")) {
if (Objects.equals(scheme, "content")) {
Cursor returnCursor = context.getContentResolver().
query(uri, null, null, null, null);
int sizeIndex = returnCursor.getColumnIndex(OpenableColumns.SIZE);
int sizeIndex = Objects.requireNonNull(returnCursor).getColumnIndex(OpenableColumns.SIZE);
returnCursor.moveToFirst();
long size = returnCursor.getLong(sizeIndex);
@ -80,8 +81,8 @@ public enum Files {
result = size;
}
if (scheme.equals("file")) {
result = new File(uri.getPath()).length();
if (Objects.equals(scheme, "file")) {
result = new File(Objects.requireNonNull(uri.getPath())).length();
}
} catch (Exception e) {
Timber.e(Log.getStackTraceString(e));
@ -91,6 +92,8 @@ public enum Files {
}
public static void write(File file, byte[] bytes) throws IOException {
// make the dir if necessary
Objects.requireNonNull(file.getParentFile()).mkdirs();
try (OutputStream outputStream = new FileOutputStream(file)) {
outputStream.write(bytes);
outputStream.flush();
@ -104,6 +107,8 @@ public enum Files {
}
public static void writeSU(File file, byte[] bytes) throws IOException {
// make the dir if necessary
Objects.requireNonNull(file.getParentFile()).mkdirs();
try (OutputStream outputStream = SuFileOutputStream.open(file)) {
outputStream.write(bytes);
outputStream.flush();

View File

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">
<PreferenceScreen xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">
<PreferenceCategory
app:key="pref_androidacy_repo"
app:title="@string/androidacy_repo_name">
@ -8,7 +9,8 @@
app:key="pref_androidacy_repo_enabled"
app:singleLineTitle="false"
app:switchTextOff="@string/repo_disabled"
app:switchTextOn="@string/repo_enabled" />
app:switchTextOn="@string/repo_enabled"
tools:ignore="DuplicateSpeakableTextCheck" />
<!-- Initially hidden edittextpreference for pref_androidacy_api_token -->
<EditTextPreference
app:icon="@drawable/ic_baseline_vpn_key_24"