From 30f963b05f5feb46d8a620e7acf8fc263e267844 Mon Sep 17 00:00:00 2001 From: M66B Date: Sat, 11 Aug 2018 14:00:37 +0000 Subject: [PATCH] Simple loader fixes --- .../java/eu/faircode/email/ActivityView.java | 6 ++-- .../java/eu/faircode/email/SimpleLoader.java | 29 +++++++++++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/ActivityView.java b/app/src/main/java/eu/faircode/email/ActivityView.java index f6cad41c..c610d535 100644 --- a/app/src/main/java/eu/faircode/email/ActivityView.java +++ b/app/src/main/java/eu/faircode/email/ActivityView.java @@ -184,9 +184,9 @@ public class ActivityView extends ActivityBase implements FragmentManager.OnBack new SimpleLoader() { @Override public Long onLoad(Bundle args) throws Throwable { - File file = new File(getCacheDir(), "crash.log"); + File file = new File(getContext().getCacheDir(), "crash.log"); if (file.exists()) { - DB db = DB.getInstance(ActivityView.this); + DB db = DB.getInstance(getContext()); EntityFolder drafts = db.folder().getPrimaryDrafts(); if (drafts != null) { Address to = new InternetAddress("marcel+email@faircode.eu", "FairCode"); @@ -352,7 +352,7 @@ public class ActivityView extends ActivityBase implements FragmentManager.OnBack @Override public Object onLoad(Bundle args) { long time = args.getLong("time"); - DaoAccount dao = DB.getInstance(ActivityView.this).account(); + DaoAccount dao = DB.getInstance(getContext()).account(); for (EntityAccount account : dao.getAccounts(true)) { account.seen_until = time; dao.updateAccount(account); diff --git a/app/src/main/java/eu/faircode/email/SimpleLoader.java b/app/src/main/java/eu/faircode/email/SimpleLoader.java index a30688c4..75fc6e03 100644 --- a/app/src/main/java/eu/faircode/email/SimpleLoader.java +++ b/app/src/main/java/eu/faircode/email/SimpleLoader.java @@ -1,5 +1,24 @@ package eu.faircode.email; +/* + This file is part of Safe email. + + Safe email is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + NetGuard is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with NetGuard. If not, see . + + Copyright 2018 by Marcel Bokhorst (M66B) +*/ + import android.content.Context; import android.os.Bundle; import android.util.Log; @@ -10,6 +29,11 @@ import androidx.loader.app.LoaderManager; import androidx.loader.content.AsyncTaskLoader; import androidx.loader.content.Loader; +// +// This simple loader is simple to use, but it is also simple to cause bugs that can easily lead to crashes +// Make sure to not access any member in any outer scope from onLoad +// + public abstract class SimpleLoader { private Context context; private LoaderManager manager; @@ -27,6 +51,7 @@ public abstract class SimpleLoader { } public T onLoad(Bundle args) throws Throwable { + // Be careful not to access members in outer scopes return null; } @@ -36,6 +61,10 @@ public abstract class SimpleLoader { public void onException(Bundle args, Throwable ex) { } + protected Context getContext() { + return context; + } + private static class CommonLoader extends AsyncTaskLoader { Bundle args; SimpleLoader loader;