From 79f3cad36d1f2dc3bd2fab7ddb3fe1d71ac5f2a5 Mon Sep 17 00:00:00 2001 From: Daniele Gobbetti Date: Mon, 24 Jul 2017 23:57:07 +0200 Subject: [PATCH] Pebble: some refinements to webview - rename the createWebView method to getInstance - remove the stateChangeListener after it has been fired once and remove obsolete code within - instantiate the jsInterface object only when needed - use the application context when possible to limit the usage of the mutableContextWrapper --- .../activities/BackgroundWebViewActivity.java | 2 +- .../activities/ExternalPebbleJSActivity.java | 19 +------------------ .../gadgetbridge/util/WebViewSingleton.java | 14 ++++++-------- 3 files changed, 8 insertions(+), 27 deletions(-) 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 efb88399c..d903fe0d9 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/BackgroundWebViewActivity.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/BackgroundWebViewActivity.java @@ -9,7 +9,7 @@ public class BackgroundWebViewActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - WebViewSingleton.createWebView(this); + WebViewSingleton.getInstance(this); finish(); } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ExternalPebbleJSActivity.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ExternalPebbleJSActivity.java index 1f443e968..cc70d4f7c 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ExternalPebbleJSActivity.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ExternalPebbleJSActivity.java @@ -25,7 +25,6 @@ import android.support.v4.app.NavUtils; import android.view.MenuItem; import android.view.View; import android.webkit.JavascriptInterface; -import android.webkit.ValueCallback; import android.webkit.WebView; import android.widget.FrameLayout; import android.widget.Toast; @@ -69,31 +68,15 @@ public class ExternalPebbleJSActivity extends GBActivity { FrameLayout fl = (FrameLayout) findViewById(R.id.webview_placeholder); fl.addView(myWebView); - myWebView.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() { @Override public void onViewAttachedToWindow(View v) { - v.setLayerType(View.LAYER_TYPE_HARDWARE, null); - //show configuration - moved to JS -// myWebView.evaluateJavascript("Pebble.evaluate('showConfiguration');", new ValueCallback() { -// @Override -// public void onReceiveValue(String s) { -// LOG.debug("Callback from showConfiguration: " + s); -// } -// }); } @Override public void onViewDetachedFromWindow(View v) { - myWebView.removeJavascriptInterface("GBActivity"); - myWebView.setWillNotDraw(true); - myWebView.evaluateJavascript("showStep('step1')", new ValueCallback() { - @Override - public void onReceiveValue(String s) { - LOG.debug("Callback from window detach: " + s); - } - }); + v.removeOnAttachStateChangeListener(this); FrameLayout fl = (FrameLayout) findViewById(R.id.webview_placeholder); fl.removeAllViews(); } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/WebViewSingleton.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/WebViewSingleton.java index 70b38f9f4..7042dd34e 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/WebViewSingleton.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/WebViewSingleton.java @@ -69,7 +69,7 @@ public class WebViewSingleton { private WebViewSingleton() { } - public static synchronized WebView createWebView(Activity context) { + public static synchronized WebView getInstance(Activity context) { if (instance == null) { webViewSingleton.contextWrapper = new MutableContextWrapper(context); webViewSingleton.mainLooper = context.getMainLooper(); @@ -90,7 +90,7 @@ public class WebViewSingleton { } public static void updateActivityContext(Activity context) { - if (context instanceof Activity) { + if (context != null) { webViewSingleton.contextWrapper.setBaseContext(context); } } @@ -101,12 +101,10 @@ public class WebViewSingleton { } public static void runJavascriptInterface(GBDevice device, UUID uuid) { - - final JSInterface jsInterface = new JSInterface(device, uuid); - if (uuid.equals(currentRunningUUID)) { LOG.debug("WEBVIEW uuid not changed keeping the old context"); } else { + final JSInterface jsInterface = new JSInterface(device, uuid); LOG.debug("WEBVIEW uuid changed, restarting"); currentRunningUUID = uuid; new Handler(webViewSingleton.mainLooper).post(new Runnable() { @@ -178,9 +176,9 @@ public class WebViewSingleton { this.timestamp = (System.currentTimeMillis() / 1000) - 86400; //let accessor know this value is really old - if (ActivityCompat.checkSelfPermission(webViewSingleton.contextWrapper, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED && + if (ActivityCompat.checkSelfPermission(GBApplication.getContext(), Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED && prefs.getBoolean("use_updated_location_if_available", false)) { - LocationManager locationManager = (LocationManager) webViewSingleton.contextWrapper.getSystemService(Context.LOCATION_SERVICE); + LocationManager locationManager = (LocationManager) GBApplication.getContext().getSystemService(Context.LOCATION_SERVICE); Criteria criteria = new Criteria(); String provider = locationManager.getBestProvider(criteria, false); if (provider != null) { @@ -328,7 +326,7 @@ public class WebViewSingleton { if (url.startsWith("http://") || url.startsWith("https://")) { Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK); - webViewSingleton.contextWrapper.startActivity(i); + GBApplication.getContext().startActivity(i); } else { url = url.replaceFirst("^pebblejs://close#", "file:///android_asset/app_config/configure.html?config=true&json="); view.loadUrl(url);