aboutsummaryrefslogtreecommitdiffstats
path: root/src/org
diff options
context:
space:
mode:
authorHerbert von Broeuschmeul <Herbert.Broeuschmeul@gmail.com>2010-10-27 01:55:24 +0200
committerHerbert von Broeuschmeul <Herbert.Broeuschmeul@gmail.com>2010-10-27 01:55:24 +0200
commit04001207adcf5575c813797d9897e5dd2a672938 (patch)
tree349534e435c7a6333988119b4736a98e73960f6a /src/org
parentd8b89a578820620079353293cab0776f0d72e101 (diff)
parent442660e78d09eacc40ff1ac8545a13c23c0355b7 (diff)
downloadBlueGPS-04001207adcf5575c813797d9897e5dd2a672938.zip
BlueGPS-04001207adcf5575c813797d9897e5dd2a672938.tar.gz
BlueGPS-04001207adcf5575c813797d9897e5dd2a672938.tar.bz2
merging refs/heads/BlueGPS4Droid_1.1.x into master (1.2.x)
Diffstat (limited to 'src/org')
-rw-r--r--src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java66
-rw-r--r--src/org/broeuschmeul/android/gps/bluetooth/provider/BluetoothGpsProviderService.java23
-rw-r--r--src/org/broeuschmeul/android/gps/nmea/util/NmeaParser.java119
3 files changed, 136 insertions, 72 deletions
diff --git a/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java b/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java
index 1bf766c..ad17857 100644
--- a/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java
+++ b/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java
@@ -48,7 +48,6 @@ import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Context;
import android.content.SharedPreferences;
-import android.location.Criteria;
import android.content.Intent;
import android.location.LocationManager;
import android.location.GpsStatus.NmeaListener;
@@ -162,6 +161,7 @@ public class BlueetoothGpsManager {
private LocationManager locationManager;
private SharedPreferences sharedPreferences;
private ConnectedGps connectedGps;
+ private int disableReason = 0;
private Notification connectionProblemNotification;
private Notification serviceStoppedNotification;
private Context appContext;
@@ -196,9 +196,16 @@ public class BlueetoothGpsManager {
appContext.getString(R.string.service_closed_because_connection_problem_notification_title),
appContext.getString(R.string.service_closed_because_connection_problem_notification),
restartPendingIntent);
+ }
-
+ private void setDisableReason(int reasonId){
+ disableReason = reasonId;
}
+
+ public int getDisableReason(){
+ return disableReason;
+ }
+
/**
* @return true if the bluetooth GPS is enabled
*/
@@ -213,29 +220,36 @@ public class BlueetoothGpsManager {
if (bluetoothAdapter == null) {
// Device does not support Bluetooth
Log.e("BT test", "Device does not support Bluetooth");
+ disable(R.string.msg_bluetooth_unsupported);
} else if (!bluetoothAdapter.isEnabled()) {
// Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
// startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
Log.e("BT test", "Bluetooth is not enabled");
+ disable(R.string.msg_bluetooth_disabled);
} else if (Settings.Secure.getInt(callingService.getContentResolver(),Settings.Secure.ALLOW_MOCK_LOCATION, 0)==0){
Log.e("BT test", "Mock location provider OFF");
- } else if ( (! locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
- // && (sharedPreferences.getBoolean(BluetoothGpsProviderService.PREF_REPLACE_STD_GPS, true))
- ) {
- Log.e("BT test", "GPS location provider OFF");
+ disable(R.string.msg_mock_location_disabled);
+// } else if ( (! locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
+// && (sharedPreferences.getBoolean(BluetoothGpsProviderService.PREF_REPLACE_STD_GPS, true))
+// ) {
+// Log.e("BT test", "GPS location provider OFF");
+// disable(R.string.msg_gps_provider_disabled);
} else {
final BluetoothDevice gpsDevice = bluetoothAdapter.getRemoteDevice(gpsDeviceAddress);
if (gpsDevice == null){
Log.e("BT test", "GPS device not found");
+ disable(R.string.msg_bluetooth_gps_unavaible);
} else {
Log.e("BT test", "current device: "+gpsDevice.getName() + " -- " + gpsDevice.getAddress());
try {
gpsSocket = gpsDevice.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
} catch (IOException e) {
Log.e("BT test", "Error during connection", e);
+ gpsSocket = null;
}
if (gpsSocket == null){
Log.e("BT test", "Error while establishing connection: no socket");
+ disable(R.string.msg_bluetooth_gps_unavaible);
} else {
Runnable connectThread = new Runnable() {
@Override
@@ -256,9 +270,11 @@ public class BlueetoothGpsManager {
gpsSocket = gpsDevice.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
} catch (IOException e) {
Log.e("BT test", "Error during connection", e);
+ gpsSocket = null;
}
if (gpsSocket == null){
Log.e("BT test", "Error while establishing connection: no socket");
+ disable(R.string.msg_bluetooth_gps_unavaible);
} else {
// Cancel discovery because it will slow down the connection
bluetoothAdapter.cancelDiscovery();
@@ -268,15 +284,20 @@ public class BlueetoothGpsManager {
gpsSocket.connect();
// connection obtained so reset the number of connection try
connected = true;
+ // reset eventual disabling cause
+// setDisableReason(0);
nbRetriesRemaining = 1+maxConnectionRetries ;
notificationManager.cancel(R.string.connection_problem_notification_title);
connectedGps = new ConnectedGps(gpsSocket);
connectionAndReadingPool.execute(connectedGps);
}
+// } else if (! bluetoothAdapter.isEnabled()) {
+// setDisableReason(R.string.msg_bluetooth_disabled);
}
} catch (IOException connectException) {
// Unable to connect
Log.e("BT test", "error while connecting to socket", connectException);
+ // disable(R.string.msg_bluetooth_gps_unavaible);
} finally {
nbRetriesRemaining--;
if (! connected) {
@@ -309,15 +330,29 @@ public class BlueetoothGpsManager {
connectionProblemNotification.number = 1 + maxConnectionRetries - nbRetriesRemaining;
notificationManager.notify(R.string.connection_problem_notification_title, connectionProblemNotification);
} else {
- notificationManager.cancel(R.string.connection_problem_notification_title);
- serviceStoppedNotification.when = System.currentTimeMillis();
- notificationManager.notify(R.string.service_closed_because_connection_problem_notification_title, serviceStoppedNotification);
- disable();
+// notificationManager.cancel(R.string.connection_problem_notification_title);
+// serviceStoppedNotification.when = System.currentTimeMillis();
+// notificationManager.notify(R.string.service_closed_because_connection_problem_notification_title, serviceStoppedNotification);
+ disable(R.string.msg_two_many_connection_problems);
}
}
}
+ public synchronized void disable(int reasonId) {
+ setDisableReason(reasonId);
+ disable();
+ }
+
public synchronized void disable() {
+ notificationManager.cancel(R.string.connection_problem_notification_title);
+ if (getDisableReason() != 0){
+ serviceStoppedNotification.when = System.currentTimeMillis();
+ serviceStoppedNotification.setLatestEventInfo(appContext,
+ appContext.getString(R.string.service_closed_because_connection_problem_notification_title),
+ appContext.getString(R.string.service_closed_because_connection_problem_notification, appContext.getString(getDisableReason())),
+ serviceStoppedNotification.contentIntent);
+ notificationManager.notify(R.string.service_closed_because_connection_problem_notification_title, serviceStoppedNotification);
+ }
if (enabled){
enabled = false;
if (gpsSocket != null){
@@ -331,7 +366,6 @@ public class BlueetoothGpsManager {
disableMockLocationProvider();
notificationPool.shutdown();
connectionAndReadingPool.shutdown();
- notificationManager.cancel(R.string.connection_problem_notification_title);
callingService.stopSelf();
}
}
@@ -388,7 +422,15 @@ public class BlueetoothGpsManager {
private void notifyNmeaSentence(final String nmeaSentence){
if (enabled){
- final String recognizedSentence = parser.parseNmeaSentence(nmeaSentence);
+ String sentence = null;
+ try {
+ sentence = parser.parseNmeaSentence(nmeaSentence);
+ } catch (SecurityException e){
+ // a priori Mock Location is disabled
+ sentence = null;
+ disable(R.string.msg_mock_location_disabled);
+ }
+ final String recognizedSentence = sentence;
final long timestamp = System.currentTimeMillis();
if (recognizedSentence != null){
Log.e("BT test", "NMEA : "+timestamp+" "+recognizedSentence);
diff --git a/src/org/broeuschmeul/android/gps/bluetooth/provider/BluetoothGpsProviderService.java b/src/org/broeuschmeul/android/gps/bluetooth/provider/BluetoothGpsProviderService.java
index 47b5cf2..37ce43a 100644
--- a/src/org/broeuschmeul/android/gps/bluetooth/provider/BluetoothGpsProviderService.java
+++ b/src/org/broeuschmeul/android/gps/bluetooth/provider/BluetoothGpsProviderService.java
@@ -107,17 +107,12 @@ public class BluetoothGpsProviderService extends Service implements NmeaListener
}
if (ACTION_START_GPS_PROVIDER.equals(intent.getAction())){
if (gpsManager == null){
- 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);
if (BluetoothAdapter.checkBluetoothAddress(deviceAddress)){
String mockProvider = LocationManager.GPS_PROVIDER;
if (! sharedPreferences.getBoolean(PREF_REPLACE_STD_GPS, true)){
mockProvider = sharedPreferences.getString(PREF_MOCK_GPS_NAME, getString(R.string.defaultMockGpsName));
}
gpsManager = new BlueetoothGpsManager(this, deviceAddress, maxConRetries);
- gpsManager.enableMockLocationProvider(mockProvider);
boolean enabled = gpsManager.enable();
// Bundle extras = intent.getExtras();
if (sharedPreferences.getBoolean(PREF_START_GPS_PROVIDER, false) != enabled){
@@ -125,12 +120,19 @@ public class BluetoothGpsProviderService extends Service implements NmeaListener
edit.commit();
}
if (enabled) {
+ gpsManager.enableMockLocationProvider(mockProvider);
+ 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);
if (sharedPreferences.getBoolean(PREF_SIRF_GPS, false)){
enableSirfConfig(sharedPreferences);
}
toast.setText(this.getString(R.string.msg_gps_provider_started));
toast.show();
+ } else {
+ stopSelf();
}
} else {
stopSelf();
@@ -352,6 +354,13 @@ public class BluetoothGpsProviderService extends Service implements NmeaListener
BlueetoothGpsManager manager = gpsManager;
gpsManager = null;
if (manager != null){
+ if (manager.getDisableReason() != 0){
+ toast.setText(getString(R.string.msg_gps_provider_stopped_by_problem, getString(manager.getDisableReason())));
+ toast.show();
+ } else {
+ toast.setText(R.string.msg_gps_provider_stopped);
+ toast.show();
+ }
manager.removeNmeaListener(this);
manager.disableMockLocationProvider();
manager.disable();
@@ -367,10 +376,6 @@ public class BluetoothGpsProviderService extends Service implements NmeaListener
edit.putBoolean(PREF_START_GPS_PROVIDER,false);
edit.commit();
}
-// toast.setText(this.getString(R.string.msg_nmea_recording_stopped));
-// toast.show();
- toast.setText(this.getString(R.string.msg_gps_provider_stopped));
- toast.show();
super.onDestroy();
}
diff --git a/src/org/broeuschmeul/android/gps/nmea/util/NmeaParser.java b/src/org/broeuschmeul/android/gps/nmea/util/NmeaParser.java
index 426855f..dbfae17 100644
--- a/src/org/broeuschmeul/android/gps/nmea/util/NmeaParser.java
+++ b/src/org/broeuschmeul/android/gps/nmea/util/NmeaParser.java
@@ -36,19 +36,20 @@ import android.text.TextUtils.SimpleStringSplitter;
import android.util.Log;
public class NmeaParser {
- private String fixTime;
+ private String fixTime = null;
private long fixTimestamp;
private boolean hasGGA = false;
private boolean hasRMC = false;
private LocationManager lm;
private float precision = 10f;
+ private boolean mockGpsAutoEnabled = false;
private boolean mockGpsEnabled = false;
private String mockLocationProvider = null;
private int mockStatus = LocationProvider.OUT_OF_SERVICE;
- private Location fix = new Location(mockLocationProvider);
+ private Location fix = null;
public NmeaParser(){
this(5f);
@@ -62,65 +63,81 @@ public class NmeaParser {
}
public void enableMockLocationProvider(String gpsName){
- LocationProvider prov;
- if (gpsName != null && gpsName != "" ){
- if (! gpsName.equals(mockLocationProvider)){
- disableMockLocationProvider();
- mockLocationProvider = gpsName;
- }
- if (! mockGpsEnabled){
+ try {
+ LocationProvider prov;
+ if (gpsName != null && gpsName != "" ){
+ if (! gpsName.equals(mockLocationProvider)){
+ disableMockLocationProvider();
+ mockLocationProvider = gpsName;
+ }
+ if (! mockGpsEnabled){
+ prov = lm.getProvider(mockLocationProvider);
+ if (prov != null){
+ Log.e("BT test", "Mock provider: "+prov.getName()+" "+prov.getPowerRequirement()+" "+prov.getAccuracy()+" "+lm.isProviderEnabled(mockLocationProvider));
+ }
+ lm.addTestProvider(mockLocationProvider, false, true,false, false, true, true, true, Criteria.POWER_HIGH, Criteria.ACCURACY_FINE);
+ if ((prov == null)
+ // || (! LocationManager.GPS_PROVIDER.equals(mockLocationProvider))
+ ){
+ Log.e("BT test", "enabling Mock provider: "+mockLocationProvider);
+ lm.setTestProviderEnabled(mockLocationProvider, true);
+ mockGpsAutoEnabled = true;
+ }
+ mockGpsEnabled = true;
+ } else {
+ Log.e("BT test", "Mock provider already enabled: "+mockLocationProvider);
+ }
prov = lm.getProvider(mockLocationProvider);
if (prov != null){
Log.e("BT test", "Mock provider: "+prov.getName()+" "+prov.getPowerRequirement()+" "+prov.getAccuracy()+" "+lm.isProviderEnabled(mockLocationProvider));
}
- lm.addTestProvider(mockLocationProvider, false, true,false, false, true, true, true, Criteria.POWER_HIGH, Criteria.ACCURACY_FINE);
- if (! LocationManager.GPS_PROVIDER.equals(mockLocationProvider)){
- lm.setTestProviderEnabled(mockLocationProvider, true);
- }
- mockGpsEnabled = true;
- } else {
- Log.e("BT test", "Mock provider already enabled: "+mockLocationProvider);
- }
- prov = lm.getProvider(mockLocationProvider);
- if (prov != null){
- Log.e("BT test", "Mock provider: "+prov.getName()+" "+prov.getPowerRequirement()+" "+prov.getAccuracy()+" "+lm.isProviderEnabled(mockLocationProvider));
}
+ } catch (SecurityException e){
+ Log.e("BT test", "Error while enabling Mock Mocations Provider", e);
+ disableMockLocationProvider();
}
}
public void disableMockLocationProvider(){
- LocationProvider prov;
- if (mockLocationProvider != null && mockLocationProvider != "" && mockGpsEnabled){
- prov = lm.getProvider(mockLocationProvider);
- if (prov != null){
- Log.e("BT test", "Mock provider: "+prov.getName()+" "+prov.getPowerRequirement()+" "+prov.getAccuracy()+" "+lm.isProviderEnabled(mockLocationProvider));
+ try {
+ LocationProvider prov;
+ if (mockLocationProvider != null && mockLocationProvider != "" && mockGpsEnabled){
+ prov = lm.getProvider(mockLocationProvider);
+ if (prov != null){
+ Log.e("BT test", "Mock provider: "+prov.getName()+" "+prov.getPowerRequirement()+" "+prov.getAccuracy()+" "+lm.isProviderEnabled(mockLocationProvider));
+ }
+ mockGpsEnabled = false;
+ if ( mockGpsAutoEnabled ) {
+ Log.e("BT test", "disabling Mock provider: "+mockLocationProvider);
+ lm.setTestProviderEnabled(mockLocationProvider, false);
+ }
+ prov = lm.getProvider(mockLocationProvider);
+ if (prov != null){
+ Log.e("BT test", "Mock provider: "+prov.getName()+" "+prov.getPowerRequirement()+" "+prov.getAccuracy()+" "+lm.isProviderEnabled(mockLocationProvider));
+ }
+ lm.clearTestProviderEnabled(mockLocationProvider);
+ prov = lm.getProvider(mockLocationProvider);
+ if (prov != null){
+ Log.e("BT test", "Mock provider: "+prov.getName()+" "+prov.getPowerRequirement()+" "+prov.getAccuracy()+" "+lm.isProviderEnabled(mockLocationProvider));
+ }
+ lm.clearTestProviderStatus(mockLocationProvider);
+ lm.removeTestProvider(mockLocationProvider);
+ prov = lm.getProvider(mockLocationProvider);
+ if (prov != null){
+ Log.e("BT test", "Mock provider: "+prov.getName()+" "+prov.getPowerRequirement()+" "+prov.getAccuracy()+" "+lm.isProviderEnabled(mockLocationProvider));
+ }
+ Log.e("BT test", "removed mock GPS");
+ } else {
+ Log.e("BT test", "Mock provider already disabled: "+mockLocationProvider);
}
+ } catch (SecurityException e){
+ Log.e("BT test", "Error while enabling Mock Mocations Provider", e);
+ } finally {
+ mockLocationProvider = null;
mockGpsEnabled = false;
- if (! LocationManager.GPS_PROVIDER.equals(mockLocationProvider)){
- lm.setTestProviderEnabled(mockLocationProvider, false);
- }
- prov = lm.getProvider(mockLocationProvider);
- if (prov != null){
- Log.e("BT test", "Mock provider: "+prov.getName()+" "+prov.getPowerRequirement()+" "+prov.getAccuracy()+" "+lm.isProviderEnabled(mockLocationProvider));
- }
- lm.clearTestProviderEnabled(mockLocationProvider);
- prov = lm.getProvider(mockLocationProvider);
- if (prov != null){
- Log.e("BT test", "Mock provider: "+prov.getName()+" "+prov.getPowerRequirement()+" "+prov.getAccuracy()+" "+lm.isProviderEnabled(mockLocationProvider));
- }
- lm.clearTestProviderStatus(mockLocationProvider);
- lm.removeTestProvider(mockLocationProvider);
- prov = lm.getProvider(mockLocationProvider);
- if (prov != null){
- Log.e("BT test", "Mock provider: "+prov.getName()+" "+prov.getPowerRequirement()+" "+prov.getAccuracy()+" "+lm.isProviderEnabled(mockLocationProvider));
- }
- Log.e("BT test", "removed mock GPS");
- } else {
- Log.e("BT test", "Mock provider already disabled: "+mockLocationProvider);
+ mockGpsAutoEnabled = false;
+ mockStatus = LocationProvider.OUT_OF_SERVICE;
}
- mockLocationProvider = null;
- mockGpsEnabled = false;
- mockStatus = LocationProvider.OUT_OF_SERVICE;
}
/**
@@ -141,7 +158,7 @@ public class NmeaParser {
return mockLocationProvider;
}
- private void notifyFix(Location fix){
+ private void notifyFix(Location fix) throws SecurityException {
fixTime = null;
hasGGA = false;
hasRMC=false;
@@ -173,7 +190,7 @@ public class NmeaParser {
}
// parse NMEA Sentence
- public String parseNmeaSentence(String gpsSentence){
+ public String parseNmeaSentence(String gpsSentence) throws SecurityException {
String nmeaSentence = null;
Log.e("BT test", "data: "+System.currentTimeMillis()+" "+gpsSentence);
Pattern xx = Pattern.compile("\\$([^*$]*)\\*([0-9A-F][0-9A-F])?\r\n");