Location without Cell Radio

This commit is contained in:
jens 2021-09-07 17:30:45 +02:00
parent cb430b957f
commit a6edab75ce
6 changed files with 156 additions and 109 deletions

View File

@ -29,6 +29,7 @@ import androidx.core.text.HtmlCompat;
import com.jens.automation2.AutomationService.serviceCommands; import com.jens.automation2.AutomationService.serviceCommands;
import com.jens.automation2.Trigger.Trigger_Enum; import com.jens.automation2.Trigger.Trigger_Enum;
import com.jens.automation2.location.CellLocationChangedReceiver;
import com.jens.automation2.location.LocationProvider; import com.jens.automation2.location.LocationProvider;
import java.io.File; import java.io.File;

View File

@ -22,6 +22,8 @@ import android.widget.EditText;
import android.widget.ImageButton; import android.widget.ImageButton;
import android.widget.Toast; import android.widget.Toast;
import com.jens.automation2.receivers.ConnectivityReceiver;
import java.util.Calendar; import java.util.Calendar;
import java.util.Timer; import java.util.Timer;
import java.util.TimerTask; import java.util.TimerTask;
@ -163,7 +165,7 @@ public class ActivityManagePoi extends Activity
locationSearchStart = Calendar.getInstance(); locationSearchStart = Calendar.getInstance();
startTimeout(); startTimeout();
if(!Settings.privacyLocationing) if(!Settings.privacyLocationing || !ConnectivityReceiver.isDataConnectionAvailable(AutomationService.getInstance()))
{ {
Miscellaneous.logEvent("i", "POI Manager", getResources().getString(R.string.logGettingPositionWithProvider) + " " + provider1, 3); Miscellaneous.logEvent("i", "POI Manager", getResources().getString(R.string.logGettingPositionWithProvider) + " " + provider1, 3);
myLocationManager.requestLocationUpdates(provider1, 500, Settings.satisfactoryAccuracyNetwork, myLocationListenerNetwork); myLocationManager.requestLocationUpdates(provider1, 500, Settings.satisfactoryAccuracyNetwork, myLocationListenerNetwork);

View File

@ -333,7 +333,7 @@ public class AutomationService extends Service implements OnInitListener
protected void startLocationProvider() protected void startLocationProvider()
{ {
if(ActivityPermissions.havePermission("android.permission.ACCESS_COARSE_LOCATION", AutomationService.this)) if(ActivityPermissions.havePermission(Manifest.permission.ACCESS_COARSE_LOCATION, AutomationService.this))
myLocationProvider = new LocationProvider(this); //autostart with this (only) constructor myLocationProvider = new LocationProvider(this); //autostart with this (only) constructor
} }
@ -632,6 +632,9 @@ public class AutomationService extends Service implements OnInitListener
// } // }
// else // else
// { // {
if(notificationBuilder == null)
notificationBuilder = createDefaultNotificationBuilder();
notificationBuilder.setContentText(textToDisplay); notificationBuilder.setContentText(textToDisplay);
notificationBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(textToDisplay)); notificationBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(textToDisplay));

View File

@ -144,14 +144,14 @@ public class ReceiverCoordinator
ConnectivityReceiver.startConnectivityReceiver(AutomationService.getInstance()); ConnectivityReceiver.startConnectivityReceiver(AutomationService.getInstance());
// startCellLocationChangedReceiver // startCellLocationChangedReceiver
if(!ConnectivityReceiver.isAirplaneMode(AutomationService.getInstance()) && WifiBroadcastReceiver.mayCellLocationReceiverBeActivated() && (Rule.isAnyRuleUsing(Trigger.Trigger_Enum.pointOfInterest) | Rule.isAnyRuleUsing(Trigger.Trigger_Enum.speed))) if(!ConnectivityReceiver.isAirplaneMode(AutomationService.getInstance()) && WifiBroadcastReceiver.mayCellLocationReceiverBeActivated() && (Rule.isAnyRuleUsing(Trigger.Trigger_Enum.pointOfInterest) || Rule.isAnyRuleUsing(Trigger.Trigger_Enum.speed)))
{ {
if(!Miscellaneous.googleToBlameForLocation(true)) if(!Miscellaneous.googleToBlameForLocation(true))
CellLocationChangedReceiver.startCellLocationChangedReceiver(); CellLocationChangedReceiver.startCellLocationChangedReceiver();
} }
// startBatteryReceiver // startBatteryReceiver
if(Rule.isAnyRuleUsing(Trigger.Trigger_Enum.charging) | Rule.isAnyRuleUsing(Trigger.Trigger_Enum.usb_host_connection) | Rule.isAnyRuleUsing(Trigger.Trigger_Enum.batteryLevel)) if(Rule.isAnyRuleUsing(Trigger.Trigger_Enum.charging) || Rule.isAnyRuleUsing(Trigger.Trigger_Enum.usb_host_connection) || Rule.isAnyRuleUsing(Trigger.Trigger_Enum.batteryLevel))
BatteryReceiver.startBatteryReceiver(AutomationService.getInstance()); BatteryReceiver.startBatteryReceiver(AutomationService.getInstance());
// startAlarmListener // startAlarmListener

View File

@ -130,6 +130,21 @@ public class CellLocationChangedReceiver extends PhoneStateListener
} }
} }
public static boolean isCellLocationChangedReceiverPossible()
{
if(telephonyManager == null)
telephonyManager = (TelephonyManager) AutomationService.getInstance().getSystemService(Context.TELEPHONY_SERVICE);
if(
ConnectivityReceiver.isAirplaneMode(AutomationService.getInstance())
||
telephonyManager.getSimState() != TelephonyManager.SIM_STATE_READY
)
return false;
else
return true;
}
public Location getLocation(String accuracy) public Location getLocation(String accuracy)
{ {
Criteria crit = new Criteria(); Criteria crit = new Criteria();
@ -137,7 +152,7 @@ public class CellLocationChangedReceiver extends PhoneStateListener
String myProviderName; String myProviderName;
// If privacy mode or no data connection available // If privacy mode or no data connection available
if(Settings.privacyLocationing | !ConnectivityReceiver.isDataConnectionAvailable(AutomationService.getInstance())) if(Settings.privacyLocationing || !ConnectivityReceiver.isDataConnectionAvailable(AutomationService.getInstance()))
{ {
Miscellaneous.logEvent("i", "CellLocation", Miscellaneous.getAnyContext().getResources().getString(R.string.enforcingGps), 4); Miscellaneous.logEvent("i", "CellLocation", Miscellaneous.getAnyContext().getResources().getString(R.string.enforcingGps), 4);
myProviderName = LocationManager.GPS_PROVIDER; myProviderName = LocationManager.GPS_PROVIDER;
@ -175,6 +190,9 @@ public class CellLocationChangedReceiver extends PhoneStateListener
} }
else else
{ {
if(myLocationManager == null)
myLocationManager = (LocationManager) AutomationService.getInstance().getSystemService(Context.LOCATION_SERVICE);
if(!myLocationManager.isProviderEnabled(myProviderName)) if(!myLocationManager.isProviderEnabled(myProviderName))
{ {
if(myProviderName.equals(LocationManager.NETWORK_PROVIDER)) if(myProviderName.equals(LocationManager.NETWORK_PROVIDER))
@ -226,13 +244,11 @@ public class CellLocationChangedReceiver extends PhoneStateListener
return currentLocation; return currentLocation;
} }
public void setCurrentLocation(Location currentLocation) public void setCurrentLocation(Location currentLocation)
{ {
this.currentLocation = currentLocation; this.currentLocation = currentLocation;
} }
public class MyLocationListener implements LocationListener public class MyLocationListener implements LocationListener
{ {
@Override @Override
@ -327,7 +343,7 @@ public class CellLocationChangedReceiver extends PhoneStateListener
{ {
if(!cellLocationListenerActive) if(!cellLocationListenerActive)
{ {
if(!ConnectivityReceiver.isAirplaneMode(AutomationService.getInstance())) if(!ConnectivityReceiver.isAirplaneMode(AutomationService.getInstance()) && telephonyManager.getSimState() == TelephonyManager.SIM_STATE_READY)
{ {
if(WifiBroadcastReceiver.mayCellLocationReceiverBeActivated()) if(WifiBroadcastReceiver.mayCellLocationReceiverBeActivated())
{ {
@ -356,7 +372,7 @@ public class CellLocationChangedReceiver extends PhoneStateListener
Miscellaneous.logEvent("w", "cellReceiver", "Wanted to activate CellLocationChangedReceiver, but Wifi-Receiver says not to.", 4); Miscellaneous.logEvent("w", "cellReceiver", "Wanted to activate CellLocationChangedReceiver, but Wifi-Receiver says not to.", 4);
} }
else else
Miscellaneous.logEvent("i", "cellReceiver", "Not starting cellLocationListener because Airplane mode is active.", 4); Miscellaneous.logEvent("i", "cellReceiver", "Not starting cellLocationListener because Airplane mode is active or SIM_STATE is not ready.", 4);
} }
} }
catch(Exception ex) catch(Exception ex)
@ -410,4 +426,3 @@ public class CellLocationChangedReceiver extends PhoneStateListener
ActivityPermissions.havePermission("android.permission.ACCESS_WIFI_STATE", Miscellaneous.getAnyContext()); ActivityPermissions.havePermission("android.permission.ACCESS_WIFI_STATE", Miscellaneous.getAnyContext());
} }
} }

View File

@ -7,6 +7,7 @@ import android.location.LocationManager;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.os.Message; import android.os.Message;
import android.telephony.TelephonyManager;
import android.util.Log; import android.util.Log;
import com.jens.automation2.ActivityMainScreen; import com.jens.automation2.ActivityMainScreen;
@ -26,13 +27,9 @@ import java.util.Calendar;
public class LocationProvider public class LocationProvider
{ {
protected static boolean passiveLocationListenerActive = false; protected static boolean passiveLocationListenerActive = false;
protected static LocationListener passiveLocationListener; protected static LocationListener passiveLocationListener;
protected static LocationProvider locationProviderInstance = null; protected static LocationProvider locationProviderInstance = null;
protected AutomationService parentService; protected AutomationService parentService;
public AutomationService getParentService() public AutomationService getParentService()
{ {
@ -108,6 +105,8 @@ public class LocationProvider
} }
public void setCurrentLocation(Location newLocation, boolean skipVerification) public void setCurrentLocation(Location newLocation, boolean skipVerification)
{
if(newLocation != null)
{ {
Miscellaneous.logEvent("i", "Location", "Setting location.", 4); Miscellaneous.logEvent("i", "Location", "Setting location.", 4);
@ -227,6 +226,9 @@ public class LocationProvider
if (AutomationService.isMainActivityRunning(parentService)) if (AutomationService.isMainActivityRunning(parentService))
ActivityMainScreen.updateMainScreen(); ActivityMainScreen.updateMainScreen();
} }
else
Miscellaneous.logEvent("w", "Location", "New location given is null. Ignoring.", 5);
}
public void startLocationService() public void startLocationService()
{ {
@ -244,14 +246,38 @@ public class LocationProvider
if(Settings.positioningEngine == 0) if(Settings.positioningEngine == 0)
{ {
if(Rule.isAnyRuleUsing(Trigger_Enum.pointOfInterest) | Rule.isAnyRuleUsing(Trigger_Enum.speed))
{
// TelephonyManager telephonyManager = (TelephonyManager) AutomationService.getInstance().getSystemService(Context.TELEPHONY_SERVICE);
// startCellLocationChangedReceiver // startCellLocationChangedReceiver
if (!ConnectivityReceiver.isAirplaneMode(this.parentService) && WifiBroadcastReceiver.mayCellLocationReceiverBeActivated() && (Rule.isAnyRuleUsing(Trigger_Enum.pointOfInterest) | Rule.isAnyRuleUsing(Trigger_Enum.speed))) if (CellLocationChangedReceiver.isCellLocationChangedReceiverPossible())
{
if (WifiBroadcastReceiver.mayCellLocationReceiverBeActivated())
CellLocationChangedReceiver.startCellLocationChangedReceiver(); CellLocationChangedReceiver.startCellLocationChangedReceiver();
}
else
{
/*
Reasons why we may end up here:
- Airplane mode is active
- No phone module present (pure wifi device)
- No SIM card is inserted or it's not unlocked
We'd have to try GPS now to get an initial position.
For permanent use there is no way we could know when it
would make sense to check the position again.
*/
// Trigger a one-time-position-search
Location loc = CellLocationChangedReceiver.getInstance().getLocation("fine");
LocationProvider.getInstance().setCurrentLocation(loc, true);
}
// startPassiveLocationListener // startPassiveLocationListener
if(Rule.isAnyRuleUsing(Trigger_Enum.pointOfInterest) | Rule.isAnyRuleUsing(Trigger_Enum.speed))
startPassiveLocationListener(); startPassiveLocationListener();
} }
}
else else
{ {
// if(Rule.isAnyRuleUsing(Trigger_Enum.pointOfInterest)) // if(Rule.isAnyRuleUsing(Trigger_Enum.pointOfInterest))