From 3852e55aa57b4826ac8dabc3c50f32e60e383aa8 Mon Sep 17 00:00:00 2001 From: Pavol Zibrita Date: Sat, 10 Sep 2011 08:06:34 +0200 Subject: [PATCH] Fixed some exceptions. Don't release app if backround service runs. --- .../net/osmand/plus/NavigationService.java | 15 +++++++++--- .../plus/activities/MainMenuActivity.java | 19 ++++++++++----- .../plus/activities/SavingTrackHelper.java | 23 +++++++++++-------- 3 files changed, 39 insertions(+), 18 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/NavigationService.java b/OsmAnd/src/net/osmand/plus/NavigationService.java index 51a0a734f7..9eb109ed27 100644 --- a/OsmAnd/src/net/osmand/plus/NavigationService.java +++ b/OsmAnd/src/net/osmand/plus/NavigationService.java @@ -49,6 +49,7 @@ public class NavigationService extends Service implements LocationListener { private static WakeLock lockStatic; private PendingIntent pendingIntent; + private BroadcastReceiver broadcastReceiver; @Override public IBinder onBind(Intent intent) { @@ -106,13 +107,14 @@ public class NavigationService extends Service implements LocationListener { } // registering icon at top level - registerReceiver(new BroadcastReceiver() { + broadcastReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { NavigationService.this.stopSelf(); } - }, new IntentFilter(OSMAND_STOP_SERVICE_ACTION)); + }; + registerReceiver(broadcastReceiver, new IntentFilter(OSMAND_STOP_SERVICE_ACTION)); Intent notificationIntent = new Intent(OSMAND_STOP_SERVICE_ACTION); Notification notification = new Notification(R.drawable.icon, "", //$NON-NLS-1$ System.currentTimeMillis()); @@ -150,6 +152,10 @@ public class NavigationService extends Service implements LocationListener { // remove notification NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); mNotificationManager.cancel(NOTIFICATION_SERVICE_ID); + if (broadcastReceiver != null) { + unregisterReceiver(broadcastReceiver); + broadcastReceiver = null; + } } @@ -161,7 +167,10 @@ public class NavigationService extends Service implements LocationListener { // unregister listener and wait next time LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); locationManager.removeUpdates(this); - getLock(this).release(); + WakeLock lock = getLock(this); + if (lock.isHeld()) { + lock.release(); + } } savingTrackHelper.insertData(location.getLatitude(), location.getLongitude(), location.getAltitude(), location.getSpeed(), location.getAccuracy(), location.getTime(), settings); diff --git a/OsmAnd/src/net/osmand/plus/activities/MainMenuActivity.java b/OsmAnd/src/net/osmand/plus/activities/MainMenuActivity.java index d95d4b8a80..54d6f4c4c3 100644 --- a/OsmAnd/src/net/osmand/plus/activities/MainMenuActivity.java +++ b/OsmAnd/src/net/osmand/plus/activities/MainMenuActivity.java @@ -212,12 +212,15 @@ public class MainMenuActivity extends Activity { closeButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { - ((OsmandApplication) activity.getApplication()).closeApplication(); + getMyApplication().closeApplication(); //moveTaskToBack(true); activity.finish(); - //http://stackoverflow.com/questions/2092951/how-to-close-android-application - System.runFinalizersOnExit(true); //if any threads are running, they will prevent app from exit - System.exit(0); //exit + //TODO this is different from MapActivity close... + if (getMyApplication().getNavigationService() == null) { + //http://stackoverflow.com/questions/2092951/how-to-close-android-application + System.runFinalizersOnExit(true); + System.exit(0); + } } }); @@ -235,7 +238,7 @@ public class MainMenuActivity extends Activity { } startProgressDialog = new ProgressDialog(this); - ((OsmandApplication)getApplication()).checkApplicationIsBeingInitialized(this, startProgressDialog); + getMyApplication().checkApplicationIsBeingInitialized(this, startProgressDialog); SharedPreferences pref = getPreferences(MODE_WORLD_WRITEABLE); boolean firstTime = false; if(!pref.contains(FIRST_TIME_APP_RUN)){ @@ -306,7 +309,7 @@ public class MainMenuActivity extends Activity { } protected void checkVectorIndexesDownloaded() { - MapRenderRepositories maps = ((OsmandApplication) getApplication()).getResourceManager().getRenderer(); + MapRenderRepositories maps = getMyApplication().getResourceManager().getRenderer(); SharedPreferences pref = getPreferences(MODE_WORLD_WRITEABLE); boolean check = pref.getBoolean(VECTOR_INDEXES_CHECK, true); // do not show each time @@ -338,6 +341,10 @@ public class MainMenuActivity extends Activity { } } + + private OsmandApplication getMyApplication() { + return (OsmandApplication) getApplication(); + } @Override protected Dialog onCreateDialog(int id) { diff --git a/OsmAnd/src/net/osmand/plus/activities/SavingTrackHelper.java b/OsmAnd/src/net/osmand/plus/activities/SavingTrackHelper.java index d274be4b5e..b9cb7783dc 100644 --- a/OsmAnd/src/net/osmand/plus/activities/SavingTrackHelper.java +++ b/OsmAnd/src/net/osmand/plus/activities/SavingTrackHelper.java @@ -239,20 +239,25 @@ public class SavingTrackHelper extends SQLiteOpenHelper { public void insertData(double lat, double lon, double alt, double speed, double hdop, long time, OsmandSettings settings){ if (time - lastTimeUpdated > settings.SAVE_TRACK_INTERVAL.get()*1000) { - SQLiteDatabase db = getWritableDatabase(); - if (db != null) { - db.execSQL(updateScript, new Object[] { lat, lon, alt, speed, hdop, time }); - } + execWithClose(updateScript, new Object[] { lat, lon, alt, speed, hdop, time }); lastTimeUpdated = time; } } public void insertPointData(double lat, double lon, long time, String description) { - SQLiteDatabase db = getWritableDatabase(); - if (db != null) { - db.execSQL(updatePointsScript, new Object[] { lat, lon, time, description }); - } + execWithClose(updatePointsScript, new Object[] { lat, lon, time, description }); } - + private void execWithClose(String script, Object[] objects) { + SQLiteDatabase db = getWritableDatabase(); + try { + if (db != null) { + db.execSQL(script, objects); + } + } finally { + if (db != null) { + db.close(); + } + } + } }