start implement background service

git-svn-id: https://osmand.googlecode.com/svn/trunk@357 e29c36b1-1cfa-d876-8d93-3434fc2bb7b8
This commit is contained in:
Victor Shcherb 2010-07-16 08:49:43 +00:00
parent c01a387121
commit 7a17a41a39
9 changed files with 194 additions and 8 deletions

View file

@ -12,10 +12,13 @@ public class ToDoConstants {
// for 0.3 // for 0.3
// 68. Implement service to app work with screen offline // 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) // (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 : redesign poi selecting (show on map )
// Improvement : progress while loading tiles
// BUG add button show my location // BUG add button show my location

View file

@ -8,10 +8,12 @@
<Button android:text="@string/search_POI_level_btn" android:id="@+id/SearchPOILevelButton" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_weight="1"></Button> <Button android:text="@string/search_POI_level_btn" android:id="@+id/SearchPOILevelButton" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_weight="1"></Button>
<TextView android:layout_width="wrap_content" android:text="" android:id="@+id/SearchAreaText" android:layout_height="wrap_content"/> <TextView android:layout_width="wrap_content" android:text="" android:id="@+id/SearchAreaText" android:layout_height="wrap_content"/>
</LinearLayout> </LinearLayout>
<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/SearchFilterLayout" android:visibility="invisible">
<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/SearchFilterLayout" android:visibility="gone">
<TextView android:layout_width="wrap_content" android:text="@string/search_poi_filter" android:id="@+id/TextView" android:layout_height="wrap_content"/> <TextView android:layout_width="wrap_content" android:text="@string/search_poi_filter" android:id="@+id/TextView" android:layout_height="wrap_content"/>
<EditText android:layout_marginLeft = "5dp" android:layout_marginRight = "5dp" android:layout_width="fill_parent" android:text="" android:id="@+id/SearchFilter" android:layout_height="wrap_content"/> <EditText android:layout_marginLeft = "5dp" android:layout_marginRight = "5dp" android:layout_width="fill_parent" android:text="" android:id="@+id/SearchFilter" android:layout_height="wrap_content"/>
</LinearLayout> </LinearLayout>
<ListView android:id="@android:id/list" android:layout_width="fill_parent" <ListView android:id="@android:id/list" android:layout_width="fill_parent"
android:layout_height="fill_parent"></ListView> android:layout_height="fill_parent"></ListView>
</LinearLayout> </LinearLayout>

View file

@ -1,5 +1,17 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<resources> <resources>
<string name="network_provider">Сеть</string>
<string name="gps_provider">GPS</string>
<string name="int_min">мин.</string>
<string name="background_service_int_descr">Выберите интервал позиционирования для фонового сервиса</string>
<string name="background_service_int">Интервал позиционирования</string>
<string name="background_service_provider_descr">Выберите источник позиционирования для фонового сервиса</string>
<string name="background_service_provider">Источник позиционирования</string>
<string name="background_router_service_descr">Включить фоновый сервис для позиционирования через долгий период времени</string>
<string name="background_router_service">Сервис навигации</string>
<string name="off_router_service_no_gps_available">Сервис маршрутизации OsmAnd включен, отключив позиционирование, он перестает вести по маршруту.</string>
<string name="routing_settings_descr">Изменить настройки маршрутизации</string>
<string name="routing_settings">Маршрутизация</string>
<string name="hide_poi_filter">Скрыть фильтр</string> <string name="hide_poi_filter">Скрыть фильтр</string>
<string name="show_poi_filter">Показать фильтр</string> <string name="show_poi_filter">Показать фильтр</string>
<string name="search_poi_filter">Фильтр</string> <string name="search_poi_filter">Фильтр</string>

View file

@ -1,5 +1,17 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<resources> <resources>
<string name="network_provider">Network</string>
<string name="gps_provider">GPS</string>
<string name="int_min">min.</string>
<string name="background_service_int_descr">Choose interval to determine location for background service</string>
<string name="background_service_int">Positioning interval</string>
<string name="background_service_provider_descr">Choose location provider for background service</string>
<string name="background_service_provider">Location provider</string>
<string name="background_router_service_descr">Enable background service to track position in long time periods</string>
<string name="background_router_service">Router service</string>
<string name="off_router_service_no_gps_available">Background routing service OsmAnd requires turned on location.</string>
<string name="routing_settings_descr">Specify routing options</string>
<string name="routing_settings">Routing</string>
<string name="hide_poi_filter">Hide filter</string> <string name="hide_poi_filter">Hide filter</string>
<string name="show_poi_filter">Show filter</string> <string name="show_poi_filter">Show filter</string>
<string name="search_poi_filter">Filter</string> <string name="search_poi_filter">Filter</string>

View file

@ -21,10 +21,16 @@
<CheckBoxPreference android:summary="@string/use_english_names_descr" android:title="@string/use_english_names" android:key="use_english_names"></CheckBoxPreference> <CheckBoxPreference android:summary="@string/use_english_names_descr" android:title="@string/use_english_names" android:key="use_english_names"></CheckBoxPreference>
<Preference android:title="@string/reload_indexes" android:key="reload_indexes" android:summary="@string/reload_indexes_descr"></Preference> <Preference android:title="@string/reload_indexes" android:key="reload_indexes" android:summary="@string/reload_indexes_descr"></Preference>
<Preference android:title="@string/download_indexes" android:key="download_indexes" android:summary="@string/download_indexes_descr"></Preference> <Preference android:title="@string/download_indexes" android:key="download_indexes" android:summary="@string/download_indexes_descr"></Preference>
<ListPreference android:title="@string/router_service" android:key="router_service" android:summary="@string/router_service_descr"></ListPreference>
<ListPreference android:title="@string/voice_provider" android:key="voice_provider" android:summary="@string/voice_provider_descr"></ListPreference> <ListPreference android:title="@string/voice_provider" android:key="voice_provider" android:summary="@string/voice_provider_descr"></ListPreference>
</PreferenceScreen> </PreferenceScreen>
<PreferenceScreen android:title="@string/routing_settings" android:summary="@string/routing_settings_descr">
<ListPreference android:title="@string/router_service" android:key="router_service" android:summary="@string/router_service_descr"></ListPreference>
<CheckBoxPreference android:summary="@string/background_router_service_descr" android:title="@string/background_router_service" android:key="service_off_enabled"></CheckBoxPreference>
<ListPreference android:key="service_off_provider" android:title="@string/background_service_provider" android:summary="@string/background_service_provider_descr"></ListPreference>
<ListPreference android:key="service_off_interval" android:title="@string/background_service_int" android:summary="@string/background_service_int_descr"></ListPreference>
</PreferenceScreen>
<PreferenceScreen android:title="@string/monitor_preferences" android:summary="@string/monitor_preferences_descr"> <PreferenceScreen android:title="@string/monitor_preferences" android:summary="@string/monitor_preferences_descr">
<CheckBoxPreference android:summary="@string/save_track_to_gpx_descrp" android:title="@string/save_track_to_gpx" android:key="save_track_to_gpx"></CheckBoxPreference> <CheckBoxPreference android:summary="@string/save_track_to_gpx_descrp" android:title="@string/save_track_to_gpx" android:key="save_track_to_gpx"></CheckBoxPreference>
<ListPreference android:summary="@string/save_track_interval_descr" android:title="@string/save_track_interval" android:key="save_track_interval"></ListPreference> <ListPreference android:summary="@string/save_track_interval_descr" android:title="@string/save_track_interval" android:key="save_track_interval"></ListPreference>

View file

@ -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) {
}
}

View file

@ -6,6 +6,7 @@ import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor; import android.content.SharedPreferences.Editor;
import android.content.pm.ActivityInfo; import android.content.pm.ActivityInfo;
import android.location.LocationManager;
import com.osmand.activities.RouteProvider.RouteService; import com.osmand.activities.RouteProvider.RouteService;
import com.osmand.activities.search.SearchHistoryHelper; import com.osmand.activities.search.SearchHistoryHelper;
@ -532,5 +533,37 @@ public class OsmandSettings {
return prefs.edit().putBoolean(VOICE_MUTE, mute).commit(); 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);
}
} }

View file

@ -12,6 +12,7 @@ import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor; import android.content.SharedPreferences.Editor;
import android.content.pm.ActivityInfo; import android.content.pm.ActivityInfo;
import android.content.res.Resources; import android.content.res.Resources;
import android.location.LocationManager;
import android.os.Environment; import android.os.Environment;
import android.os.Bundle; import android.os.Bundle;
import android.preference.CheckBoxPreference; import android.preference.CheckBoxPreference;
@ -78,6 +79,9 @@ public class SettingsActivity extends PreferenceActivity implements OnPreference
private ListPreference maxLevelToDownload; private ListPreference maxLevelToDownload;
private ListPreference mapScreenOrientation; private ListPreference mapScreenOrientation;
private ListPreference voicePreference; private ListPreference voicePreference;
private ListPreference routeServiceInterval;
private ListPreference routeServiceProvider;
private CheckBoxPreference routeServiceEnabled;
private BooleanPreference[] booleanPreferences = new BooleanPreference[]{ private BooleanPreference[] booleanPreferences = new BooleanPreference[]{
@ -93,6 +97,7 @@ public class SettingsActivity extends PreferenceActivity implements OnPreference
}; };
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@ -136,6 +141,13 @@ public class SettingsActivity extends PreferenceActivity implements OnPreference
voicePreference =(ListPreference) screen.findPreference(OsmandSettings.VOICE_PROVIDER); voicePreference =(ListPreference) screen.findPreference(OsmandSettings.VOICE_PROVIDER);
voicePreference.setOnPreferenceChangeListener(this); 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 @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.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$ 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<intDescriptions.length; i++){
intDescriptions[i] = ints[i] + " " + getString(R.string.int_min); //$NON-NLS-1$
}
routeServiceInterval.setEntries(intDescriptions);
routeServiceInterval.setEntryValues(ints);
routeServiceInterval.setValue(OsmandSettings.getServiceOffInterval(this)/60000+""); //$NON-NLS-1$
routeServiceProvider.setEntries(new String[]{getString(R.string.gps_provider), getString(R.string.network_provider)});
routeServiceProvider.setEntryValues(new String[]{LocationManager.GPS_PROVIDER, LocationManager.NETWORK_PROVIDER});
routeServiceProvider.setValue(OsmandSettings.getServiceOffProvider(this));
routeServiceEnabled.setChecked(OsmandSettings.getServiceOffEnabled(this));
mapScreenOrientation.setEntries(new String[]{ mapScreenOrientation.setEntries(new String[]{
resources.getString(R.string.map_orientation_portrait), resources.getString(R.string.map_orientation_portrait),
@ -294,13 +319,20 @@ public class SettingsActivity extends PreferenceActivity implements OnPreference
} else if(preference == userName){ } else if(preference == userName){
edit.putString(OsmandSettings.USER_NAME, (String) newValue); edit.putString(OsmandSettings.USER_NAME, (String) newValue);
edit.commit(); edit.commit();
} else if(preference == positionOnMap){ } else if(preference == positionOnMap){
edit.putInt(OsmandSettings.POSITION_ON_MAP, positionOnMap.findIndexOfValue((String) newValue)); edit.putInt(OsmandSettings.POSITION_ON_MAP, positionOnMap.findIndexOfValue((String) newValue));
edit.commit(); edit.commit();
} else if (preference == maxLevelToDownload) { } else if (preference == maxLevelToDownload) {
edit.putInt(OsmandSettings.MAX_LEVEL_TO_DOWNLOAD_TILE, Integer.parseInt((String) newValue)); edit.putInt(OsmandSettings.MAX_LEVEL_TO_DOWNLOAD_TILE, Integer.parseInt((String) newValue));
edit.commit(); edit.commit();
} else if (preference == routeServiceInterval) {
edit.putInt(OsmandSettings.SERVICE_OFF_INTERVAL, Integer.parseInt((String) newValue) * 60000);
edit.commit();
} else if (preference == routeServiceProvider) {
edit.putString(OsmandSettings.SERVICE_OFF_PROVIDER, (String) newValue);
edit.commit();
} else if (preference == routeServiceEnabled) {
// TODO !!!
} else if (preference == routerPreference) { } else if (preference == routerPreference) {
RouteService s = null; RouteService s = null;
for(RouteService r : RouteService.values()){ for(RouteService r : RouteService.values()){

View file

@ -235,12 +235,12 @@ public class SearchPOIActivity extends ListActivity implements SensorEventListen
@Override @Override
public boolean onMenuItemClick(MenuItem item) { public boolean onMenuItemClick(MenuItem item) {
if(searchFilterLayout.getVisibility() == View.INVISIBLE){ if(searchFilterLayout.getVisibility() == View.GONE){
searchFilterLayout.setVisibility(View.VISIBLE); searchFilterLayout.setVisibility(View.VISIBLE);
me.setTitle(R.string.hide_poi_filter); me.setTitle(R.string.hide_poi_filter);
} else { } else {
searchFilter.setText(""); //$NON-NLS-1$ searchFilter.setText(""); //$NON-NLS-1$
searchFilterLayout.setVisibility(View.INVISIBLE); searchFilterLayout.setVisibility(View.GONE);
me.setTitle(R.string.show_poi_filter); me.setTitle(R.string.show_poi_filter);
} }
return true; return true;