1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-12-01 14:32:54 +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:name=".activities.DiscoveryActivity"
android:label="@string/title_activity_discovery" android:label="@string/title_activity_discovery"
android:parentActivityName=".activities.ControlCenter" /> android:parentActivityName=".activities.ControlCenter" />
<activity
android:name=".activities.BackgroundWebViewActivity"
android:label="@string/activity_web_view"
android:parentActivityName=".activities.ControlCenter" />
<activity <activity
android:name=".activities.AndroidPairingActivity" android:name=".activities.AndroidPairingActivity"
android:label="@string/title_activity_android_pairing" /> 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.Lock;
import java.util.concurrent.locks.ReentrantLock; import java.util.concurrent.locks.ReentrantLock;
import nodomain.freeyourgadget.gadgetbridge.activities.BackgroundWebViewActivity;
import nodomain.freeyourgadget.gadgetbridge.database.DBHandler; import nodomain.freeyourgadget.gadgetbridge.database.DBHandler;
import nodomain.freeyourgadget.gadgetbridge.database.DBHelper; import nodomain.freeyourgadget.gadgetbridge.database.DBHelper;
import nodomain.freeyourgadget.gadgetbridge.database.DBOpenHelper; import nodomain.freeyourgadget.gadgetbridge.database.DBOpenHelper;
@ -124,6 +125,8 @@ public class GBApplication extends Application {
deviceManager = new DeviceManager(this); deviceManager = new DeviceManager(this);
createWebViewActivity();
deviceService = createDeviceService(); deviceService = createDeviceService();
loadBlackList(); loadBlackList();
@ -132,6 +135,10 @@ public class GBApplication extends Application {
} }
} }
private void createWebViewActivity() {
startActivity(new Intent(getContext(), BackgroundWebViewActivity.class));
}
@Override @Override
public void onTrimMemory(int level) { public void onTrimMemory(int level) {
super.onTrimMemory(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); setContentView(R.layout.activity_external_pebble_js);
myWebView = WebViewSingleton.getorInitWebView(this, mGBDevice, appUuid); WebViewSingleton.updateActivityContext(this);
myWebView.setWillNotDraw(false); myWebView = WebViewSingleton.getWebView();
myWebView.addJavascriptInterface(new ActivityJSInterface(ExternalPebbleJSActivity.this), "GBActivity"); myWebView.addJavascriptInterface(new ActivityJSInterface(ExternalPebbleJSActivity.this), "GBActivity");
FrameLayout fl = (FrameLayout) findViewById(R.id.webview_placeholder); FrameLayout fl = (FrameLayout) findViewById(R.id.webview_placeholder);
fl.addView(myWebView); fl.addView(myWebView);

View File

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

View File

@ -82,7 +82,7 @@ class PebbleIoThread extends GBDeviceIoThread {
private int mBytesWritten = -1; private int mBytesWritten = -1;
private void sendAppMessageJS(GBDeviceEventAppMessage appMessage) { private void sendAppMessageJS(GBDeviceEventAppMessage appMessage) {
WebViewSingleton.getorInitWebView(getContext(), gbDevice, appMessage.appUUID); // WebViewSingleton.runJavascriptInterface(gbDevice, appMessage.appUUID);
WebViewSingleton.appMessage(appMessage.message); WebViewSingleton.appMessage(appMessage.message);
} }
@ -487,8 +487,7 @@ class PebbleIoThread extends GBDeviceIoThread {
break; break;
case START: case START:
LOG.info("got GBDeviceEventAppManagement START event for uuid: " + appMgmt.uuid); LOG.info("got GBDeviceEventAppManagement START event for uuid: " + appMgmt.uuid);
WebViewSingleton.getorInitWebView(getContext(), gbDevice, appMgmt.uuid); WebViewSingleton.runJavascriptInterface(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
break; break;
default: default:
break; break;

View File

@ -13,6 +13,7 @@ import android.net.Uri;
import android.os.Build; import android.os.Build;
import android.os.Handler; import android.os.Handler;
import android.os.Looper; import android.os.Looper;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat; import android.support.v4.app.ActivityCompat;
import android.webkit.ConsoleMessage; import android.webkit.ConsoleMessage;
import android.webkit.JavascriptInterface; import android.webkit.JavascriptInterface;
@ -58,37 +59,9 @@ public class WebViewSingleton extends Activity {
private WebViewSingleton() { private WebViewSingleton() {
} }
public static WebView getorInitWebView(Context context, final GBDevice device, final UUID uuid) { public static synchronized WebView createWebView(Context context) {
if (jsInterface == null || (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");
}
}
});
} 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) { if (instance == null) {
LOG.debug("WEBV: creating webview"); contextWrapper = new MutableContextWrapper(context);
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
instance = new WebView(contextWrapper); instance = new WebView(contextWrapper);
instance.setWillNotDraw(true); instance.setWillNotDraw(true);
instance.clearCache(true); instance.clearCache(true);
@ -101,20 +74,35 @@ public class WebViewSingleton extends Activity {
webSettings.setDomStorageEnabled(true); webSettings.setDomStorageEnabled(true);
//needed for localstorage //needed for localstorage
webSettings.setDatabaseEnabled(true); webSettings.setDatabaseEnabled(true);
if (jsInterface != null) { }
LOG.debug("Attaching the existing jsInterface to the new webview instance"); return instance;
}
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() {
instance.removeJavascriptInterface("GBjs");
instance.addJavascriptInterface(jsInterface, "GBjs"); instance.addJavascriptInterface(jsInterface, "GBjs");
instance.loadUrl("file:///android_asset/app_config/configure.html"); instance.loadUrl("file:///android_asset/app_config/configure.html");
} }
}
}); });
}
} else { } else {
LOG.debug("WEBV: not using the passed context, as it is not an activity"); LOG.debug("Not replacing the JS in the webview. JS uuid " + jsInterface.mUuid.toString());
} }
return instance;
} }
public static void appMessage(final String message) { public static void appMessage(final String message) {

View File

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