Merge branch 'master' of ssh://cahost.average.org/~/WhereAmI
[WhereAmI.git] / src / org / average / whereami / WhereAmI.java
index 1d3c2686e26da6b773d8da8b0042efab0bbc6bd8..2fed78cbbada79d093932cee1959f7943ea85fa0 100644 (file)
@@ -1,13 +1,15 @@
 package org.average.whereami;
 
 import org.average.whereami.WhereAmIprefs;
+import org.average.whereami.Oracle;
+import org.average.whereami.PhoneLog;
+import org.average.whereami.Location;
 
 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;
@@ -16,6 +18,7 @@ import android.content.IntentFilter;
 import android.content.Context;
 import android.content.res.Resources;
 import android.content.SharedPreferences;
+import android.preference.PreferenceManager;
 import android.text.format.Time;
 import android.util.Log;
 import android.view.View;
@@ -28,32 +31,34 @@ import android.widget.TextView;
 
 public class WhereAmI extends Activity
 {
+    final String TAG = getClass().getName();
+
     private WifiManager wifiman;
+    private Boolean managewifi = false;
+    private Long updatedelay = 60000L;
     private Integer runningtasks = 0;
     private SharedPreferences prefs;
 
     private class UpdateTarget {
         private TextView tv;
-        private Integer updater; // will be the function/object
+        private Oracle updater;
         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;
+                Log.w(TAG, "BgUpdate " + updater + " starting");
+                return updater.getResult();
             }
         
             @Override
             protected void onPostExecute(String str) {
-                Log.w("WhereAmI", "BgUpdate callback executing");
+                Log.w(TAG, "BgUpdate callback executing");
                 tv.setText(str);
                 runningtasks--;
                 if (runningtasks <= 0) {
                     boolean wifion = wifiman.setWifiEnabled(false);
-                    Log.w("WhereAmI", "disabling wifi result " + wifion);
+                    Log.w(TAG, "disabling wifi result " + wifion);
                     Time tm = new Time();
                     tm.setToNow();
                     tvs.setText(R.string.lasttry);
@@ -63,12 +68,12 @@ public class WhereAmI extends Activity
 
             @Override
             protected void onCancelled() {
-                Log.w("WhereAmI", "BgUpdate callback cancelled");
+                Log.w(TAG, "BgUpdate callback cancelled");
                 runningtasks--;
             }
         }
 
-        public UpdateTarget(TextView tv, Integer updater) {
+        public UpdateTarget(TextView tv, Oracle updater) {
             this.tv = tv;
             this.updater = updater;
         }
@@ -126,21 +131,28 @@ public class WhereAmI extends Activity
 
     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);
-            connChangedRegistered = true;
-            boolean wifion = wifiman.setWifiEnabled(true);
-            Log.w("WhereAmI", "enabling wifi result " + wifion);
-            mHandler.postDelayed(resetInfo, 30000);
-            mHandler.postDelayed(this, 60000);
+            Log.w(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.w(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.w("WhereAmI", "resetInfo starting");
+            Log.w(TAG, "resetInfo starting");
             if (connChangedRegistered) {
                 unregisterReceiver(connChanged);
                 connChangedRegistered = false;
@@ -154,8 +166,10 @@ public class WhereAmI extends Activity
                 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);
+            if (managewifi) {
+                boolean wifion = wifiman.setWifiEnabled(false);
+                Log.w(TAG, "disabling wifi result " + wifion);
+            }
         }
     };
 
@@ -168,7 +182,7 @@ public class WhereAmI extends Activity
             NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
             boolean isConnected = activeNetwork != null &&
                                   activeNetwork.isConnectedOrConnecting();
-            Log.w("WhereAmI", "Connectivity changed to " + isConnected);
+            Log.w(TAG, "Connectivity changed to " + isConnected);
             if (isConnected) {
                 for (int i = 0; i < ut.length; i++) {
                     runningtasks++;
@@ -180,19 +194,11 @@ public class WhereAmI extends Activity
 
     /** Called when the activity is first created. */
     @Override
-    public void onCreate(Bundle savedInstanceState)
-    {
+    public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
-        prefs = getSharedPreferences("WhereAmI", MODE_PRIVATE);
         wifiman = (WifiManager)getSystemService(Context.WIFI_SERVICE);
         requestWindowFeature(Window.FEATURE_NO_TITLE);
         getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
-        boolean fullscreen = prefs.getBoolean("fullscreen", false);
-        if (fullscreen) {
-            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);
@@ -201,30 +207,50 @@ public class WhereAmI extends Activity
         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)
+            new UpdateTarget((TextView)findViewById(R.id.location),
+                             new PhoneLog()),
+            new UpdateTarget((TextView)findViewById(R.id.phonecall),
+                             new Location())
         };
-        Log.w("WhereAmI", "created UI, about to start update task");
+        Log.w(TAG, "created UI, about to start update task");
        mHandler.post(updateClock);
        mHandler.post(updateCal);
        mHandler.post(updateInfo);
-        Log.w("WhereAmI", "created UI, update task created");
+        Log.w(TAG, "created UI, update task created");
+    }
+
+    /** Called when reactivated */
+    @Override
+    public void onResume() {
+        super.onResume();
+        prefs = PreferenceManager.getDefaultSharedPreferences(this);
+        boolean fullscreen = prefs.getBoolean("fullscreen", false);
+        managewifi = prefs.getBoolean("managewifi", false);
+        updatedelay = Long.parseLong(prefs.getString("updateperiod", "1200000"));
+        Log.w("WhereAmI", "fullscreen: " + fullscreen +
+                          ", managewifi: " + managewifi +
+                          ", updatedelay: " + updatedelay);
+        if (fullscreen) {
+            getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
+            getWindow().clearFlags(WindowManager.LayoutParams.
+                                                  FLAG_FORCE_NOT_FULLSCREEN);
+        } else {
+            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
+        }
     }
 
     /** Called when put to background */
     @Override
-    public void onPause()
-    {
+    public void onPause() {
         super.onPause();
-        Log.w("WhereAmI", "going background");
+        Log.w(TAG, "going background");
     }
 
     /** Called when the activity is destroyed. */
     @Override
-    public void onDestroy()
-    {
+    public void onDestroy() {
         super.onDestroy();
-        Log.w("WhereAmI", "going down");
+        Log.w(TAG, "going down");
        mHandler.removeCallbacks(updateClock);
        mHandler.removeCallbacks(updateCal);
        mHandler.removeCallbacks(updateInfo);
@@ -233,7 +259,7 @@ public class WhereAmI extends Activity
             connChangedRegistered = false;
         }
         boolean wifion = wifiman.setWifiEnabled(false);
-        Log.w("WhereAmI", "disabling wifi result " + wifion);
+        Log.w(TAG, "disabling wifi result " + wifion);
     }
 
     /** Called when the menu is activated. */
@@ -251,8 +277,12 @@ public class WhereAmI extends Activity
         case R.id.quit:
             finish();
             return true;
+        case R.id.authorize:
+            Log.w(TAG, "authorize requested");
+            startActivity(new Intent(this, Authorize.class));
+            return true;
         case R.id.settings:
-            Log.w("WhereAmI", "settings requested");
+            Log.w(TAG, "settings requested");
             startActivity(new Intent(this, WhereAmIprefs.class));
             return true;
         default: