1 package org.average.whereami;
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.Menu;
20 import android.view.MenuInflater;
21 import android.view.MenuItem;
22 import android.view.Window;
23 import android.view.WindowManager;
24 import android.widget.TextView;
26 public class WhereAmI extends Activity
28 private WifiManager wifiman;
29 private Integer runningtasks = 0;
31 private class UpdateTarget {
33 private Integer updater; // will be the function/object
34 private BgUpdate task;
36 private class BgUpdate extends AsyncTask<Void, Void, String> {
38 protected String doInBackground(Void... params) {
39 Log.w("WhereAmI", "BgUpdate " + updater + " starting");
40 SystemClock.sleep(5000); // real job do be done here
41 Log.w("WhereAmI", "BgUpdate about to return");
42 return "5 seconds passed in " + updater;
46 protected void onPostExecute(String str) {
47 Log.w("WhereAmI", "BgUpdate callback executing");
50 if (runningtasks <= 0) {
51 boolean wifion = wifiman.setWifiEnabled(false);
52 Log.w("WhereAmI", "disabling wifi result " + wifion);
55 tvs.setText(R.string.lasttry);
56 tvs.append(tm.format(" %d/%m/%Y %H:%M:%S"));
61 protected void onCancelled() {
62 Log.w("WhereAmI", "BgUpdate callback cancelled");
67 public UpdateTarget(TextView tv, Integer updater) {
69 this.updater = updater;
72 public void launch() {
73 tv.setText(R.string.updating);
74 task = new BgUpdate();
78 public void cancel() {
82 private UpdateTarget[] ut;
84 private TextView tvt, tvd, tvs;
85 private Resources res;
86 private String[] month;
87 private String[] wday;
89 private Handler mHandler = new Handler();
91 private Runnable updateClock = new Runnable () {
93 long now = System.currentTimeMillis();
96 tvt.setText(tm.format("%H:%M"));
99 long next = tm.toMillis(false);
100 mHandler.postDelayed(this, next-now+1);
104 private Runnable updateCal = new Runnable () {
106 long now = System.currentTimeMillis();
107 Time tm = new Time();
114 tm.set(now + 86400000);
118 long next = tm.toMillis(false);
119 mHandler.postDelayed(this, next-now+1);
123 private Runnable updateInfo = new Runnable () {
125 Log.w("WhereAmI", "updateInfo starting");
126 IntentFilter intentFilter =
127 new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE");
128 registerReceiver(connChanged,intentFilter);
129 connChangedRegistered = true;
130 boolean wifion = wifiman.setWifiEnabled(true);
131 Log.w("WhereAmI", "enabling wifi result " + wifion);
132 mHandler.postDelayed(resetInfo, 30000);
133 mHandler.postDelayed(this, 60000);
137 private Runnable resetInfo = new Runnable () {
139 Log.w("WhereAmI", "resetInfo starting");
140 if (connChangedRegistered) {
141 unregisterReceiver(connChanged);
142 connChangedRegistered = false;
144 if (runningtasks > 0) {
145 for (int i = 0; i < ut.length; i++) {
148 Time tm = new Time();
150 tvs.setText(R.string.failtry);
151 tvs.append(tm.format(" %d/%m/%Y %H:%M:%S"));
153 boolean wifion = wifiman.setWifiEnabled(false);
154 Log.w("WhereAmI", "disabling wifi result " + wifion);
158 private boolean connChangedRegistered = false;
159 private final BroadcastReceiver connChanged = new BroadcastReceiver() {
161 public void onReceive(Context context, Intent intent) {
162 ConnectivityManager cm = (ConnectivityManager)context.
163 getSystemService(Context.CONNECTIVITY_SERVICE);
164 NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
165 boolean isConnected = activeNetwork != null &&
166 activeNetwork.isConnectedOrConnecting();
167 Log.w("WhereAmI", "Connectivity changed to " + isConnected);
169 for (int i = 0; i < ut.length; i++) {
177 /** Called when the activity is first created. */
179 public void onCreate(Bundle savedInstanceState)
181 super.onCreate(savedInstanceState);
182 wifiman = (WifiManager)getSystemService(Context.WIFI_SERVICE);
183 requestWindowFeature(Window.FEATURE_NO_TITLE);
184 getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
185 //getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
186 //getWindow().clearFlags(WindowManager.LayoutParams.
187 // FLAG_FORCE_NOT_FULLSCREEN);
188 setContentView(R.layout.main);
189 res = getResources();
190 month = res.getStringArray(R.array.month);
191 wday = res.getStringArray(R.array.wday);
192 tvt = (TextView)findViewById(R.id.time);
193 tvd = (TextView)findViewById(R.id.date);
194 tvs = (TextView)findViewById(R.id.timestamp);
195 ut = new UpdateTarget[] {
196 new UpdateTarget((TextView)findViewById(R.id.location), 1),
197 new UpdateTarget((TextView)findViewById(R.id.phonecall), 2)
199 Log.w("WhereAmI", "created UI, about to start update task");
200 mHandler.post(updateClock);
201 mHandler.post(updateCal);
202 mHandler.post(updateInfo);
203 Log.w("WhereAmI", "created UI, update task created");
206 /** Called when put to background */
208 public void onPause()
211 Log.w("WhereAmI", "going background");
214 /** Called when the activity is destroyed. */
216 public void onDestroy()
219 Log.w("WhereAmI", "going down");
220 mHandler.removeCallbacks(updateClock);
221 mHandler.removeCallbacks(updateCal);
222 mHandler.removeCallbacks(updateInfo);
223 if (connChangedRegistered) {
224 unregisterReceiver(connChanged);
225 connChangedRegistered = false;
227 boolean wifion = wifiman.setWifiEnabled(false);
228 Log.w("WhereAmI", "disabling wifi result " + wifion);
231 /** Called when the menu is activated. */
233 public boolean onCreateOptionsMenu(Menu menu) {
234 MenuInflater inflater = getMenuInflater();
235 inflater.inflate(R.menu.main_menu, menu);
239 /** Called when the menu item is selected */
241 public boolean onOptionsItemSelected(MenuItem item) {
242 switch (item.getItemId()) {
247 Log.w("WhereAmI", "settings requested");
250 return super.onOptionsItemSelected(item);