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/org/broeuschmeul/android/gps/bluetooth') 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