From eddc206850ad7b7b624c3a71cae70ae37789602c Mon Sep 17 00:00:00 2001 From: Herbert von Broeuschmeul Date: Mon, 18 Oct 2010 21:48:53 +0200 Subject: autodisable when needed When Bluetooth, Mock Location or GPS are not enabled, the service cannot be started correctly and so it will auto-disable itself. --- .../gps/bluetooth/provider/BlueetoothGpsManager.java | 4 ++-- .../bluetooth/provider/BluetoothGpsProviderService.java | 16 ++++++++++------ 2 files changed, 12 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java b/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java index e114b45..2ccc28d 100644 --- a/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java +++ b/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java @@ -147,13 +147,12 @@ public class BlueetoothGpsManager { /** * @return true if the bluetooth GPS is enabled */ - public boolean isEnabled() { + public synchronized boolean isEnabled() { return enabled; } public synchronized void enable() { if (! enabled){ - this.enabled = true; final BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if (bluetoothAdapter == null) { // Device does not support Bluetooth @@ -228,6 +227,7 @@ public class BlueetoothGpsManager { } } }; + this.enabled = true; notificationPool = Executors.newSingleThreadExecutor(); notificationPool.execute(connectThread); // enableMockLocationProvider(LocationManager.GPS_PROVIDER); diff --git a/src/org/broeuschmeul/android/gps/bluetooth/provider/BluetoothGpsProviderService.java b/src/org/broeuschmeul/android/gps/bluetooth/provider/BluetoothGpsProviderService.java index 0ab972b..2a30ea8 100644 --- a/src/org/broeuschmeul/android/gps/bluetooth/provider/BluetoothGpsProviderService.java +++ b/src/org/broeuschmeul/android/gps/bluetooth/provider/BluetoothGpsProviderService.java @@ -105,14 +105,18 @@ public class BluetoothGpsProviderService extends Service implements NmeaListener startForeground(R.string.foreground_gps_provider_started_notification, notification); if (BluetoothAdapter.checkBluetoothAddress(deviceAddress)){ gpsManager = new BlueetoothGpsManager(this, deviceAddress); - gpsManager.enableMockLocationProvider(LocationManager.GPS_PROVIDER); gpsManager.enable(); - if (! sharedPreferences.getBoolean(PREF_START_GPS_PROVIDER, false)){ - edit.putBoolean(PREF_START_GPS_PROVIDER,true); - edit.commit(); + 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(); + } + toast.setText(this.getString(R.string.msg_gps_provider_started)); + toast.show(); + } else { + stopSelf(); } - toast.setText(this.getString(R.string.msg_gps_provider_started)); - toast.show(); } else { // if (! sharedPreferences.getBoolean(PREF_START_GPS_PROVIDER, true)){ // edit.putBoolean(PREF_START_GPS_PROVIDER,false); -- cgit v1.1 From 142734b2aceda5e4374991dc18816ccfc5a28b91 Mon Sep 17 00:00:00 2001 From: Herbert von Broeuschmeul Date: Mon, 18 Oct 2010 22:43:56 +0200 Subject: Code cleanup --- .../bluetooth/provider/BlueetoothGpsManager.java | 76 +--------------------- .../bluetooth/provider/BluetoothGpsActivity.java | 3 - .../provider/BluetoothGpsProviderService.java | 22 ------- 3 files changed, 1 insertion(+), 100 deletions(-) (limited to 'src') diff --git a/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java b/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java index 2ccc28d..a86b701 100644 --- a/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java +++ b/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java @@ -29,7 +29,6 @@ import java.io.PrintStream; import java.util.Collections; import java.util.LinkedList; import java.util.List; -import java.util.Locale; import java.util.UUID; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -42,14 +41,11 @@ import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothSocket; import android.content.Context; import android.content.SharedPreferences; -import android.location.Criteria; import android.location.LocationManager; -import android.location.LocationProvider; import android.location.GpsStatus.NmeaListener; import android.preference.PreferenceManager; import android.provider.Settings; import android.util.Log; -import android.util.PrintStreamPrinter; public class BlueetoothGpsManager { @@ -83,10 +79,6 @@ public class BlueetoothGpsManager { while((enabled && (s = reader.readLine()) != null)){ Log.e("BT test", "data: "+System.currentTimeMillis()+" "+s + "xxx"); notifyNmeaSentence(s+"\r\n"); -// parser.parseNmeaSentence(s); -// // writer.println(s); -// addNMEAString(s); -// nmeaSentenceHandler.ob } } catch (IOException e) { Log.e("BT test", "error while getting data", e); @@ -94,30 +86,6 @@ public class BlueetoothGpsManager { disable(); } } - - /** - * Write to the connected OutStream. - * @param buffer The bytes to write - */ - public void write(byte[] buffer) { - try { - out.write(buffer); - } catch (IOException e) { -// Log.e(TAG, "Exception during write", e); - } - } - /** - * Write to the connected OutStream. - * @param buffer The data to write - */ - public void write(String buffer) { - try { - out2.print(buffer); - out.flush(); - } catch (IOException e) { -// Log.e(TAG, "Exception during write", e); - } - } } private Service callingService; @@ -130,12 +98,8 @@ public class BlueetoothGpsManager { private List nmeaListeners = Collections.synchronizedList(new LinkedList()); private LocationManager locationManager; private SharedPreferences sharedPreferences; -// private boolean mockGpsEnabled = true; -// private String mockLocationProvider = LocationManager.GPS_PROVIDER; private ConnectedThread connectedThread; -// private Handler nmeaSentenceHandler = new Handler(); - public BlueetoothGpsManager(Service callingService, String deviceAddress) { this.gpsDeviceAddress = deviceAddress; this.callingService = callingService; @@ -181,14 +145,6 @@ public class BlueetoothGpsManager { if (gpsSocket == null){ Log.e("BT test", "Error while establishing connection: no socket"); } else { -//// mockGpsEnabled = locationManager.isProviderEnabled(mockLocationProvider); -// LocationProvider prov = locationManager.getProvider(mockLocationProvider); -// Log.e("BT test", "Mock power: "+prov.getPowerRequirement()+" "+prov.getAccuracy()+" "+locationManager.isProviderEnabled(mockLocationProvider)); -// locationManager.addTestProvider(mockLocationProvider, false, true,false, false, -// true, true, true, Criteria.POWER_HIGH, Criteria.ACCURACY_FINE); -//// locationManager.setTestProviderEnabled(mockLocationProvider, true); -// prov = locationManager.getProvider(mockLocationProvider); -// Log.e("BT test", "Mock power: "+prov.getPowerRequirement()+" "+prov.getAccuracy()+" "+locationManager.isProviderEnabled(mockLocationProvider)); Runnable connectThread = new Runnable() { @Override @@ -201,26 +157,8 @@ public class BlueetoothGpsManager { gpsSocket.connect(); connectedThread = new ConnectedThread(gpsSocket); connectedThread.start(); -// String command = callingService.getString(R.string.sirf_gll_on); -// String sentence = String.format((Locale)null,"$%s*%X\r\n", command, parser.computeChecksum(command)); -// String command1 = callingService.getString(R.string.sirf_gll_off); -// String sentence1 = String.format((Locale)null,"$%s*%X\r\n", command1, parser.computeChecksum(command1)); -// String command2 = callingService.getString(R.string.sirf_vtg_off); -// String sentence2 = String.format((Locale)null,"$%s*%X\r\n", command2, parser.computeChecksum(command2)); -// try { -// Thread.sleep(5000); -// } catch (InterruptedException e) { -// // TODO Auto-generated catch block -// e.printStackTrace(); -// } -// Log.e("BT test", "sending NMEA sentence: "+"$PSRF105,1*3E\r\n"); -// connectedThread.write("$PSRF105,1*3E\r\n"); -// Log.e("BT test", "sending NMEA sentence: "+sentence1); -// connectedThread.write(sentence1); -// Log.e("BT test", "sending NMEA sentence: "+sentence2); -// connectedThread.write(sentence2); } catch (IOException connectException) { - // Unable to connect; close everything and get out + // Unable to connect; So close everything and get out Log.e("BT test", "error while connecting to socket", connectException); disable(); // callingService.stopSelf(); @@ -250,18 +188,6 @@ public class BlueetoothGpsManager { nmeaListeners.clear(); disableMockLocationProvider(); notificationPool.shutdown(); -//// locationManager.setTestProviderEnabled(mockLocationProvider, mockGpsEnabled); -// LocationProvider prov = locationManager.getProvider(mockLocationProvider); -// Log.e("BT test", "Mock power: "+prov.getPowerRequirement()+" "+prov.getAccuracy()+" "+locationManager.isProviderEnabled(mockLocationProvider)); -// locationManager.clearTestProviderEnabled(mockLocationProvider); -// prov = locationManager.getProvider(mockLocationProvider); -// Log.e("BT test", "Mock power: "+prov.getPowerRequirement()+" "+prov.getAccuracy()+" "+locationManager.isProviderEnabled(mockLocationProvider)); -// locationManager.clearTestProviderStatus(mockLocationProvider); -// locationManager.removeTestProvider(mockLocationProvider); -// prov = locationManager.getProvider(mockLocationProvider); -// Log.e("BT test", "Mock power: "+prov.getPowerRequirement()+" "+prov.getAccuracy()+" "+locationManager.isProviderEnabled(mockLocationProvider)); -// Log.e("BT test", "removed mock GPS"); - callingService.stopSelf(); } } diff --git a/src/org/broeuschmeul/android/gps/bluetooth/provider/BluetoothGpsActivity.java b/src/org/broeuschmeul/android/gps/bluetooth/provider/BluetoothGpsActivity.java index 8ce8e62..22993bb 100644 --- a/src/org/broeuschmeul/android/gps/bluetooth/provider/BluetoothGpsActivity.java +++ b/src/org/broeuschmeul/android/gps/bluetooth/provider/BluetoothGpsActivity.java @@ -21,8 +21,6 @@ package org.broeuschmeul.android.gps.bluetooth.provider; import java.util.Set; -import java.util.prefs.Preferences; -import java.util.zip.CheckedInputStream; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; @@ -35,7 +33,6 @@ import android.preference.ListPreference; import android.preference.Preference; import android.preference.PreferenceActivity; import android.preference.PreferenceManager; -import android.preference.PreferenceScreen; import android.preference.Preference.OnPreferenceChangeListener; import android.util.Log; diff --git a/src/org/broeuschmeul/android/gps/bluetooth/provider/BluetoothGpsProviderService.java b/src/org/broeuschmeul/android/gps/bluetooth/provider/BluetoothGpsProviderService.java index 2a30ea8..6ae24d6 100644 --- a/src/org/broeuschmeul/android/gps/bluetooth/provider/BluetoothGpsProviderService.java +++ b/src/org/broeuschmeul/android/gps/bluetooth/provider/BluetoothGpsProviderService.java @@ -88,12 +88,8 @@ public class BluetoothGpsProviderService extends Service implements NmeaListener // super.onStart(intent, startId); SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); SharedPreferences.Editor edit = sharedPreferences.edit(); -// long minTime = Long.parseLong(sharedPreferences.getString(PREF_TRACK_MIN_TIME, this.getString(R.string.defaultTrackMinTime))); -// float minDistance = Float.parseFloat(sharedPreferences.getString(PREF_TRACK_MIN_DISTANCE, this.getString(R.string.defaultTrackMinDistance))); String deviceAddress = sharedPreferences.getString(PREF_BLUETOOTH_DEVICE, null); if (Config.LOGD){ -// Log.d(BluetoothGpsProviderService.class.getName(), "prefs minTime: "+minTime); -// Log.d(BluetoothGpsProviderService.class.getName(), "prefs minDistance: "+minDistance); Log.d(BluetoothGpsProviderService.class.getName(), "prefs device addr: "+deviceAddress); } if (ACTION_START_GPS_PROVIDER.equals(intent.getAction())){ @@ -287,22 +283,4 @@ public class BluetoothGpsProviderService extends Service implements NmeaListener public void onNmeaReceived(long timestamp, String data) { addNMEAString(data); } - -// private void startBluetoothGps(String deviceAddress){ -// Notification notification = new Notification(R.drawable.icon, this.getString(R.string.foreground_service_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_service_started_notification), myPendingIntent); -// startForeground(R.string.foreground_service_started_notification, notification); -// if (BluetoothAdapter.checkBluetoothAddress(deviceAddress)){ -// gpsManager = new BlueetoothGpsManager(this, deviceAddress); -// gpsManager.enable(); -// gpsManager.enableMockLocationProvider(LocationManager.GPS_PROVIDER); -// toast.setText(this.getString(R.string.msg_started)); -// toast.show(); -//// } else { -//// toast.setText(this.getString(R.string.msg_invalid_device)); -//// toast.show(); -// } -// } } -- cgit v1.1 From 94c5532b243d746d7c53067fad600fb3f35246d7 Mon Sep 17 00:00:00 2001 From: Herbert von Broeuschmeul Date: Fri, 22 Oct 2010 22:31:59 +0200 Subject: add alert messages and code cleanup * add alert messages when bluetooth GPS connection cannot be enabled * code cleanup --- .../bluetooth/provider/BlueetoothGpsManager.java | 37 +++++++++++++++++----- .../provider/BluetoothGpsProviderService.java | 23 ++++++++------ 2 files changed, 42 insertions(+), 18 deletions(-) (limited to 'src') diff --git a/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java b/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java index a86b701..6ef4eff 100644 --- a/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java +++ b/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java @@ -51,8 +51,6 @@ public class BlueetoothGpsManager { private class ConnectedThread extends Thread { private final InputStream in; - private final OutputStream out; - private final PrintStream out2; public ConnectedThread(BluetoothSocket socket) { InputStream tmpIn = null; @@ -68,8 +66,6 @@ public class BlueetoothGpsManager { Log.e("BT test", "error while getting socket streams", e); } in = tmpIn; - out = tmpOut; - out2 = tmpOut2; } public void run() { @@ -99,6 +95,7 @@ public class BlueetoothGpsManager { private LocationManager locationManager; private SharedPreferences sharedPreferences; private ConnectedThread connectedThread; + private int disableReason = 0; public BlueetoothGpsManager(Service callingService, String deviceAddress) { this.gpsDeviceAddress = deviceAddress; @@ -108,6 +105,14 @@ public class BlueetoothGpsManager { parser.setLocationManager(locationManager); } + private void setDisableReason(int reasonId){ + disableReason = reasonId; + } + + public int getDisableReason(){ + return disableReason; + } + /** * @return true if the bluetooth GPS is enabled */ @@ -121,29 +126,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"); + 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 { 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() { @@ -160,8 +172,8 @@ public class BlueetoothGpsManager { } catch (IOException connectException) { // Unable to connect; So close everything and get out Log.e("BT test", "error while connecting to socket", connectException); - disable(); -// callingService.stopSelf(); + disable(R.string.msg_bluetooth_gps_unavaible); + // callingService.stopSelf(); } } }; @@ -175,6 +187,11 @@ public class BlueetoothGpsManager { } } + public synchronized void disable(int reasonId) { + setDisableReason(reasonId); + disable(); + } + public synchronized void disable() { if (enabled){ enabled = false; @@ -191,7 +208,6 @@ public class BlueetoothGpsManager { callingService.stopSelf(); } } - public void enableMockLocationProvider(String gpsName){ if (parser != null){ @@ -240,7 +256,12 @@ public class BlueetoothGpsManager { private void notifyNmeaSentence(final String nmeaSentence){ if (enabled){ - parser.parseNmeaSentence(nmeaSentence); + try { + parser.parseNmeaSentence(nmeaSentence); + } catch (SecurityException e){ + // a priori Mock Location is disabled + disable(R.string.msg_mock_location_disabled); + } final long timestamp = System.currentTimeMillis(); synchronized(nmeaListeners) { for(final NmeaListener listener : nmeaListeners){ diff --git a/src/org/broeuschmeul/android/gps/bluetooth/provider/BluetoothGpsProviderService.java b/src/org/broeuschmeul/android/gps/bluetooth/provider/BluetoothGpsProviderService.java index 6ae24d6..5df1104 100644 --- a/src/org/broeuschmeul/android/gps/bluetooth/provider/BluetoothGpsProviderService.java +++ b/src/org/broeuschmeul/android/gps/bluetooth/provider/BluetoothGpsProviderService.java @@ -94,11 +94,6 @@ 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); - startForeground(R.string.foreground_gps_provider_started_notification, notification); if (BluetoothAdapter.checkBluetoothAddress(deviceAddress)){ gpsManager = new BlueetoothGpsManager(this, deviceAddress); gpsManager.enable(); @@ -108,6 +103,11 @@ public class BluetoothGpsProviderService extends Service implements NmeaListener 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 { @@ -184,6 +184,13 @@ public class BluetoothGpsProviderService extends Service implements NmeaListener 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(); @@ -199,13 +206,9 @@ 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(); } - + private void beginTrack(){ SimpleDateFormat fmt = new SimpleDateFormat("_yyyy-MM-dd_HH-mm-ss'.nmea'"); SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); -- cgit v1.1 From 32d6cc92009c2212fcacd03529b99ffc87ea1a4d Mon Sep 17 00:00:00 2001 From: Herbert von Broeuschmeul Date: Sat, 23 Oct 2010 00:40:57 +0200 Subject: auto-enable gps provider if it doesn't exist and prevent forceclose if Mock Locations is disabled --- .../android/gps/nmea/util/NmeaParser.java | 120 ++++++++++++--------- 1 file changed, 68 insertions(+), 52 deletions(-) (limited to 'src') diff --git a/src/org/broeuschmeul/android/gps/nmea/util/NmeaParser.java b/src/org/broeuschmeul/android/gps/nmea/util/NmeaParser.java index be4e973..a86f17a 100644 --- a/src/org/broeuschmeul/android/gps/nmea/util/NmeaParser.java +++ b/src/org/broeuschmeul/android/gps/nmea/util/NmeaParser.java @@ -43,6 +43,7 @@ public class NmeaParser { private boolean hasRMC = false; private LocationManager lm; private float precision = 10f; + private boolean mockGpsAutoEnabled = false; private boolean mockGpsEnabled = false; private String mockLocationProvider = null; @@ -60,67 +61,80 @@ 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)){ - // mockGpsEnabled = locationManager.isProviderEnabled(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)){ - // mockGpsEnabled = locationManager.isProviderEnabled(mockLocationProvider); - lm.setTestProviderEnabled(mockLocationProvider, false); - } - // locationManager.setTestProviderEnabled(mockLocationProvider, mockGpsEnabled); - 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; } - mockLocationProvider = null; - mockGpsEnabled = false; } /** @@ -137,8 +151,7 @@ public class NmeaParser { return mockLocationProvider; } - private void notifyFix(Location fix){ - //R.drawable.stat + private void notifyFix(Location fix) throws SecurityException { fixTime = null; hasGGA = false; hasRMC=false; @@ -153,11 +166,13 @@ public class NmeaParser { } // parse NMEA Sentence - public void 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"); Matcher m = xx.matcher(gpsSentence); if (m.matches()){ + nmeaSentence = m.group(0); String sentence = m.group(1); String checkSum = m.group(2); Log.e("BT test", "data: "+System.currentTimeMillis()+" "+sentence+" cheksum; "+checkSum +" control: "+String.format("%X",computeChecksum(sentence))); @@ -405,6 +420,7 @@ public class NmeaParser { // Mode indicator, (A=autonomous, D=differential, E=Estimated, N=not valid, S=Simulator ) } } + return nmeaSentence; } public double parseNmeaLatitude(String lat,String orientation){ -- cgit v1.1 From cddf1671d1c29f05219c33c96d1133b17941c0c6 Mon Sep 17 00:00:00 2001 From: Herbert von Broeuschmeul Date: Sat, 23 Oct 2010 23:50:41 +0200 Subject: Remove test on gps provider Remove test for verifying if internal GPS is not enabled. So not enabling internal GPS doesn't prevent starting service. --- .../gps/bluetooth/provider/BlueetoothGpsManager.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java b/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java index 6ef4eff..bedaf36 100644 --- a/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java +++ b/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java @@ -128,18 +128,18 @@ public class BlueetoothGpsManager { 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); + // 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"); 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 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 { BluetoothDevice gpsDevice = bluetoothAdapter.getRemoteDevice(gpsDeviceAddress); if (gpsDevice == null){ -- cgit v1.1 From c36454f4313b1514603ae91f284542885ebe38f1 Mon Sep 17 00:00:00 2001 From: Herbert von Broeuschmeul Date: Sun, 24 Oct 2010 21:47:42 +0200 Subject: autodisable when needed When Bluetooth, Mock Location or GPS are not enabled, the service cannot be started correctly and so it will auto-disable itself. --- .../android/gps/bluetooth/provider/BlueetoothGpsManager.java | 2 +- .../android/gps/bluetooth/provider/BluetoothGpsProviderService.java | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java b/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java index 92c5c3e..bca3ca5 100644 --- a/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java +++ b/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java @@ -162,7 +162,7 @@ public class BlueetoothGpsManager { } 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)) + && (sharedPreferences.getBoolean(BluetoothGpsProviderService.PREF_REPLACE_STD_GPS, true)) ) { Log.e("BT test", "GPS location provider OFF"); } else { diff --git a/src/org/broeuschmeul/android/gps/bluetooth/provider/BluetoothGpsProviderService.java b/src/org/broeuschmeul/android/gps/bluetooth/provider/BluetoothGpsProviderService.java index ef2410d..22b6a0c 100644 --- a/src/org/broeuschmeul/android/gps/bluetooth/provider/BluetoothGpsProviderService.java +++ b/src/org/broeuschmeul/android/gps/bluetooth/provider/BluetoothGpsProviderService.java @@ -104,16 +104,18 @@ public class BluetoothGpsProviderService extends Service implements NmeaListener mockProvider = sharedPreferences.getString(PREF_MOCK_GPS_NAME, getString(R.string.defaultMockGpsName)); } gpsManager = new BlueetoothGpsManager(this, deviceAddress, maxConRetries); - gpsManager.enableMockLocationProvider(mockProvider); boolean enabled = gpsManager.enable(); if (sharedPreferences.getBoolean(PREF_START_GPS_PROVIDER, false) != enabled){ edit.putBoolean(PREF_START_GPS_PROVIDER,enabled); edit.commit(); } if (enabled) { + gpsManager.enableMockLocationProvider(mockProvider); startForeground(R.string.foreground_gps_provider_started_notification, notification); toast.setText(this.getString(R.string.msg_gps_provider_started)); toast.show(); + } else { + stopSelf(); } } else { stopSelf(); -- cgit v1.1 From 59451244a4d1e9f71735a089889a6bbdaf3ca375 Mon Sep 17 00:00:00 2001 From: Herbert von Broeuschmeul Date: Sun, 24 Oct 2010 22:01:26 +0200 Subject: code cleanup --- .../android/gps/bluetooth/provider/BlueetoothGpsManager.java | 1 - 1 file changed, 1 deletion(-) (limited to 'src') diff --git a/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java b/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java index bca3ca5..91489a5 100644 --- a/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java +++ b/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java @@ -44,7 +44,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; -- cgit v1.1 From 5b11f327cf16eeecd279aaed8b384385c0f5fa10 Mon Sep 17 00:00:00 2001 From: Herbert von Broeuschmeul Date: Sun, 24 Oct 2010 23:41:55 +0200 Subject: add alert messages when bluetooth GPS connection cannot be enabled --- .../bluetooth/provider/BlueetoothGpsManager.java | 35 +++++++++++++++++++++- .../provider/BluetoothGpsProviderService.java | 19 +++++++----- 2 files changed, 45 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java b/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java index 91489a5..bb52922 100644 --- a/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java +++ b/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java @@ -105,6 +105,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; @@ -140,6 +141,15 @@ public class BlueetoothGpsManager { 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 */ @@ -154,29 +164,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"); + 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 @@ -197,9 +214,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(); @@ -218,6 +237,7 @@ public class BlueetoothGpsManager { } 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) { @@ -259,6 +279,11 @@ public class BlueetoothGpsManager { } } + public synchronized void disable(int reasonId) { + setDisableReason(reasonId); + disable(); + } + public synchronized void disable() { if (enabled){ enabled = false; @@ -330,7 +355,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 22b6a0c..1b5c238 100644 --- a/src/org/broeuschmeul/android/gps/bluetooth/provider/BluetoothGpsProviderService.java +++ b/src/org/broeuschmeul/android/gps/bluetooth/provider/BluetoothGpsProviderService.java @@ -94,10 +94,6 @@ 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)){ @@ -111,6 +107,10 @@ public class BluetoothGpsProviderService extends Service implements NmeaListener } 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); toast.setText(this.getString(R.string.msg_gps_provider_started)); toast.show(); @@ -180,6 +180,13 @@ public class BluetoothGpsProviderService extends Service implements NmeaListener 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(); @@ -195,10 +202,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(); } -- cgit v1.1 From 587d3dfa8b63dd6da2d13ecd81279703ce71cec1 Mon Sep 17 00:00:00 2001 From: Herbert von Broeuschmeul Date: Sat, 23 Oct 2010 00:40:57 +0200 Subject: auto-enable gps provider if it doesn't exist and prevent forceclose if Mock Locations is disabled --- .../android/gps/nmea/util/NmeaParser.java | 115 ++++++++++++--------- 1 file changed, 66 insertions(+), 49 deletions(-) (limited to 'src') diff --git a/src/org/broeuschmeul/android/gps/nmea/util/NmeaParser.java b/src/org/broeuschmeul/android/gps/nmea/util/NmeaParser.java index 2f67441..6a373c6 100644 --- a/src/org/broeuschmeul/android/gps/nmea/util/NmeaParser.java +++ b/src/org/broeuschmeul/android/gps/nmea/util/NmeaParser.java @@ -43,6 +43,7 @@ public class NmeaParser { private boolean hasRMC = false; private LocationManager lm; private float precision = 10f; + private boolean mockGpsAutoEnabled = false; private boolean mockGpsEnabled = false; private String mockLocationProvider = null; @@ -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"); -- cgit v1.1 From b28391e9e3d580bf7853d21f485d13e78eebca7f Mon Sep 17 00:00:00 2001 From: Herbert von Broeuschmeul Date: Sat, 23 Oct 2010 23:50:41 +0200 Subject: Remove test on gps provider Remove test for verifying if internal GPS is not enabled. So not enabling internal GPS doesn't prevent starting service. --- .../gps/bluetooth/provider/BlueetoothGpsManager.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java b/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java index bb52922..ff94ac6 100644 --- a/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java +++ b/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java @@ -166,18 +166,18 @@ public class BlueetoothGpsManager { 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); + // 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"); 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 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){ -- cgit v1.1 From 8a5334f4bd6fc34e9041b7ed6999315d36121511 Mon Sep 17 00:00:00 2001 From: Herbert von Broeuschmeul Date: Mon, 25 Oct 2010 01:43:35 +0200 Subject: code cleanup --- .../android/gps/bluetooth/provider/BlueetoothGpsManager.java | 9 --------- 1 file changed, 9 deletions(-) (limited to 'src') diff --git a/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java b/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java index bedaf36..78c4706 100644 --- a/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java +++ b/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java @@ -24,8 +24,6 @@ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; -import java.io.OutputStream; -import java.io.PrintStream; import java.util.Collections; import java.util.LinkedList; import java.util.List; @@ -54,14 +52,8 @@ public class BlueetoothGpsManager { public ConnectedThread(BluetoothSocket socket) { InputStream tmpIn = null; - OutputStream tmpOut = null; - PrintStream tmpOut2 = null; try { tmpIn = socket.getInputStream(); - tmpOut = socket.getOutputStream(); - if (tmpOut != null){ - tmpOut2 = new PrintStream(tmpOut, false, "US-ASCII"); - } } catch (IOException e) { Log.e("BT test", "error while getting socket streams", e); } @@ -85,7 +77,6 @@ public class BlueetoothGpsManager { } private Service callingService; - private BluetoothDevice gpsDevice; private BluetoothSocket gpsSocket; private String gpsDeviceAddress; private NmeaParser parser = new NmeaParser(10f); -- cgit v1.1 From a3b73fd4921983fda49f0efa485a894e1c87c5ba Mon Sep 17 00:00:00 2001 From: Herbert von Broeuschmeul Date: Tue, 26 Oct 2010 02:49:21 +0200 Subject: add notifications when service cannot be started * add notification when service cannot be started --- .../bluetooth/provider/BlueetoothGpsManager.java | 24 ++++++++++++++++------ .../provider/BluetoothGpsProviderService.java | 2 +- 2 files changed, 19 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java b/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java index ff94ac6..5f1860b 100644 --- a/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java +++ b/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java @@ -228,16 +228,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); + // disable(R.string.msg_bluetooth_gps_unavaible); } finally { nbRetriesRemaining--; if (! connected) { @@ -271,10 +275,10 @@ 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); } } } @@ -285,6 +289,15 @@ public class BlueetoothGpsManager { } 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){ @@ -298,7 +311,6 @@ public class BlueetoothGpsManager { disableMockLocationProvider(); notificationPool.shutdown(); connectionAndReadingPool.shutdown(); - notificationManager.cancel(R.string.connection_problem_notification_title); callingService.stopSelf(); } } diff --git a/src/org/broeuschmeul/android/gps/bluetooth/provider/BluetoothGpsProviderService.java b/src/org/broeuschmeul/android/gps/bluetooth/provider/BluetoothGpsProviderService.java index 1b5c238..5146d1d 100644 --- a/src/org/broeuschmeul/android/gps/bluetooth/provider/BluetoothGpsProviderService.java +++ b/src/org/broeuschmeul/android/gps/bluetooth/provider/BluetoothGpsProviderService.java @@ -181,7 +181,7 @@ public class BluetoothGpsProviderService extends Service implements NmeaListener gpsManager = null; if (manager != null){ if (manager.getDisableReason() != 0){ - toast.setText(manager.getDisableReason()); + 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); -- cgit v1.1 From aeab97d163eac31d78d6cc6bd98c20d5f0f80a03 Mon Sep 17 00:00:00 2001 From: Herbert von Broeuschmeul Date: Tue, 26 Oct 2010 19:38:32 +0200 Subject: Correct first fix Actually there is always a first fix with lat=0 and lon=0. This modification should remove it. --- src/org/broeuschmeul/android/gps/nmea/util/NmeaParser.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/org/broeuschmeul/android/gps/nmea/util/NmeaParser.java b/src/org/broeuschmeul/android/gps/nmea/util/NmeaParser.java index a86f17a..a381ac4 100644 --- a/src/org/broeuschmeul/android/gps/nmea/util/NmeaParser.java +++ b/src/org/broeuschmeul/android/gps/nmea/util/NmeaParser.java @@ -36,7 +36,7 @@ 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; @@ -47,7 +47,7 @@ public class NmeaParser { private boolean mockGpsEnabled = false; private String mockLocationProvider = null; - private Location fix = new Location(mockLocationProvider); + private Location fix = null; public NmeaParser(){ this(5f); -- cgit v1.1