re-classify bg update
[WhereAmI.git] / src / org / average / whereami / WhereAmI.java
index c74ab2d09e0f758a0e73ef9190beee84d4c01035..5a665d1ed02346e3eae46830b3ebf9558eef60dd 100644 (file)
 package org.average.whereami;
 
 import android.app.Activity;
+import android.net.wifi.WifiManager;
 import android.os.Bundle;
+import android.os.AsyncTask;
+import android.os.Handler;
+import android.os.SystemClock;
+import android.net.ConnectivityManager;
+import android.net.NetworkInfo;
+import android.content.BroadcastReceiver;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.content.Context;
+import android.content.res.Resources;
+import android.text.format.Time;
+import android.util.Log;
+import android.view.View;
+import android.view.Window;
+import android.view.WindowManager;
+import android.widget.TextView;
 
 public class WhereAmI extends Activity
 {
+    private WifiManager wifiman;
+    private Integer runningtasks = 0;
+
+    private class UpdateTarget {
+        private TextView tv;
+        private Integer updater; // will be the function/object
+        private BgUpdate task;
+
+        private class BgUpdate extends AsyncTask<Void, Void, String> {
+            @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.task = new BgUpdate();
+        }
+
+        public void launch() {
+            tv.setText(R.string.updating);
+            task.execute();
+        }
+    }
+    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.w("WhereAmI", "updateInfo starting");
+            IntentFilter intentFilter =
+                new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE");
+            registerReceiver(connChanged,intentFilter);
+            boolean wifion = wifiman.setWifiEnabled(true);
+            Log.w("WhereAmI", "enabling wifi result " + wifion);
+            mHandler.postDelayed(resetInfo, 30000);
+            mHandler.postDelayed(this, 1200000);
+        }
+    };
+
+    private Runnable resetInfo = new Runnable () {
+        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) {
+                Time tm = new Time();
+                tm.setToNow();
+                tvs.setText(R.string.failtry);
+                tvs.append(tm.format(" %d/%m/%Y %H:%M:%S"));
+            }
+        }
+    };
+
+    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.w("WhereAmI", "Connectivity changed to " + isConnected);
+            if (isConnected) {
+                for (int i = 0; i < ut.length; i++) {
+                    runningtasks++;
+                    ut[i].launch();
+                }
+            }
+        }
+    };
+
     /** Called when the activity is first created. */
     @Override
     public void onCreate(Bundle savedInstanceState)
     {
         super.onCreate(savedInstanceState);
+        wifiman = (WifiManager)getSystemService(Context.WIFI_SERVICE);
+        requestWindowFeature(Window.FEATURE_NO_TITLE);
+        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
+        //getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
+        //getWindow().clearFlags(WindowManager.LayoutParams.
+        //                                      FLAG_FORCE_NOT_FULLSCREEN);
         setContentView(R.layout.main);
+        res = getResources();
+        month = res.getStringArray(R.array.month);
+        wday = res.getStringArray(R.array.wday);
+        tvt = (TextView)findViewById(R.id.time);
+        tvd = (TextView)findViewById(R.id.date);
+        tvs = (TextView)findViewById(R.id.timestamp);
+       ut = new UpdateTarget[] {
+            new UpdateTarget((TextView)findViewById(R.id.location),  1),
+            new UpdateTarget((TextView)findViewById(R.id.phonecall), 2)
+        };
+        Log.w("WhereAmI", "created UI, about to start update task");
+       mHandler.post(updateClock);
+       mHandler.post(updateCal);
+       mHandler.post(updateInfo);
+        Log.w("WhereAmI", "created UI, update task created");
+    }
+
+    /** Called when put to background */
+    @Override
+    public void onPause()
+    {
+        super.onPause();
+        Log.w("WhereAmI", "calling finish");
+        finish();
+    }
+
+    /** Called when the activity is destroyed. */
+    @Override
+    public void onDestroy()
+    {
+        super.onDestroy();
+        Log.w("WhereAmI", "going down");
+       mHandler.removeCallbacks(updateClock);
+       mHandler.removeCallbacks(updateCal);
+       mHandler.removeCallbacks(updateInfo);
+        //unregisterReceiver(connChanged);
+        boolean wifion = wifiman.setWifiEnabled(false);
+        Log.w("WhereAmI", "disabling wifi result " + wifion);
     }
 }