Skip to content

Commit

Permalink
Version 3.3.2:
Browse files Browse the repository at this point in the history
- fixed OCM background task execution on Android >= 3.0
- running OCM background task now cancelled on cache invalidation
  • Loading branch information
dexterbg committed Feb 1, 2015
1 parent b2d75db commit 0a3741c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
4 changes: 2 additions & 2 deletions OpenVehicleApp/AndroidManifest.xml
Expand Up @@ -2,8 +2,8 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.openvehicles.OVMS"
android:versionCode="20150131"
android:versionName="3.3.1">
android:versionCode="20150201"
android:versionName="3.3.2">

<uses-sdk
android:minSdkVersion="9"
Expand Down
16 changes: 12 additions & 4 deletions OpenVehicleApp/src/com/openvehicles/OVMS/ui/GetMapDetails.java
Expand Up @@ -49,9 +49,17 @@ protected void onPreExecute() {
// main.Diashow();
}

@Override
protected void onCancelled(Void result) {
Log.d("OCM", "GetMapDetails cancelled");
}

@Override
protected Void doInBackground(Void... params) {

if (isCancelled())
return null;

// read from OCM:

Log.d("OCM", "GetMapDetails from url=" + url);
Expand All @@ -65,12 +73,12 @@ protected Void doInBackground(Void... params) {
Log.d("OCM", "GetMapDetails read " + chargePoints.size() + " chargepoints");

// update database:

for (int i = 0; i < chargePoints.size(); i++) {
int i;
for (i = 0; !isCancelled() && (i < chargePoints.size()); i++) {
database.insert_mapdetails(chargePoints.get(i));
}

Log.d("OCM", "GetMapDetails saved " + chargePoints.size() + " chargepoints to database");
Log.d("OCM", "GetMapDetails saved " + i + " chargepoints to database");

return null;
}
Expand All @@ -96,7 +104,7 @@ private void getdata() throws IOException {
// read charge points array:

reader.beginArray();
while (reader.hasNext()) {
while (!isCancelled() && reader.hasNext()) {
ChargePoint chargePoint = gson.fromJson(reader, ChargePoint.class);
chargePoints.add(chargePoint);
}
Expand Down
23 changes: 21 additions & 2 deletions OpenVehicleApp/src/com/openvehicles/OVMS/ui/MainActivity.java
Expand Up @@ -10,6 +10,8 @@
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
Expand Down Expand Up @@ -45,6 +47,7 @@ public class MainActivity extends ApiActivity implements
AppPrefes appPrefes;
Database database;
public static UpdateLocation updateLocation;
public GetMapDetails getMapDetails;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -371,8 +374,24 @@ else if (cursor.moveToFirst()) {
+ ", lat_main=" + appPrefes.getData("lat_main")
+ " => url=" + url);

// start fetcher:
new GetMapDetails(MainActivity.this, url, this).execute();
// cancel old fetcher task:
if (getMapDetails != null) {
getMapDetails.cancel(true);
}

// create new fetcher task:
getMapDetails = new GetMapDetails(MainActivity.this, url, this);

// After Android 3.0, the default behavior of AsyncTask is execute in a single
// thread using SERIAL_EXECUTOR. This means the thread will no longer run while
// the App is in the foreground...
// Solution source:
// http://stackoverflow.com/questions/13080367/android-async-task-behavior-in-2-3-3-and-4-0-os
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.GINGERBREAD_MR1) {
getMapDetails.execute();
} else {
getMapDetails.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}

}

Expand Down

0 comments on commit 0a3741c

Please sign in to comment.