Skip to content

Commit

Permalink
Merge pull request #189 from prey/feat/no-services
Browse files Browse the repository at this point in the history
Feat/no services
  • Loading branch information
oaliaga authored Jul 23, 2024
2 parents 43ca1b7 + 40d93c3 commit 1601e15
Show file tree
Hide file tree
Showing 11 changed files with 261 additions and 80 deletions.
12 changes: 6 additions & 6 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ android {

targetSdkVersion 34

versionCode 341
versionCode 342
versionName '2.6.1'

multiDexEnabled true
Expand Down Expand Up @@ -49,12 +49,12 @@ dependencies {
implementation 'com.google.android.gms:play-services-location:21.3.0'
implementation 'com.google.android.gms:play-services-gcm:17.0.0'
implementation 'com.google.android.gms:play-services-vision:20.1.3'
implementation 'com.google.android.gms:play-services-maps:18.2.0'
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.1'
implementation 'com.google.firebase:firebase-crashlytics:19.0.1'
implementation 'com.google.firebase:firebase-analytics:22.0.2'
implementation 'com.google.firebase:firebase-crashlytics:19.0.3'
implementation 'com.google.firebase:firebase-database:21.0.0'

implementation 'com.android.installreferrer:installreferrer:2.2'
Expand All @@ -63,8 +63,8 @@ dependencies {
implementation 'androidx.biometric:biometric:1.2.0-alpha05'

testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
androidTestImplementation 'androidx.test.ext:junit:1.2.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.6.1'

}

Expand Down
7 changes: 5 additions & 2 deletions app/src/main/java/com/prey/PreyApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ public void run() {
if (!verifyNotification) {
EventFactory.notification(ctx);
}
if ((accessCoarseLocation || accessFineLocation) && canAccessBackgroundLocation) {
boolean isGooglePlayServicesAvailable = PreyUtils.isGooglePlayServicesAvailable(ctx);
if (isGooglePlayServicesAvailable && (accessCoarseLocation || accessFineLocation) && canAccessBackgroundLocation) {
GeofenceController.getInstance().run(ctx);
AwareController.getInstance().init(ctx);
AwareScheduled.getInstance(ctx).run();
Expand All @@ -129,7 +130,9 @@ public void run() {
try {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
PreyJobService.schedule(ctx);
AwareJobService.schedule(ctx);
if (isGooglePlayServicesAvailable) {
AwareJobService.schedule(ctx);
}
}
} catch (Exception e) {
PreyLogger.e(String.format("error jobService.schedule : %s", e.getMessage()), e);
Expand Down
11 changes: 11 additions & 0 deletions app/src/main/java/com/prey/PreyConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -1621,4 +1621,15 @@ public void setDailyLocation(String dailyLocation){
PreyLogger.d(String.format("DAILY setDailyLocation [%s]", dailyLocation));
saveString(PreyConfig.DAILY_LOCATION, dailyLocation);
}

public static final String MINUTES_TO_QUERY_SERVER = "MINUTES_TO_QUERY_SERVER";

public int getMinutesToQueryServer() {
return getInt(PreyConfig.MINUTES_TO_QUERY_SERVER, 15);
}

public void setMinutesToQueryServer(int minutesToQueryServer) {
PreyLogger.d(String.format("setMinutesToQueryServer [%s]", minutesToQueryServer));
saveInt(PreyConfig.MINUTES_TO_QUERY_SERVER, minutesToQueryServer);
}
}
14 changes: 11 additions & 3 deletions app/src/main/java/com/prey/PreyStatus.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public void setPreyPopUpOnclick(boolean preyPopUpOnclick) {
public void initConfig(Context ctx){
boolean aware = false;
boolean autoconnect = false;
int minutesToQueryServer;
try {
JSONObject jsnobject = PreyWebServices.getInstance().getStatus(ctx);
if (jsnobject != null) {
Expand All @@ -97,11 +98,18 @@ public void initConfig(Context ctx){
}
PreyConfig.getPreyConfig(ctx).setAware(aware);
PreyConfig.getPreyConfig(ctx).setAutoConnect(autoconnect);
PreyLogger.d("STATUS aware :" + aware);
PreyLogger.d("STATUS autoconnect :" + autoconnect);
PreyLogger.d(String.format("STATUS aware :%b", aware));
PreyLogger.d(String.format("STATUS autoconnect :%b", autoconnect));
try {
minutesToQueryServer = jsnobject.getInt("minutes_to_query_server");
} catch (Exception e) {
minutesToQueryServer = PreyConfig.getPreyConfig(ctx).getMinutesToQueryServer();
}
PreyConfig.getPreyConfig(ctx).setMinutesToQueryServer(minutesToQueryServer);
PreyLogger.d(String.format("STATUS minutesToQueryServer :%s", minutesToQueryServer));
}
} catch (Exception e) {
PreyLogger.e("STATUS Error:" + e.getMessage(), e);
PreyLogger.e(String.format("STATUS Error:%s", e.getMessage()), e);
}
}

Expand Down
15 changes: 15 additions & 0 deletions app/src/main/java/com/prey/PreyUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import android.util.DisplayMetrics;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
import com.prey.backwardcompatibility.AboveCupcakeSupport;

public class PreyUtils {
Expand Down Expand Up @@ -146,4 +148,17 @@ public static long copyFile(InputStream input, OutputStream output) throws IOExc
return count;
}

public static boolean isGooglePlayServicesAvailable(Context context) {
boolean isGooglePlayServicesAvailable;
try {
GoogleApiAvailability googleApiAvailability = GoogleApiAvailability.getInstance();
int resultCode = googleApiAvailability.isGooglePlayServicesAvailable(context);
isGooglePlayServicesAvailable = (resultCode == ConnectionResult.SUCCESS);
} catch (Exception e) {
isGooglePlayServicesAvailable = false;
}
PreyLogger.d(String.format("isGooglePlayServicesAvailable:%s", isGooglePlayServicesAvailable));
return isGooglePlayServicesAvailable;
}

}
56 changes: 25 additions & 31 deletions app/src/main/java/com/prey/actions/location/LocationUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.Manifest;
Expand All @@ -21,18 +22,19 @@

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.tasks.OnSuccessListener;
import com.prey.PreyConfig;
import com.prey.PreyLogger;
import com.prey.PreyPhone;
import com.prey.PreyUtils;
import com.prey.actions.HttpDataService;
import com.prey.actions.geofences.GeofenceController;
import com.prey.json.UtilJson;
import com.prey.managers.PreyWifiManager;
import com.prey.net.PreyWebServices;
import com.prey.services.LocationService;

import org.json.JSONException;
import org.json.JSONObject;

public class LocationUtil {

public static final String LAT = "lat";
Expand All @@ -51,6 +53,7 @@ public static HttpDataService dataLocation(final Context ctx, String messageId,
if (preyLocation != null && (preyLocation.getLat() != 0 && preyLocation.getLng() != 0)) {
PreyLogger.d(String.format("locationData:%s %s %s", preyLocation.getLat(), preyLocation.getLng(), preyLocation.getAccuracy()));
PreyConfig.getPreyConfig(ctx).setLocation(preyLocation);
PreyLocationManager.getInstance(ctx).setLastLocation(preyLocation);
data = convertData(preyLocation);
} else {
PreyLogger.d("locationData else:");
Expand All @@ -71,32 +74,39 @@ public static PreyLocation getLocation(Context ctx, String messageId, boolean as
boolean isGpsEnabled = PreyLocationManager.getInstance(ctx).isGpsLocationServiceActive();
boolean isNetworkEnabled = PreyLocationManager.getInstance(ctx).isNetworkLocationServiceActive();
boolean isWifiEnabled = PreyWifiManager.getInstance(ctx).isWifiEnabled();
boolean isGooglePlayServicesAvailable=isGooglePlayServicesAvailable(ctx);
String locationInfo="{\"gps\":" + isGpsEnabled + ",\"net\":" + isNetworkEnabled + ",\"wifi\":" + isWifiEnabled+",\"play\":"+isGooglePlayServicesAvailable+"}";
boolean isGooglePlayServicesAvailable = PreyUtils.isGooglePlayServicesAvailable(ctx);
JSONObject json = new JSONObject();
try {
json.put("gps", isGpsEnabled);
json.put("net", isNetworkEnabled);
json.put("wifi", isWifiEnabled);
json.put("play", isGooglePlayServicesAvailable);
} catch (JSONException e) {
PreyLogger.e(String.format("Error:%s", e.getMessage()), e);
}
String locationInfo = json.toString();
PreyConfig.getPreyConfig(ctx).setLocationInfo(locationInfo);
PreyLogger.d(locationInfo);
String method = getMethod(isGpsEnabled, isNetworkEnabled);
try {
preyLocation = getPreyLocationAppService(ctx, method, asynchronous, preyLocation, maximum);
} catch (Exception e) {
PreyLogger.e("Error PreyLocationApp:"+e.getMessage(),e);
PreyLogger.e(String.format("Error PreyLocationApp:%s", e.getMessage()), e);
}
try {
if(preyLocation==null||preyLocation.getLocation()==null||(preyLocation.getLocation().getLatitude()==0&&preyLocation.getLocation().getLongitude()==0)) {
if (preyLocation == null || preyLocation.getLocation() == null || (preyLocation.getLocation().getLatitude() == 0 && preyLocation.getLocation().getLongitude() == 0)) {
preyLocation = getPreyLocationAppServiceOreo(ctx, method, asynchronous, preyLocation);
}
} catch (Exception e) {
PreyLogger.e("Error AppServiceOreo:"+e.getMessage(),e);
PreyLogger.e(String.format("Error AppServiceOreo:%s", e.getMessage()), e);
}
if (!isGooglePlayServicesAvailable && (preyLocation == null || preyLocation.getLocation() == null || (preyLocation.getLocation().getLatitude() == 0 && preyLocation.getLocation().getLongitude() == 0))) {
List<PreyPhone.Wifi> listWifi = new PreyPhone(ctx).getListWifi();
preyLocation = PreyWebServices.getInstance().getLocationWithWifi(ctx, listWifi);
}
if (preyLocation != null) {
PreyLogger.d(String.format("preyLocation lat:%s lng:%s acc:%s", preyLocation.getLat(), preyLocation.getLng(), preyLocation.getAccuracy()));
}
final PreyLocation location = preyLocation;
new Thread() {
public void run() {
GeofenceController.verifyGeozone(ctx, location);
}
}.start();
return preyLocation;
}

Expand Down Expand Up @@ -207,22 +217,6 @@ private static PreyLocation getPreyLocationAppService(final Context ctx, String
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M &&
(ActivityCompat.checkSelfPermission(ctx, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(ctx, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED)) {
try {
FusedLocationProviderClient fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(ctx);
fusedLocationProviderClient.getLastLocation()
.addOnSuccessListener(new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
if (location != null) {
PreyLocationManager.getInstance(ctx).setLastLocation(new PreyLocation(location));
}
}
});
preyLocation = waitLocation(ctx, method, asynchronous, maximum);
} catch (Exception e) {
PreyLogger.e(String.format("getPreyLocationAppService e:%s", e.getMessage()), e);
}
} else {
Intent intentLocation = new Intent(ctx, LocationService.class);
try {
ctx.startService(intentLocation);
Expand Down
75 changes: 41 additions & 34 deletions app/src/main/java/com/prey/json/actions/Location.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@

import com.prey.PreyConfig;
import com.prey.PreyLogger;
import com.prey.PreyUtils;
import com.prey.actions.HttpDataService;
import com.prey.actions.aware.AwareConfig;
import com.prey.actions.aware.AwareController;
import com.prey.actions.location.LocationThread;
import com.prey.actions.location.LocationUtil;
import com.prey.actions.location.PreyLocation;
import com.prey.actions.location.PreyLocationManager;
import com.prey.actions.observer.ActionResult;
import com.prey.json.JsonAction;
Expand Down Expand Up @@ -60,6 +58,7 @@ public List<HttpDataService> get(Context ctx, List<ActionResult> list, JSONObje
reason = "{\"device_job_id\":\"" + jobId + "\"}";
}
PreyLocationManager.getInstance(ctx).setLastLocation(null);
PreyConfig.getPreyConfig(ctx).setLocation(null);
PreyConfig.getPreyConfig(ctx).setLocationInfo("");
PreyWebServices.getInstance().sendNotifyActionResultPreyHttp(ctx,"processed", messageId, UtilJson.makeMapParam("get", "location", "started",reason));
PreyLogger.d(this.getClass().getName());
Expand All @@ -73,40 +72,48 @@ public List<HttpDataService> get(Context ctx, List<ActionResult> list, JSONObje
while (i < maximum) {
send = false;
try {
data = LocationUtil.dataLocation(ctx, messageId, true);
String acc = data.getDataListKey(LocationUtil.ACC);
if (acc != null && !acc.equals("")) {
float newAccuracy = 0;
try {
newAccuracy = Float.parseFloat(acc);
PreyLogger.d(String.format("accuracy_:%s newAccuracy:%s", accuracy, newAccuracy));
} catch (Exception e) {
}
if (newAccuracy > 0) {
if (accuracy == -1 || accuracy > newAccuracy) {
send = true;
accuracy = newAccuracy;
LocationUtil.dataLocation(ctx, messageId, true);
PreyLocation location = PreyConfig.getPreyConfig(ctx).getLocation();
if (location != null) {
data = LocationUtil.convertData(location);
String acc = data.getDataListKey(LocationUtil.ACC);
if (acc != null && !acc.equals("")) {
float newAccuracy = 0;
try {
newAccuracy = Float.parseFloat(acc);
PreyLogger.d(String.format("accuracy_:%s newAccuracy:%s", accuracy, newAccuracy));
} catch (Exception e) {
PreyLogger.e(String.format("Error:%s", e.getMessage()), e);
}
if (newAccuracy > 0) {
if (accuracy == -1 || accuracy > newAccuracy) {
send = true;
accuracy = newAccuracy;
}
}
}
if (send) {
//It is added if it is the first time the location is sent
HttpDataService dataToast = new HttpDataService("skip_toast");
dataToast.setList(false);
dataToast.setKey("skip_toast");
dataToast.setSingleData(Boolean.toString(!first));
dataToBeSent = new ArrayList<HttpDataService>();
dataToBeSent.add(data);
dataToBeSent.add(dataToast);
PreyLogger.d(String.format("send [%s]:%s", i, accuracy));
PreyWebServices.getInstance().sendPreyHttpData(ctx, dataToBeSent);
first = false;
i = LocationUtil.MAXIMUM_OF_ATTEMPTS;
}
}
if (send) {
//It is added if it is the first time the location is sent
HttpDataService dataToast = new HttpDataService("skip_toast");
dataToast.setList(false);
dataToast.setKey("skip_toast");
dataToast.setSingleData(Boolean.toString(! first));
dataToBeSent = new ArrayList<HttpDataService>();
dataToBeSent.add(data);
dataToBeSent.add(dataToast);
PreyLogger.d(String.format("send [%s]:%s", i, accuracy));
PreyWebServices.getInstance().sendPreyHttpData(ctx, dataToBeSent);
first = false;
}
try {
Thread.sleep(LocationUtil.SLEEP_OF_ATTEMPTS[i] * 1000);
} catch (Exception e) {
i = LocationUtil.MAXIMUM_OF_ATTEMPTS;
break;
if (i < maximum) {
try {
Thread.sleep(LocationUtil.SLEEP_OF_ATTEMPTS[i] * 1000);
} catch (Exception e) {
i = LocationUtil.MAXIMUM_OF_ATTEMPTS;
break;
}
}
} catch (Exception e) {
i = LocationUtil.MAXIMUM_OF_ATTEMPTS;
Expand Down
Loading

0 comments on commit 1601e15

Please sign in to comment.