Added option to disable list caching, dynamically remove lists on low mem

pull/156/head
M66B 6 years ago
parent 0781549a38
commit 36c691eb25

@ -19,6 +19,8 @@ package eu.faircode.email;
Copyright 2018-2019 by Marcel Bokhorst (M66B)
*/
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
@ -50,6 +52,8 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc
private SwitchCompat swAuthentication;
private SwitchCompat swParanoid;
private TextView tvParanoidHint;
private SwitchCompat swCacheLists;
private TextView tvCacheListsHint;
private SwitchCompat swWatchdog;
private SwitchCompat swUpdates;
private SwitchCompat swCrashReports;
@ -60,7 +64,7 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc
private Group grpSearchLocal;
private final static String[] RESET_OPTIONS = new String[]{
"badge", "subscriptions", "english", "authentication", "paranoid", "watchdog", "updates", "crash_reports", "debug"
"badge", "subscriptions", "english", "authentication", "paranoid", "cache_lists", "watchdog", "updates", "crash_reports", "debug"
};
private final static String[] RESET_QUESTIONS = new String[]{
@ -83,6 +87,8 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc
swAuthentication = view.findViewById(R.id.swAuthentication);
swParanoid = view.findViewById(R.id.swParanoid);
tvParanoidHint = view.findViewById(R.id.tvParanoidHint);
swCacheLists = view.findViewById(R.id.swCacheLists);
tvCacheListsHint = view.findViewById(R.id.tvCacheListsHint);
swWatchdog = view.findViewById(R.id.swWatchdog);
swUpdates = view.findViewById(R.id.swUpdates);
swCrashReports = view.findViewById(R.id.swCrashReports);
@ -148,6 +154,14 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc
});
}
swCacheLists.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("cache_lists", checked).apply();
WorkerWatchdog.init(getContext());
}
});
swWatchdog.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
@ -238,12 +252,18 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc
swEnglish.setChecked(prefs.getBoolean("english", false));
swAuthentication.setChecked(prefs.getBoolean("authentication", false));
swParanoid.setChecked(prefs.getBoolean("paranoid", true));
swCacheLists.setChecked(prefs.getBoolean("cache_lists", true));
swWatchdog.setChecked(prefs.getBoolean("watchdog", true));
swUpdates.setChecked(prefs.getBoolean("updates", true));
swUpdates.setVisibility(Helper.isPlayStoreInstall(getContext()) ? View.GONE : View.VISIBLE);
swCrashReports.setChecked(prefs.getBoolean("crash_reports", false));
swDebug.setChecked(prefs.getBoolean("debug", false));
ActivityManager am = (ActivityManager) getContext().getSystemService(Context.ACTIVITY_SERVICE);
int class_mb = am.getMemoryClass();
tvCacheListsHint.setText(getString(R.string.title_advanced_cache_list_hint, class_mb + " MB"));
grpSearchLocal.setVisibility(Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.M ? View.GONE : View.VISIBLE);
}

@ -51,9 +51,10 @@ public class ViewModelMessages extends ViewModel {
private static final int LOCAL_PAGE_SIZE = 100;
private static final int REMOTE_PAGE_SIZE = 10;
private static final int LOW_MEM_MB = 64;
Model getModel(
Context context, final LifecycleOwner owner,
final Context context, final LifecycleOwner owner,
final AdapterMessage.ViewType viewType,
long account, long folder, String thread, long id,
String query, boolean server) {
@ -153,8 +154,21 @@ public class ViewModelMessages extends ViewModel {
public void onDestroyed() {
Log.i("Destroy model " + viewType);
if (viewType == AdapterMessage.ViewType.THREAD)
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean cache = prefs.getBoolean("cache_lists", true);
Runtime rt = Runtime.getRuntime();
long used = (rt.totalMemory() - rt.freeMemory());
long max = rt.maxMemory();
long free_mb = (max - used) / 1024L / 1024L;
boolean lowmem = (free_mb < LOW_MEM_MB);
if (viewType == AdapterMessage.ViewType.THREAD || !cache || lowmem) {
Log.i("Remove model " + viewType +
" cache=" + cache + " lowmem=" + lowmem + " free=" + free_mb + " MB");
remove(viewType);
}
dump();
}

@ -127,6 +127,28 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/swParanoid" />
<androidx.appcompat.widget.SwitchCompat
android:id="@+id/swCacheLists"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:text="@string/title_advanced_cache_lists"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvParanoidHint"
app:switchPadding="12dp" />
<TextView
android:id="@+id/tvCacheListsHint"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="48dp"
android:text="@string/title_advanced_cache_list_hint"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
android:textStyle="italic"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/swCacheLists" />
<androidx.appcompat.widget.SwitchCompat
android:id="@+id/swWatchdog"
android:layout_width="match_parent"
@ -134,7 +156,7 @@
android:layout_marginTop="12dp"
android:text="@string/title_advanced_watchdog"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvParanoidHint"
app:layout_constraintTop_toBottomOf="@id/tvCacheListsHint"
app:switchPadding="12dp" />
<androidx.appcompat.widget.SwitchCompat

@ -223,6 +223,7 @@
<string name="title_advanced_english">Force English language</string>
<string name="title_advanced_authentication">Show a warning when the receiving server could not authenticate the message</string>
<string name="title_advanced_paranoid">Extra privacy features</string>
<string name="title_advanced_cache_lists">Cache message lists</string>
<string name="title_advanced_watchdog">Periodically check if FairEmail is still active</string>
<string name="title_advanced_updates">Check for updates</string>
<string name="title_advanced_crash_reports">Send error reports</string>
@ -250,6 +251,7 @@
<string name="title_advanced_badge_hint">Only available on supported launchers</string>
<string name="title_advanced_english_hint">This will restart the app</string>
<string name="title_advanced_paranoid_hint">See the FAQ for details</string>
<string name="title_advanced_cache_list_hint">Memory available: %1$s</string>
<string name="title_advanced_debug_hint">Enable extra logging and show debug information at various places</string>
<string name="title_select">Select &#8230;</string>

Loading…
Cancel
Save