2016-02-28 22:25:21 +01:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.activities;
|
|
|
|
|
2016-06-18 21:08:51 +02:00
|
|
|
import android.content.Context;
|
2016-03-13 08:31:50 +01:00
|
|
|
import android.content.Intent;
|
2016-02-28 22:25:21 +01:00
|
|
|
import android.net.Uri;
|
2016-12-28 20:53:17 +01:00
|
|
|
import android.os.Build;
|
2016-02-28 22:25:21 +01:00
|
|
|
import android.os.Bundle;
|
2016-03-03 13:05:51 +01:00
|
|
|
import android.support.v4.app.NavUtils;
|
|
|
|
import android.view.MenuItem;
|
2017-01-01 18:33:39 +01:00
|
|
|
import android.view.View;
|
2016-02-28 22:25:21 +01:00
|
|
|
import android.webkit.JavascriptInterface;
|
2016-12-28 20:53:17 +01:00
|
|
|
import android.webkit.ValueCallback;
|
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;
|
|
|
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
import java.util.UUID;
|
|
|
|
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.R;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
2016-06-18 18:40:57 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.DeviceService;
|
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
|
|
|
|
2016-04-14 15:21:25 +02:00
|
|
|
public class ExternalPebbleJSActivity extends GBActivity {
|
2016-02-28 22:25:21 +01:00
|
|
|
|
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(ExternalPebbleJSActivity.class);
|
|
|
|
|
|
|
|
private UUID appUuid;
|
2016-06-18 18:40:57 +02:00
|
|
|
private Uri confUri;
|
2016-03-03 11:52:30 +01:00
|
|
|
private GBDevice mGBDevice = null;
|
2016-06-18 18:40:57 +02:00
|
|
|
private WebView myWebView;
|
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();
|
|
|
|
if (extras != null) {
|
|
|
|
mGBDevice = extras.getParcelable(GBDevice.EXTRA_DEVICE);
|
2016-06-18 18:40:57 +02:00
|
|
|
appUuid = (UUID) extras.getSerializable(DeviceService.EXTRA_APP_UUID);
|
2016-03-03 11:52:30 +01:00
|
|
|
} else {
|
|
|
|
throw new IllegalArgumentException("Must provide a device when invoking this activity");
|
|
|
|
}
|
|
|
|
|
2016-02-28 22:25:21 +01:00
|
|
|
setContentView(R.layout.activity_external_pebble_js);
|
|
|
|
|
2017-01-01 18:33:39 +01:00
|
|
|
myWebView = WebViewSingleton.getorInitWebView(this, mGBDevice, appUuid);
|
|
|
|
myWebView.setWillNotDraw(false);
|
|
|
|
myWebView.addJavascriptInterface(new ActivityJSInterface(ExternalPebbleJSActivity.this), "GBActivity");
|
|
|
|
FrameLayout fl = (FrameLayout) findViewById(R.id.webview_placeholder);
|
|
|
|
fl.addView(myWebView);
|
2016-02-28 22:25:21 +01:00
|
|
|
|
2016-06-18 18:40:57 +02:00
|
|
|
|
2017-01-01 18:33:39 +01:00
|
|
|
myWebView.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {
|
|
|
|
@Override
|
|
|
|
public void onViewAttachedToWindow(View v) {
|
2016-06-18 18:40:57 +02:00
|
|
|
|
|
|
|
|
2017-01-01 18:33:39 +01:00
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
|
|
|
|
// chromium, enable hardware acceleration
|
|
|
|
v.setLayerType(View.LAYER_TYPE_HARDWARE, null);
|
|
|
|
} else {
|
|
|
|
// older android version, disable hardware acceleration
|
|
|
|
v.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
|
|
|
|
}
|
2016-12-28 20:53:17 +01:00
|
|
|
|
|
|
|
|
2017-01-01 18:33:39 +01:00
|
|
|
String queryString = "";
|
|
|
|
if (confUri != null) {
|
|
|
|
//getting back with configuration data
|
|
|
|
try {
|
|
|
|
appUuid = UUID.fromString(confUri.getHost());
|
|
|
|
queryString = confUri.getEncodedQuery();
|
|
|
|
} catch (IllegalArgumentException e) {
|
|
|
|
GB.toast("returned uri: " + confUri.toString(), Toast.LENGTH_LONG, GB.ERROR);
|
|
|
|
}
|
|
|
|
((WebView) v).loadUrl("file:///android_asset/app_config/configure.html?" + queryString);
|
|
|
|
} else {
|
|
|
|
//show configuration
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
|
|
|
|
myWebView.evaluateJavascript("Pebble.evaluate('showConfiguration');", new ValueCallback<String>() {
|
|
|
|
@Override
|
|
|
|
public void onReceiveValue(String s) {
|
|
|
|
LOG.debug("Callback from showConfiguration", s);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
((WebView) v).loadUrl("javascript:Pebble.evaluate('showConfiguration');");
|
|
|
|
}
|
|
|
|
}
|
2016-12-28 20:53:17 +01:00
|
|
|
|
|
|
|
|
2016-06-18 18:40:57 +02:00
|
|
|
}
|
2017-01-01 18:33:39 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onViewDetachedFromWindow(View v) {
|
|
|
|
myWebView.removeJavascriptInterface("GBActivity");
|
|
|
|
myWebView.setWillNotDraw(true);
|
|
|
|
FrameLayout fl = (FrameLayout) findViewById(R.id.webview_placeholder);
|
|
|
|
fl.removeAllViews();
|
2016-03-21 21:19:32 +01:00
|
|
|
}
|
2017-01-01 18:33:39 +01:00
|
|
|
});
|
2016-03-21 21:19:32 +01:00
|
|
|
|
2016-03-18 17:50:24 +01:00
|
|
|
}
|
2016-03-20 15:00:05 +01:00
|
|
|
|
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 21:08:51 +02:00
|
|
|
Context mContext;
|
|
|
|
|
2016-12-28 20:53:17 +01:00
|
|
|
public ActivityJSInterface(Context c) {
|
2016-06-18 21:08:51 +02:00
|
|
|
mContext = c;
|
2016-02-28 22:25:21 +01:00
|
|
|
}
|
|
|
|
|
2016-06-18 18:40:57 +02:00
|
|
|
@JavascriptInterface
|
|
|
|
public void closeActivity() {
|
2016-06-18 21:08:51 +02:00
|
|
|
NavUtils.navigateUpFromSameTask((ExternalPebbleJSActivity) mContext);
|
2016-06-18 18:40:57 +02:00
|
|
|
}
|
2016-02-28 22:25:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|