]> www.average.org Git - WhereAmI.git/blob - src/org/average/whereami/WhereAmI.java
only can execute every new async task once
[WhereAmI.git] / src / org / average / whereami / WhereAmI.java
1 package org.average.whereami;
2
3 import android.app.Activity;
4 import android.net.wifi.WifiManager;
5 import android.os.Bundle;
6 import android.os.AsyncTask;
7 import android.os.Handler;
8 import android.os.SystemClock;
9 import android.net.ConnectivityManager;
10 import android.net.NetworkInfo;
11 import android.content.BroadcastReceiver;
12 import android.content.Intent;
13 import android.content.IntentFilter;
14 import android.content.Context;
15 import android.content.res.Resources;
16 import android.text.format.Time;
17 import android.util.Log;
18 import android.view.View;
19 import android.view.Window;
20 import android.view.WindowManager;
21 import android.widget.TextView;
22
23 public class WhereAmI extends Activity
24 {
25     private WifiManager wifiman;
26     private Integer runningtasks = 0;
27
28     private class UpdateTarget {
29         private TextView tv;
30         private Integer updater; // will be the function/object
31         private BgUpdate task;
32
33         private class BgUpdate extends AsyncTask<Void, Void, String> {
34             @Override
35             protected String doInBackground(Void... params) {
36                 Log.w("WhereAmI", "BgUpdate " + updater + " starting");
37                 SystemClock.sleep(5000); // real job do be done here
38                 Log.w("WhereAmI", "BgUpdate about to return");
39                 return "5 seconds passed in " + updater;
40             }
41         
42             @Override
43             protected void onPostExecute(String str) {
44                 Log.w("WhereAmI", "BgUpdate callback executing");
45                 tv.setText(str);
46                 runningtasks--;
47                 if (runningtasks <= 0) {
48                     boolean wifion = wifiman.setWifiEnabled(false);
49                     Log.w("WhereAmI", "disabling wifi result " + wifion);
50                     Time tm = new Time();
51                     tm.setToNow();
52                     tvs.setText(R.string.lasttry);
53                     tvs.append(tm.format(" %d/%m/%Y %H:%M:%S"));
54                 }
55             }
56         }
57
58         public UpdateTarget(TextView tv, Integer updater) {
59             this.tv = tv;
60             this.updater = updater;
61         }
62
63         public void launch() {
64             tv.setText(R.string.updating);
65             task = new BgUpdate();
66             task.execute();
67         }
68     }
69     private UpdateTarget[] ut;
70
71     private TextView tvt, tvd, tvs;
72     private Resources res;
73     private String[] month;
74     private String[] wday;
75
76     private Handler mHandler = new Handler();
77
78     private Runnable updateClock = new Runnable () {
79         public void run() {
80             long now = System.currentTimeMillis();
81             Time tm = new Time();
82             tm.set(now);
83             tvt.setText(tm.format("%H:%M"));
84             tm.set(now + 60000);
85             tm.second=0;
86             long next = tm.toMillis(false);
87             mHandler.postDelayed(this, next-now+1);
88         }
89     };
90
91     private Runnable updateCal = new Runnable () {
92         public void run() {
93             long now = System.currentTimeMillis();
94             Time tm = new Time();
95             tm.set(now);
96             tvd.setText(
97                        wday[tm.weekDay] +
98                        tm.format("\n%d ") +
99                        month[tm.month] +
100                        tm.format(" %Y"));
101             tm.set(now + 86400000);
102             tm.hour=0;
103             tm.minute=0;
104             tm.second=0;
105             long next = tm.toMillis(false);
106             mHandler.postDelayed(this, next-now+1);
107         }
108     };
109
110     private Runnable updateInfo = new Runnable () {
111         public void run() {
112             Log.w("WhereAmI", "updateInfo starting");
113             IntentFilter intentFilter =
114                 new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE");
115             registerReceiver(connChanged,intentFilter);
116             boolean wifion = wifiman.setWifiEnabled(true);
117             Log.w("WhereAmI", "enabling wifi result " + wifion);
118             mHandler.postDelayed(resetInfo, 30000);
119             mHandler.postDelayed(this, 60000);
120         }
121     };
122
123     private Runnable resetInfo = new Runnable () {
124         public void run() {
125             Log.w("WhereAmI", "resetInfo starting");
126             unregisterReceiver(connChanged);
127             boolean wifion = wifiman.setWifiEnabled(false);
128             Log.w("WhereAmI", "disabling wifi result " + wifion);
129             if (runningtasks > 0) {
130                 Time tm = new Time();
131                 tm.setToNow();
132                 tvs.setText(R.string.failtry);
133                 tvs.append(tm.format(" %d/%m/%Y %H:%M:%S"));
134             }
135         }
136     };
137
138     private final BroadcastReceiver connChanged = new BroadcastReceiver() {
139         @Override
140         public void onReceive(Context context, Intent intent) {
141             ConnectivityManager cm = (ConnectivityManager)context.
142                            getSystemService(Context.CONNECTIVITY_SERVICE);
143             NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
144             boolean isConnected = activeNetwork != null &&
145                                   activeNetwork.isConnectedOrConnecting();
146             Log.w("WhereAmI", "Connectivity changed to " + isConnected);
147             if (isConnected) {
148                 for (int i = 0; i < ut.length; i++) {
149                     runningtasks++;
150                     ut[i].launch();
151                 }
152             }
153         }
154     };
155
156     /** Called when the activity is first created. */
157     @Override
158     public void onCreate(Bundle savedInstanceState)
159     {
160         super.onCreate(savedInstanceState);
161         wifiman = (WifiManager)getSystemService(Context.WIFI_SERVICE);
162         requestWindowFeature(Window.FEATURE_NO_TITLE);
163         getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
164         //getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
165         //getWindow().clearFlags(WindowManager.LayoutParams.
166         //                                      FLAG_FORCE_NOT_FULLSCREEN);
167         setContentView(R.layout.main);
168         res = getResources();
169         month = res.getStringArray(R.array.month);
170         wday = res.getStringArray(R.array.wday);
171         tvt = (TextView)findViewById(R.id.time);
172         tvd = (TextView)findViewById(R.id.date);
173         tvs = (TextView)findViewById(R.id.timestamp);
174         ut = new UpdateTarget[] {
175             new UpdateTarget((TextView)findViewById(R.id.location),  1),
176             new UpdateTarget((TextView)findViewById(R.id.phonecall), 2)
177         };
178         Log.w("WhereAmI", "created UI, about to start update task");
179         mHandler.post(updateClock);
180         mHandler.post(updateCal);
181         mHandler.post(updateInfo);
182         Log.w("WhereAmI", "created UI, update task created");
183     }
184
185     /** Called when put to background */
186     @Override
187     public void onPause()
188     {
189         super.onPause();
190         Log.w("WhereAmI", "calling finish");
191         finish();
192     }
193
194     /** Called when the activity is destroyed. */
195     @Override
196     public void onDestroy()
197     {
198         super.onDestroy();
199         Log.w("WhereAmI", "going down");
200         mHandler.removeCallbacks(updateClock);
201         mHandler.removeCallbacks(updateCal);
202         mHandler.removeCallbacks(updateInfo);
203         //unregisterReceiver(connChanged);
204         boolean wifion = wifiman.setWifiEnabled(false);
205         Log.w("WhereAmI", "disabling wifi result " + wifion);
206     }
207 }