1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-04 01:09:47 +01:00

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
This commit is contained in:
Daniele Gobbetti 2017-07-24 23:57:07 +02:00
parent f6946c4402
commit 79f3cad36d
3 changed files with 8 additions and 27 deletions

View File

@ -9,7 +9,7 @@ public class BackgroundWebViewActivity extends Activity {
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
WebViewSingleton.createWebView(this); WebViewSingleton.getInstance(this);
finish(); finish();
} }
} }

View File

@ -25,7 +25,6 @@ import android.support.v4.app.NavUtils;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.webkit.JavascriptInterface; import android.webkit.JavascriptInterface;
import android.webkit.ValueCallback;
import android.webkit.WebView; import android.webkit.WebView;
import android.widget.FrameLayout; import android.widget.FrameLayout;
import android.widget.Toast; import android.widget.Toast;
@ -69,31 +68,15 @@ public class ExternalPebbleJSActivity extends GBActivity {
FrameLayout fl = (FrameLayout) findViewById(R.id.webview_placeholder); FrameLayout fl = (FrameLayout) findViewById(R.id.webview_placeholder);
fl.addView(myWebView); fl.addView(myWebView);
myWebView.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() { myWebView.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {
@Override @Override
public void onViewAttachedToWindow(View v) { public void onViewAttachedToWindow(View v) {
v.setLayerType(View.LAYER_TYPE_HARDWARE, null); v.setLayerType(View.LAYER_TYPE_HARDWARE, null);
//show configuration - moved to JS
// myWebView.evaluateJavascript("Pebble.evaluate('showConfiguration');", new ValueCallback<String>() {
// @Override
// public void onReceiveValue(String s) {
// LOG.debug("Callback from showConfiguration: " + s);
// }
// });
} }
@Override @Override
public void onViewDetachedFromWindow(View v) { public void onViewDetachedFromWindow(View v) {
myWebView.removeJavascriptInterface("GBActivity"); v.removeOnAttachStateChangeListener(this);
myWebView.setWillNotDraw(true);
myWebView.evaluateJavascript("showStep('step1')", new ValueCallback<String>() {
@Override
public void onReceiveValue(String s) {
LOG.debug("Callback from window detach: " + s);
}
});
FrameLayout fl = (FrameLayout) findViewById(R.id.webview_placeholder); FrameLayout fl = (FrameLayout) findViewById(R.id.webview_placeholder);
fl.removeAllViews(); fl.removeAllViews();
} }

View File

@ -69,7 +69,7 @@ public class WebViewSingleton {
private WebViewSingleton() { private WebViewSingleton() {
} }
public static synchronized WebView createWebView(Activity context) { public static synchronized WebView getInstance(Activity context) {
if (instance == null) { if (instance == null) {
webViewSingleton.contextWrapper = new MutableContextWrapper(context); webViewSingleton.contextWrapper = new MutableContextWrapper(context);
webViewSingleton.mainLooper = context.getMainLooper(); webViewSingleton.mainLooper = context.getMainLooper();
@ -90,7 +90,7 @@ public class WebViewSingleton {
} }
public static void updateActivityContext(Activity context) { public static void updateActivityContext(Activity context) {
if (context instanceof Activity) { if (context != null) {
webViewSingleton.contextWrapper.setBaseContext(context); webViewSingleton.contextWrapper.setBaseContext(context);
} }
} }
@ -101,12 +101,10 @@ public class WebViewSingleton {
} }
public static void runJavascriptInterface(GBDevice device, UUID uuid) { public static void runJavascriptInterface(GBDevice device, UUID uuid) {
final JSInterface jsInterface = new JSInterface(device, uuid);
if (uuid.equals(currentRunningUUID)) { if (uuid.equals(currentRunningUUID)) {
LOG.debug("WEBVIEW uuid not changed keeping the old context"); LOG.debug("WEBVIEW uuid not changed keeping the old context");
} else { } else {
final JSInterface jsInterface = new JSInterface(device, uuid);
LOG.debug("WEBVIEW uuid changed, restarting"); LOG.debug("WEBVIEW uuid changed, restarting");
currentRunningUUID = uuid; currentRunningUUID = uuid;
new Handler(webViewSingleton.mainLooper).post(new Runnable() { 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 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)) { 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(); Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, false); String provider = locationManager.getBestProvider(criteria, false);
if (provider != null) { if (provider != null) {
@ -328,7 +326,7 @@ public class WebViewSingleton {
if (url.startsWith("http://") || url.startsWith("https://")) { if (url.startsWith("http://") || url.startsWith("https://")) {
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK); i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
webViewSingleton.contextWrapper.startActivity(i); GBApplication.getContext().startActivity(i);
} else { } else {
url = url.replaceFirst("^pebblejs://close#", "file:///android_asset/app_config/configure.html?config=true&json="); url = url.replaceFirst("^pebblejs://close#", "file:///android_asset/app_config/configure.html?config=true&json=");
view.loadUrl(url); view.loadUrl(url);