1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-25 19:36:50 +01:00

Collaborative attempt to initialize the background webview on startup

This commit is contained in:
cpfeiffer 2017-02-25 18:01:08 +01:00
parent a4ac108287
commit 711800f3d0
8 changed files with 67 additions and 54 deletions

View File

@ -281,6 +281,10 @@
android:name=".activities.DiscoveryActivity"
android:label="@string/title_activity_discovery"
android:parentActivityName=".activities.ControlCenter" />
<activity
android:name=".activities.BackgroundWebViewActivity"
android:label="@string/activity_web_view"
android:parentActivityName=".activities.ControlCenter" />
<activity
android:name=".activities.AndroidPairingActivity"
android:label="@string/title_activity_android_pairing" />

View File

@ -27,6 +27,7 @@ import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import nodomain.freeyourgadget.gadgetbridge.activities.BackgroundWebViewActivity;
import nodomain.freeyourgadget.gadgetbridge.database.DBHandler;
import nodomain.freeyourgadget.gadgetbridge.database.DBHelper;
import nodomain.freeyourgadget.gadgetbridge.database.DBOpenHelper;
@ -124,6 +125,8 @@ public class GBApplication extends Application {
deviceManager = new DeviceManager(this);
createWebViewActivity();
deviceService = createDeviceService();
loadBlackList();
@ -132,6 +135,10 @@ public class GBApplication extends Application {
}
}
private void createWebViewActivity() {
startActivity(new Intent(getContext(), BackgroundWebViewActivity.class));
}
@Override
public void onTrimMemory(int level) {
super.onTrimMemory(level);

View File

@ -0,0 +1,16 @@
package nodomain.freeyourgadget.gadgetbridge.activities;
import android.app.Activity;
import android.os.Bundle;
import android.os.PersistableBundle;
import nodomain.freeyourgadget.gadgetbridge.util.WebViewSingleton;
public class BackgroundWebViewActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
WebViewSingleton.createWebView(this);
setVisible(false);
}
}

View File

@ -48,8 +48,8 @@ public class ExternalPebbleJSActivity extends GBActivity {
setContentView(R.layout.activity_external_pebble_js);
myWebView = WebViewSingleton.getorInitWebView(this, mGBDevice, appUuid);
myWebView.setWillNotDraw(false);
WebViewSingleton.updateActivityContext(this);
myWebView = WebViewSingleton.getWebView();
myWebView.addJavascriptInterface(new ActivityJSInterface(ExternalPebbleJSActivity.this), "GBActivity");
FrameLayout fl = (FrameLayout) findViewById(R.id.webview_placeholder);
fl.addView(myWebView);

View File

@ -338,8 +338,6 @@ public abstract class AbstractAppManagerFragment extends Fragment {
view.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
popupMenu.show();
//TODO: replace with local broadcast on app start
WebViewSingleton.getorInitWebView(getActivity(), mGBDevice, selectedApp.getUUID());
return true;
}

View File

@ -82,7 +82,7 @@ class PebbleIoThread extends GBDeviceIoThread {
private int mBytesWritten = -1;
private void sendAppMessageJS(GBDeviceEventAppMessage appMessage) {
WebViewSingleton.getorInitWebView(getContext(), gbDevice, appMessage.appUUID);
// WebViewSingleton.runJavascriptInterface(gbDevice, appMessage.appUUID);
WebViewSingleton.appMessage(appMessage.message);
}
@ -487,8 +487,7 @@ class PebbleIoThread extends GBDeviceIoThread {
break;
case START:
LOG.info("got GBDeviceEventAppManagement START event for uuid: " + appMgmt.uuid);
WebViewSingleton.getorInitWebView(getContext(), gbDevice, appMgmt.uuid);
//TODO: the method call above will not work the first time as we need an activity. Either we find a way to have one here, or replace it with a local broadcast
WebViewSingleton.runJavascriptInterface(gbDevice, appMgmt.uuid);
break;
default:
break;

View File

@ -13,6 +13,7 @@ import android.net.Uri;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.webkit.ConsoleMessage;
import android.webkit.JavascriptInterface;
@ -58,63 +59,50 @@ public class WebViewSingleton extends Activity {
private WebViewSingleton() {
}
public static WebView getorInitWebView(Context context, final GBDevice device, final UUID uuid) {
public static synchronized WebView createWebView(Context context) {
if (instance == null) {
contextWrapper = new MutableContextWrapper(context);
instance = new WebView(contextWrapper);
instance.setWillNotDraw(true);
instance.clearCache(true);
instance.setWebViewClient(new GBWebClient());
instance.setWebChromeClient(new GBChromeClient());
instance.setWebContentsDebuggingEnabled(true);
WebSettings webSettings = instance.getSettings();
webSettings.setJavaScriptEnabled(true);
//needed to access the DOM
webSettings.setDomStorageEnabled(true);
//needed for localstorage
webSettings.setDatabaseEnabled(true);
}
return instance;
}
if (jsInterface == null || (jsInterface != null && (!device.equals(jsInterface.device) || !uuid.equals(jsInterface.mUuid)))) {
public static void updateActivityContext(Context context) {
if (context instanceof Activity) {
contextWrapper.setBaseContext(context);
}
}
@NonNull
public static WebView getWebView() {
return instance;
}
public static void runJavascriptInterface(final GBDevice device, final UUID uuid) {
if (jsInterface == null || !device.equals(jsInterface.device) || !uuid.equals(jsInterface.mUuid)) {
jsInterface = new JSInterface(device, uuid);
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
if (instance != null) {
instance.removeJavascriptInterface("GBjs");
instance.addJavascriptInterface(jsInterface, "GBjs");
instance.loadUrl("file:///android_asset/app_config/configure.html");
}
instance.removeJavascriptInterface("GBjs");
instance.addJavascriptInterface(jsInterface, "GBjs");
instance.loadUrl("file:///android_asset/app_config/configure.html");
}
});
} else {
LOG.debug("Not replacing the JS in the webview. JS uuid " + jsInterface.mUuid.toString());
}
if (context instanceof Activity) {
if (contextWrapper != null) {
contextWrapper.setBaseContext(context);
} else {
contextWrapper = new MutableContextWrapper(context);
}
if (instance == null) {
LOG.debug("WEBV: creating webview");
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
instance = new WebView(contextWrapper);
instance.setWillNotDraw(true);
instance.clearCache(true);
instance.setWebViewClient(new GBWebClient());
instance.setWebChromeClient(new GBChromeClient());
instance.setWebContentsDebuggingEnabled(true);
WebSettings webSettings = instance.getSettings();
webSettings.setJavaScriptEnabled(true);
//needed to access the DOM
webSettings.setDomStorageEnabled(true);
//needed for localstorage
webSettings.setDatabaseEnabled(true);
if (jsInterface != null) {
LOG.debug("Attaching the existing jsInterface to the new webview instance");
instance.addJavascriptInterface(jsInterface, "GBjs");
instance.loadUrl("file:///android_asset/app_config/configure.html");
}
}
});
}
} else {
LOG.debug("WEBV: not using the passed context, as it is not an activity");
}
return instance;
}
public static void appMessage(final String message) {

View File

@ -393,4 +393,5 @@
<string name="timeformat_24h">24H</string>
<string name="timeformat_am_pm">AM/PM</string>
<string name="pref_screen_notification_profile_alarm_clock">Alarm Clock</string>
<string name="activity_web_view">Web View Activity</string>
</resources>