@@ -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
0 commit comments