Skip to content

Commit eb48241

Browse files
authored
Merge pull request #193 from prey/feat/improve-battery-use
Feat/improve battery use
2 parents 39ddfe7 + f5302ba commit eb48241

File tree

8 files changed

+211
-126
lines changed

8 files changed

+211
-126
lines changed

app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ android {
1212

1313
targetSdkVersion 34
1414

15-
versionCode 351
16-
versionName '2.6.4'
15+
versionCode 352
16+
versionName '2.6.5'
1717

1818
multiDexEnabled true
1919

app/src/main/java/com/prey/PreyPhone.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import android.net.wifi.WifiInfo;
3636
import android.net.wifi.WifiManager;
3737
import android.os.Build;
38+
import android.provider.Settings;
3839
import android.telephony.TelephonyManager;
3940

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

715+
/**
716+
* Checks if the airplane mode is currently enabled on the device.
717+
*
718+
* @param context the application context
719+
* @return true if airplane mode is on, false otherwise
720+
*/
721+
public static boolean isAirplaneModeOn(Context context) {
722+
// Get the current airplane mode setting from the system settings
723+
boolean isAirplaneModeOn = Settings.Global.getInt(context.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 0) == 1;
724+
// Log the result for debugging purposes
725+
PreyLogger.d(String.format("isAirplaneModeOn: %s", isAirplaneModeOn));
726+
// Return the result
727+
return isAirplaneModeOn;
728+
}
729+
714730
}

app/src/main/java/com/prey/actions/aware/AwareController.java

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import com.prey.FileConfigReader;
2727
import com.prey.PreyConfig;
2828
import com.prey.PreyLogger;
29+
import com.prey.PreyPhone;
2930
import com.prey.actions.location.LocationUpdatesService;
3031
import com.prey.actions.location.LocationUtil;
3132
import com.prey.actions.location.PreyLocation;
@@ -53,34 +54,45 @@ public static AwareController getInstance() {
5354
return INSTANCE;
5455
}
5556

57+
/**
58+
* Initializes the AwareController.
59+
*
60+
* @param ctx The context of the application.
61+
*/
5662
public void init(Context ctx) {
57-
try{
58-
boolean isLocationAware=PreyConfig.getPreyConfig(ctx).getAware();
59-
PreyLogger.d("AWARE AwareController init isLocationAware:"+isLocationAware);
60-
if (isLocationAware) {
63+
try {
64+
// Check if location awareness is enabled in the Prey configuration
65+
boolean isLocationAware = PreyConfig.getPreyConfig(ctx).getAware();
66+
PreyLogger.d(String.format("AWARE AwareController init isLocationAware:%s", isLocationAware));
67+
// Check if airplane mode is currently enabled on the device
68+
boolean isAirplaneModeOn = PreyPhone.isAirplaneModeOn(ctx);
69+
PreyLogger.d(String.format("AWARE AwareController init isAirplaneModeOn:%s", isAirplaneModeOn));
70+
// Only proceed if location awareness is enabled and airplane mode is not on
71+
if (isLocationAware && !isAirplaneModeOn) {
6172
PreyLocationManager.getInstance(ctx).setLastLocation(null);
62-
PreyLocation locationNow=LocationUtil.getLocation(ctx,null,false);
63-
if (locationNow!=null&&locationNow.getLat()!=0&&locationNow.getLng()!=0){
73+
PreyLocation locationNow = LocationUtil.getLocation(ctx, null, false);
74+
if (locationNow != null && locationNow.getLat() != 0 && locationNow.getLng() != 0) {
6475
PreyLocationManager.getInstance(ctx).setLastLocation(locationNow);
65-
PreyLogger.d("AWARE locationNow[i]:"+locationNow.toString());
76+
PreyLogger.d(String.format("AWARE locationNow:%s", locationNow.toString()));
6677
}
6778
new LocationUpdatesService().startForegroundService(ctx);
6879
PreyLocation locationAware = null;
69-
int i=0;
80+
int i = 0;
7081
while (i < LocationUtil.MAXIMUM_OF_ATTEMPTS) {
71-
PreyLogger.d("AWARE getPreyLocationApp[i]:"+i);
82+
PreyLogger.d(String.format("AWARE getPreyLocationApp[i]:%s", i));
7283
try {
73-
Thread.sleep(LocationUtil.SLEEP_OF_ATTEMPTS[i]*1000);
84+
Thread.sleep(LocationUtil.SLEEP_OF_ATTEMPTS[i] * 1000);
7485
} catch (InterruptedException e) {
86+
PreyLogger.e("AWARE error:" + e.getMessage(), e);
7587
}
7688
locationAware = PreyLocationManager.getInstance(ctx).getLastLocation();
77-
if (locationAware!=null) {
89+
if (locationAware != null) {
7890
locationAware.setMethod("native");
79-
PreyLogger.d("AWARE init:" + locationAware.toString());
80-
}else{
81-
PreyLogger.d("AWARE init nulo" +i);
91+
PreyLogger.d(String.format("AWARE init[%s]:%s", i, locationAware.toString()));
92+
} else {
93+
PreyLogger.d(String.format("AWARE init[%s] null", i));
8294
}
83-
if (locationAware!=null&&locationAware.getLat()!=0&&locationAware.getLng()!=0){
95+
if (locationAware != null && locationAware.getLat() != 0 && locationAware.getLng() != 0) {
8496
break;
8597
}
8698
i++;

app/src/main/java/com/prey/actions/location/LocationUtil.java

Lines changed: 64 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -69,44 +69,73 @@ public static PreyLocation getLocation(Context ctx, String messageId, boolean as
6969
return getLocation(ctx, messageId, asynchronous, MAXIMUM_OF_ATTEMPTS);
7070
}
7171

72-
public static PreyLocation getLocation(Context ctx, String messageId, boolean asynchronous, int maximum) throws Exception{
72+
/**
73+
* Retrieves the current location of the device.
74+
*
75+
* This method checks if airplane mode is enabled and if not, it proceeds to retrieve the location using various methods.
76+
*
77+
* @param ctx The context of the application.
78+
* @param messageId The ID of the message.
79+
* @param asynchronous Whether the location retrieval should be done asynchronously.
80+
* @param maximum The maximum number of attempts to retrieve the location.
81+
* @return The current location of the device, or null if it cannot be retrieved.
82+
* @throws Exception If an error occurs during location retrieval.
83+
*/
84+
public static PreyLocation getLocation(Context ctx, String messageId, boolean asynchronous, int maximum) throws Exception {
7385
PreyLocation preyLocation = null;
74-
boolean isGpsEnabled = PreyLocationManager.getInstance(ctx).isGpsLocationServiceActive();
75-
boolean isNetworkEnabled = PreyLocationManager.getInstance(ctx).isNetworkLocationServiceActive();
76-
boolean isWifiEnabled = PreyWifiManager.getInstance(ctx).isWifiEnabled();
77-
boolean isGooglePlayServicesAvailable = PreyUtils.isGooglePlayServicesAvailable(ctx);
78-
JSONObject json = new JSONObject();
79-
try {
80-
json.put("gps", isGpsEnabled);
81-
json.put("net", isNetworkEnabled);
82-
json.put("wifi", isWifiEnabled);
83-
json.put("play", isGooglePlayServicesAvailable);
84-
} catch (JSONException e) {
85-
PreyLogger.e(String.format("Error:%s", e.getMessage()), e);
86-
}
87-
String locationInfo = json.toString();
88-
PreyConfig.getPreyConfig(ctx).setLocationInfo(locationInfo);
89-
PreyLogger.d(locationInfo);
90-
String method = getMethod(isGpsEnabled, isNetworkEnabled);
91-
try {
92-
preyLocation = getPreyLocationAppService(ctx, method, asynchronous, preyLocation, maximum);
93-
} catch (Exception e) {
94-
PreyLogger.e(String.format("Error PreyLocationApp:%s", e.getMessage()), e);
95-
}
96-
try {
97-
if (preyLocation == null || preyLocation.getLocation() == null || (preyLocation.getLocation().getLatitude() == 0 && preyLocation.getLocation().getLongitude() == 0)) {
98-
preyLocation = getPreyLocationAppServiceOreo(ctx, method, asynchronous, preyLocation);
86+
boolean isAirplaneModeOn = PreyPhone.isAirplaneModeOn(ctx);
87+
PreyLogger.d(String.format("PreyLocation getLocation isAirplaneModeOn:%s", isAirplaneModeOn));
88+
/**
89+
* Proceed with location retrieval only if airplane mode is not enabled.
90+
* This is because location services are typically disabled in airplane mode.
91+
*/
92+
if (!isAirplaneModeOn) {
93+
// Get the status of GPS, network, and Wi-Fi location services
94+
boolean isGpsEnabled = PreyLocationManager.getInstance(ctx).isGpsLocationServiceActive();
95+
boolean isNetworkEnabled = PreyLocationManager.getInstance(ctx).isNetworkLocationServiceActive();
96+
boolean isWifiEnabled = PreyWifiManager.getInstance(ctx).isWifiEnabled();
97+
boolean isGooglePlayServicesAvailable = PreyUtils.isGooglePlayServicesAvailable(ctx);
98+
// Create a JSON object to store the location service status
99+
JSONObject json = new JSONObject();
100+
try {
101+
// Add the location service status to the JSON object
102+
json.put("gps", isGpsEnabled);
103+
json.put("net", isNetworkEnabled);
104+
json.put("wifi", isWifiEnabled);
105+
json.put("play", isGooglePlayServicesAvailable);
106+
} catch (JSONException e) {
107+
PreyLogger.e(String.format("Error:%s", e.getMessage()), e);
108+
}
109+
String locationInfo = json.toString();
110+
PreyConfig.getPreyConfig(ctx).setLocationInfo(locationInfo);
111+
PreyLogger.d(locationInfo);
112+
// Determine the location method based on the GPS and network status
113+
String method = getMethod(isGpsEnabled, isNetworkEnabled);
114+
try {
115+
// Attempt to retrieve the location using the App Service
116+
preyLocation = getPreyLocationAppService(ctx, method, asynchronous, preyLocation, maximum);
117+
} catch (Exception e) {
118+
PreyLogger.e(String.format("Error PreyLocationApp:%s", e.getMessage()), e);
119+
}
120+
try {
121+
// If the location is not retrieved using the App Service, attempt to retrieve it using the App Service Oreo
122+
if (preyLocation == null || preyLocation.getLocation() == null || (preyLocation.getLocation().getLatitude() == 0 && preyLocation.getLocation().getLongitude() == 0)) {
123+
preyLocation = getPreyLocationAppServiceOreo(ctx, method, asynchronous, preyLocation);
124+
}
125+
} catch (Exception e) {
126+
PreyLogger.e(String.format("Error AppServiceOreo:%s", e.getMessage()), e);
127+
}
128+
// If Google Play Services is not available and the location is not retrieved, attempt to retrieve it using Wi-Fi
129+
if (!isGooglePlayServicesAvailable && (preyLocation == null || preyLocation.getLocation() == null || (preyLocation.getLocation().getLatitude() == 0 && preyLocation.getLocation().getLongitude() == 0))) {
130+
List<PreyPhone.Wifi> listWifi = new PreyPhone(ctx).getListWifi();
131+
preyLocation = PreyWebServices.getInstance().getLocationWithWifi(ctx, listWifi);
132+
}
133+
// Log the retrieved location
134+
if (preyLocation != null) {
135+
PreyLogger.d(String.format("preyLocation lat:%s lng:%s acc:%s", preyLocation.getLat(), preyLocation.getLng(), preyLocation.getAccuracy()));
99136
}
100-
} catch (Exception e) {
101-
PreyLogger.e(String.format("Error AppServiceOreo:%s", e.getMessage()), e);
102-
}
103-
if (!isGooglePlayServicesAvailable && (preyLocation == null || preyLocation.getLocation() == null || (preyLocation.getLocation().getLatitude() == 0 && preyLocation.getLocation().getLongitude() == 0))) {
104-
List<PreyPhone.Wifi> listWifi = new PreyPhone(ctx).getListWifi();
105-
preyLocation = PreyWebServices.getInstance().getLocationWithWifi(ctx, listWifi);
106-
}
107-
if (preyLocation != null) {
108-
PreyLogger.d(String.format("preyLocation lat:%s lng:%s acc:%s", preyLocation.getLat(), preyLocation.getLng(), preyLocation.getAccuracy()));
109137
}
138+
// Return the retrieved location
110139
return preyLocation;
111140
}
112141

app/src/main/java/com/prey/actions/location/daily/DailyLocation.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import com.prey.PreyConfig;
1313
import com.prey.PreyLogger;
14+
import com.prey.PreyPhone;
1415
import com.prey.actions.location.LocationUpdatesService;
1516
import com.prey.actions.location.LocationUtil;
1617
import com.prey.actions.location.PreyLocation;
@@ -33,7 +34,9 @@ public class DailyLocation {
3334
public void run(Context context) {
3435
String dailyLocation = PreyConfig.getPreyConfig(context).getDailyLocation();
3536
String nowDailyLocation = PreyConfig.FORMAT_SDF_AWARE.format(new Date());
36-
if (!nowDailyLocation.equals(dailyLocation)) {
37+
boolean isAirplaneModeOn = PreyPhone.isAirplaneModeOn(context);
38+
PreyLogger.d(String.format("DailyLocation run isAirplaneModeOn:%s", isAirplaneModeOn));
39+
if (!nowDailyLocation.equals(dailyLocation) && !isAirplaneModeOn) {
3740
PreyLocationManager.getInstance(context).setLastLocation(null);
3841
try {
3942
PreyLocationManager.getInstance(context).setLastLocation(null);

0 commit comments

Comments
 (0)