100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps
Java asset-managementbuildingdashboarddataplatformdevice-managementedge-computingenergyenergy-managementenergy-monitorfleet-managementfleet-telematicsiotiot-applicationiot-platformmodbusmqttmqtt-brokerota-updaterules-enginesmartcity
Added GeofenceProvider for Android. Finished iOS code for GeofenceProvider. Still needs refactoring
Michael Rademakerfae479040d3a
diff --git a/console/android/build.gradle b/console/android/build.gradleindex cd8ca7085..f856e66cd 100644--- a/console/android/build.gradle+++ b/console/android/build.gradle@@ -28,19 +28,22 @@ repositories {}apply plugin: "com.android.library"+apply plugin: 'kotlin-android'version = projectVersiondependencies {- compile "com.squareup.okhttp3:okhttp:$okhttp3Version"- compile "com.squareup.retrofit2:retrofit:$retrofitVersion"- compile "com.android.support:support-compat:$androidSupportVersion"- compile "com.android.support:appcompat-v7:$androidSupportVersion"- compile "com.google.firebase:firebase-core:$fcmVersion"- compile "com.google.firebase:firebase-messaging:$fcmVersion"- compile "com.squareup.retrofit2:converter-jackson:$jacksonVersion"- compile "com.squareup.retrofit2:converter-scalars:$retrofitVersion"+ implementation "com.squareup.okhttp3:okhttp:$okhttp3Version"+ implementation "com.squareup.retrofit2:retrofit:$retrofitVersion"+ api "com.android.support:support-compat:$androidSupportVersion"+ api "com.android.support:appcompat-v7:$androidSupportVersion"+ api "com.google.firebase:firebase-core:$fcmVersion"+ implementation "com.google.firebase:firebase-messaging:$fcmVersion"+ implementation "com.squareup.retrofit2:converter-jackson:$jacksonVersion"+ implementation "com.squareup.retrofit2:converter-scalars:$retrofitVersion"+ implementation "com.google.android.gms:play-services-location:$googlePlayVersion"+ implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"}android {@@ -80,5 +83,5 @@ android {buildTypes {}- buildToolsVersion '26.0.2'+ buildToolsVersion '27.0.3'}diff --git a/console/android/gradle.properties b/console/android/gradle.propertiesindex 7b1a10f5d..4caffda8e 100644--- a/console/android/gradle.properties+++ b/console/android/gradle.properties@@ -16,5 +16,6 @@ androidSupportVersion = 25.1.1okhttp3Version = 3.6.0retrofitVersion = 2.2.0-fcmVersion = 10.2.0+googlePlayVersion = 15.0.1+fcmVersion = 15.0.2jacksonVersion = 2.2.0diff --git a/console/android/src/main/AndroidManifest.xml b/console/android/src/main/AndroidManifest.xmlindex 1214630dc..d30903779 100644--- a/console/android/src/main/AndroidManifest.xml+++ b/console/android/src/main/AndroidManifest.xml@@ -2,6 +2,7 @@<manifest xmlns:android="http://schemas.android.com/apk/res/android"package="org.openremote.android">+ <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/><uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /><uses-permission android:name="android.permission.INTERNET" /><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />@@ -55,6 +56,9 @@</intent-filter></service>+ <service android:name=".service.GeofenceTransitionsIntentService"/>++</application>diff --git a/console/android/src/main/java/org/openremote/android/MainActivity.java b/console/android/src/main/java/org/openremote/android/MainActivity.javaindex fb307e3db..ea0f980c1 100644--- a/console/android/src/main/java/org/openremote/android/MainActivity.java+++ b/console/android/src/main/java/org/openremote/android/MainActivity.java@@ -38,10 +38,14 @@ import android.widget.Button;import android.widget.TextView;import android.widget.Toast;+import com.fasterxml.jackson.databind.ObjectMapper;+import org.json.JSONException;import org.json.JSONObject;+import org.openremote.android.service.GeofenceProvider;import org.openremote.android.service.TokenService;+import java.util.Map;import java.util.logging.Logger;public class MainActivity extends Activity {@@ -55,6 +59,8 @@ public class MainActivity extends Activity {protected ErrorViewHolder errorViewHolder;protected Context context;+ protected GeofenceProvider geofenceProvider;+BroadcastReceiver onDownloadCompleteReciever = new BroadcastReceiver() {public void onReceive(Context ctxt, Intent intent) {String action = intent.getAction();@@ -199,7 +205,7 @@ public class MainActivity extends Activity {protected void initializeWebView() {LOG.fine("Initializing web view");- final WebAppInterface webAppInterface = new WebAppInterface(this);+ final WebAppInterface webAppInterface = new WebAppInterface(this, webView);webView.addJavascriptInterface(webAppInterface, "MobileInterface");WebSettings webSettings = webView.getSettings();@@ -220,19 +226,19 @@ public class MainActivity extends Activity {public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {//TODO should we ignore images?if (request.getUrl().getLastPathSegment() != null &&- (request.getUrl().getLastPathSegment().endsWith("png")- || request.getUrl().getLastPathSegment().endsWith("jpg")- || request.getUrl().getLastPathSegment().endsWith("ico"))- )+ (request.getUrl().getLastPathSegment().endsWith("png")+ || request.getUrl().getLastPathSegment().endsWith("jpg")+ || request.getUrl().getLastPathSegment().endsWith("ico"))+ )return;// When initialising Keycloak with an invalid offline refresh token (e.g. wrong nonce because// server was reinstalled), we detect the failure and then don't show an error view. We clear the stored// invalid token. The web app will then start a new login.if (request.getUrl().getLastPathSegment() != null &&- request.getUrl().getLastPathSegment().equals("token") &&- request.getMethod().equals("POST") &&- errorResponse.getStatusCode() == 400) {+ request.getUrl().getLastPathSegment().equals("token") &&+ request.getMethod().equals("POST") &&+ errorResponse.getStatusCode() == 400) {webAppInterface.tokenService.clearToken();return;}@@ -257,10 +263,10 @@ public class MainActivity extends Activity {public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {//TODO should we ignore images?if (request.getUrl().getLastPathSegment() != null &&- (request.getUrl().getLastPathSegment().endsWith("png")- || request.getUrl().getLastPathSegment().endsWith("jpg")- || request.getUrl().getLastPathSegment().endsWith("ico"))- )+ (request.getUrl().getLastPathSegment().endsWith("png")+ || request.getUrl().getLastPathSegment().endsWith("jpg")+ || request.getUrl().getLastPathSegment().endsWith("ico"))+ )return;if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {@@ -309,7 +315,7 @@ public class MainActivity extends Activity {ActivityCompat.requestPermissions((MainActivity) context, new String[]{writePermission}, WRITE_PERMISSION_REQUEST);} else {DownloadManager.Request request = new- DownloadManager.Request(Uri.parse(url));+ DownloadManager.Request(Uri.parse(url));request.setMimeType(mimetype);//------------------------COOKIE!!------------------------@@ -340,6 +346,21 @@ public class MainActivity extends Activity {if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {Toast.makeText(getApplicationContext(), R.string.downloading_file, Toast.LENGTH_LONG).show();}+ } else if (requestCode == GeofenceProvider.Companion.getLocationReponseCode()) {+ if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {+ try {+ Map<String, Object> initData = geofenceProvider.initialize();+ final String jsonString = new ObjectMapper().writeValueAsString(initData);+ this.runOnUiThread(new Runnable() {+ @Override+ public void run() {+ webView.evaluateJavascript(String.format("openremote.INSTANCE.console.handleProviderResponse('%s')", jsonString), null);+ }+ });+ } catch (Exception e) {+ e.printStackTrace();+ }+ }} else {super.onRequestPermissionsResult(requestCode, permissions, grantResults);}@@ -347,10 +368,14 @@ public class MainActivity extends Activity {protected class WebAppInterface {+ private final Activity activity;private final TokenService tokenService;+ private final WebView webView;- public WebAppInterface(Activity activity) {+ public WebAppInterface(Activity activity, WebView webView) {tokenService = new TokenService(activity);+ this.activity = activity;+ this.webView = webView;}@JavascriptInterface@@ -382,6 +407,53 @@ public class MainActivity extends Activity {}});break;+ case "provider":+ String action = data.getString("action");+ if (action != null) {+ String provider = data.getString("provider");+ if (provider.equalsIgnoreCase("geofence")) {+ if (action.equalsIgnoreCase("PROVIDER_INIT")) {+ geofenceProvider = new GeofenceProvider(activity);+ if (!geofenceProvider.checkPermission()) {+ geofenceProvider.registerPermissions();+ } else {+ try {+ Map<String, Object> initData = geofenceProvider.initialize();+ final String jsonString = new ObjectMapper().writeValueAsString(initData);+ activity.runOnUiThread(new Runnable() {+ @Override+ public void run() {+ webView.evaluateJavascript(String.format("openremote.INSTANCE.console.handleProviderResponse('%s')", jsonString), null);+ }+ });+ } catch (Exception e) {+ e.printStackTrace();+ }+ }+ } else if (action.equalsIgnoreCase("PROVIDER_ENABLE")) {+ JSONObject innerData = data.getJSONObject("data");+ if (innerData != null) {+ String consoleId = innerData.getString("consoleId");+ if (consoleId != null) {+ try {+ Map<String, Object> enableData = geofenceProvider.enable(String.format("%s/%s", getString(R.string.OR_BASE_SERVER), getString(R.string.OR_REALM)), consoleId);+ final String jsonString = new ObjectMapper().writeValueAsString(enableData);++ activity.runOnUiThread(new Runnable() {+ @Override+ public void run() {+ webView.evaluateJavascript(String.format("openremote.INSTANCE.console.handleProviderResponse('%s')", jsonString), null);+ }+ });+ } catch (Exception e) {+ e.printStackTrace();+ }+ }+ }+ }+ }+ }+ break;default:}}@@ -413,7 +485,7 @@ public class MainActivity extends Activity {@Overridepublic void onReceive(Context context, Intent intent) {ConnectivityManager cm- = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);+ = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);NetworkInfo activeNetwork = cm.getActiveNetworkInfo();onConnectivityChanged(activeNetwork != null && activeNetwork.isConnectedOrConnecting());@@ -423,7 +495,6 @@ public class MainActivity extends Activity {protected boolean isRemoteDebuggingEnabled() {return Boolean.valueOf(getString(R.string.ENABLE_REMOTE_DEBUGGING));}-}diff --git a/manager/src/main/java/org/openremote/manager/rules/AssetQueryPredicate.java b/manager/src/main/java/org/openremote/manager/rules/AssetQueryPredicate.javaindex 4638c1d5e..ed9fe7443 100644--- a/manager/src/main/java/org/openremote/manager/rules/AssetQueryPredicate.java+++ b/manager/src/main/java/org/openremote/manager/rules/AssetQueryPredicate.java@@ -215,6 +215,7 @@ public class AssetQueryPredicate implements Predicate<AssetState> {protected Predicate<Coordinate> asPredicate(LocationPredicate predicate) {return coordinate -> {if (predicate instanceof RadialLocationPredicate) {+ //TODO geotools version to gradle propertiesRadialLocationPredicate radialLocationPredicate = (RadialLocationPredicate) predicate;GeodeticCalculator calculator = new GeodeticCalculator();calculator.setStartingGeographicPoint(radialLocationPredicate.lng, radialLocationPredicate.lat);