LocationService

This commit is contained in:
Milos Kozak 2018-09-19 15:26:58 +02:00
parent efec01c6e5
commit 3eee5b13c7
9 changed files with 264 additions and 2 deletions

View file

@ -17,6 +17,7 @@
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.permission.PROVIDE_BACKGROUND" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="sugar.free.sightremote.HISTORY_BROADCASTS" />
@ -141,6 +142,9 @@
<service
android:name=".services.DataService"
android:exported="false" />
<service
android:name=".services.LocationService"
android:exported="false" />
<service
android:name=".plugins.PumpDanaR.services.DanaRExecutionService"
android:enabled="true"

View file

@ -41,6 +41,7 @@ import info.nightscout.androidaps.plugins.SmsCommunicator.SmsCommunicatorPlugin;
import info.nightscout.androidaps.plugins.Source.SourceDexcomG5Plugin;
import info.nightscout.androidaps.plugins.Wear.WearPlugin;
import info.nightscout.androidaps.plugins.XDripStatusline.StatuslinePlugin;
import info.nightscout.androidaps.plugins.general.automation.AutomationPlugin;
import info.nightscout.utils.LocaleHelper;
import info.nightscout.utils.OKDialog;
import info.nightscout.utils.SP;
@ -182,6 +183,7 @@ public class PreferencesActivity extends PreferenceActivity implements SharedPre
addPreferencesFromResourceIfEnabled(NSClientPlugin.getPlugin(), PluginType.GENERAL);
addPreferencesFromResourceIfEnabled(SmsCommunicatorPlugin.getPlugin(), PluginType.GENERAL);
addPreferencesFromResourceIfEnabled(AutomationPlugin.getPlugin(), PluginType.GENERAL);
addPreferencesFromResource(R.xml.pref_others);
addPreferencesFromResource(R.xml.pref_datachoices);

View file

@ -0,0 +1,11 @@
package info.nightscout.androidaps.events;
import android.location.Location;
public class EventLocationChange extends Event {
public Location location;
public EventLocationChange(Location location) {
this.location = location;
}
}

View file

@ -95,6 +95,7 @@ public class L {
public static final String PROFILE = "PROFILE";
public static final String CONFIGBUILDER = "CONFIGBUILDER";
public static final String UI = "UI";
public static final String LOCATION = "LOCATION";
private static void initialize() {
logElements = new ArrayList<>();
@ -109,6 +110,7 @@ public class L {
logElements.add(new LogElement(DATASERVICE, true));
logElements.add(new LogElement(DATATREATMENTS, true));
logElements.add(new LogElement(EVENTS, false, true));
logElements.add(new LogElement(LOCATION, true));
logElements.add(new LogElement(NOTIFICATION, true));
logElements.add(new LogElement(NSCLIENT, true));
logElements.add(new LogElement(OVERVIEW, true));

View file

@ -1,13 +1,25 @@
package info.nightscout.androidaps.plugins.general.automation;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import com.squareup.otto.Subscribe;
import java.util.ArrayList;
import java.util.List;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.events.EventChargingState;
import info.nightscout.androidaps.events.EventLocationChange;
import info.nightscout.androidaps.events.EventNetworkChange;
import info.nightscout.androidaps.events.EventPreferenceChange;
import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.interfaces.PluginDescription;
import info.nightscout.androidaps.interfaces.PluginType;
import info.nightscout.androidaps.plugins.general.automation.actions.Action;
import info.nightscout.androidaps.plugins.IobCobCalculator.events.EventAutosensCalculationFinished;
import info.nightscout.androidaps.services.LocationService;
public class AutomationPlugin extends PluginBase {
@ -20,6 +32,9 @@ public class AutomationPlugin extends PluginBase {
}
List<AutomationEvent> automationEvents = new ArrayList<>();
EventLocationChange eventLocationChange;
EventChargingState eventChargingState;
EventNetworkChange eventNetworkChange;
private AutomationPlugin() {
super(new PluginDescription()
@ -27,8 +42,63 @@ public class AutomationPlugin extends PluginBase {
.fragmentClass(AutomationFragment.class.getName())
.pluginName(R.string.automation)
.shortName(R.string.automation_short)
.preferencesId(R.xml.pref_safety)
.preferencesId(R.xml.pref_automation)
.description(R.string.automation_description)
);
}
@Override
protected void onStart() {
Context context = MainApp.instance().getApplicationContext();
context.startService(new Intent(context, LocationService.class));
MainApp.bus().register(this);
super.onStart();
}
@Override
protected void onStop() {
Context context = MainApp.instance().getApplicationContext();
context.stopService(new Intent(context, LocationService.class));
MainApp.bus().unregister(this);
}
@Subscribe
public void onEventPreferenceChange(EventPreferenceChange e) {
if (e.isChanged(R.string.key_location)) {
Context context = MainApp.instance().getApplicationContext();
context.stopService(new Intent(context, LocationService.class));
context.startService(new Intent(context, LocationService.class));
}
}
@Subscribe
public void onEventLocationChange(EventLocationChange e) {
eventLocationChange = e;
processActions();
}
@Subscribe
public void onEventChargingState(EventChargingState e) {
eventChargingState = e;
processActions();
}
@Subscribe
public void onEventNetworkChange(EventNetworkChange e) {
eventNetworkChange = e;
processActions();
}
@Subscribe
public void onEventAutosensCalculationFinished(EventAutosensCalculationFinished e) {
processActions();
}
// TODO keepalive
void processActions() {
}
}

View file

@ -0,0 +1,146 @@
package info.nightscout.androidaps.services;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v4.app.ActivityCompat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.events.EventLocationChange;
import info.nightscout.androidaps.logging.L;
import info.nightscout.utils.SP;
public class LocationService extends Service {
private static Logger log = LoggerFactory.getLogger(L.LOCATION);
private LocationManager mLocationManager = null;
private static final int LOCATION_INTERVAL = 1000;
private static final float LOCATION_DISTANCE = 10f;
public LocationService() {
MainApp.bus().register(this);
}
private class LocationListener implements android.location.LocationListener {
Location mLastLocation;
public LocationListener(String provider) {
if (L.isEnabled(L.LOCATION))
log.debug("LocationListener " + provider);
mLastLocation = new Location(provider);
}
@Override
public void onLocationChanged(Location location) {
if (L.isEnabled(L.LOCATION))
log.debug("onLocationChanged: " + location);
mLastLocation.set(location);
MainApp.bus().post(new EventLocationChange(location));
}
@Override
public void onProviderDisabled(String provider) {
if (L.isEnabled(L.LOCATION))
log.debug("onProviderDisabled: " + provider);
}
@Override
public void onProviderEnabled(String provider) {
if (L.isEnabled(L.LOCATION))
log.debug("onProviderEnabled: " + provider);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
if (L.isEnabled(L.LOCATION))
log.debug("onStatusChanged: " + provider);
}
}
LocationListener mLocationListener = new LocationListener(LocationManager.PASSIVE_PROVIDER);
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (L.isEnabled(L.LOCATION))
log.debug("onStartCommand");
super.onStartCommand(intent, flags, startId);
return START_STICKY;
}
@Override
public void onCreate() {
if (L.isEnabled(L.LOCATION))
log.debug("onCreate");
initializeLocationManager();
try {
if (SP.getString(R.string.key_location, "NONE").equals("NETWORK"))
mLocationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
LOCATION_INTERVAL,
LOCATION_DISTANCE,
mLocationListener
);
if (SP.getString(R.string.key_location, "NONE").equals("GPS"))
mLocationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
LOCATION_INTERVAL,
LOCATION_DISTANCE,
mLocationListener
);
if (SP.getString(R.string.key_location, "NONE").equals("PASSIVE"))
mLocationManager.requestLocationUpdates(
LocationManager.PASSIVE_PROVIDER,
LOCATION_INTERVAL,
LOCATION_DISTANCE,
mLocationListener
);
} catch (java.lang.SecurityException ex) {
log.error("fail to request location update, ignore", ex);
} catch (IllegalArgumentException ex) {
log.error("network provider does not exist, " + ex.getMessage());
}
}
@Override
public void onDestroy() {
if (L.isEnabled(L.LOCATION))
log.debug("onDestroy");
super.onDestroy();
MainApp.bus().unregister(this);
if (mLocationManager != null) {
try {
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
mLocationManager.removeUpdates(mLocationListener);
} catch (Exception ex) {
log.error("fail to remove location listener, ignore", ex);
}
}
}
private void initializeLocationManager() {
if (L.isEnabled(L.LOCATION))
log.debug("initializeLocationManager - LOCATION_INTERVAL: " + LOCATION_INTERVAL + " LOCATION_DISTANCE: " + LOCATION_DISTANCE);
if (mLocationManager == null) {
mLocationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
}
}
}

View file

@ -114,6 +114,18 @@
<item>@string/yes</item>
</string-array>
<string-array name="location">
<item>@string/use_passive_location</item>
<item>@string/use_network_location</item>
<item>@string/use_gps_location</item>
</string-array>
<string-array name="locationValues" translatable="false">
<item>PASSIVE</item>
<item>NETWORK</item>
<item>GPS</item>
</string-array>
<string-array name="virtualPumpTypes">
<item>Generic AAPS</item>
<item>Accu-Chek Spirit</item>

View file

@ -1225,6 +1225,11 @@
<string name="and">And</string>
<string name="or">Or</string>
<string name="atspecifiedtime">At %1$s</string>
<string name="use_network_location">Use network location</string>
<string name="use_gps_location">Use GPS location</string>
<string name="use_passive_location">Use passive location</string>
<string name="locationservice">Location service</string>
<string name="key_location" translatable="false">location</string>
<plurals name="objective_days">
<item quantity="one">%1$d day</item>

View file

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:validate="http://schemas.android.com/apk/res-auto">
<ListPreference
android:title="@string/locationservice"
android:defaultValue="PASSIVE"
android:entries="@array/location"
android:entryValues="@array/locationValues"
android:key="@string/key_location" />
</PreferenceScreen>