Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fix async. download+parser tasks, no need to restart them on

orientation change anymore
  • Loading branch information...
commit edb4b35ff1b9b2d4ddbf2c7a37a4f318a2a0f165 1 parent 79e35f1
@tuxmobil authored
View
89 src/nerd/tuxmobil/fahrplan/camp11/Fahrplan.java
@@ -47,8 +47,7 @@
import android.widget.Toast;
import android.widget.LinearLayout.LayoutParams;
-public class Fahrplan extends Activity implements response_callback,
- OnClickListener, parser_callback {
+public class Fahrplan extends Activity implements OnClickListener {
private MyApp global;
private ProgressDialog progress = null;
private String LOG_TAG = "Fahrplan";
@@ -84,14 +83,20 @@
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
+
context = this;
-// requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
-// getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
-// R.layout.custom_title);
global = (MyApp) getApplicationContext();
- fetcher = new FetchFahrplan();
- parser = new FahrplanParser();
+ if (MyApp.fetcher == null) {
+ fetcher = new FetchFahrplan();
+ } else {
+ fetcher = MyApp.fetcher;
+ }
+ if (MyApp.parser == null) {
+ parser = new FahrplanParser(getApplicationContext());
+ } else {
+ parser = MyApp.parser;
+ }
scale = getResources().getDisplayMetrics().density;
progress = null;
@@ -156,21 +161,10 @@ public void ready(View view) {
}
});
-// final TextView leftText = (TextView) findViewById(R.id.title_left_text);
-// dayTextView = (TextView) findViewById(R.id.title_right_text);
-// leftText.setText(getString(R.string.app_name));
statusLineText = (TextView) findViewById(R.id.statusLineText);
statusBar = (LinearLayout) findViewById(R.id.statusLine);
statusBar.setVisibility(View.GONE);
-// refreshBtn = (ImageButton) findViewById(R.id.refresh_button);
-/* refreshBtn.setOnClickListener(new OnClickListener() {
-
- @Override
- public void onClick(View v) {
- fetchFahrplan();
- }
- });*/
slideUpIn = AnimationUtils.loadAnimation(this, R.anim.slide_up_in);
slideDownOut = AnimationUtils
@@ -198,16 +192,18 @@ public void onClick(View v) {
Log.d(LOG_TAG,"day "+day);
}
+ MyApp.fetcher.setActivity(this); // save current activity and trigger possible completion event
+ MyApp.parser.setActivity(this); // save current activity and trigger possible completion event
+
switch (MyApp.task_running) {
case FETCH:
Log.d(LOG_TAG, "fetch was pending, restart");
- MyApp.task_running = TASKS.NONE;
- fetchFahrplan();
+ showFetchingStatus();
viewDay(false);
break;
case PARSE:
Log.d(LOG_TAG, "parse was pending, restart");
- parseFahrplan();
+ showParsingStatus();
break;
case NONE:
if (MyApp.numdays == 0) {
@@ -224,7 +220,7 @@ public void onClick(View v) {
}
}
- public void parseFahrplan() {
+ public void showParsingStatus() {
if (MyApp.numdays == 0) {
// initial load
progress = ProgressDialog.show(this, "", getResources().getString(
@@ -239,17 +235,24 @@ public void parseFahrplan() {
statusBar.startAnimation(slideUpIn);
}
}
-
+ }
+
+ public void parseFahrplan() {
+ showParsingStatus();
MyApp.task_running = TASKS.PARSE;
- parser.parse(this, MyApp.fahrplan_xml, global);
+ parser.parse(MyApp.fahrplan_xml);
}
@Override
public void onDestroy() {
Log.d(LOG_TAG, "onDestroy");
super.onDestroy();
- fetcher.cancel();
- parser.cancel();
+ if (MyApp.fetcher != null) {
+ MyApp.fetcher.setActivity(null);
+ }
+ if (MyApp.parser != null) {
+ MyApp.parser.setActivity(null);
+ }
if (metadb != null) metadb.close();
if (lecturedb != null) lecturedb.close();
if (highlightdb != null) highlightdb.close();
@@ -288,22 +291,26 @@ public boolean onCreateOptionsMenu(Menu menu) {
return true;
}
+ public void showFetchingStatus() {
+ if (MyApp.numdays == 0) {
+ // initial load
+ Log.d(LOG_TAG, "fetchFahrplan with numdays == 0");
+ progress = ProgressDialog.show(this, "", getResources().getString(
+ R.string.progress_loading_data), true);
+ } else {
+ refreshBtn.setVisibility(View.GONE);
+ actionBar.setProgressBarVisibility(View.VISIBLE);
+ statusLineText.setText(getString(R.string.progress_loading_data));
+ statusBar.setVisibility(View.VISIBLE);
+ statusBar.startAnimation(slideUpIn);
+ }
+ }
+
public void fetchFahrplan() {
if (MyApp.task_running == TASKS.NONE) {
MyApp.task_running = TASKS.FETCH;
- fetcher.fetch(this, "/camp/2011/Fahrplan/schedule.en.xml", global);
- if (MyApp.numdays == 0) {
- // initial load
- Log.d(LOG_TAG, "fetchFahrplan with numdays == 0");
- progress = ProgressDialog.show(this, "", getResources().getString(
- R.string.progress_loading_data), true);
- } else {
- refreshBtn.setVisibility(View.GONE);
- actionBar.setProgressBarVisibility(View.VISIBLE);
- statusLineText.setText(getString(R.string.progress_loading_data));
- statusBar.setVisibility(View.VISIBLE);
- statusBar.startAnimation(slideUpIn);
- }
+ showFetchingStatus();
+ fetcher.fetch("/camp/2011/Fahrplan/schedule.en.xml");
} else {
Log.d(LOG_TAG, "fetch already in progress");
}
@@ -469,7 +476,6 @@ public void onClick(DialogInterface dialog, int item) {
alert.show();
}
- @Override
public void onGotResponse(HTTP_STATUS status, String response) {
Log.d(LOG_TAG, "Response... " + status);
MyApp.task_running = TASKS.NONE;
@@ -915,9 +921,8 @@ public void onClick(View v) {
startActivity(intent);
}
- @Override
public void onParseDone(Boolean result, String version) {
- Log.d(LOG_TAG, "parseDone: " + result);
+ Log.d(LOG_TAG, "parseDone: " + result + " , numdays="+MyApp.numdays);
MyApp.task_running = TASKS.NONE;
MyApp.fahrplan_xml = null;
View
59 src/nerd/tuxmobil/fahrplan/camp11/FahrplanParser.java
@@ -5,6 +5,7 @@
import org.xmlpull.v1.XmlPullParser;
+import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
@@ -12,19 +13,19 @@
import android.util.Log;
import android.util.Xml;
-interface parser_callback {
- void onParseDone(Boolean result, String version);
-}
-
public class FahrplanParser {
private parser task;
+ private Activity activity;
+ private Context context;
- public FahrplanParser() {
+ public FahrplanParser(Context context) {
task = null;
+ MyApp.parser = this;
+ this.context = context;
}
- public void parse(parser_callback cb, String fahrplan, MyApp global) {
- task = new parser(cb, global);
+ public void parse(String fahrplan) {
+ task = new parser(activity, context);
task.execute(fahrplan);
}
@@ -32,26 +33,44 @@ public void cancel()
{
if (task != null) task.cancel(false);
}
+
+ public void setActivity(Activity activity) {
+ this.activity = activity;
+ if (task != null) {
+ task.setActivity(activity);
+ }
+ }
}
class parser extends AsyncTask<String, Void, Boolean> {
- private parser_callback callback;
- private MyApp global;
private String LOG_TAG = "ParseFahrplan";
private ArrayList<Lecture> lectures;
private MetaInfo meta;
private MetaDBOpenHelper metaDB;
private SQLiteDatabase db;
+ private Activity activity;
+ private boolean completed;
+ private boolean result;
+ private Context context;
- public parser(parser_callback cb, MyApp global) {
- this.global = global;
- this.callback = cb;
+ public parser(Activity activity, Context context) {
+ this.activity = activity;
+ this.completed = false;
this.db = null;
+ this.context = context;
+ }
+
+ public void setActivity(Activity activity) {
+ this.activity = activity;
+
+ if (completed && (activity != null)) {
+ notifyActivity();
+ }
}
protected Boolean doInBackground(String... args) {
- return parseFahrplan(args[0], (Context)this.callback);
+ return parseFahrplan(args[0]);
}
@@ -60,8 +79,18 @@ protected void onCancelled() {
if (db != null) db.close();
}
+ private void notifyActivity() {
+ ((Fahrplan)activity).onParseDone(result, meta.version);
+ completed = false;
+ }
+
protected void onPostExecute(Boolean result) {
- this.callback.onParseDone(result, meta.version);
+ completed = true;
+ this.result = result;
+
+ if (activity != null) {
+ notifyActivity();
+ }
}
public void storeMeta(Context context, MetaInfo meta) {
@@ -111,7 +140,7 @@ public void storeLectureList(Context context, ArrayList<Lecture> lectures) {
db.close();
}
- private Boolean parseFahrplan(String fahrplan, Context context) {
+ private Boolean parseFahrplan(String fahrplan) {
XmlPullParser parser = Xml.newPullParser();
try {
parser.setInput(new StringReader(fahrplan));
View
50 src/nerd/tuxmobil/fahrplan/camp11/FetchFahrplan.java
@@ -21,39 +21,55 @@
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
+import android.app.Activity;
import android.os.AsyncTask;
import android.util.Log;
-interface response_callback {
- void onGotResponse(HTTP_STATUS status, String response);
-}
public class FetchFahrplan {
private fetcher task;
+ private Activity activity;
public FetchFahrplan() {
task = null;
+ MyApp.fetcher = this;
}
- public void fetch(response_callback cb, String arg, MyApp global) {
- task = new fetcher(cb, global);
+ public void fetch(String arg) {
+ task = new fetcher(this.activity);
task.execute(arg);
}
public void cancel() {
if (task != null) task.cancel(true);
}
+
+ public void setActivity(Activity activity) {
+ this.activity = activity;
+ if (task != null) {
+ task.setActivity(activity);
+ }
+ }
}
class fetcher extends AsyncTask<String, Void, HTTP_STATUS> {
- private response_callback callback;
- private MyApp global;
private String responseStr;
private String LOG_TAG = "FetchFahrplan";
+ private Activity activity;
+ private boolean completed;
+ private HTTP_STATUS status;
+
+ public fetcher(Activity activity) {
+ this.activity = activity;
+ this.completed = false;
+ }
- public fetcher(response_callback cb, MyApp global) {
- this.global = global;
- this.callback = cb;
+ public void setActivity(Activity activity) {
+ this.activity = activity;
+
+ if (completed && (activity != null)) {
+ notifyActivity();
+ }
}
protected HTTP_STATUS doInBackground(String... args) {
@@ -68,13 +84,23 @@ protected void onCancelled() {
}
protected void onPostExecute(HTTP_STATUS status) {
+ completed = true;
+ this.status = status;
+
+ if (activity != null) {
+ notifyActivity();
+ }
+ }
+
+ private void notifyActivity() {
if (status == HTTP_STATUS.HTTP_OK) {
Log.d(LOG_TAG, "fetch done successfully");
- this.callback.onGotResponse(status, responseStr);
+ ((Fahrplan)activity).onGotResponse(status, responseStr);
} else {
Log.d(LOG_TAG, "fetch failed");
- this.callback.onGotResponse(status, null);
+ ((Fahrplan)activity).onGotResponse(status, null);
}
+ completed = false; // notifiy only once
}
private HTTP_STATUS fetchthis(String addr, String arg) {
View
2  src/nerd/tuxmobil/fahrplan/camp11/MyApp.java
@@ -13,6 +13,8 @@
public static String title;
public static String subtitle;
public static ArrayList<DateList> dateList = null;
+ public static FetchFahrplan fetcher = null;
+ public static FahrplanParser parser = null;
enum TASKS {
NONE,
Please sign in to comment.
Something went wrong with that request. Please try again.