Skip to content

armgit / core

Public Imported from GitHub
0

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

Add android logout hook and handle back button

Mica Hallin057b7dcbc0a5
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 dbb0d8bdb..07541e3ff 100644
--- a/console/android/src/main/java/org/openremote/android/MainActivity.java
+++ b/console/android/src/main/java/org/openremote/android/MainActivity.java
@@ -1,6 +1,8 @@
package org.openremote.android;
import android.app.Activity;
+import android.app.Notification;
+import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
@@ -10,7 +12,10 @@ import android.net.NetworkInfo;
import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;
+import android.support.v4.app.NotificationBuilderWithBuilderAccessor;
+import android.support.v4.app.NotificationCompat;
import android.util.Log;
+import android.view.KeyEvent;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
@@ -21,6 +26,8 @@ import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
+import org.openremote.android.service.AlertNotification;
+import org.openremote.android.service.NotificationService;
import org.openremote.android.service.TokenService;
@@ -37,6 +44,11 @@ public class MainActivity extends Activity {
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);
@@ -68,6 +80,22 @@ public class MainActivity extends Activity {
unregisterReceiver(connectivityChangeReceiver);
}
+ @Override
+ public boolean onKeyDown(int keyCode, KeyEvent event) {
+ if (event.getAction() == KeyEvent.ACTION_DOWN) {
+ switch (keyCode) {
+ case KeyEvent.KEYCODE_BACK:
+ if (webView.canGoBack()) {
+ webView.goBack();
+ } else {
+ finish();
+ }
+ return true;
+ }
+ }
+ return super.onKeyDown(keyCode, event);
+ }
+
@Override
protected void onSaveInstanceState(Bundle outState) {
webView.saveState(outState);
@@ -137,6 +165,12 @@ public class MainActivity extends Activity {
tokenService = new TokenService(activity);
}
+
+ @JavascriptInterface
+ public void logOut() {
+ tokenService.clearToken();
+ }
+
@JavascriptInterface
public String getMobileToken() {
return tokenService.getJsonToken();
diff --git a/console/android/src/main/java/org/openremote/android/ORFirebaseMessagingService.java b/console/android/src/main/java/org/openremote/android/ORFirebaseMessagingService.java
index c882ebf23..3d45b23b8 100644
--- a/console/android/src/main/java/org/openremote/android/ORFirebaseMessagingService.java
+++ b/console/android/src/main/java/org/openremote/android/ORFirebaseMessagingService.java
@@ -2,6 +2,7 @@ package org.openremote.android;
import android.app.NotificationManager;
import android.app.PendingIntent;
+import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
@@ -31,6 +32,14 @@ public class ORFirebaseMessagingService extends com.google.firebase.messaging.Fi
private TokenService tokenService;
+ public class MyBroadcastReceiver extends BroadcastReceiver {
+ @Override
+ public void onReceive(Context context, Intent intent) {
+ Log.i("NOTIFICATION","TODO: Remove Notification");
+ }
+
+ }
+
@Override
public void onCreate() {
super.onCreate();
@@ -103,27 +112,31 @@ public class ORFirebaseMessagingService extends com.google.firebase.messaging.Fi
private void sendNotification(AlertNotification alertNotification) {
+ int notificationId = alertNotification.getId().hashCode();
Intent intent = new Intent(this, MainActivity.class);
+ intent.putExtra("notifcation", alertNotification);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
- PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
+ PendingIntent pendingIntent = PendingIntent.getActivity(this, notificationId, intent,
PendingIntent.FLAG_ONE_SHOT);
+
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setContentTitle(alertNotification.getTitle())
.setContentText(alertNotification.getMessage())
- .setSmallIcon(R.drawable.ic_launcher)
+
+ .setSmallIcon(R.drawable.app_icon)
.setAutoCancel(true)
.setWhen(0)
.setSound(defaultSoundUri);
for (AlertAction alertAction : alertNotification.getActions()) {
- notificationBuilder = notificationBuilder.addAction(new NotificationCompat.Action(-1, alertAction.getTitle(), pendingIntent));
+ notificationBuilder = notificationBuilder.addAction(new NotificationCompat.Action(R.drawable.empty, alertAction.getTitle(), pendingIntent));
}
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
- notificationManager.notify(alertNotification.getId().hashCode(), notificationBuilder.build());
+ notificationManager.notify(notificationId, notificationBuilder.build());
}
}
\ No newline at end of file
diff --git a/console/android/src/main/java/org/openremote/android/service/AlertNotification.java b/console/android/src/main/java/org/openremote/android/service/AlertNotification.java
index 19f1c9b1f..71961d43d 100644
--- a/console/android/src/main/java/org/openremote/android/service/AlertNotification.java
+++ b/console/android/src/main/java/org/openremote/android/service/AlertNotification.java
@@ -1,9 +1,10 @@
package org.openremote.android.service;
+import java.io.Serializable;
import java.util.List;
-public class AlertNotification {
+public class AlertNotification implements Serializable {
private Long id;
private String title;
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 a6fb3d54e..125f30cef 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
@@ -156,4 +156,12 @@ public class TokenService {
}
});
}
+
+ public void clearToken() {
+ SharedPreferences.Editor editor = sharedPref.edit();
+ editor.remove(tokenKey);
+ editor.remove(refreshTokenKey);
+ editor.remove(tokenIdKey);
+ editor.commit();
+ }
}
diff --git a/console/android/src/main/res/drawable/app_icon.xml b/console/android/src/main/res/drawable/app_icon.xml
new file mode 100644
index 000000000..b8513a82c
--- /dev/null
+++ b/console/android/src/main/res/drawable/app_icon.xml
@@ -0,0 +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>
+</selector>
\ No newline at end of file
diff --git a/console/android/src/main/res/drawable/empty.xml b/console/android/src/main/res/drawable/empty.xml
new file mode 100644
index 000000000..a8b409b1d
--- /dev/null
+++ b/console/android/src/main/res/drawable/empty.xml
@@ -0,0 +1,4 @@
+<?xml version="1.0" encoding="utf-8"?>
+<selector xmlns:android="http://schemas.android.com/apk/res/android">
+
+</selector>
\ No newline at end of file