From 9bebf1d32f2b0449d282d9fcd86dca1a94784ccf Mon Sep 17 00:00:00 2001 From: cpfeiffer Date: Thu, 24 Nov 2016 21:11:28 +0100 Subject: [PATCH] When memory is really low, free up some memory #436 (although we probably can't save much) --- .../gadgetbridge/GBApplication.java | 28 +++++++++++++++++++ .../gadgetbridge/database/DBHelper.java | 10 +++++++ 2 files changed, 38 insertions(+) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java index 984f03ec..c3e7d447 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java @@ -22,6 +22,7 @@ import android.util.TypedValue; import java.io.File; import java.io.IOException; import java.util.HashSet; +import java.util.List; import java.util.concurrent.TimeUnit; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; @@ -32,9 +33,11 @@ import nodomain.freeyourgadget.gadgetbridge.database.DBHelper; import nodomain.freeyourgadget.gadgetbridge.database.DBOpenHelper; import nodomain.freeyourgadget.gadgetbridge.devices.DeviceManager; import nodomain.freeyourgadget.gadgetbridge.entities.DaoMaster; +import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceService; import nodomain.freeyourgadget.gadgetbridge.model.ActivityUser; import nodomain.freeyourgadget.gadgetbridge.model.DeviceService; +import nodomain.freeyourgadget.gadgetbridge.util.DeviceHelper; import nodomain.freeyourgadget.gadgetbridge.util.FileUtils; import nodomain.freeyourgadget.gadgetbridge.util.GB; import nodomain.freeyourgadget.gadgetbridge.util.GBPrefs; @@ -131,6 +134,31 @@ public class GBApplication extends Application { } } + @Override + public void onTrimMemory(int level) { + super.onTrimMemory(level); + if (level >= TRIM_MEMORY_BACKGROUND) { + if (!hasBusyDevice()) { + DBHelper.clearSession(); + } + } + } + + /** + * Returns true if at least a single device is busy, e.g synchronizing activity data + * or something similar. + * Note: busy is not the same as connected or initialized! + */ + private boolean hasBusyDevice() { + List devices = getDeviceManager().getDevices(); + for (GBDevice device: devices) { + if (device.isBusy()) { + return true; + } + } + return true; + } + public static void setupLogging(boolean enabled) { logging.setupLogging(enabled); } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/database/DBHelper.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/database/DBHelper.java index 0794131d..3ec0ec6c 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/database/DBHelper.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/database/DBHelper.java @@ -26,6 +26,7 @@ import de.greenrobot.dao.query.Query; import de.greenrobot.dao.query.QueryBuilder; import de.greenrobot.dao.query.WhereCondition; import nodomain.freeyourgadget.gadgetbridge.GBApplication; +import nodomain.freeyourgadget.gadgetbridge.GBException; import nodomain.freeyourgadget.gadgetbridge.devices.AbstractSampleProvider; import nodomain.freeyourgadget.gadgetbridge.devices.DeviceCoordinator; import nodomain.freeyourgadget.gadgetbridge.devices.pebble.PebbleHealthSampleProvider; @@ -685,4 +686,13 @@ public class DBHelper { } return cursor.getInt(columnIndex); } + + public static void clearSession() { + try (DBHandler dbHandler = GBApplication.acquireDB()) { + DaoSession session = dbHandler.getDaoSession(); + session.clear(); + } catch (Exception e) { + LOG.warn("Unable to acquire database to clear the session", e); + } + } }