Skip to content

armgit / core

Public Imported from GitHub
0

100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps

Change Javascript interface and Error View

Mica Hallin8e343877f2e4
diff --git a/console/android/src/main/java/org/openremote/android/MainActivity.java b/console/android/src/main/java/org/openremote/android/MainActivity.java
index 8d8d6cd91..00e4be405 100644
--- a/console/android/src/main/java/org/openremote/android/MainActivity.java
+++ b/console/android/src/main/java/org/openremote/android/MainActivity.java
@@ -1,18 +1,15 @@
package org.openremote.android;
import android.app.Activity;
-import android.app.Notification;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
-import android.content.pm.ApplicationInfo;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.http.SslError;
import android.os.Build;
import android.os.Bundle;
-import android.os.StrictMode;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
@@ -25,8 +22,11 @@ import android.webkit.WebResourceResponse;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
+import android.widget.Button;
+import android.widget.TextView;
-import org.openremote.android.service.AlertNotification;
+import org.json.JSONException;
+import org.json.JSONObject;
import org.openremote.android.service.TokenService;
import java.util.logging.Logger;
@@ -37,8 +37,42 @@ public class MainActivity extends Activity {
private final ConnectivityChangeReceiver connectivityChangeReceiver = new ConnectivityChangeReceiver();
private WebView webView;
- private View errorView;
- private View noConnectivityView;
+
+ private boolean hasNetwork = false;
+
+ private ErrorViewHolder errorViewHolder;
+
+
+ class ErrorViewHolder {
+ View errorView;
+
+ Button btnReload;
+ Button btnExit;
+ TextView tvErrorTitle;
+ TextView tvErrorExplanation;
+
+ public ErrorViewHolder(View errorView) {
+ this.errorView = errorView;
+ btnReload = (Button) errorView.findViewById(R.id.reload);
+ btnExit = (Button) errorView.findViewById(R.id.exit);
+ tvErrorTitle = (TextView) errorView.findViewById(R.id.errorTitle);
+ tvErrorExplanation = (TextView) errorView.findViewById(R.id.errorExplanation);
+ }
+
+ void show(int errorTitle, int errorDescription, boolean canReload, boolean canExit) {
+
+ btnReload.setVisibility(canReload ? View.VISIBLE : View.GONE);
+ btnExit.setVisibility(canExit ? View.VISIBLE : View.GONE);
+
+ tvErrorTitle.setText(errorTitle);
+ tvErrorExplanation.setText(errorDescription);
+ errorView.setVisibility(View.VISIBLE);
+ }
+
+ private void hide() {
+ errorView.setVisibility(View.GONE);
+ }
+ }
@Override
public void onActivityReenter(int resultCode, Intent data) {
@@ -55,28 +89,17 @@ public class MainActivity extends Activity {
WebView.setWebContentsDebuggingEnabled(true);
}
- // TODO Do we need this? Document why
- StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
- StrictMode.setThreadPolicy(policy);
setContentView(R.layout.activity_web);
- if (getIntent().hasExtra("notification")) {
- AlertNotification alertNotification = (AlertNotification) getIntent().getSerializableExtra("notification");
- Notification notification = new Notification();
- }
webView = (WebView) findViewById(R.id.webview);
if (savedInstanceState != null) {
webView.restoreState(savedInstanceState);
} else {
loadIndex();
}
- errorView = findViewById(R.id.errorView);
- noConnectivityView = findViewById(R.id.noConnectivityView);
- ConnectivityManager cm
- = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
- NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
- boolean hasConnectivity = activeNetwork != null && activeNetwork.isConnectedOrConnecting();
+ errorViewHolder = new ErrorViewHolder(findViewById(R.id.errorView));
+
}
@@ -100,7 +123,6 @@ public class MainActivity extends Activity {
@Override
protected void onPause() {
super.onPause();
-
unregisterReceiver(connectivityChangeReceiver);
}
@@ -147,9 +169,12 @@ public class MainActivity extends Activity {
Log.i("WEBVIEW", "request url :" + request.getUrl().toString());
Log.i("WEBVIEW", "error :" + errorResponse.getStatusCode());
if (request.getUrl().toString().startsWith(getString(R.string.OR_BASE_SERVER) + getString(R.string.OR_CONSOLE_URL))) {
- webView.loadUrl("about:blank");
- errorView.setVisibility(View.VISIBLE);
+ errorViewHolder.show(R.string.httpError,R.string.httpErrorExplain,true,true);
}
+
+
+ //
+ //todo: translate common error into user friendly dialog msg ex 400-500 => try again later service not avaible (app clean exit with confirm button)
}
@Override
@@ -165,16 +190,18 @@ public class MainActivity extends Activity {
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
- Log.i("WEBVIEW", "request url :" + request.getUrl().toString());
- Log.i("WEBVIEW", "error :" + error.getErrorCode());
- Log.i("WEBVIEW", "error :" + error.getDescription());
- }
- // Remote debugging sessions from Chrome trigger "ERR_CACHE_MISS" that don't hurt, but we should not redirect the view
- if (isRemoteDebuggingEnabled() && error.getErrorCode() == ERROR_UNKNOWN) {
- return;
+ LOG.info("request url :" + request.getUrl().toString());
+ LOG.info( "error :" + error.getErrorCode());
+ LOG.info( "error :" + error.getDescription());
+ // Remote debugging sessions from Chrome trigger "ERR_CACHE_MISS" that don't hurt, but we should not redirect the view
+ if (isRemoteDebuggingEnabled() && error.getErrorCode() == ERROR_UNKNOWN) {
+ return;
+ }
}
- webView.loadUrl("about:blank");
- errorView.setVisibility(View.VISIBLE);
+
+ //TODO => try again later service not avaible (app clean exit with confirm button) // good message
+ //webView.loadUrl("about:blank");
+ errorViewHolder.show(R.string.fatalError,R.string.fatalErrorExplain,false,true);
}
});
webView.setWebChromeClient(new WebChromeClient());
@@ -185,9 +212,15 @@ public class MainActivity extends Activity {
webView.loadUrl(url);
}
- public void reloadPage() {
- errorView.setVisibility(View.GONE);
- webView.loadUrl(getString(R.string.OR_BASE_SERVER) + getString(R.string.OR_CONSOLE_URL));
+
+
+ public void reloadPage(View view) {
+ errorViewHolder.hide();
+ webView.loadUrl(webView.getUrl());
+ }
+
+ public void exitOnClick(View view) {
+ finish();
}
private class WebAppInterface {
@@ -212,14 +245,42 @@ public class MainActivity extends Activity {
public void setMobileToken(String token, String refreshToken, String idToken) {
tokenService.saveToken(token, refreshToken, idToken);
}
+
+ @JavascriptInterface
+ public void postMessage(String jsonMessage) throws JSONException {
+ JSONObject reader = new JSONObject(jsonMessage);
+ String messageType = reader.getString("type");
+ JSONObject data = reader.getJSONObject("data");
+ switch (messageType) {
+ case "token" :
+ tokenService.saveToken(data.getString("token"), data.getString("refreshToken") , data.getString("idToken"));
+ break;
+ case "error" :
+ //
+ default:
+ }
+ }
+
+ @JavascriptInterface
+ public String getMessage(String messageKey) {
+ switch (messageKey) {
+ case "token" :
+ return tokenService.getJsonToken();
+ default:
+ return "{}";
+ }
+ }
+ //TODO receive error with severity
}
private void onConnectivityChanged(boolean connectivity) {
+ this.hasNetwork = connectivity;
if (connectivity) {
- noConnectivityView.setVisibility(View.GONE);
+ //TODO : pass conectivity information to wwebapp
+ reloadPage(getCurrentFocus());
} else {
- noConnectivityView.setVisibility(View.VISIBLE);
+ errorViewHolder.show(R.string.noConnectivity,R.string.noConnectivityExplain,false,true);
}
}
diff --git a/console/android/src/main/java/org/openremote/android/ORMessagingActionService.java b/console/android/src/main/java/org/openremote/android/ORMessagingActionService.java
index e62d71f84..a436fd280 100644
--- a/console/android/src/main/java/org/openremote/android/ORMessagingActionService.java
+++ b/console/android/src/main/java/org/openremote/android/ORMessagingActionService.java
@@ -33,9 +33,9 @@ public class ORMessagingActionService extends IntentService {
manager.cancel(notification.getId().hashCode());
AlertAction alertAction = (AlertAction) intent.getSerializableExtra("action");
- if(AlertNotification.ActionType.ACTUATOR.equals(alertAction.getType())) {
+ if(alertAction != null && AlertNotification.ActionType.ACTUATOR.equals(alertAction.getType())) {
tokenService.executeAction(alertAction);
- } else {
+ } else if (intent.hasExtra("url")) {
Intent activityIntent = new Intent(this, MainActivity.class);
activityIntent.setAction(Intent.ACTION_MAIN);
activityIntent.addCategory(Intent.CATEGORY_LAUNCHER);
diff --git a/console/android/src/main/java/org/openremote/android/service/TokenService.java b/console/android/src/main/java/org/openremote/android/service/TokenService.java
index e9b910294..8c9b0ad6e 100644
--- a/console/android/src/main/java/org/openremote/android/service/TokenService.java
+++ b/console/android/src/main/java/org/openremote/android/service/TokenService.java
@@ -137,19 +137,25 @@ public class TokenService {
@Override
public void onToken(String accessToken) {
Call call = notificationService.updateToken(realm, accessToken, fcmToken, id, "ANDROID");
- try {
- Response response = call.execute();
- if (response.code() != 204) {
- Log.e("TOKEN_SERVICE", "save fcm token failed");
- } else {
- SharedPreferences.Editor editor = sharedPref.edit();
- editor.remove(fcmTokenKey);
- editor.remove(deviceIdKey);
- editor.commit();
- }
- } catch (IOException e) {
- Log.e("TOKEN_SERVICE", "save fcm token failed ");
- }
+
+ call.enqueue(new Callback() {
+ @Override
+ public void onResponse(Call call, Response response) {
+ if (response.code() != 204) {
+ Log.e("TOKEN_SERVICE", "save fcm token failed");
+ } else {
+ SharedPreferences.Editor editor = sharedPref.edit();
+ editor.remove(fcmTokenKey);
+ editor.remove(deviceIdKey);
+ editor.commit();
+ }
+ }
+
+ @Override
+ public void onFailure(Call call, Throwable t) {
+ Log.e("TOKEN_SERVICE", "save fcm token failed ");
+ }
+ });
}
diff --git a/console/android/src/main/res/drawable/app_icon.xml b/console/android/src/main/res/drawable/app_icon.xml
index b8513a82c..87cba36ae 100644
--- a/console/android/src/main/res/drawable/app_icon.xml
+++ b/console/android/src/main/res/drawable/app_icon.xml
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
- <item android:drawable="@drawable/ic_launcher"></item>
+ <item android:drawable="@drawable/ic_launcher" />
</selector>
\ No newline at end of file
diff --git a/console/android/src/main/res/layout/activity_web.xml b/console/android/src/main/res/layout/activity_web.xml
index af5a3b621..5d5d8bcd2 100644
--- a/console/android/src/main/res/layout/activity_web.xml
+++ b/console/android/src/main/res/layout/activity_web.xml
@@ -16,5 +16,5 @@
</WebView>
<include layout="@layout/error_layout"></include>
- <include layout="@layout/no_connectivity_layout"></include>
+ <include layout="@layout/error_layout"></include>
</RelativeLayout>
diff --git a/console/android/src/main/res/layout/error_layout.xml b/console/android/src/main/res/layout/error_layout.xml
index 0ba51e57a..a5fe7d37c 100644
--- a/console/android/src/main/res/layout/error_layout.xml
+++ b/console/android/src/main/res/layout/error_layout.xml
@@ -1,16 +1,66 @@
<?xml version="1.0" encoding="utf-8"?>
-<android.support.v7.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
+<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
+ xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/errorView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentEnd="true"
android:layout_alignParentStart="true"
android:gravity="center"
+ android:background="@color/white"
android:visibility="gone">
- <Button
- android:layout_width="wrap_content"
+ <TextView
+ android:layout_width="wrap_content"
android:layout_height="wrap_content"
- android:onClick="reloadPage"
- android:text="@string/reload" />
-</android.support.v7.widget.LinearLayoutCompat>
\ No newline at end of file
+ android:text="@string/noConnectivity"
+
+ android:id="@+id/errorTitle"
+ android:layout_centerVertical="true"
+ android:layout_centerHorizontal="true" />
+
+ <TextView
+ android:id="@+id/errorExplanation"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:gravity="center"
+ android:text="@string/noConnectivityExplain"
+ android:layout_margin="20dp"
+ android:layout_below="@+id/errorTitle"
+ android:layout_centerHorizontal="true" />
+
+ <LinearLayout
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+
+ android:orientation="vertical"
+ android:layout_below="@+id/errorExplanation"
+ android:layout_centerHorizontal="true">
+
+ <Button
+ android:id="@+id/reload"
+ android:layout_width="match_parent"
+ android:layout_height="wrap_content"
+ android:onClick="reloadPage"
+ android:text="@string/reload"
+ tools:text="reload" />
+
+ <Button
+ android:id="@+id/exit"
+ android:layout_width="match_parent"
+ android:layout_height="wrap_content"
+ android:onClick="exitOnClick"
+ android:text="@string/exit"
+ tools:text="exit" />
+ </LinearLayout>
+
+ <ImageView
+ android:layout_width="48dp"
+ android:layout_height="48dp"
+ android:src="@drawable/app_icon"
+ android:layout_marginBottom="16dp"
+ android:layout_above="@+id/errorTitle"
+ android:layout_centerHorizontal="true" />
+
+
+</RelativeLayout>
\ No newline at end of file
diff --git a/console/android/src/main/res/layout/no_connectivity_layout.xml b/console/android/src/main/res/layout/no_connectivity_layout.xml
deleted file mode 100644
index 0e199a1ae..000000000
--- a/console/android/src/main/res/layout/no_connectivity_layout.xml
+++ /dev/null
@@ -1,16 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<android.support.v7.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/noConnectivityView"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_alignParentEnd="true"
- android:layout_alignParentStart="true"
- android:gravity="center"
- android:background="@color/white"
- android:visibility="gone">
-
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/noConnectivity" />
-</android.support.v7.widget.LinearLayoutCompat>
\ No newline at end of file
diff --git a/console/android/src/main/res/values/strings.xml b/console/android/src/main/res/values/strings.xml
index bb5191ba1..ba97f7b36 100644
--- a/console/android/src/main/res/values/strings.xml
+++ b/console/android/src/main/res/values/strings.xml
@@ -11,7 +11,13 @@
<string name="SHARED_PREF_DEVICE_ID">FCM_DEVICE_ID_KEY</string>
<string name="reload">reload</string>
<string name="noConnectivity">No connectivity Detected</string>
+ <string name="noConnectivityExplain">This application can not work without internet connectivity, you can wait to have it back or exit app</string>
<string name="SHARED_PREF_OPENED_ALERT">OPENED_ALERT</string>
<string name="AUTH_URL">/auth</string>
<string name="SSL_IGNORE">false</string>
+ <string name="exit">exit</string>
+ <string name="fatalErrorExplain">Blok61 console is avaible, please try again later</string>
+ <string name="fatalError">Something go wrong</string>
+ <string name="httpError">Failled to contact Blok61 server</string>
+ <string name="httpErrorExplain">Something went wrong when communicating with blok61.</string>
</resources>