From 7a17a41a399aa14b2aecc6bdefe4a5024dbdd6e7 Mon Sep 17 00:00:00 2001 From: Victor Shcherb Date: Fri, 16 Jul 2010 08:49:43 +0000 Subject: [PATCH] start implement background service git-svn-id: https://osmand.googlecode.com/svn/trunk@357 e29c36b1-1cfa-d876-8d93-3434fc2bb7b8 --- .../src/com/osmand/ToDoConstants.java | 7 +- OsmAnd/res/layout/searchpoi.xml | 6 +- OsmAnd/res/values-ru/strings.xml | 12 +++ OsmAnd/res/values/strings.xml | 12 +++ OsmAnd/res/xml/settings_pref.xml | 8 +- OsmAnd/src/com/osmand/NavigationService.java | 86 +++++++++++++++++++ OsmAnd/src/com/osmand/OsmandSettings.java | 33 +++++++ .../osmand/activities/SettingsActivity.java | 34 +++++++- .../activities/search/SearchPOIActivity.java | 4 +- 9 files changed, 194 insertions(+), 8 deletions(-) create mode 100644 OsmAnd/src/com/osmand/NavigationService.java diff --git a/DataExtractionOSM/src/com/osmand/ToDoConstants.java b/DataExtractionOSM/src/com/osmand/ToDoConstants.java index f4130c197a..1a2b050427 100644 --- a/DataExtractionOSM/src/com/osmand/ToDoConstants.java +++ b/DataExtractionOSM/src/com/osmand/ToDoConstants.java @@ -12,10 +12,13 @@ public class ToDoConstants { // for 0.3 // 68. Implement service to app work with screen offline // (introduce special settings how often update location to monitoring & audio guidance & add new item to status bar) + // for offline service save gpx (combine in one trkseg) - - // Improvement : Show stops in the transport route on the map + // Improvement : Show stops in the transport route on the map + // Improvement : show favorites on the map? + // Improvement : show detailed route on the map with turns and show route information directly // Improvement : redesign poi selecting (show on map ) + // Improvement : progress while loading tiles // BUG add button show my location diff --git a/OsmAnd/res/layout/searchpoi.xml b/OsmAnd/res/layout/searchpoi.xml index 94655f59e9..9c98c05bf2 100644 --- a/OsmAnd/res/layout/searchpoi.xml +++ b/OsmAnd/res/layout/searchpoi.xml @@ -8,10 +8,12 @@ - + + - + \ No newline at end of file diff --git a/OsmAnd/res/values-ru/strings.xml b/OsmAnd/res/values-ru/strings.xml index 4a0b306928..bdaea7fcd7 100644 --- a/OsmAnd/res/values-ru/strings.xml +++ b/OsmAnd/res/values-ru/strings.xml @@ -1,5 +1,17 @@ + Сеть + GPS + мин. + Выберите интервал позиционирования для фонового сервиса + Интервал позиционирования + Выберите источник позиционирования для фонового сервиса + Источник позиционирования + Включить фоновый сервис для позиционирования через долгий период времени + Сервис навигации + Сервис маршрутизации OsmAnd включен, отключив позиционирование, он перестает вести по маршруту. + Изменить настройки маршрутизации + Маршрутизация Скрыть фильтр Показать фильтр Фильтр diff --git a/OsmAnd/res/values/strings.xml b/OsmAnd/res/values/strings.xml index 1677acf589..75f5a59fc0 100644 --- a/OsmAnd/res/values/strings.xml +++ b/OsmAnd/res/values/strings.xml @@ -1,5 +1,17 @@ + Network + GPS + min. + Choose interval to determine location for background service + Positioning interval + Choose location provider for background service + Location provider + Enable background service to track position in long time periods + Router service + Background routing service OsmAnd requires turned on location. + Specify routing options + Routing Hide filter Show filter Filter diff --git a/OsmAnd/res/xml/settings_pref.xml b/OsmAnd/res/xml/settings_pref.xml index 00cffea30a..c72a60456d 100644 --- a/OsmAnd/res/xml/settings_pref.xml +++ b/OsmAnd/res/xml/settings_pref.xml @@ -21,10 +21,16 @@ - + + + + + + + diff --git a/OsmAnd/src/com/osmand/NavigationService.java b/OsmAnd/src/com/osmand/NavigationService.java new file mode 100644 index 0000000000..845ccfb233 --- /dev/null +++ b/OsmAnd/src/com/osmand/NavigationService.java @@ -0,0 +1,86 @@ +package com.osmand; + + +import android.app.Service; +import android.content.Intent; +import android.location.Location; +import android.location.LocationListener; +import android.location.LocationManager; +import android.os.Binder; +import android.os.Bundle; +import android.os.IBinder; +import android.widget.Toast; + +import com.osmand.activities.SavingTrackHelper; + +public class NavigationService extends Service implements LocationListener { + + public static class NavigationServiceBinder extends Binder { + + } + private NavigationServiceBinder binder = new NavigationServiceBinder(); + private int serviceOffInterval; + private String serviceOffProvider; + private SavingTrackHelper savingTrackHelper; + + @Override + public IBinder onBind(Intent intent) { + return binder; + } + + + + @Override + public void onCreate() { + super.onCreate(); + setForeground(true); + LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); + serviceOffInterval = OsmandSettings.getServiceOffInterval(this); + serviceOffProvider = OsmandSettings.getServiceOffProvider(this); + locationManager.requestLocationUpdates(serviceOffProvider, serviceOffInterval, 0, this); + savingTrackHelper = new SavingTrackHelper(this); + + OsmandSettings.setServiceOffEnabled(this, true); + } + + + @Override + public void onDestroy() { + super.onDestroy(); + LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); + locationManager.removeUpdates(this); + OsmandSettings.setServiceOffEnabled(this, false); + } + + + + @Override + public void onLocationChanged(Location location) { + // TODO check that MapActivity is not foreground + if(location != null){ + savingTrackHelper.insertData(location.getLatitude(), location.getLongitude(), location.getAltitude(), + location.getSpeed(), location.getTime()); + } + + } + + + + @Override + public void onProviderDisabled(String provider) { + Toast.makeText(this, getString(R.string.off_router_service_no_gps_available), Toast.LENGTH_LONG).show(); + } + + + + @Override + public void onProviderEnabled(String provider) { + } + + + + @Override + public void onStatusChanged(String provider, int status, Bundle extras) { + } + +} diff --git a/OsmAnd/src/com/osmand/OsmandSettings.java b/OsmAnd/src/com/osmand/OsmandSettings.java index c323cf316e..c7bdb218c4 100644 --- a/OsmAnd/src/com/osmand/OsmandSettings.java +++ b/OsmAnd/src/com/osmand/OsmandSettings.java @@ -6,6 +6,7 @@ import android.content.Context; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.content.pm.ActivityInfo; +import android.location.LocationManager; import com.osmand.activities.RouteProvider.RouteService; import com.osmand.activities.search.SearchHistoryHelper; @@ -532,5 +533,37 @@ public class OsmandSettings { return prefs.edit().putBoolean(VOICE_MUTE, mute).commit(); } + // this value string is synchronized with settings_pref.xml preference name + public static final String SERVICE_OFF_ENABLED = "service_off_enabled"; //$NON-NLS-1$ + public static final boolean SERVICE_OFF_ENABLED_DEF = false; + public static boolean getServiceOffEnabled(Context ctx) { + SharedPreferences prefs = ctx.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_WORLD_READABLE); + return prefs.getBoolean(SERVICE_OFF_ENABLED, SERVICE_OFF_ENABLED_DEF); + } + + public static boolean setServiceOffEnabled(Context ctx, boolean en) { + SharedPreferences prefs = ctx.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_WORLD_READABLE); + return prefs.edit().putBoolean(SERVICE_OFF_ENABLED, en).commit(); + } + + + // this value string is synchronized with settings_pref.xml preference name + public static final String SERVICE_OFF_PROVIDER = "service_off_provider"; //$NON-NLS-1$ + public static String getServiceOffProvider(Context ctx) { + SharedPreferences prefs = ctx.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_WORLD_READABLE); + return prefs.getString(SERVICE_OFF_PROVIDER, LocationManager.GPS_PROVIDER); + } + + + // this value string is synchronized with settings_pref.xml preference name + public static final String SERVICE_OFF_INTERVAL = "service_off_interval"; //$NON-NLS-1$ + public static final int SERVICE_OFF_INTERVAL_DEF = 5 * 60 * 1000; + public static int getServiceOffInterval(Context ctx) { + SharedPreferences prefs = ctx.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_WORLD_READABLE); + return prefs.getInt(SERVICE_OFF_INTERVAL, SERVICE_OFF_INTERVAL_DEF); + } + + + } diff --git a/OsmAnd/src/com/osmand/activities/SettingsActivity.java b/OsmAnd/src/com/osmand/activities/SettingsActivity.java index 55220a611e..3e178c685c 100644 --- a/OsmAnd/src/com/osmand/activities/SettingsActivity.java +++ b/OsmAnd/src/com/osmand/activities/SettingsActivity.java @@ -12,6 +12,7 @@ import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.content.pm.ActivityInfo; import android.content.res.Resources; +import android.location.LocationManager; import android.os.Environment; import android.os.Bundle; import android.preference.CheckBoxPreference; @@ -78,6 +79,9 @@ public class SettingsActivity extends PreferenceActivity implements OnPreference private ListPreference maxLevelToDownload; private ListPreference mapScreenOrientation; private ListPreference voicePreference; + private ListPreference routeServiceInterval; + private ListPreference routeServiceProvider; + private CheckBoxPreference routeServiceEnabled; private BooleanPreference[] booleanPreferences = new BooleanPreference[]{ @@ -93,6 +97,7 @@ public class SettingsActivity extends PreferenceActivity implements OnPreference }; + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -136,6 +141,13 @@ public class SettingsActivity extends PreferenceActivity implements OnPreference voicePreference =(ListPreference) screen.findPreference(OsmandSettings.VOICE_PROVIDER); voicePreference.setOnPreferenceChangeListener(this); + routeServiceInterval =(ListPreference) screen.findPreference(OsmandSettings.SERVICE_OFF_INTERVAL); + routeServiceInterval.setOnPreferenceChangeListener(this); + routeServiceProvider =(ListPreference) screen.findPreference(OsmandSettings.SERVICE_OFF_PROVIDER); + routeServiceProvider.setOnPreferenceChangeListener(this); + routeServiceEnabled =(CheckBoxPreference) screen.findPreference(OsmandSettings.SERVICE_OFF_ENABLED); + routeServiceEnabled.setOnPreferenceChangeListener(this); + } @Override @@ -171,7 +183,20 @@ public class SettingsActivity extends PreferenceActivity implements OnPreference saveTrackInterval.setEntryValues(new String[]{"1", "2", "5", "15", "30", "60", "300"}); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ saveTrackInterval.setValue(OsmandSettings.getSavingTrackInterval(this)+""); //$NON-NLS-1$ + String[] ints = new String[]{"1", "2", "5", "8", "10", "15", "20", "25", "30", "40", "60", }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$ //$NON-NLS-10$ //$NON-NLS-11$ + String[] intDescriptions = new String[ints.length]; + for(int i=0; i