1 package org.average.whereami;
3 import org.average.whereami.WhereAmIprefs;
5 import android.app.Activity;
6 import android.net.wifi.WifiManager;
7 import android.os.Bundle;
8 import android.os.AsyncTask;
9 import android.os.Handler;
10 import android.os.SystemClock;
11 import android.net.ConnectivityManager;
12 import android.net.NetworkInfo;
13 import android.content.BroadcastReceiver;
14 import android.content.Intent;
15 import android.content.IntentFilter;
16 import android.content.Context;
17 import android.content.res.Resources;
18 import android.content.SharedPreferences;
19 import android.text.format.Time;
20 import android.util.Log;
21 import android.view.View;
22 import android.view.Menu;
23 import android.view.MenuInflater;
24 import android.view.MenuItem;
25 import android.view.Window;
26 import android.view.WindowManager;
27 import android.widget.TextView;
29 public class WhereAmI extends Activity
31 private WifiManager wifiman;
32 private Integer runningtasks = 0;
33 private SharedPreferences prefs;
35 private class UpdateTarget {
37 private Integer updater; // will be the function/object
38 private BgUpdate task;
40 private class BgUpdate extends AsyncTask<Void, Void, String> {
42 protected String doInBackground(Void... params) {
43 Log.w("WhereAmI", "BgUpdate " + updater + " starting");
44 SystemClock.sleep(5000); // real job do be done here
45 Log.w("WhereAmI", "BgUpdate about to return");
46 return "5 seconds passed in " + updater;
50 protected void onPostExecute(String str) {
51 Log.w("WhereAmI", "BgUpdate callback executing");
54 if (runningtasks <= 0) {
55 boolean wifion = wifiman.setWifiEnabled(false);
56 Log.w("WhereAmI", "disabling wifi result " + wifion);
59 tvs.setText(R.string.lasttry);
60 tvs.append(tm.format(" %d/%m/%Y %H:%M:%S"));
65 protected void onCancelled() {
66 Log.w("WhereAmI", "BgUpdate callback cancelled");
71 public UpdateTarget(TextView tv, Integer updater) {
73 this.updater = updater;
76 public void launch() {
77 tv.setText(R.string.updating);
78 task = new BgUpdate();
82 public void cancel() {
86 private UpdateTarget[] ut;
88 private TextView tvt, tvd, tvs;
89 private Resources res;
90 private String[] month;
91 private String[] wday;
93 private Handler mHandler = new Handler();
95 private Runnable updateClock = new Runnable () {
97 long now = System.currentTimeMillis();
100 tvt.setText(tm.format("%H:%M"));
103 long next = tm.toMillis(false);
104 mHandler.postDelayed(this, next-now+1);
108 private Runnable updateCal = new Runnable () {
110 long now = System.currentTimeMillis();
111 Time tm = new Time();
118 tm.set(now + 86400000);
122 long next = tm.toMillis(false);
123 mHandler.postDelayed(this, next-now+1);
127 private Runnable updateInfo = new Runnable () {
129 Log.w("WhereAmI", "updateInfo starting");
130 IntentFilter intentFilter =
131 new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE");
132 registerReceiver(connChanged,intentFilter);
133 connChangedRegistered = true;
134 boolean wifion = wifiman.setWifiEnabled(true);
135 Log.w("WhereAmI", "enabling wifi result " + wifion);
136 mHandler.postDelayed(resetInfo, 30000);
137 mHandler.postDelayed(this, 60000);
141 private Runnable resetInfo = new Runnable () {
143 Log.w("WhereAmI", "resetInfo starting");
144 if (connChangedRegistered) {
145 unregisterReceiver(connChanged);
146 connChangedRegistered = false;
148 if (runningtasks > 0) {
149 for (int i = 0; i < ut.length; i++) {
152 Time tm = new Time();
154 tvs.setText(R.string.failtry);
155 tvs.append(tm.format(" %d/%m/%Y %H:%M:%S"));
157 boolean wifion = wifiman.setWifiEnabled(false);
158 Log.w("WhereAmI", "disabling wifi result " + wifion);
162 private boolean connChangedRegistered = false;
163 private final BroadcastReceiver connChanged = new BroadcastReceiver() {
165 public void onReceive(Context context, Intent intent) {
166 ConnectivityManager cm = (ConnectivityManager)context.
167 getSystemService(Context.CONNECTIVITY_SERVICE);
168 NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
169 boolean isConnected = activeNetwork != null &&
170 activeNetwork.isConnectedOrConnecting();
171 Log.w("WhereAmI", "Connectivity changed to " + isConnected);
173 for (int i = 0; i < ut.length; i++) {
181 /** Called when the activity is first created. */
183 public void onCreate(Bundle savedInstanceState)
185 super.onCreate(savedInstanceState);
186 prefs = getSharedPreferences("WhereAmI", MODE_PRIVATE);
187 wifiman = (WifiManager)getSystemService(Context.WIFI_SERVICE);
188 requestWindowFeature(Window.FEATURE_NO_TITLE);
189 getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
190 boolean fullscreen = prefs.getBoolean("fullscreen", false);
192 getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
193 getWindow().clearFlags(WindowManager.LayoutParams.
194 FLAG_FORCE_NOT_FULLSCREEN);
196 setContentView(R.layout.main);
197 res = getResources();
198 month = res.getStringArray(R.array.month);
199 wday = res.getStringArray(R.array.wday);
200 tvt = (TextView)findViewById(R.id.time);
201 tvd = (TextView)findViewById(R.id.date);
202 tvs = (TextView)findViewById(R.id.timestamp);
203 ut = new UpdateTarget[] {
204 new UpdateTarget((TextView)findViewById(R.id.location), 1),
205 new UpdateTarget((TextView)findViewById(R.id.phonecall), 2)
207 Log.w("WhereAmI", "created UI, about to start update task");
208 mHandler.post(updateClock);
209 mHandler.post(updateCal);
210 mHandler.post(updateInfo);
211 Log.w("WhereAmI", "created UI, update task created");
214 /** Called when put to background */
216 public void onPause()
219 Log.w("WhereAmI", "going background");
222 /** Called when the activity is destroyed. */
224 public void onDestroy()
227 Log.w("WhereAmI", "going down");
228 mHandler.removeCallbacks(updateClock);
229 mHandler.removeCallbacks(updateCal);
230 mHandler.removeCallbacks(updateInfo);
231 if (connChangedRegistered) {
232 unregisterReceiver(connChanged);
233 connChangedRegistered = false;
235 boolean wifion = wifiman.setWifiEnabled(false);
236 Log.w("WhereAmI", "disabling wifi result " + wifion);
239 /** Called when the menu is activated. */
241 public boolean onCreateOptionsMenu(Menu menu) {
242 MenuInflater inflater = getMenuInflater();
243 inflater.inflate(R.menu.main_menu, menu);
247 /** Called when the menu item is selected */
249 public boolean onOptionsItemSelected(MenuItem item) {
250 switch (item.getItemId()) {
255 Log.w("WhereAmI", "settings requested");
256 startActivity(new Intent(this, WhereAmIprefs.class));
259 return super.onOptionsItemSelected(item);