X-Git-Url: http://www.average.org/gitweb/?p=WhereAmI.git;a=blobdiff_plain;f=src%2Forg%2Faverage%2Fwhereami%2FWhereAmI.java;h=988f1fb53149b88be2f366ce151d50483db5e3e6;hp=9d012c35997d4f4b11d625b678b0879ca3ce1e1a;hb=c1a4c2224d33c39c436462c3d8a1ad63840c74d5;hpb=558910007298559dde1b3d3cae13cd6c6dd79f75 diff --git a/src/org/average/whereami/WhereAmI.java b/src/org/average/whereami/WhereAmI.java index 9d012c3..988f1fb 100644 --- a/src/org/average/whereami/WhereAmI.java +++ b/src/org/average/whereami/WhereAmI.java @@ -26,11 +26,54 @@ 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")); + } + } + + @Override + protected void onCancelled() { + Log.w("WhereAmI", "BgUpdate callback cancelled"); + runningtasks--; + } + } + public UpdateTarget(TextView tv, Integer updater) { - this.tv = tv; - this.updater = updater; + this.tv = tv; + this.updater = updater; + } + + public void launch() { + tv.setText(R.string.updating); + task = new BgUpdate(); + task.execute(); + } + + public void cancel() { + task.cancel(true); } } private UpdateTarget[] ut; @@ -91,14 +134,17 @@ public class WhereAmI extends Activity public void run() { Log.w("WhereAmI", "resetInfo starting"); unregisterReceiver(connChanged); - boolean wifion = wifiman.setWifiEnabled(false); - Log.w("WhereAmI", "disabling wifi result " + wifion); if (runningtasks > 0) { + for (int i = 0; i < ut.length; i++) { + ut[i].cancel(); + } Time tm = new Time(); tm.setToNow(); tvs.setText(R.string.failtry); tvs.append(tm.format(" %d/%m/%Y %H:%M:%S")); } + boolean wifion = wifiman.setWifiEnabled(false); + Log.w("WhereAmI", "disabling wifi result " + wifion); } }; @@ -114,40 +160,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 +201,7 @@ public class WhereAmI extends Activity { super.onPause(); Log.w("WhereAmI", "calling finish"); - this.finish(); + finish(); } /** Called when the activity is destroyed. */