]> average.org Git - WhereAmI.git/commitdiff
start updates when connectivity is established
authorEugene Crosser <crosser@average.org>
Sun, 29 Jan 2012 21:00:48 +0000 (01:00 +0400)
committerEugene Crosser <crosser@average.org>
Sun, 29 Jan 2012 21:00:48 +0000 (01:00 +0400)
AndroidManifest.xml
project.properties
res/layout/main.xml
res/values/strings.xml
src/org/average/whereami/WhereAmI.java

index 93d11193ac00cfadf0d2d0019501ff1efaab3f32..b262867a3b0dec824aad4c799fdc033fa100aea3 100644 (file)
@@ -4,9 +4,12 @@
       android:versionCode="1"
       android:versionName="1.0">
     <uses-permission android:name="android.permission.INTERNET" />
+    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
+    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
     <uses-sdk android:minSdkVersion="8"/>
     <supports-screens android:largeScreens="true"/>
-    <application android:label="@string/app_name" >
+    <application android:label="@string/app_name"
+                 android:debuggable="true" >
         <activity android:name="WhereAmI"
                   android:label="@string/app_name">
             <intent-filter>
index 882c28df1ba98979e7df95fafe80ae866823a796..663686fb137ad6cd33585a2b2f4e05317363b999 100644 (file)
@@ -7,6 +7,6 @@
 # "ant.properties", and override values to adapt the script to your
 # project structure.
 
-proguard.config=proguard.cfg
+proguard.config=proguard.cfg
 # Project target.
 target=android-8
index 2a604b3760cc7dcc08d796547eb7c22563091bb7..1803a66f32dc946d9e25475c8a035f19c7786d10 100644 (file)
@@ -13,7 +13,7 @@
   >
     <TextView
       android:id="@+id/time"
-      android:layout_width="wrap_content
+      android:layout_width="0px
       android:layout_height="match_parent" 
       android:layout_weight="2"
       android:gravity="center"
@@ -29,7 +29,7 @@
       android:id="@+id/date"
       android:layout_width="0px" 
       android:layout_height="match_parent" 
-      android:layout_weight="5"
+      android:layout_weight="3"
       android:gravity="center"
       android:textColor="#ff000000"
       android:textSize="24sp"
index ba098c5f92c4bf2d16a3a71322aa524c23ffd497..d321d3c58bc39c354fb0e9ebeb48494accc71274 100644 (file)
@@ -4,6 +4,7 @@
     <string name="updating">Обновление...</string>
     <string name="failure">Неудача</string>
     <string name="lasttry">Последнее обновление</string>
+    <string name="failtry">Неудачная попытка</string>
     <string-array name="month">
         <item>января</item>
         <item>февраля</item>
index 0f193cd71d3a13d8047f3ee6108c165ac034e45a..9d012c35997d4f4b11d625b678b0879ca3ce1e1a 100644 (file)
@@ -1,10 +1,17 @@
 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;
@@ -15,6 +22,9 @@ import android.widget.TextView;
 
 public class WhereAmI extends Activity
 {
+    private WifiManager wifiman;
+    private Integer runningtasks = 0;
+
     private class UpdateTarget {
         public TextView tv;
         public Integer updater; // will be the function/object
@@ -30,32 +40,6 @@ public class WhereAmI extends Activity
     private String[] month;
     private String[] wday;
 
-    /** Called when the activity is first created. */
-    @Override
-    public void onCreate(Bundle savedInstanceState)
-    {
-        super.onCreate(savedInstanceState);
-        requestWindowFeature(Window.FEATURE_NO_TITLE);
-        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.v("WhereAmI", "created UI, about to start update task");
-       updateClock.run();
-       updateCal.run();
-       updateInfo.run();
-        Log.v("WhereAmI", "created UI, update task created");
-    }
-
     private Handler mHandler = new Handler();
 
     private Runnable updateClock = new Runnable () {
@@ -92,34 +76,127 @@ public class WhereAmI extends Activity
 
     private Runnable updateInfo = new Runnable () {
         public void run() {
-            Log.v("updateInfo", "starting");
-            for (int i = 0; i < ut.length; i++) {
-                ut[i].tv.setText(R.string.updating);
-                new TimedUpdateTask().execute(ut[i]);
+            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, 60000);
+        }
+    };
+
+    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"));
             }
-            mHandler.postDelayed(this, 10000);
         }
     };
 
-    private class TimedUpdateTask extends AsyncTask<UpdateTarget, Void, String> {
+    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].tv.setText(R.string.updating);
+                    new BgUpdate().execute(ut[i]);
+                }
+            }
+        }
+    };
+
+    private class BgUpdate extends AsyncTask<UpdateTarget, Void, String> {
         private UpdateTarget ut;
        @Override
         protected String doInBackground(UpdateTarget... whereto) {
-            Log.v("TimedUpdateTask", "starting");
+            Log.w("WhereAmI", "BgUpdate starting");
             ut = whereto[0];
             SystemClock.sleep(5000); // real job do be done here
-            Log.v("TimedUpdateTask", "about to return");
+            Log.w("WhereAmI", "BgUpdate about to return");
             return "5 seconds passed";
         }
     
        @Override
         protected void onPostExecute(String str) {
-            Log.v("TimedUpdateTask", "callback executing");
+            Log.w("WhereAmI", "BgUpdate callback executing");
             ut.tv.setText(str);
-            Time tm = new Time();
-            tm.setToNow();
-            tvs.setText(R.string.lasttry);
-            tvs.append(tm.format(" %d/%m/%Y %H:%M:%S"));
+            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)
+    {
+        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");
+        this.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);
+    }
 }