+ final String TAG = getClass().getName();
+
+ private WifiManager wifiman;
+ private Boolean managewifi = false;
+ private Long updatedelay = 60000L;
+ private Integer runningtasks = 0;
+ private SharedPreferences prefs;
+ private PersistentStore store;
+
+ private class UpdateTarget {
+ private TextView tv;
+ private Oracle updater;
+ private BgUpdate task;
+
+ private class BgUpdate extends AsyncTask<Void, Void, String> {
+ @Override
+ protected String doInBackground(Void... params) {
+ Log.v(TAG, "BgUpdate " + updater + " starting");
+ return updater.getResult();
+ }
+
+ @Override
+ protected void onPostExecute(String str) {
+ Log.v(TAG, "BgUpdate callback executing");
+ tv.setText(str);
+ runningtasks--;
+ if (runningtasks <= 0) {
+ if (managewifi) {
+ boolean wifion = wifiman.setWifiEnabled(false);
+ Log.v(TAG, "disabling wifi result " + wifion);
+ }
+ Time tm = new Time();
+ tm.setToNow();
+ tvs.setText(R.string.lasttry);
+ tvs.append(tm.format(" %d/%m/%Y %H:%M"));
+ }
+ }
+
+ @Override
+ protected void onCancelled() {
+ Log.v(TAG, "BgUpdate callback cancelled");
+ runningtasks--;
+ }
+ }
+
+ public UpdateTarget(TextView tv, Oracle 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;
+
+ private TextView tvt, tvd, tvs;
+ private Resources res;
+ private String[] month;
+ private String[] wday;
+
+ private Handler mHandler = new Handler();
+
+ private Runnable updateClock = new Runnable () {
+ public void run() {
+ long now = System.currentTimeMillis();
+ Time tm = new Time();
+ tm.set(now);
+ tvt.setText(tm.format("%H:%M"));
+ tm.set(now + 60000);
+ tm.second=0;
+ long next = tm.toMillis(false);
+ mHandler.postDelayed(this, next-now+1);
+ }
+ };
+
+ private Runnable updateCal = new Runnable () {
+ public void run() {
+ long now = System.currentTimeMillis();
+ Time tm = new Time();
+ tm.set(now);
+ tvd.setText(
+ wday[tm.weekDay] +
+ tm.format("\n%d ") +
+ month[tm.month] +
+ tm.format(" %Y"));
+ tm.set(now + 86400000);
+ tm.hour=0;
+ tm.minute=0;
+ tm.second=0;
+ long next = tm.toMillis(false);
+ mHandler.postDelayed(this, next-now+1);
+ }
+ };
+
+ private Runnable updateInfo = new Runnable () {
+ public void run() {
+ Log.v(TAG, "updateInfo starting");
+ if (managewifi) {
+ IntentFilter intentFilter =
+ new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE");
+ registerReceiver(connChanged,intentFilter);
+ connChangedRegistered = true;
+ boolean wifion = wifiman.setWifiEnabled(true);
+ Log.v(TAG, "enabling wifi result " + wifion);
+ } else {
+ for (int i = 0; i < ut.length; i++) {
+ runningtasks++;
+ ut[i].launch();
+ }
+ }
+ mHandler.postDelayed(resetInfo, 50000);
+ mHandler.postDelayed(this, updatedelay);
+ }
+ };
+
+ private Runnable resetInfo = new Runnable () {
+ public void run() {
+ Log.v(TAG, "resetInfo starting");
+ if (connChangedRegistered) {
+ unregisterReceiver(connChanged);
+ connChangedRegistered = false;
+ }
+ 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"));
+ }
+ if (managewifi) {
+ boolean wifion = wifiman.setWifiEnabled(false);
+ Log.v(TAG, "disabling wifi result " + wifion);
+ }
+ }
+ };
+
+ private boolean connChangedRegistered = false;
+ private final BroadcastReceiver connChanged = new BroadcastReceiver() {
+ @Override
+ public void onReceive(Context context, Intent intent) {
+ ConnectivityManager cm = (ConnectivityManager)context.
+ getSystemService(Context.CONNECTIVITY_SERVICE);
+ NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
+ boolean isConnected = activeNetwork != null &&
+ activeNetwork.isConnectedOrConnecting();
+ Log.v(TAG, "Connectivity changed to " + isConnected);
+ if (isConnected) {
+ for (int i = 0; i < ut.length; i++) {
+ runningtasks++;
+ ut[i].launch();
+ }
+ }
+ }
+ };
+