aboutsummaryrefslogtreecommitdiffstats
path: root/src/org/broeuschmeul/android/gps/bluetooth/provider/BluetoothGpsProviderService.java
blob: 5df110485a3620a2024121d163c95e00af86c7d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
 * Copyright (C) 2010 Herbert von Broeuschmeul
 * Copyright (C) 2010 BluetoothGPS4Droid Project
 * 
 * This file is part of BluetoothGPS4Droid.
 *
 * BluetoothGPS4Droid is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * BluetoothGPS4Droid is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with BluetoothGPS4Droid. If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * 
 */
package org.broeuschmeul.android.gps.bluetooth.provider;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.GpsStatus.NmeaListener;
import android.os.Bundle;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.util.Config;
import android.util.Log;
import android.widget.Toast;

/**
 * A Service used to replace Android internal GPS with a bluetooth GPS and/or write GPS NMEA data in a File.
 * 
 * @author Herbert von Broeuschmeul
 *
 */
public class BluetoothGpsProviderService extends Service implements NmeaListener, LocationListener {
	
	public static final String ACTION_START_TRACK_RECORDING = "org.broeuschmeul.android.gps.bluetooth.tracker.nmea.intent.action.START_TRACK_RECORDING";
	public static final String ACTION_STOP_TRACK_RECORDING = "org.broeuschmeul.android.gps.bluetooth.tracker.nmea.intent.action.STOP_TRACK_RECORDING";
	public static final String ACTION_START_GPS_PROVIDER = "org.broeuschmeul.android.gps.bluetooth.provider.nmea.intent.action.START_GPS_PROVIDER";
	public static final String ACTION_STOP_GPS_PROVIDER = "org.broeuschmeul.android.gps.bluetooth.provider.nmea.intent.action.STOP_GPS_PROVIDER";
	public static final String PREF_START_GPS_PROVIDER = "startGps";
	public static final String PREF_TRACK_RECORDING = "trackRecording";
	public static final String PREF_TRACK_MIN_DISTANCE = "trackMinDistance";
	public static final String PREF_TRACK_MIN_TIME = "trackMinTime";
	public static final String PREF_TRACK_FILE_DIR = "trackFileDirectory";
	public static final String PREF_TRACK_FILE_PREFIX = "trackFilePrefix";
	public static final String PREF_BLUETOOTH_DEVICE = "bluetoothDevice";

	private LocationManager lm;
    private BlueetoothGpsManager gpsManager = null;
	private PrintWriter writer;
	private File trackFile;
	private boolean preludeWritten = false;
	private Toast toast ;
	
	@Override
	public void onCreate() {
		super.onCreate();
	    toast = Toast.makeText(getApplicationContext(), "NMEA track recording... on", Toast.LENGTH_SHORT);		
        lm = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
	}

	@Override
	public void onStart(Intent intent, int startId) {
//		super.onStart(intent, startId);
		SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
		SharedPreferences.Editor edit = sharedPreferences.edit();
		String deviceAddress = sharedPreferences.getString(PREF_BLUETOOTH_DEVICE, null);
		if (Config.LOGD){
			Log.d(BluetoothGpsProviderService.class.getName(), "prefs device addr: "+deviceAddress);
		}
		if (ACTION_START_GPS_PROVIDER.equals(intent.getAction())){
			if (gpsManager == null){
				if (BluetoothAdapter.checkBluetoothAddress(deviceAddress)){
					gpsManager = new BlueetoothGpsManager(this, deviceAddress);
					gpsManager.enable();
					if (gpsManager.isEnabled()){
						gpsManager.enableMockLocationProvider(LocationManager.GPS_PROVIDER);
						if (! sharedPreferences.getBoolean(PREF_START_GPS_PROVIDER, false)){
							edit.putBoolean(PREF_START_GPS_PROVIDER,true);
							edit.commit();
						}
						Notification notification = new Notification(R.drawable.icon, this.getString(R.string.foreground_gps_provider_started_notification),  System.currentTimeMillis());
						Intent myIntent = new Intent(this, BluetoothGpsActivity.class);
						PendingIntent myPendingIntent = PendingIntent.getActivity(this, 0, myIntent, PendingIntent.FLAG_CANCEL_CURRENT);
						notification.setLatestEventInfo(getApplicationContext(), this.getString(R.string.foreground_service_started_notification_title), this.getString(R.string.foreground_gps_provider_started_notification), myPendingIntent);
						startForeground(R.string.foreground_gps_provider_started_notification, notification);
						toast.setText(this.getString(R.string.msg_gps_provider_started));
						toast.show();	
					} else {
						stopSelf();
					}
				} else {
//					if (! sharedPreferences.getBoolean(PREF_START_GPS_PROVIDER, true)){
//						edit.putBoolean(PREF_START_GPS_PROVIDER,false);
//						edit.commit();
//					}
//					toast.setText(this.getString(R.string.msg_invalid_device));
//					toast.show();				
					stopSelf();
				}
			} else {
				toast.setText(this.getString(R.string.msg_gps_provider_already_started));
				toast.show();
			}
		} else if (ACTION_START_TRACK_RECORDING.equals(intent.getAction())){
			if (trackFile == null){
				if (gpsManager != null){
					beginTrack();
					gpsManager.addNmeaListener(this);
					if (! sharedPreferences.getBoolean(PREF_TRACK_RECORDING, false)){
						edit.putBoolean(PREF_TRACK_RECORDING,true);
						edit.commit();
					}
					toast.setText(this.getString(R.string.msg_nmea_recording_started));
					toast.show();				
				} else {
					endTrack();
					if ( sharedPreferences.getBoolean(PREF_TRACK_RECORDING, true)){
						edit.putBoolean(PREF_TRACK_RECORDING,false);
						edit.commit();
					}
//					toast.setText(this.getString(R.string.msg_device_not_started));
//					toast.show();				
				}
			} else {
				toast.setText(this.getString(R.string.msg_nmea_recording_already_started));
				toast.show();
			}
		} else if (ACTION_STOP_TRACK_RECORDING.equals(intent.getAction())){
			if (gpsManager != null){
				gpsManager.removeNmeaListener(this);
				endTrack();
				toast.setText(this.getString(R.string.msg_nmea_recording_stopped));
				toast.show();
			}
			if (sharedPreferences.getBoolean(PREF_TRACK_RECORDING, true)){
				edit.putBoolean(PREF_TRACK_RECORDING,false);
				edit.commit();
			}
		} else if (ACTION_STOP_GPS_PROVIDER.equals(intent.getAction())){
			if (sharedPreferences.getBoolean(PREF_START_GPS_PROVIDER, true)){
				edit.putBoolean(PREF_START_GPS_PROVIDER,false);
				edit.commit();
			}
			stopSelf();
		}
	}

	/* (non-Javadoc)
	 * @see android.app.Service#onStartCommand(android.content.Intent, int, int)
	 */
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		onStart(intent, startId);
		return Service.START_NOT_STICKY;
	}

	@Override
	public void onDestroy() {
		BlueetoothGpsManager manager = gpsManager;
		gpsManager  = null;
		if (manager != null){
			if (manager.getDisableReason() != 0){
				toast.setText(manager.getDisableReason());
				toast.show();
			} else {
				toast.setText(R.string.msg_gps_provider_stopped);
				toast.show();
			}
			manager.removeNmeaListener(this);
			manager.disableMockLocationProvider();
			manager.disable();
		}
		endTrack();
		SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
		SharedPreferences.Editor edit = sharedPreferences.edit();
		if (sharedPreferences.getBoolean(PREF_TRACK_RECORDING, true)){
			edit.putBoolean(PREF_TRACK_RECORDING,false);
			edit.commit();
		}
		if (sharedPreferences.getBoolean(PREF_START_GPS_PROVIDER, true)){
			edit.putBoolean(PREF_START_GPS_PROVIDER,false);
			edit.commit();
		}
		super.onDestroy();
	}
	
	private void beginTrack(){
		SimpleDateFormat fmt = new SimpleDateFormat("_yyyy-MM-dd_HH-mm-ss'.nmea'");
		SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
		String trackDirName = sharedPreferences.getString(PREF_TRACK_FILE_DIR, this.getString(R.string.defaultTrackFileDirectory));
		String trackFilePrefix = sharedPreferences.getString(PREF_TRACK_FILE_PREFIX, this.getString(R.string.defaultTrackFilePrefix));
		trackFile = new File(trackDirName,trackFilePrefix+fmt.format(new Date()));
		Log.i(BluetoothGpsProviderService.class.getName(), "Writing the prelude of the NMEA file: "+trackFile.getAbsolutePath());
		File trackDir = trackFile.getParentFile();
		try {
			if ((! trackDir.mkdirs()) && (! trackDir.isDirectory())){
				Log.e(BluetoothGpsProviderService.class.getName(), "Error while creating parent dir of NMEA file: "+trackDir.getAbsolutePath());
			}
			writer = new PrintWriter(new BufferedWriter(new FileWriter(trackFile)));
			preludeWritten = true;
		} catch (IOException e) {
			Log.e(BluetoothGpsProviderService.class.getName(), "Error while writing the prelude of the NMEA file: "+trackFile.getAbsolutePath(), e);
			// there was an error while writing the prelude of the NMEA file, stopping the service...
			stopSelf();
//		} catch (SecurityException e) {
//			Log.e(BluetoothGpsProviderService.class.getName(), "Error while writing the prelude of the NMEA file: "+trackFile.getAbsolutePath(), e);
//			// there was an error while writing the prelude of the NMEA file, stopping the service...
//			stopSelf();
		}
	}
	private void endTrack(){
		if (trackFile != null && writer != null){
			Log.i(BluetoothGpsProviderService.class.getName(), "Ending the NMEA file: "+trackFile.getAbsolutePath());
			preludeWritten = false;
			writer.close();
			trackFile = null;
		}
	}
	private void addNMEAString(String data){
		if (! preludeWritten){
			beginTrack();
		}
		Log.d(BluetoothGpsProviderService.class.getName(), "Adding data in the NMEA file: "+ data);
		if (trackFile != null && writer != null){
			writer.print(data);
		}
	}
	/* (non-Javadoc)
	 * @see android.app.Service#onBind(android.content.Intent)
	 */
	@Override
	public IBinder onBind(Intent intent) {
		if (Config.LOGD){
			Log.d(BluetoothGpsProviderService.class.getName(), "trying access IBinder");
		}				
		return null;
	}

	@Override
	public void onLocationChanged(Location location) {
		// TODO Auto-generated method stub
	}

	@Override
	public void onProviderDisabled(String provider) {
		Log.e(BluetoothGpsProviderService.class.getName(), "The GPS has been disabled.....stopping the NMEA tracker service.");
		stopSelf();
	}

	@Override
	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub
	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub		
	}

	@Override
	public void onNmeaReceived(long timestamp, String data) {
		addNMEAString(data);
	}
}