Skip to content

Commit

Permalink
Merge pull request #190 from prey/feat/force-location
Browse files Browse the repository at this point in the history
Feat/force location
  • Loading branch information
oaliaga authored Oct 15, 2024
2 parents 1601e15 + aec7098 commit 0cba9fb
Show file tree
Hide file tree
Showing 16 changed files with 578 additions and 88 deletions.
11 changes: 6 additions & 5 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ android {

targetSdkVersion 34

versionCode 342
versionName '2.6.1'
versionCode 348
versionName '2.6.2'

multiDexEnabled true

Expand Down Expand Up @@ -52,15 +52,16 @@ dependencies {
implementation 'com.google.android.gms:play-services-maps:19.0.0'
implementation 'com.google.firebase:firebase-core:21.1.1'
implementation 'com.google.firebase:firebase-iid:21.1.0'
implementation 'com.google.firebase:firebase-messaging:24.0.0'
implementation 'com.google.firebase:firebase-analytics:22.0.2'
implementation 'com.google.firebase:firebase-crashlytics:19.0.3'
implementation 'com.google.firebase:firebase-messaging:24.0.2'
implementation 'com.google.firebase:firebase-analytics:22.1.2'
implementation 'com.google.firebase:firebase-crashlytics:19.2.0'
implementation 'com.google.firebase:firebase-database:21.0.0'

implementation 'com.android.installreferrer:installreferrer:2.2'
implementation 'com.android.support:multidex:1.0.3'
implementation 'com.google.code.gson:gson:2.10.1'
implementation 'androidx.biometric:biometric:1.2.0-alpha05'
implementation "androidx.work:work-runtime:2.9.1"

testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.2.1'
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.BIND_DEVICE_ADMIN" />
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
<uses-permission android:name="android.permission.READ_MEDIA_AUDIO" />
Expand Down Expand Up @@ -91,6 +92,9 @@
<meta-data android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key" />

<meta-data android:name="android.content.APP_RESTRICTIONS"
android:resource="@xml/app_restrictions" />

<!-- Exported added for android 12 -->
<activity
android:name="com.prey.activities.LoginActivity"
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/com/prey/PreyApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.prey.services.AwareJobService;
import com.prey.services.PreyDisablePowerOptionsService;
import com.prey.services.PreyJobService;
import com.prey.workers.PreyWorker;

import java.util.Date;
import java.util.Map;
Expand Down Expand Up @@ -117,6 +118,7 @@ public void run() {
AwareController.getInstance().init(ctx);
AwareScheduled.getInstance(ctx).run();
LocationScheduled.getInstance().run(ctx);
PreyWorker.getInstance().startPeriodicWork(ctx);
}
FileretrievalController.getInstance().run(ctx);
TriggerController.getInstance().run(ctx);
Expand Down
109 changes: 94 additions & 15 deletions app/src/main/java/com/prey/PreyConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,17 @@
import android.preference.PreferenceManager;
import android.view.View;

import androidx.annotation.NonNull;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.InstanceIdResult;
import com.google.firebase.messaging.FirebaseMessaging;
import com.prey.actions.location.PreyLocation;
import com.prey.activities.FeedbackActivity;
import com.prey.managers.PreyConnectivityManager;
Expand Down Expand Up @@ -616,7 +621,22 @@ public void onSuccess(InstanceIdResult instanceIdResult) {
}
});
} catch (Exception ex) {
PreyLogger.e("registerC2dm error2:" + ex.getMessage(), ex);
try {
FirebaseMessaging.getInstance().getToken()
.addOnCompleteListener(new OnCompleteListener<String>() {
@Override
public void onComplete(@NonNull Task<String> task) {
if (!task.isSuccessful()) {
PreyLogger.e(String.format("registerC2dm error:%s", task.getException().getMessage()), task.getException());
}
String token = task.getResult();
PreyLogger.d(String.format("registerC2dm token:%s", token));
sendToken(ctx, token);
}
});
} catch (Exception exception) {
PreyLogger.e(String.format("registerC2dm error:%s", exception.getMessage()), exception);
}
}
}
}
Expand Down Expand Up @@ -1125,15 +1145,15 @@ public PreyLocation getLocation(){

public void setLocationAware(PreyLocation location){
if(location!=null) {
saveFloat(PreyConfig.AWARE_LAT, location.getLat().floatValue());
saveFloat(PreyConfig.AWARE_LNG, location.getLng().floatValue());
saveString(PreyConfig.AWARE_LAT, location.getLat().toString());
saveString(PreyConfig.AWARE_LNG, location.getLng().toString());
saveFloat(PreyConfig.AWARE_ACC, location.getAccuracy());
}
}

public void removeLocationAware(){
saveFloat(PreyConfig.AWARE_LAT, 0);
saveFloat(PreyConfig.AWARE_LNG, 0);
saveString(PreyConfig.AWARE_LAT, "");
saveString(PreyConfig.AWARE_LNG, "");
saveFloat(PreyConfig.AWARE_ACC, 0);
saveString(PreyConfig.AWARE_DATE, "");
}
Expand All @@ -1147,20 +1167,40 @@ public void setAwareDate(String awareDate){
saveString(PreyConfig.AWARE_DATE, awareDate);
}

public PreyLocation getLocationAware(){
try{
float lat=getFloat(PreyConfig.AWARE_LAT,0);
float lng=getFloat(PreyConfig.AWARE_LNG,0);
float acc=getFloat(PreyConfig.AWARE_ACC,0);
if(lat==0||lng==0){
/**
* Retrieves the location aware settings.
*
* @return A PreyLocation object containing the location aware settings, or null if the settings are not available.
*/
public PreyLocation getLocationAware() {
try {
// Initialize latitude and longitude variables
String lat = "";
String lng = "";
// Attempt to retrieve the latitude and longitude values from storage
//The data saving is changed to string because decimals are lost
try {
lat = getString(PreyConfig.AWARE_LAT, "");
lng = getString(PreyConfig.AWARE_LNG, "");
} catch (Exception e) {
PreyLogger.e(String.format("Error getLocationAware:%s", e.getMessage()), e);
}
// Retrieve the accuracy value from storage
float acc = getFloat(PreyConfig.AWARE_ACC, 0f);
// Check if the latitude or longitude values are empty or null
if (lat == null || "".equals(lat) || lng == null || "".equals(lng)) {
// If either value is empty or null, return null
return null;
}
PreyLocation location= new PreyLocation();
location.setLat(lat);
location.setLng(lng);
// Create a new PreyLocation object
PreyLocation location = new PreyLocation();
location.setLat(Double.parseDouble(lat));
location.setLng(Double.parseDouble(lng));
location.setAccuracy(acc);
// Return the PreyLocation object
return location;
}catch(Exception e){
} catch (Exception e) {
PreyLogger.e(String.format("Error getLocationAware:%s", e.getMessage()), e);
return null;
}
}
Expand Down Expand Up @@ -1632,4 +1672,43 @@ public void setMinutesToQueryServer(int minutesToQueryServer) {
PreyLogger.d(String.format("setMinutesToQueryServer [%s]", minutesToQueryServer));
saveInt(PreyConfig.MINUTES_TO_QUERY_SERVER, minutesToQueryServer);
}

/**
* Key for storing the aware time in the configuration.
*/
public static final String AWARE_TIME = "AWARE_TIME";

/**
* Sets the aware time to 10 minutes in the future.
*
* This method updates the aware time stored in the configuration.
*/
public void setAwareTime() {
//the date is saved 10 minutes in the future
Calendar cal=Calendar.getInstance();
cal.setTime(new Date());
cal.add(Calendar.MINUTE ,10);
long dateTimeLong=cal.getTimeInMillis();
PreyLogger.d(String.format("AWARE WORK AwareTime [%s]", dateTimeLong));
saveLong(PreyConfig.AWARE_TIME, dateTimeLong);
}

/**
* Checks if it's time for the next aware event.
*
* This method compares the current time with the saved aware time.
* It is used to not request the location for at least 10 minutes
*
* @return true if it's time for the next aware event, false otherwise
*/
public boolean isTimeNextAware() {
//validates if the saved date is old
long awareTime = getLong(AWARE_TIME, 0);
if (awareTime == 0)
return true;
long timeNow = new Date().getTime();
PreyLogger.d(String.format("AWARE WORK AwareTime difference [%s] current[%s] > save[%s] ", (timeNow - awareTime), timeNow, awareTime));
return timeNow > awareTime;
}

}
55 changes: 55 additions & 0 deletions app/src/main/java/com/prey/PreyPhone.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
Expand Down Expand Up @@ -77,6 +78,8 @@ private void updateHardware() {
}
hardware.setCpuCores(String.valueOf(getCpuCores()));
hardware.setRamSize(String.valueOf(getMemoryRamSize()));
hardware.setSerialNumber(getSerialNumber());
hardware.setUuid(getSerialNumber());
initMemory();
}

Expand Down Expand Up @@ -655,4 +658,56 @@ public static String getNetworkClass(Context ctx) {
}
}

/**
* Retrieves the device's serial number.
*
* This method attempts to retrieve the serial number from various system properties.
* If all attempts fail, it falls back to using the Build.SERIAL property.
*
* @return the device's serial number, or null if it could not be retrieved
*/
public static String getSerialNumber() {
// Initialize the serial number to null
String serialNumber = null;
try {
// Get the SystemProperties class
Class<?> c = Class.forName("android.os.SystemProperties");
// Get the get() method of the SystemProperties class
Method getMethod = c.getMethod("get", String.class);
// Attempt to retrieve the serial number from various system properties
serialNumber = getSerialNumberFromProperty(getMethod, "gsm.sn1"); // GSM serial number
if (serialNumber == null) {
serialNumber = getSerialNumberFromProperty(getMethod, "ril.serialnumber"); // RIL serial number
}
if (serialNumber == null) {
serialNumber = getSerialNumberFromProperty(getMethod, "ro.serialno");// Serial number from ro.serialno property
}
if (serialNumber == null) {
serialNumber = getSerialNumberFromProperty(getMethod, "sys.serialnumber");// Serial number from sys.serialnumber property
}
if (serialNumber == null) {
// If all else fails, use the Build.SERIAL property
serialNumber = Build.SERIAL;
}
} catch (Exception e) {
PreyLogger.e(String.format("Error getSerialNumber:%s", e.getMessage()), e);
serialNumber = null;
}
// Return the retrieved serial number, or null if it could not be retrieved
return serialNumber;
}

/**
* Retrieves the value of the specified system property.
*
* @param getMethod the method used to retrieve the system property value
* @param propertyName the name of the system property to retrieve
* @return the value of the system property, or null if it could not be retrieved
* @throws Exception if an error occurs while retrieving the system property value
*/
private static String getSerialNumberFromProperty(Method getMethod, String propertyName) throws Exception {
// Invoke the getMethod with the propertyName as an argument to retrieve the system property value
return (String) getMethod.invoke(null, propertyName);
}

}
Loading

0 comments on commit 0cba9fb

Please sign in to comment.