aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHerbert von Broeuschmeul <Herbert.Broeuschmeul@gmail.com>2010-09-29 01:55:15 +0200
committerHerbert von Broeuschmeul <Herbert.Broeuschmeul@gmail.com>2010-09-29 01:55:15 +0200
commit605e2c6f59105c9bd9f7b4d23230e837b3041a89 (patch)
tree925bb884132b20f5141207e71961b79bb1a97334
parenta0059260f73288f663e7f4d4ce9e04b006605806 (diff)
downloadBlueGPS-605e2c6f59105c9bd9f7b4d23230e837b3041a89.zip
BlueGPS-605e2c6f59105c9bd9f7b4d23230e837b3041a89.tar.gz
BlueGPS-605e2c6f59105c9bd9f7b4d23230e837b3041a89.tar.bz2
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.
-rw-r--r--res/values/strings.xml10
-rw-r--r--src/org/broeuschmeul/android/gps/bluetooth/provider/BlueetoothGpsManager.java63
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 @@
<string name="foreground_service_started_notification_title">BlueGps</string>
<string name="foreground_gps_provider_started_notification">BlueGps service started</string>
<string name="foreground_nmea_recording_started_notification">BlueGps NMEA recording service started</string>
+ <string name="connection_problem_notification_title">BlueGps connection problem</string>
+ <plurals name="connection_problem_notification">
+ <item quantity="one">will retry one more time</item>
+ <item quantity="other">will retry %d more times</item>
+ </plurals>
+ <!--
+ <string name="connection_problem_notification">Have tried %d times - will retry %d more times</string>
+ -->
+ <string name="service_closed_because_connection_problem_notification_title">BlueGps stopped</string>
+ <string name="service_closed_because_connection_problem_notification">Reason: too many connection problems</string>
</resources>
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<NmeaListener> nmeaListeners = Collections.synchronizedList(new LinkedList<NmeaListener>());
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();
}
}