Skip to content

Commit

Permalink
Add notification when tracker is being moved
Browse files Browse the repository at this point in the history
  • Loading branch information
Philipp0002 committed Mar 17, 2024
1 parent c86ee61 commit 840434e
Show file tree
Hide file tree
Showing 15 changed files with 407 additions and 38 deletions.
6 changes: 4 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ plugins {

android {
namespace 'de.raffaelhahn.xadgps_client'
compileSdk 33
compileSdk 34

defaultConfig {
applicationId "de.raffaelhahn.xadgps_client"
minSdk 24
targetSdk 33
targetSdk 34
versionCode 1
versionName "1.0"

Expand Down Expand Up @@ -43,4 +43,6 @@ dependencies {
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
implementation 'org.osmdroid:osmdroid-android:6.1.17'
implementation "androidx.work:work-runtime:2.9.0"

}
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>

<application
android:allowBackup="true"
Expand Down
22 changes: 1 addition & 21 deletions app/src/main/java/de/raffaelhahn/xadgps_client/Device.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
import java.lang.reflect.Field;
import java.util.Iterator;

public class Device {
public String id;
public String name;
public class Device extends NotifyDevice {
public String sn;
public String groupID;
public String groupName;
Expand All @@ -21,10 +19,6 @@ public class Device {
public String iccid;
public String icon;
public String iconid;
public String olat;
public String olng;
public String latitude;
public String longitude;
public String StopTime;
public String iconurl;
public String course;
Expand All @@ -41,20 +35,6 @@ public class Device {
public String GPS;
public String GSM;

public void setFromJson(JSONObject jsonObject) {
Iterator<String> keys = jsonObject.keys();
while(keys.hasNext()) {
String key = keys.next();
try {
Field f = Device.class.getDeclaredField(key);
f.setAccessible(true);
f.set(this, jsonObject.get(key));
} catch (Throwable e) {
Log.w("Device", "setFromJson: " + e.getMessage());
}
}
}

public String[] getDeviceInfoShort(Context context) {
String[] description = new String[]{"?", ""};
if(isStop != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package de.raffaelhahn.xadgps_client;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand All @@ -12,11 +13,13 @@
import androidx.annotation.Nullable;

import com.google.android.material.bottomsheet.BottomSheetDialogFragment;
import com.google.android.material.button.MaterialButton;

import java.util.ArrayList;
import java.util.Optional;

import de.raffaelhahn.xadgps_client.services.DeviceListService;
import de.raffaelhahn.xadgps_client.services.MovementMonitorService;

public class DeviceInfoSheet extends BottomSheetDialogFragment implements DeviceListService.DeviceListUpdateListener {

Expand Down Expand Up @@ -48,6 +51,9 @@ public class DeviceInfoSheet extends BottomSheetDialogFragment implements Device
private TextView deviceSpeed;
private TextView deviceDistance;
private Button deviceShowOnMapButton;
private MaterialButton deviceNotifyButton;

private Device deviceRef;

public DeviceInfoSheet() {
}
Expand Down Expand Up @@ -103,12 +109,15 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
deviceSpeed = view.findViewById(R.id.deviceSpeed);
deviceDistance = view.findViewById(R.id.deviceDistance);
deviceShowOnMapButton = view.findViewById(R.id.deviceShowOnMap);
deviceNotifyButton = view.findViewById(R.id.deviceNotify);

deviceShowOnMapButton.setVisibility(View.GONE);
deviceShowOnMapButton.setOnClickListener(v -> showDeviceOnMap());
deviceNotifyButton.setOnClickListener(v -> notifyOnMovement());
}

private void updateView(Device device) {
this.deviceRef = device;
int gpsSignal = Integer.parseInt(device.GPS);
int gsmSignal = Integer.parseInt(device.GSM);
deviceName.setText(device.name);
Expand Down Expand Up @@ -136,6 +145,11 @@ private void updateView(Device device) {
deviceSpeed.setText(device.speed + " km/h");
deviceDistance.setText(device.distance);
deviceShowOnMapButton.setVisibility(View.VISIBLE);
if(MovementMonitorService.isMonitoring(getActivity(), deviceRef)) {
deviceNotifyButton.setIconResource(R.drawable.no_notify);
} else {
deviceNotifyButton.setIconResource(R.drawable.notify);
}
}

@Override
Expand Down Expand Up @@ -170,4 +184,16 @@ public void showDeviceOnMap() {
((MainActivity)getActivity()).showDeviceOnMap(id);
this.dismiss();
}

private void notifyOnMovement() {
if(!MovementMonitorService.isMonitoring(getActivity(), deviceRef)) {
boolean started = MovementMonitorService.startMonitoring(getActivity(), deviceRef);
if(started) {
deviceNotifyButton.setIconResource(R.drawable.no_notify);
}
} else {
MovementMonitorService.stopMonitoring(getActivity(), deviceRef);
deviceNotifyButton.setIconResource(R.drawable.notify);
}
}
}
67 changes: 67 additions & 0 deletions app/src/main/java/de/raffaelhahn/xadgps_client/NotifyDevice.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package de.raffaelhahn.xadgps_client;

import android.content.Context;
import android.util.Log;

import org.json.JSONObject;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Optional;

public class NotifyDevice {
public String id;
public String name;
public String olat;
public String olng;
public String latitude;
public String longitude;

public NotifyDevice setFromJson(JSONObject jsonObject) {
Field[] fields = getDeclaredFields(getClass());
Iterator<String> keys = jsonObject.keys();
while(keys.hasNext()) {
String key = keys.next();
try {
Optional<Field> possibleField = Arrays.stream(fields).filter(x -> x.getName().equals(key)).findFirst();
if(possibleField.isPresent()) {
Field f = possibleField.get();
f.setAccessible(true);
f.set(this, jsonObject.get(key));
} else {
throw new RuntimeException("No field " + key + " in class " + getClass().getName());
}
} catch (Throwable e) {
Log.w(getClass().getSimpleName(), "setFromJson: " + e.getMessage());
}
}
return this;
}

public JSONObject getAsJson() {
JSONObject jsonObject = new JSONObject();
try {
for(Field field : getDeclaredFields(getClass())) {
field.setAccessible(true);
jsonObject.put(field.getName(), field.get(this));
}
} catch (Throwable e) {
Log.w(getClass().getSimpleName(), "getAsJson: " + e.getMessage());
}
return jsonObject;
}

private Field[] getDeclaredFields(Class clazz) {
final Field[] fields = clazz.getDeclaredFields();

if ( clazz.getSuperclass() != Object.class ) {
final Field[] pFields = getDeclaredFields(clazz.getSuperclass());
final Field[] allFields = new Field[fields.length + pFields.length];
Arrays.setAll(allFields, i -> (i < pFields.length ? pFields[i] : fields[i - pFields.length]));
return allFields;
} else
return fields;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ public class Constants {
public static final String API_URL = "https://appapi.xadgps.com/openapiv4.asmx";
public static final String APP_ID = "25";
public static final String SP_NAME = "de.raffaelhahn.xadgps_client";

public static final String NOTIFY_DEVICES_PREF_KEY = "notifyDevices";
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public class GetDeviceListAsync extends AsyncTask<Void, Void, Void> {
public String paramMapType;
public String paramLanguage;

public JSONObject resultObject;

@Override
protected Void doInBackground(Void... voids) {
try {
Expand All @@ -30,10 +32,9 @@ protected Void doInBackground(Void... voids) {
}

public void runFetch() throws Exception {
JSONObject obj = null;
try {
Log.d("TESTT", Constants.API_URL + "/GetDeviceList?ID=" + paramUserId + "&TypeID=" + paramTypeId + "&MapType=" + paramMapType + "&Language=" + paramLanguage);
obj = AsyncUtils.readJsonFromUrl(Constants.API_URL + "/GetDeviceList?ID=" + paramUserId + "&TypeID=" + paramTypeId + "&MapType=" + paramMapType + "&Language=" + paramLanguage);
resultObject = AsyncUtils.readJsonFromUrl(Constants.API_URL + "/GetDeviceList?ID=" + paramUserId + "&TypeID=" + paramTypeId + "&MapType=" + paramMapType + "&Language=" + paramLanguage);
} catch (java.io.FileNotFoundException e) {
e.printStackTrace();
} catch (JSONException e) {
Expand All @@ -45,12 +46,14 @@ public void runFetch() throws Exception {
}


if (obj != null) {
callback.received(obj);
} else {
callback.error();
if(callback != null) {
if (resultObject != null) {
callback.received(resultObject);
} else {
callback.error();
}
callback.finished();
}
callback.finished();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package de.raffaelhahn.xadgps_client.background;

import android.content.Context;

import androidx.work.BackoffPolicy;
import androidx.work.Constraints;
import androidx.work.ExistingPeriodicWorkPolicy;
import androidx.work.NetworkType;
import androidx.work.PeriodicWorkRequest;
import androidx.work.WorkManager;

import java.util.concurrent.TimeUnit;

public class BackgroundWorkService {

public static void createWorkRequestMovementDetection(Context context) {
Constraints constraints = new Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
.build();

PeriodicWorkRequest workRequest =
new PeriodicWorkRequest.Builder(
NotificationWorker.class,
PeriodicWorkRequest.MIN_PERIODIC_INTERVAL_MILLIS,
TimeUnit.MILLISECONDS
)
.setBackoffCriteria(
BackoffPolicy.LINEAR,
PeriodicWorkRequest.MIN_BACKOFF_MILLIS,
TimeUnit.MILLISECONDS)
.setConstraints(constraints)
.build();

WorkManager.getInstance(context).enqueueUniquePeriodicWork(
"notificationWork",
ExistingPeriodicWorkPolicy.UPDATE,
workRequest);
}

}
Loading

0 comments on commit 840434e

Please sign in to comment.