From ae3fc908b14c9ebea03529f769b6530151462599 Mon Sep 17 00:00:00 2001 From: Eugene Crosser Date: Mon, 30 Jan 2012 11:40:22 +0400 Subject: [PATCH] re-classify bg update --- src/org/average/whereami/WhereAmI.java | 75 ++++++++++++++------------ 1 file changed, 40 insertions(+), 35 deletions(-) diff --git a/src/org/average/whereami/WhereAmI.java b/src/org/average/whereami/WhereAmI.java index 9d012c3..5a665d1 100644 --- a/src/org/average/whereami/WhereAmI.java +++ b/src/org/average/whereami/WhereAmI.java @@ -26,11 +26,44 @@ public class WhereAmI extends Activity private Integer runningtasks = 0; private class UpdateTarget { - public TextView tv; - public Integer updater; // will be the function/object + private TextView tv; + private Integer updater; // will be the function/object + private BgUpdate task; + + private class BgUpdate extends AsyncTask { + @Override + protected String doInBackground(Void... params) { + Log.w("WhereAmI", "BgUpdate " + updater + " starting"); + SystemClock.sleep(5000); // real job do be done here + Log.w("WhereAmI", "BgUpdate about to return"); + return "5 seconds passed in " + updater; + } + + @Override + protected void onPostExecute(String str) { + Log.w("WhereAmI", "BgUpdate callback executing"); + tv.setText(str); + runningtasks--; + if (runningtasks <= 0) { + boolean wifion = wifiman.setWifiEnabled(false); + Log.w("WhereAmI", "disabling wifi result " + wifion); + Time tm = new Time(); + tm.setToNow(); + tvs.setText(R.string.lasttry); + tvs.append(tm.format(" %d/%m/%Y %H:%M:%S")); + } + } + } + public UpdateTarget(TextView tv, Integer updater) { - this.tv = tv; - this.updater = updater; + this.tv = tv; + this.updater = updater; + this.task = new BgUpdate(); + } + + public void launch() { + tv.setText(R.string.updating); + task.execute(); } } private UpdateTarget[] ut; @@ -83,7 +116,7 @@ public class WhereAmI extends Activity boolean wifion = wifiman.setWifiEnabled(true); Log.w("WhereAmI", "enabling wifi result " + wifion); mHandler.postDelayed(resetInfo, 30000); - mHandler.postDelayed(this, 60000); + mHandler.postDelayed(this, 1200000); } }; @@ -114,40 +147,12 @@ public class WhereAmI extends Activity if (isConnected) { for (int i = 0; i < ut.length; i++) { runningtasks++; - ut[i].tv.setText(R.string.updating); - new BgUpdate().execute(ut[i]); + ut[i].launch(); } } } }; - private class BgUpdate extends AsyncTask { - private UpdateTarget ut; - @Override - protected String doInBackground(UpdateTarget... whereto) { - Log.w("WhereAmI", "BgUpdate starting"); - ut = whereto[0]; - SystemClock.sleep(5000); // real job do be done here - Log.w("WhereAmI", "BgUpdate about to return"); - return "5 seconds passed"; - } - - @Override - protected void onPostExecute(String str) { - Log.w("WhereAmI", "BgUpdate callback executing"); - ut.tv.setText(str); - runningtasks--; - if (runningtasks <= 0) { - boolean wifion = wifiman.setWifiEnabled(false); - Log.w("WhereAmI", "disabling wifi result " + wifion); - Time tm = new Time(); - tm.setToNow(); - tvs.setText(R.string.lasttry); - tvs.append(tm.format(" %d/%m/%Y %H:%M:%S")); - } - } - } - /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) @@ -183,7 +188,7 @@ public class WhereAmI extends Activity { super.onPause(); Log.w("WhereAmI", "calling finish"); - this.finish(); + finish(); } /** Called when the activity is destroyed. */ -- 2.43.0