Skip to content

Commit

Permalink
Merge pull request #193 from prey/feat/improve-battery-use
Browse files Browse the repository at this point in the history
Feat/improve battery use
  • Loading branch information
oaliaga authored Jan 21, 2025
2 parents 39ddfe7 + f5302ba commit eb48241
Show file tree
Hide file tree
Showing 8 changed files with 211 additions and 126 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ android {

targetSdkVersion 34

versionCode 351
versionName '2.6.4'
versionCode 352
versionName '2.6.5'

multiDexEnabled true

Expand Down
16 changes: 16 additions & 0 deletions app/src/main/java/com/prey/PreyPhone.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.provider.Settings;
import android.telephony.TelephonyManager;

import androidx.core.app.ActivityCompat;
Expand Down Expand Up @@ -711,4 +712,19 @@ private static String getSerialNumberFromProperty(Method getMethod, String prope
return (String) getMethod.invoke(null, propertyName);
}

/**
* Checks if the airplane mode is currently enabled on the device.
*
* @param context the application context
* @return true if airplane mode is on, false otherwise
*/
public static boolean isAirplaneModeOn(Context context) {
// Get the current airplane mode setting from the system settings
boolean isAirplaneModeOn = Settings.Global.getInt(context.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 0) == 1;
// Log the result for debugging purposes
PreyLogger.d(String.format("isAirplaneModeOn: %s", isAirplaneModeOn));
// Return the result
return isAirplaneModeOn;
}

}
42 changes: 27 additions & 15 deletions app/src/main/java/com/prey/actions/aware/AwareController.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.prey.FileConfigReader;
import com.prey.PreyConfig;
import com.prey.PreyLogger;
import com.prey.PreyPhone;
import com.prey.actions.location.LocationUpdatesService;
import com.prey.actions.location.LocationUtil;
import com.prey.actions.location.PreyLocation;
Expand Down Expand Up @@ -53,34 +54,45 @@ public static AwareController getInstance() {
return INSTANCE;
}

/**
* Initializes the AwareController.
*
* @param ctx The context of the application.
*/
public void init(Context ctx) {
try{
boolean isLocationAware=PreyConfig.getPreyConfig(ctx).getAware();
PreyLogger.d("AWARE AwareController init isLocationAware:"+isLocationAware);
if (isLocationAware) {
try {
// Check if location awareness is enabled in the Prey configuration
boolean isLocationAware = PreyConfig.getPreyConfig(ctx).getAware();
PreyLogger.d(String.format("AWARE AwareController init isLocationAware:%s", isLocationAware));
// Check if airplane mode is currently enabled on the device
boolean isAirplaneModeOn = PreyPhone.isAirplaneModeOn(ctx);
PreyLogger.d(String.format("AWARE AwareController init isAirplaneModeOn:%s", isAirplaneModeOn));
// Only proceed if location awareness is enabled and airplane mode is not on
if (isLocationAware && !isAirplaneModeOn) {
PreyLocationManager.getInstance(ctx).setLastLocation(null);
PreyLocation locationNow=LocationUtil.getLocation(ctx,null,false);
if (locationNow!=null&&locationNow.getLat()!=0&&locationNow.getLng()!=0){
PreyLocation locationNow = LocationUtil.getLocation(ctx, null, false);
if (locationNow != null && locationNow.getLat() != 0 && locationNow.getLng() != 0) {
PreyLocationManager.getInstance(ctx).setLastLocation(locationNow);
PreyLogger.d("AWARE locationNow[i]:"+locationNow.toString());
PreyLogger.d(String.format("AWARE locationNow:%s", locationNow.toString()));
}
new LocationUpdatesService().startForegroundService(ctx);
PreyLocation locationAware = null;
int i=0;
int i = 0;
while (i < LocationUtil.MAXIMUM_OF_ATTEMPTS) {
PreyLogger.d("AWARE getPreyLocationApp[i]:"+i);
PreyLogger.d(String.format("AWARE getPreyLocationApp[i]:%s", i));
try {
Thread.sleep(LocationUtil.SLEEP_OF_ATTEMPTS[i]*1000);
Thread.sleep(LocationUtil.SLEEP_OF_ATTEMPTS[i] * 1000);
} catch (InterruptedException e) {
PreyLogger.e("AWARE error:" + e.getMessage(), e);
}
locationAware = PreyLocationManager.getInstance(ctx).getLastLocation();
if (locationAware!=null) {
if (locationAware != null) {
locationAware.setMethod("native");
PreyLogger.d("AWARE init:" + locationAware.toString());
}else{
PreyLogger.d("AWARE init nulo" +i);
PreyLogger.d(String.format("AWARE init[%s]:%s", i, locationAware.toString()));
} else {
PreyLogger.d(String.format("AWARE init[%s] null", i));
}
if (locationAware!=null&&locationAware.getLat()!=0&&locationAware.getLng()!=0){
if (locationAware != null && locationAware.getLat() != 0 && locationAware.getLng() != 0) {
break;
}
i++;
Expand Down
99 changes: 64 additions & 35 deletions app/src/main/java/com/prey/actions/location/LocationUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,44 +69,73 @@ public static PreyLocation getLocation(Context ctx, String messageId, boolean as
return getLocation(ctx, messageId, asynchronous, MAXIMUM_OF_ATTEMPTS);
}

public static PreyLocation getLocation(Context ctx, String messageId, boolean asynchronous, int maximum) throws Exception{
/**
* Retrieves the current location of the device.
*
* This method checks if airplane mode is enabled and if not, it proceeds to retrieve the location using various methods.
*
* @param ctx The context of the application.
* @param messageId The ID of the message.
* @param asynchronous Whether the location retrieval should be done asynchronously.
* @param maximum The maximum number of attempts to retrieve the location.
* @return The current location of the device, or null if it cannot be retrieved.
* @throws Exception If an error occurs during location retrieval.
*/
public static PreyLocation getLocation(Context ctx, String messageId, boolean asynchronous, int maximum) throws Exception {
PreyLocation preyLocation = null;
boolean isGpsEnabled = PreyLocationManager.getInstance(ctx).isGpsLocationServiceActive();
boolean isNetworkEnabled = PreyLocationManager.getInstance(ctx).isNetworkLocationServiceActive();
boolean isWifiEnabled = PreyWifiManager.getInstance(ctx).isWifiEnabled();
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(String.format("Error PreyLocationApp:%s", e.getMessage()), e);
}
try {
if (preyLocation == null || preyLocation.getLocation() == null || (preyLocation.getLocation().getLatitude() == 0 && preyLocation.getLocation().getLongitude() == 0)) {
preyLocation = getPreyLocationAppServiceOreo(ctx, method, asynchronous, preyLocation);
boolean isAirplaneModeOn = PreyPhone.isAirplaneModeOn(ctx);
PreyLogger.d(String.format("PreyLocation getLocation isAirplaneModeOn:%s", isAirplaneModeOn));
/**
* Proceed with location retrieval only if airplane mode is not enabled.
* This is because location services are typically disabled in airplane mode.
*/
if (!isAirplaneModeOn) {
// Get the status of GPS, network, and Wi-Fi location services
boolean isGpsEnabled = PreyLocationManager.getInstance(ctx).isGpsLocationServiceActive();
boolean isNetworkEnabled = PreyLocationManager.getInstance(ctx).isNetworkLocationServiceActive();
boolean isWifiEnabled = PreyWifiManager.getInstance(ctx).isWifiEnabled();
boolean isGooglePlayServicesAvailable = PreyUtils.isGooglePlayServicesAvailable(ctx);
// Create a JSON object to store the location service status
JSONObject json = new JSONObject();
try {
// Add the location service status to the JSON object
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);
// Determine the location method based on the GPS and network status
String method = getMethod(isGpsEnabled, isNetworkEnabled);
try {
// Attempt to retrieve the location using the App Service
preyLocation = getPreyLocationAppService(ctx, method, asynchronous, preyLocation, maximum);
} catch (Exception e) {
PreyLogger.e(String.format("Error PreyLocationApp:%s", e.getMessage()), e);
}
try {
// If the location is not retrieved using the App Service, attempt to retrieve it using the App Service Oreo
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(String.format("Error AppServiceOreo:%s", e.getMessage()), e);
}
// If Google Play Services is not available and the location is not retrieved, attempt to retrieve it using Wi-Fi
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);
}
// Log the retrieved location
if (preyLocation != null) {
PreyLogger.d(String.format("preyLocation lat:%s lng:%s acc:%s", preyLocation.getLat(), preyLocation.getLng(), preyLocation.getAccuracy()));
}
} catch (Exception 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()));
}
// Return the retrieved location
return preyLocation;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import com.prey.PreyConfig;
import com.prey.PreyLogger;
import com.prey.PreyPhone;
import com.prey.actions.location.LocationUpdatesService;
import com.prey.actions.location.LocationUtil;
import com.prey.actions.location.PreyLocation;
Expand All @@ -33,7 +34,9 @@ public class DailyLocation {
public void run(Context context) {
String dailyLocation = PreyConfig.getPreyConfig(context).getDailyLocation();
String nowDailyLocation = PreyConfig.FORMAT_SDF_AWARE.format(new Date());
if (!nowDailyLocation.equals(dailyLocation)) {
boolean isAirplaneModeOn = PreyPhone.isAirplaneModeOn(context);
PreyLogger.d(String.format("DailyLocation run isAirplaneModeOn:%s", isAirplaneModeOn));
if (!nowDailyLocation.equals(dailyLocation) && !isAirplaneModeOn) {
PreyLocationManager.getInstance(context).setLastLocation(null);
try {
PreyLocationManager.getInstance(context).setLastLocation(null);
Expand Down
Loading

0 comments on commit eb48241

Please sign in to comment.