-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add notification when tracker is being moved
- Loading branch information
1 parent
c86ee61
commit 840434e
Showing
15 changed files
with
407 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
app/src/main/java/de/raffaelhahn/xadgps_client/NotifyDevice.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
app/src/main/java/de/raffaelhahn/xadgps_client/background/BackgroundWorkService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
Oops, something went wrong.