2020-01-09 10:44:32 +01:00
|
|
|
/* Copyright (C) 2016-2020 Andreas Shimokawa, Carsten Pfeiffer, Daniele
|
2017-03-10 14:53:19 +01:00
|
|
|
Gobbetti, Lem Dulfo, Uwe Hermann
|
|
|
|
|
|
|
|
This file is part of Gadgetbridge.
|
|
|
|
|
|
|
|
Gadgetbridge is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Affero General Public License as published
|
|
|
|
by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Gadgetbridge is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
2016-02-28 22:25:21 +01:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.activities;
|
|
|
|
|
2016-03-13 08:31:50 +01:00
|
|
|
import android.content.Intent;
|
2016-02-28 22:25:21 +01:00
|
|
|
import android.net.Uri;
|
|
|
|
import android.os.Bundle;
|
2016-03-03 13:05:51 +01:00
|
|
|
import android.view.MenuItem;
|
2017-01-01 18:33:39 +01:00
|
|
|
import android.view.View;
|
2017-09-25 17:12:35 +02:00
|
|
|
import android.view.ViewGroup;
|
2016-02-28 22:25:21 +01:00
|
|
|
import android.webkit.JavascriptInterface;
|
2017-09-25 17:12:35 +02:00
|
|
|
import android.webkit.WebSettings;
|
2016-02-28 22:25:21 +01:00
|
|
|
import android.webkit.WebView;
|
2016-12-28 20:53:17 +01:00
|
|
|
import android.widget.FrameLayout;
|
2016-02-28 22:25:21 +01:00
|
|
|
import android.widget.Toast;
|
|
|
|
|
2020-08-26 04:34:41 +02:00
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.core.app.NavUtils;
|
|
|
|
|
2016-02-28 22:25:21 +01:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
2018-03-17 18:50:53 +01:00
|
|
|
import java.util.List;
|
2018-01-13 21:04:42 +01:00
|
|
|
import java.util.Objects;
|
2016-02-28 22:25:21 +01:00
|
|
|
import java.util.UUID;
|
|
|
|
|
2017-09-25 17:12:35 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
2016-02-28 22:25:21 +01:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.R;
|
2018-03-17 18:50:53 +01:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.devices.DeviceManager;
|
2016-02-28 22:25:21 +01:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
2016-06-18 18:40:57 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.DeviceService;
|
2018-03-17 18:50:53 +01:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.DeviceType;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.DeviceCommunicationService;
|
2017-09-25 17:12:35 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.devices.pebble.webview.GBChromeClient;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.devices.pebble.webview.GBWebClient;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.devices.pebble.webview.JSInterface;
|
2018-03-17 18:50:53 +01:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.DeviceHelper;
|
2016-02-28 22:25:21 +01:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.GB;
|
2016-12-28 20:53:17 +01:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.WebViewSingleton;
|
2016-02-28 22:25:21 +01:00
|
|
|
|
2018-03-17 18:50:53 +01:00
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.ACTION_CONNECT;
|
|
|
|
|
2017-09-03 01:02:31 +02:00
|
|
|
public class ExternalPebbleJSActivity extends AbstractGBActivity {
|
2016-02-28 22:25:21 +01:00
|
|
|
|
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(ExternalPebbleJSActivity.class);
|
|
|
|
|
2016-06-18 18:40:57 +02:00
|
|
|
private Uri confUri;
|
2018-01-14 00:20:13 +01:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2016-06-18 18:40:57 +02:00
|
|
|
private WebView myWebView;
|
2017-09-25 17:12:35 +02:00
|
|
|
public static final String START_BG_WEBVIEW = "start_webview";
|
|
|
|
public static final String SHOW_CONFIG = "configure";
|
2016-02-28 22:25:21 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
2016-03-03 11:52:30 +01:00
|
|
|
Bundle extras = getIntent().getExtras();
|
2018-03-17 18:50:53 +01:00
|
|
|
|
|
|
|
boolean showConfig = false;
|
|
|
|
|
|
|
|
UUID currentUUID = null;
|
|
|
|
GBDevice currentDevice = null;
|
|
|
|
|
2018-01-14 00:20:13 +01:00
|
|
|
if (extras == null) {
|
2018-03-17 18:50:53 +01:00
|
|
|
confUri = getIntent().getData();
|
|
|
|
if(confUri.getScheme().equals("gadgetbridge")) {
|
|
|
|
try {
|
|
|
|
currentUUID = UUID.fromString(confUri.getHost());
|
|
|
|
} catch (IllegalArgumentException e) {
|
|
|
|
LOG.error("UUID in incoming configuration is not a valid UUID: " +confUri.toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
//first check if we are still connected to a pebble
|
|
|
|
DeviceManager deviceManager = ((GBApplication) getApplication()).getDeviceManager();
|
|
|
|
List<GBDevice> deviceList = deviceManager.getDevices();
|
|
|
|
for (GBDevice device : deviceList) {
|
|
|
|
if (device.getState() == GBDevice.State.INITIALIZED) {
|
|
|
|
if (device.getType().equals(DeviceType.PEBBLE)) {
|
|
|
|
currentDevice = device;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
LOG.error("attempting to load pebble configuration but a different device type is connected!!!");
|
|
|
|
finish();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (currentDevice == null) {
|
|
|
|
//then try to reconnect to last connected device
|
|
|
|
String btDeviceAddress = GBApplication.getPrefs().getPreferences().getString("last_device_address", null);
|
|
|
|
if (btDeviceAddress != null) {
|
|
|
|
GBDevice candidate = DeviceHelper.getInstance().findAvailableDevice(btDeviceAddress, this);
|
|
|
|
if(!candidate.isConnected() && candidate.getType() == DeviceType.PEBBLE){
|
|
|
|
Intent intent = new Intent(this, DeviceCommunicationService.class)
|
|
|
|
.setAction(ACTION_CONNECT)
|
|
|
|
.putExtra(GBDevice.EXTRA_DEVICE, currentDevice);
|
|
|
|
this.startService(intent);
|
|
|
|
currentDevice = candidate;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
showConfig = true; //we are getting incoming configuration data
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
currentDevice = extras.getParcelable(GBDevice.EXTRA_DEVICE);
|
|
|
|
currentUUID = (UUID) extras.getSerializable(DeviceService.EXTRA_APP_UUID);
|
2016-03-03 11:52:30 +01:00
|
|
|
|
2018-03-17 18:50:53 +01:00
|
|
|
if (extras.getBoolean(START_BG_WEBVIEW, false)) {
|
|
|
|
startBackgroundWebViewAndFinish();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
showConfig = extras.getBoolean(SHOW_CONFIG, false);
|
2018-01-14 00:20:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (GBApplication.getGBPrefs().isBackgroundJsEnabled()) {
|
2018-03-17 18:50:53 +01:00
|
|
|
if (showConfig) {
|
2018-01-14 00:20:13 +01:00
|
|
|
Objects.requireNonNull(currentDevice, "Must provide a device when invoking this activity");
|
|
|
|
Objects.requireNonNull(currentUUID, "Must provide a uuid when invoking this activity");
|
2018-03-18 00:39:36 +01:00
|
|
|
WebViewSingleton.getInstance().runJavascriptInterface(this, currentDevice, currentUUID);
|
2016-06-18 18:40:57 +02:00
|
|
|
}
|
2018-01-14 00:20:13 +01:00
|
|
|
|
|
|
|
// 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);
|
2017-09-25 17:12:35 +02:00
|
|
|
} else {
|
2018-01-14 00:20:13 +01:00
|
|
|
LOG.warn("BGJs disabled, not starting webview");
|
|
|
|
}
|
|
|
|
finish();
|
|
|
|
}
|
2017-01-01 18:33:39 +01:00
|
|
|
|
2018-01-14 00:20:13 +01:00
|
|
|
private void setupBGWebView() {
|
|
|
|
setContentView(R.layout.activity_external_pebble_js);
|
2018-03-18 00:39:36 +01:00
|
|
|
myWebView = WebViewSingleton.getInstance().getWebView(this);
|
2018-01-14 00:20:13 +01:00
|
|
|
if (myWebView.getParent() != null) {
|
|
|
|
((ViewGroup) myWebView.getParent()).removeView(myWebView);
|
2017-09-25 17:12:35 +02:00
|
|
|
}
|
2018-01-14 00:20:13 +01:00
|
|
|
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();
|
2020-08-26 04:28:03 +02:00
|
|
|
//noinspection SetJavaScriptEnabled
|
2018-01-14 00:20:13 +01:00
|
|
|
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");
|
2016-03-18 17:50:24 +01:00
|
|
|
}
|
2016-03-20 15:00:05 +01:00
|
|
|
|
2017-02-28 21:11:26 +01:00
|
|
|
@Override
|
|
|
|
protected void onResume() {
|
|
|
|
super.onResume();
|
|
|
|
String queryString = "";
|
|
|
|
if (confUri != null) {
|
|
|
|
//getting back with configuration data
|
|
|
|
LOG.debug("WEBVIEW returned config: " + confUri.toString());
|
|
|
|
try {
|
|
|
|
queryString = confUri.getEncodedQuery();
|
|
|
|
} catch (IllegalArgumentException e) {
|
|
|
|
GB.toast("returned uri: " + confUri.toString(), Toast.LENGTH_LONG, GB.ERROR);
|
|
|
|
}
|
2018-03-17 18:50:53 +01:00
|
|
|
myWebView.stopLoading();
|
2017-02-28 21:11:26 +01:00
|
|
|
myWebView.loadUrl("file:///android_asset/app_config/configure.html?" + queryString);
|
|
|
|
}
|
|
|
|
}
|
2016-12-28 20:53:17 +01:00
|
|
|
@Override
|
2017-01-01 18:33:39 +01:00
|
|
|
protected void onNewIntent(Intent incoming) {
|
|
|
|
incoming.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
|
|
|
|
super.onNewIntent(incoming);
|
|
|
|
confUri = incoming.getData();
|
2016-12-28 20:53:17 +01:00
|
|
|
}
|
2016-03-08 21:44:12 +01:00
|
|
|
|
2016-12-28 20:53:17 +01:00
|
|
|
@Override
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
|
switch (item.getItemId()) {
|
|
|
|
case android.R.id.home:
|
|
|
|
NavUtils.navigateUpFromSameTask(this);
|
|
|
|
return true;
|
2016-03-08 21:44:12 +01:00
|
|
|
}
|
2016-12-28 20:53:17 +01:00
|
|
|
return super.onOptionsItemSelected(item);
|
2016-03-08 21:44:12 +01:00
|
|
|
}
|
|
|
|
|
2016-12-28 20:53:17 +01:00
|
|
|
private class ActivityJSInterface {
|
2016-02-28 22:25:21 +01:00
|
|
|
|
2016-06-18 18:40:57 +02:00
|
|
|
@JavascriptInterface
|
|
|
|
public void closeActivity() {
|
2018-01-13 21:43:08 +01:00
|
|
|
NavUtils.navigateUpFromSameTask(ExternalPebbleJSActivity.this);
|
2016-06-18 18:40:57 +02:00
|
|
|
}
|
2016-02-28 22:25:21 +01:00
|
|
|
}
|
|
|
|
}
|