bring back the old SharedPreferences for CustomContext, implement Toast

This commit is contained in:
Aria Moradi
2021-07-31 02:10:48 +04:30
parent 1ed9bcf7c8
commit f18ca5811f
6 changed files with 114 additions and 13 deletions
@@ -38,7 +38,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import xyz.nulldev.androidcompat.info.ApplicationInfoImpl;
import xyz.nulldev.androidcompat.io.AndroidFiles;
import xyz.nulldev.androidcompat.io.sharedprefs.JavaSharedPreferences;
import xyz.nulldev.androidcompat.io.sharedprefs.JsonSharedPreferences;
import xyz.nulldev.androidcompat.service.ServiceSupport;
import xyz.nulldev.androidcompat.util.KodeinGlobalHelper;
@@ -50,9 +50,10 @@ import java.util.Map;
/**
* Custom context implementation.
*
* TODO Deal with packagemanager for extension sources
*/
public class CustomContext extends Context implements DIAware {
private final DI kodein;
private DI kodein;
public CustomContext() {
this(KodeinGlobalHelper.kodein());
}
@@ -164,22 +165,23 @@ public class CustomContext extends Context implements DIAware {
/** Fake shared prefs! **/
private Map<String, SharedPreferences> prefs = new HashMap<>(); //Cache
private File sharedPrefsFileFromString(String s) {
return new File(androidFiles.getPrefsDir(), s + ".json");
}
@Override
public synchronized SharedPreferences getSharedPreferences(String s, int i) {
SharedPreferences preferences = prefs.get(s);
//Create new shared preferences if one does not exist
if(preferences == null) {
preferences = new JavaSharedPreferences(s);
preferences = getSharedPreferences(sharedPrefsFileFromString(s), i);
prefs.put(s, preferences);
}
return preferences;
}
@Override
public SharedPreferences getSharedPreferences(@NotNull File file, int mode) {
String path = file.getAbsolutePath().replace('\\', '/');
int firstSlash = path.indexOf("/");
return new JavaSharedPreferences(path.substring(firstSlash));
public SharedPreferences getSharedPreferences(File file, int mode) {
return new JsonSharedPreferences(file);
}
@Override
@@ -189,8 +191,8 @@ public class CustomContext extends Context implements DIAware {
@Override
public boolean deleteSharedPreferences(String name) {
JavaSharedPreferences item = (JavaSharedPreferences) prefs.remove(name);
return item.deleteAll();
prefs.remove(name);
return sharedPrefsFileFromString(name).delete();
}
@Override
@@ -733,4 +735,3 @@ public class CustomContext extends Context implements DIAware {
}
}
@@ -26,6 +26,8 @@ class AndroidFiles(val configManager: ConfigManager = GlobalConfigManager) {
val downloadCacheDir: File get() = registerFile(filesConfig.downloadCacheDir)
val databasesDir: File get() = registerFile(filesConfig.databasesDir)
val prefsDir: File get() = registerFile(filesConfig.prefsDir)
val packagesDir: File get() = registerFile(filesConfig.packageDir)
fun registerFile(file: String): File {
@@ -1,5 +1,12 @@
package xyz.nulldev.androidcompat.io.sharedprefs
/*
* Copyright (C) Contributors to the Suwayomi project
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
import android.content.SharedPreferences
import com.russhwolf.settings.ExperimentalSettingsApi
import com.russhwolf.settings.ExperimentalSettingsImplementation
@@ -233,7 +233,7 @@ public class JsonSharedPreferences implements SharedPreferences {
private JsonSharedPreferencesEditor() {
}
private void recordChange(String key) {
private void recordChange(String key) {
if (!affectedKeys.contains(key)) {
affectedKeys.add(key);
}