From 53dbc5404114d5ec4de3abcb8a28f3befb9f6576 Mon Sep 17 00:00:00 2001 From: cpfeiffer Date: Sat, 25 Feb 2017 22:02:40 +0100 Subject: [PATCH] Only start the webview when needed --- .../gadgetbridge/GBApplication.java | 9 ++++++--- .../activities/BackgroundWebViewActivity.java | 5 ----- .../devices/AbstractDeviceCoordinator.java | 5 +++++ .../gadgetbridge/devices/DeviceCoordinator.java | 8 ++++++++ .../devices/pebble/PebbleCoordinator.java | 5 +++++ .../gadgetbridge/util/DeviceHelper.java | 16 ++++++++++++++++ 6 files changed, 40 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java index 54fe09288..9195981b5 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java @@ -37,6 +37,7 @@ 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; @@ -136,9 +137,11 @@ public class GBApplication extends Application { } private void createWebViewActivity() { - Intent intent = new Intent(getContext(), BackgroundWebViewActivity.class); - intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - startActivity(intent); + if (DeviceHelper.getInstance().needsBackgroundWebView(this)) { + Intent intent = new Intent(getContext(), BackgroundWebViewActivity.class); + intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + startActivity(intent); + } } @Override diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/BackgroundWebViewActivity.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/BackgroundWebViewActivity.java index 7acd28ccc..efb88399c 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/BackgroundWebViewActivity.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/BackgroundWebViewActivity.java @@ -3,14 +3,9 @@ package nodomain.freeyourgadget.gadgetbridge.activities; import android.app.Activity; import android.os.Bundle; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import nodomain.freeyourgadget.gadgetbridge.util.WebViewSingleton; public class BackgroundWebViewActivity extends Activity { - private static Logger LOG = LoggerFactory.getLogger(BackgroundWebViewActivity.class); - @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/AbstractDeviceCoordinator.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/AbstractDeviceCoordinator.java index 46a65e09c..82ae6f59b 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/AbstractDeviceCoordinator.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/AbstractDeviceCoordinator.java @@ -103,4 +103,9 @@ public abstract class AbstractDeviceCoordinator implements DeviceCoordinator { } return false; } + + @Override + public boolean needsBackgroundWebView(GBDevice device) { + return false; + } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/DeviceCoordinator.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/DeviceCoordinator.java index 346446da0..aa606c589 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/DeviceCoordinator.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/DeviceCoordinator.java @@ -186,4 +186,12 @@ public interface DeviceCoordinator { * @return */ Class getAppsManagementActivity(); + + /** + * Returns true if the given device needs a background webview for + * executing javascript or configuration, for example. + * + * @param device + */ + boolean needsBackgroundWebView(GBDevice device); } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/pebble/PebbleCoordinator.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/pebble/PebbleCoordinator.java index 316967cb0..81d694b9d 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/pebble/PebbleCoordinator.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/pebble/PebbleCoordinator.java @@ -137,4 +137,9 @@ public class PebbleCoordinator extends AbstractDeviceCoordinator { public Class getAppsManagementActivity() { return AppManagerActivity.class; } + + @Override + public boolean needsBackgroundWebView(GBDevice device) { + return true; + } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/DeviceHelper.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/DeviceHelper.java index 82791f62c..514e599e9 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/DeviceHelper.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/DeviceHelper.java @@ -255,4 +255,20 @@ public class DeviceHelper { } return false; } + + /** + * Returns true if the background webview for executing javascript is needed + * for any of the known/available devices. + * @param context + */ + public boolean needsBackgroundWebView(Context context) { + Set availableDevices = getAvailableDevices(context); + for (GBDevice device : availableDevices) { + DeviceCoordinator coordinator = getCoordinator(device); + if (coordinator.needsBackgroundWebView(device)) { + return true; + } + } + return false; + } }