1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-06-20 20:10:15 +02: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
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WebViewSingleton.createWebView(this);
WebViewSingleton.getInstance(this);
finish();
}
}

View File

@ -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<String>() {
// @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<String>() {
@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();
}

View File

@ -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);