mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge
synced 2024-11-03 17:02:13 +01:00
Trying to simplify ExternalPebbleJSActivity#onCreate() a bit
(by splitting into separate methods)
This commit is contained in:
parent
9ee1aa87e8
commit
da5d1e3685
@ -20,6 +20,7 @@ package nodomain.freeyourgadget.gadgetbridge.activities;
|
|||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.support.annotation.NonNull;
|
||||||
import android.support.v4.app.NavUtils;
|
import android.support.v4.app.NavUtils;
|
||||||
import android.view.MenuItem;
|
import android.view.MenuItem;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
@ -51,6 +52,10 @@ public class ExternalPebbleJSActivity extends AbstractGBActivity {
|
|||||||
private static final Logger LOG = LoggerFactory.getLogger(ExternalPebbleJSActivity.class);
|
private static final Logger LOG = LoggerFactory.getLogger(ExternalPebbleJSActivity.class);
|
||||||
|
|
||||||
private Uri confUri;
|
private Uri confUri;
|
||||||
|
/**
|
||||||
|
* When bgjs is enabled, this field refers to the WebViewSingleton,
|
||||||
|
* otherwise it refers to the legacy webview from the activity_legacy_external_pebble_js layout
|
||||||
|
*/
|
||||||
private WebView myWebView;
|
private WebView myWebView;
|
||||||
public static final String START_BG_WEBVIEW = "start_webview";
|
public static final String START_BG_WEBVIEW = "start_webview";
|
||||||
public static final String SHOW_CONFIG = "configure";
|
public static final String SHOW_CONFIG = "configure";
|
||||||
@ -58,74 +63,90 @@ public class ExternalPebbleJSActivity extends AbstractGBActivity {
|
|||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
GBDevice currentDevice = null;
|
|
||||||
UUID currentUUID = null;
|
|
||||||
Bundle extras = getIntent().getExtras();
|
Bundle extras = getIntent().getExtras();
|
||||||
if (extras != null) {
|
if (extras == null) {
|
||||||
currentDevice = extras.getParcelable(GBDevice.EXTRA_DEVICE);
|
|
||||||
currentUUID = (UUID) extras.getSerializable(DeviceService.EXTRA_APP_UUID);
|
|
||||||
|
|
||||||
Objects.requireNonNull(currentDevice, "Must provide a device when invoking this activity");
|
|
||||||
Objects.requireNonNull(currentUUID, "Must provide a uuid when invoking this activity");
|
|
||||||
|
|
||||||
if (GBApplication.getPrefs().getBoolean("pebble_enable_background_javascript", false)) {
|
|
||||||
if (extras.getBoolean(SHOW_CONFIG, false)) {
|
|
||||||
WebViewSingleton.runJavascriptInterface(currentDevice, currentUUID);
|
|
||||||
} else if (extras.getBoolean(START_BG_WEBVIEW, false)) {
|
|
||||||
WebViewSingleton.ensureCreated(this);
|
|
||||||
finish();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
throw new IllegalArgumentException("Must provide device and uuid in extras when invoking this activity");
|
throw new IllegalArgumentException("Must provide device and uuid in extras when invoking this activity");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (GBApplication.getPrefs().getBoolean("pebble_enable_background_javascript", false)) {
|
if (extras.getBoolean(START_BG_WEBVIEW, false)) {
|
||||||
setContentView(R.layout.activity_external_pebble_js);
|
startBackgroundWebViewAndFinish();
|
||||||
myWebView = WebViewSingleton.getWebView(this);
|
return;
|
||||||
if (myWebView.getParent() != null) {
|
|
||||||
((ViewGroup) myWebView.getParent()).removeView(myWebView);
|
|
||||||
}
|
|
||||||
myWebView.setWillNotDraw(false);
|
|
||||||
myWebView.removeJavascriptInterface("GBActivity");
|
|
||||||
myWebView.addJavascriptInterface(new ActivityJSInterface(), "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);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onViewDetachedFromWindow(View v) {
|
|
||||||
v.removeOnAttachStateChangeListener(this);
|
|
||||||
FrameLayout fl = (FrameLayout) findViewById(R.id.webview_placeholder);
|
|
||||||
fl.removeAllViews();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
setContentView(R.layout.activity_legacy_external_pebble_js);
|
|
||||||
myWebView = (WebView) findViewById(R.id.configureWebview);
|
|
||||||
myWebView.clearCache(true);
|
|
||||||
myWebView.setWebViewClient(new GBWebClient());
|
|
||||||
myWebView.setWebChromeClient(new GBChromeClient());
|
|
||||||
WebSettings webSettings = myWebView.getSettings();
|
|
||||||
webSettings.setJavaScriptEnabled(true);
|
|
||||||
//needed to access the DOM
|
|
||||||
webSettings.setDomStorageEnabled(true);
|
|
||||||
//needed for localstorage
|
|
||||||
webSettings.setDatabaseEnabled(true);
|
|
||||||
|
|
||||||
JSInterface gbJSInterface = new JSInterface(currentDevice, currentUUID);
|
|
||||||
myWebView.addJavascriptInterface(gbJSInterface, "GBjs");
|
|
||||||
myWebView.addJavascriptInterface(new ActivityJSInterface(), "GBActivity");
|
|
||||||
|
|
||||||
myWebView.loadUrl("file:///android_asset/app_config/configure.html");
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
GBDevice currentDevice = extras.getParcelable(GBDevice.EXTRA_DEVICE);
|
||||||
|
UUID currentUUID = (UUID) extras.getSerializable(DeviceService.EXTRA_APP_UUID);
|
||||||
|
|
||||||
|
if (GBApplication.getGBPrefs().isBackgroundJsEnabled()) {
|
||||||
|
if (extras.getBoolean(SHOW_CONFIG, false)) {
|
||||||
|
Objects.requireNonNull(currentDevice, "Must provide a device when invoking this activity");
|
||||||
|
Objects.requireNonNull(currentUUID, "Must provide a uuid when invoking this activity");
|
||||||
|
|
||||||
|
WebViewSingleton.runJavascriptInterface(currentDevice, currentUUID);
|
||||||
|
}
|
||||||
|
|
||||||
|
// FIXME: is this really supposed to be outside the check for SHOW_CONFIG?
|
||||||
|
setupBGWebView();
|
||||||
|
} else {
|
||||||
|
Objects.requireNonNull(currentDevice, "Must provide a device when invoking this activity without bgjs");
|
||||||
|
Objects.requireNonNull(currentUUID, "Must provide a uuid when invoking this activity without bgjs");
|
||||||
|
setupLegacyWebView(currentDevice, currentUUID);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void startBackgroundWebViewAndFinish() {
|
||||||
|
if (GBApplication.getGBPrefs().isBackgroundJsEnabled()) {
|
||||||
|
WebViewSingleton.ensureCreated(this);
|
||||||
|
} else {
|
||||||
|
LOG.warn("BGJs disabled, not starting webview");
|
||||||
|
}
|
||||||
|
finish();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setupBGWebView() {
|
||||||
|
setContentView(R.layout.activity_external_pebble_js);
|
||||||
|
myWebView = WebViewSingleton.getWebView(this);
|
||||||
|
if (myWebView.getParent() != null) {
|
||||||
|
((ViewGroup) myWebView.getParent()).removeView(myWebView);
|
||||||
|
}
|
||||||
|
myWebView.setWillNotDraw(false);
|
||||||
|
myWebView.removeJavascriptInterface("GBActivity");
|
||||||
|
myWebView.addJavascriptInterface(new ActivityJSInterface(), "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);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onViewDetachedFromWindow(View v) {
|
||||||
|
v.removeOnAttachStateChangeListener(this);
|
||||||
|
FrameLayout fl = (FrameLayout) findViewById(R.id.webview_placeholder);
|
||||||
|
fl.removeAllViews();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setupLegacyWebView(@NonNull GBDevice device, @NonNull UUID uuid) {
|
||||||
|
setContentView(R.layout.activity_legacy_external_pebble_js);
|
||||||
|
myWebView = (WebView) findViewById(R.id.configureWebview);
|
||||||
|
myWebView.clearCache(true);
|
||||||
|
myWebView.setWebViewClient(new GBWebClient());
|
||||||
|
myWebView.setWebChromeClient(new GBChromeClient());
|
||||||
|
WebSettings webSettings = myWebView.getSettings();
|
||||||
|
webSettings.setJavaScriptEnabled(true);
|
||||||
|
//needed to access the DOM
|
||||||
|
webSettings.setDomStorageEnabled(true);
|
||||||
|
//needed for localstorage
|
||||||
|
webSettings.setDatabaseEnabled(true);
|
||||||
|
|
||||||
|
JSInterface gbJSInterface = new JSInterface(device, uuid);
|
||||||
|
myWebView.addJavascriptInterface(gbJSInterface, "GBjs");
|
||||||
|
myWebView.addJavascriptInterface(new ActivityJSInterface(), "GBActivity");
|
||||||
|
|
||||||
|
myWebView.loadUrl("file:///android_asset/app_config/configure.html");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -198,7 +198,7 @@ class PebbleIoThread extends GBDeviceIoThread {
|
|||||||
mOutStream = mBtSocket.getOutputStream();
|
mOutStream = mBtSocket.getOutputStream();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (prefs.getBoolean("pebble_enable_background_javascript", false)) {
|
if (GBApplication.getGBPrefs().isBackgroundJsEnabled()) {
|
||||||
Intent startIntent = new Intent(getContext(), ExternalPebbleJSActivity.class);
|
Intent startIntent = new Intent(getContext(), ExternalPebbleJSActivity.class);
|
||||||
startIntent.putExtra(ExternalPebbleJSActivity.START_BG_WEBVIEW, true);
|
startIntent.putExtra(ExternalPebbleJSActivity.START_BG_WEBVIEW, true);
|
||||||
getContext().startActivity(startIntent);
|
getContext().startActivity(startIntent);
|
||||||
@ -422,7 +422,7 @@ class PebbleIoThread extends GBDeviceIoThread {
|
|||||||
gbDevice.setState(GBDevice.State.WAITING_FOR_RECONNECT);
|
gbDevice.setState(GBDevice.State.WAITING_FOR_RECONNECT);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (prefs.getBoolean("pebble_enable_background_javascript", false)) {
|
if (GBApplication.getGBPrefs().isBackgroundJsEnabled()) {
|
||||||
WebViewSingleton.disposeWebView();
|
WebViewSingleton.disposeWebView();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -550,7 +550,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);
|
||||||
if (prefs.getBoolean("pebble_enable_background_javascript", false)) {
|
if (GBApplication.getGBPrefs().isBackgroundJsEnabled()) {
|
||||||
if (mPebbleProtocol.hasAppMessageHandler(appMgmt.uuid)) {
|
if (mPebbleProtocol.hasAppMessageHandler(appMgmt.uuid)) {
|
||||||
WebViewSingleton.stopJavascriptInterface();
|
WebViewSingleton.stopJavascriptInterface();
|
||||||
} else {
|
} else {
|
||||||
@ -568,7 +568,7 @@ class PebbleIoThread extends GBDeviceIoThread {
|
|||||||
setInstallSlot(appInfoEvent.freeSlot);
|
setInstallSlot(appInfoEvent.freeSlot);
|
||||||
return false;
|
return false;
|
||||||
} else if (deviceEvent instanceof GBDeviceEventAppMessage) {
|
} else if (deviceEvent instanceof GBDeviceEventAppMessage) {
|
||||||
if (GBApplication.getPrefs().getBoolean("pebble_enable_background_javascript", false)) {
|
if (GBApplication.getGBPrefs().isBackgroundJsEnabled()) {
|
||||||
sendAppMessageJS((GBDeviceEventAppMessage) deviceEvent);
|
sendAppMessageJS((GBDeviceEventAppMessage) deviceEvent);
|
||||||
}
|
}
|
||||||
if (mEnablePebblekit) {
|
if (mEnablePebblekit) {
|
||||||
|
@ -407,7 +407,7 @@ public class PebbleProtocol extends GBDeviceProtocol {
|
|||||||
super(device);
|
super(device);
|
||||||
mAppMessageHandlers.put(UUID_MORPHEUZ, new AppMessageHandlerMorpheuz(UUID_MORPHEUZ, PebbleProtocol.this));
|
mAppMessageHandlers.put(UUID_MORPHEUZ, new AppMessageHandlerMorpheuz(UUID_MORPHEUZ, PebbleProtocol.this));
|
||||||
mAppMessageHandlers.put(UUID_MISFIT, new AppMessageHandlerMisfit(UUID_MISFIT, PebbleProtocol.this));
|
mAppMessageHandlers.put(UUID_MISFIT, new AppMessageHandlerMisfit(UUID_MISFIT, PebbleProtocol.this));
|
||||||
if (!(GBApplication.getPrefs().getBoolean("pebble_enable_background_javascript", false))) {
|
if (!GBApplication.getGBPrefs().isBackgroundJsEnabled()) {
|
||||||
mAppMessageHandlers.put(UUID_PEBBLE_TIMESTYLE, new AppMessageHandlerTimeStylePebble(UUID_PEBBLE_TIMESTYLE, PebbleProtocol.this));
|
mAppMessageHandlers.put(UUID_PEBBLE_TIMESTYLE, new AppMessageHandlerTimeStylePebble(UUID_PEBBLE_TIMESTYLE, PebbleProtocol.this));
|
||||||
mAppMessageHandlers.put(UUID_PEBSTYLE, new AppMessageHandlerPebStyle(UUID_PEBSTYLE, PebbleProtocol.this));
|
mAppMessageHandlers.put(UUID_PEBSTYLE, new AppMessageHandlerPebStyle(UUID_PEBSTYLE, PebbleProtocol.this));
|
||||||
mAppMessageHandlers.put(UUID_MARIOTIME, new AppMessageHandlerMarioTime(UUID_MARIOTIME, PebbleProtocol.this));
|
mAppMessageHandlers.put(UUID_MARIOTIME, new AppMessageHandlerMarioTime(UUID_MARIOTIME, PebbleProtocol.this));
|
||||||
|
@ -28,6 +28,8 @@ public class GBPrefs {
|
|||||||
public static final String AUTO_EXPORT_LOCATION = "auto_export_location";
|
public static final String AUTO_EXPORT_LOCATION = "auto_export_location";
|
||||||
public static final String AUTO_EXPORT_INTERVAL = "auto_export_interval";
|
public static final String AUTO_EXPORT_INTERVAL = "auto_export_interval";
|
||||||
private static final boolean AUTO_START_DEFAULT = true;
|
private static final boolean AUTO_START_DEFAULT = true;
|
||||||
|
private static final String BG_JS_ENABLED = "pebble_enable_background_javascript";
|
||||||
|
private static final boolean BG_JS_ENABLED_DEFAULT = false;
|
||||||
public static boolean AUTO_RECONNECT_DEFAULT = true;
|
public static boolean AUTO_RECONNECT_DEFAULT = true;
|
||||||
|
|
||||||
public static final String USER_NAME = "mi_user_alias";
|
public static final String USER_NAME = "mi_user_alias";
|
||||||
@ -48,6 +50,10 @@ public class GBPrefs {
|
|||||||
return mPrefs.getBoolean(AUTO_START, AUTO_START_DEFAULT);
|
return mPrefs.getBoolean(AUTO_START, AUTO_START_DEFAULT);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isBackgroundJsEnabled() {
|
||||||
|
return mPrefs.getBoolean(BG_JS_ENABLED, BG_JS_ENABLED_DEFAULT);
|
||||||
|
}
|
||||||
|
|
||||||
public String getUserName() {
|
public String getUserName() {
|
||||||
return mPrefs.getString(USER_NAME, USER_NAME_DEFAULT);
|
return mPrefs.getString(USER_NAME, USER_NAME_DEFAULT);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user