From 605e2c6f59105c9bd9f7b4d23230e837b3041a89 Mon Sep 17 00:00:00 2001 From: Herbert von Broeuschmeul Date: Wed, 29 Sep 2010 01:55:15 +0200 Subject: Correction of a bug and notification improvement Bug correction: I had forgotten to shutdown one ScheduledExecutorService, so BlueGPS was always trying to reconnect to the GPS, even after the application and service had been stopped. Improvement: First, when the connection with the GPS is lost, a notification is displayed. And then, if the GPS connection service is closed because of two many connection retry, an second notification is displayed. The fist notification allows the user to close BlueGPS service and the second one allows the user to re-enable it. --- res/values/strings.xml | 10 ++++ .../bluetooth/provider/BlueetoothGpsManager.java | 63 ++++++++++++++++++---- 2 files changed, 64 insertions(+), 9 deletions(-) diff --git a/res/values/strings.xml b/res/values/strings.xml index 2b5eb41..9673747 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -94,4 +94,14 @@ BlueGps BlueGps service started BlueGps NMEA recording service started + BlueGps connection problem + + will retry one more time + will retry %d more times + + + BlueGps stopped + Reason: too many connection problems diff --git a/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java b/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java index a1d3480..08c4c1d 100644 --- a/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java +++ b/src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java @@ -35,11 +35,15 @@ import java.util.concurrent.TimeUnit; import org.broeuschmeul.android.gps.nmea.util.NmeaParser; +import android.app.Notification; +import android.app.NotificationManager; +import android.app.PendingIntent; import android.app.Service; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothSocket; import android.content.Context; +import android.content.Intent; import android.location.LocationManager; import android.location.GpsStatus.NmeaListener; import android.os.SystemClock; @@ -98,22 +102,44 @@ public class BlueetoothGpsManager { private List nmeaListeners = Collections.synchronizedList(new LinkedList()); private LocationManager locationManager; private ConnectedGps connectedGps; - - /** - * @return true if the bluetooth GPS is enabled - */ - public synchronized boolean isEnabled() { - return enabled; - } + private Notification connectionProblemNotification; + private Notification serviceStoppedNotification; + private Context appContext; + private NotificationManager notificationManager; public BlueetoothGpsManager(Service callingService, String deviceAddress) { this.gpsDeviceAddress = deviceAddress; this.callingService = callingService; + this.appContext = callingService.getApplicationContext(); locationManager = (LocationManager)callingService.getSystemService(Context.LOCATION_SERVICE); + notificationManager = (NotificationManager)callingService.getSystemService(Context.NOTIFICATION_SERVICE); parser.setLocationManager(locationManager); + + connectionProblemNotification = new Notification(); + connectionProblemNotification.icon = R.drawable.icon; + Intent stopIntent = new Intent(BluetoothGpsProviderService.ACTION_STOP_GPS_PROVIDER); + // PendingIntent stopPendingIntent = PendingIntent.getService(appContext, 0, stopIntent, PendingIntent.FLAG_CANCEL_CURRENT); + PendingIntent stopPendingIntent = PendingIntent.getService(appContext, 0, stopIntent, 0); + connectionProblemNotification.contentIntent = stopPendingIntent; + + serviceStoppedNotification = new Notification(); + serviceStoppedNotification.icon=R.drawable.icon; + Intent restartIntent = new Intent(BluetoothGpsProviderService.ACTION_START_GPS_PROVIDER); + PendingIntent restartPendingIntent = PendingIntent.getService(appContext, 0, restartIntent, PendingIntent.FLAG_CANCEL_CURRENT); + serviceStoppedNotification.setLatestEventInfo(appContext, appContext.getString(R.string.service_closed_because_connection_problem_notification_title), appContext.getString(R.string.service_closed_because_connection_problem_notification_title), restartPendingIntent); + + + } + + /** + * @return true if the bluetooth GPS is enabled + */ + public synchronized boolean isEnabled() { + return enabled; } public synchronized boolean enable() { + notificationManager.cancel(R.string.service_closed_because_connection_problem_notification_title); if (! enabled){ final BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if (bluetoothAdapter == null) { @@ -168,16 +194,33 @@ public class BlueetoothGpsManager { gpsSocket.connect(); // connection obtained so reset the number of connection try connectionTry=0; + notificationManager.cancel(R.string.connection_problem_notification_title); connectedGps = new ConnectedGps(gpsSocket); connectionAndReadingPool.execute(connectedGps); } } catch (IOException connectException) { - // Unable to connect; close everything and get out + // Unable to connect Log.e("BT test", "error while connecting to socket", connectException); } finally { + if (connectionTry > 0) + { + // Unable to connect + Log.e("BT test", "Unable to establish connection"); + connectionProblemNotification.when = System.currentTimeMillis(); + String pbMessage = appContext.getResources().getQuantityString(R.plurals.connection_problem_notification, 5-connectionTry, 5-connectionTry); + connectionProblemNotification.setLatestEventInfo(appContext, + appContext.getString(R.string.connection_problem_notification_title), + pbMessage, + connectionProblemNotification.contentIntent); + connectionProblemNotification.number = connectionTry; + notificationManager.notify(R.string.connection_problem_notification_title, connectionProblemNotification); + } // if bluetooth has bean disabled or // if two much tries consider that we are enable to connect. So close everything and get out - if ((!bluetoothAdapter.isEnabled()) || (connectionTry > 5 )){ + if ((!bluetoothAdapter.isEnabled()) || (connectionTry >= 5 )){ + 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(); } } @@ -207,6 +250,8 @@ public class BlueetoothGpsManager { nmeaListeners.clear(); disableMockLocationProvider(); notificationPool.shutdown(); + connectionAndReadingPool.shutdown(); + notificationManager.cancel(R.string.connection_problem_notification_title); callingService.stopSelf(); } } -- cgit v1.1