1 package org.average.whereami;
3 import org.average.whereami.WhereAmIprefs;
4 import org.average.whereami.Oracle;
5 import org.average.whereami.PhoneLog;
6 import org.average.whereami.Location;
8 import android.app.Activity;
9 import android.net.wifi.WifiManager;
10 import android.os.Bundle;
11 import android.os.AsyncTask;
12 import android.os.Handler;
13 import android.net.ConnectivityManager;
14 import android.net.NetworkInfo;
15 import android.content.BroadcastReceiver;
16 import android.content.Intent;
17 import android.content.IntentFilter;
18 import android.content.Context;
19 import android.content.res.Resources;
20 import android.content.SharedPreferences;
21 import android.preference.PreferenceManager;
22 import android.text.format.Time;
23 import android.util.Log;
24 import android.view.View;
25 import android.view.Menu;
26 import android.view.MenuInflater;
27 import android.view.MenuItem;
28 import android.view.Window;
29 import android.view.WindowManager;
30 import android.widget.TextView;
32 public class WhereAmI extends Activity
34 final String TAG = getClass().getName();
36 private WifiManager wifiman;
37 private Boolean managewifi = false;
38 private Long updatedelay = 60000L;
39 private Integer runningtasks = 0;
40 private SharedPreferences prefs;
42 private class UpdateTarget {
44 private Oracle updater;
45 private BgUpdate task;
47 private class BgUpdate extends AsyncTask<Void, Void, String> {
49 protected String doInBackground(Void... params) {
50 Log.w(TAG, "BgUpdate " + updater + " starting");
51 return updater.getResult();
55 protected void onPostExecute(String str) {
56 Log.w(TAG, "BgUpdate callback executing");
59 if (runningtasks <= 0) {
60 boolean wifion = wifiman.setWifiEnabled(false);
61 Log.w(TAG, "disabling wifi result " + wifion);
64 tvs.setText(R.string.lasttry);
65 tvs.append(tm.format(" %d/%m/%Y %H:%M:%S"));
70 protected void onCancelled() {
71 Log.w(TAG, "BgUpdate callback cancelled");
76 public UpdateTarget(TextView tv, Oracle updater) {
78 this.updater = updater;
81 public void launch() {
82 tv.setText(R.string.updating);
83 task = new BgUpdate();
87 public void cancel() {
91 private UpdateTarget[] ut;
93 private TextView tvt, tvd, tvs;
94 private Resources res;
95 private String[] month;
96 private String[] wday;
98 private Handler mHandler = new Handler();
100 private Runnable updateClock = new Runnable () {
102 long now = System.currentTimeMillis();
103 Time tm = new Time();
105 tvt.setText(tm.format("%H:%M"));
108 long next = tm.toMillis(false);
109 mHandler.postDelayed(this, next-now+1);
113 private Runnable updateCal = new Runnable () {
115 long now = System.currentTimeMillis();
116 Time tm = new Time();
123 tm.set(now + 86400000);
127 long next = tm.toMillis(false);
128 mHandler.postDelayed(this, next-now+1);
132 private Runnable updateInfo = new Runnable () {
134 Log.w(TAG, "updateInfo starting");
136 IntentFilter intentFilter =
137 new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE");
138 registerReceiver(connChanged,intentFilter);
139 connChangedRegistered = true;
140 boolean wifion = wifiman.setWifiEnabled(true);
141 Log.w(TAG, "enabling wifi result " + wifion);
143 for (int i = 0; i < ut.length; i++) {
148 mHandler.postDelayed(resetInfo, 50000);
149 mHandler.postDelayed(this, updatedelay);
153 private Runnable resetInfo = new Runnable () {
155 Log.w(TAG, "resetInfo starting");
156 if (connChangedRegistered) {
157 unregisterReceiver(connChanged);
158 connChangedRegistered = false;
160 if (runningtasks > 0) {
161 for (int i = 0; i < ut.length; i++) {
164 Time tm = new Time();
166 tvs.setText(R.string.failtry);
167 tvs.append(tm.format(" %d/%m/%Y %H:%M:%S"));
170 boolean wifion = wifiman.setWifiEnabled(false);
171 Log.w(TAG, "disabling wifi result " + wifion);
176 private boolean connChangedRegistered = false;
177 private final BroadcastReceiver connChanged = new BroadcastReceiver() {
179 public void onReceive(Context context, Intent intent) {
180 ConnectivityManager cm = (ConnectivityManager)context.
181 getSystemService(Context.CONNECTIVITY_SERVICE);
182 NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
183 boolean isConnected = activeNetwork != null &&
184 activeNetwork.isConnectedOrConnecting();
185 Log.w(TAG, "Connectivity changed to " + isConnected);
187 for (int i = 0; i < ut.length; i++) {
195 /** Called when the activity is first created. */
197 public void onCreate(Bundle savedInstanceState) {
198 super.onCreate(savedInstanceState);
199 wifiman = (WifiManager)getSystemService(Context.WIFI_SERVICE);
200 requestWindowFeature(Window.FEATURE_NO_TITLE);
201 getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
202 setContentView(R.layout.main);
203 res = getResources();
204 month = res.getStringArray(R.array.month);
205 wday = res.getStringArray(R.array.wday);
206 tvt = (TextView)findViewById(R.id.time);
207 tvd = (TextView)findViewById(R.id.date);
208 tvs = (TextView)findViewById(R.id.timestamp);
209 ut = new UpdateTarget[] {
210 new UpdateTarget((TextView)findViewById(R.id.location),
212 new UpdateTarget((TextView)findViewById(R.id.phonecall),
215 Log.w(TAG, "created UI, about to start update task");
216 mHandler.post(updateClock);
217 mHandler.post(updateCal);
218 mHandler.post(updateInfo);
219 Log.w(TAG, "created UI, update task created");
222 /** Called when reactivated */
224 public void onResume() {
226 prefs = PreferenceManager.getDefaultSharedPreferences(this);
227 boolean fullscreen = prefs.getBoolean("fullscreen", false);
228 managewifi = prefs.getBoolean("managewifi", false);
229 updatedelay = Long.parseLong(prefs.getString("updateperiod", "1200000"));
230 Log.w("WhereAmI", "fullscreen: " + fullscreen +
231 ", managewifi: " + managewifi +
232 ", updatedelay: " + updatedelay);
234 getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
235 getWindow().clearFlags(WindowManager.LayoutParams.
236 FLAG_FORCE_NOT_FULLSCREEN);
238 getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
242 /** Called when put to background */
244 public void onPause() {
246 Log.w(TAG, "going background");
249 /** Called when the activity is destroyed. */
251 public void onDestroy() {
253 Log.w(TAG, "going down");
254 mHandler.removeCallbacks(updateClock);
255 mHandler.removeCallbacks(updateCal);
256 mHandler.removeCallbacks(updateInfo);
257 if (connChangedRegistered) {
258 unregisterReceiver(connChanged);
259 connChangedRegistered = false;
261 boolean wifion = wifiman.setWifiEnabled(false);
262 Log.w(TAG, "disabling wifi result " + wifion);
265 /** Called when the menu is activated. */
267 public boolean onCreateOptionsMenu(Menu menu) {
268 MenuInflater inflater = getMenuInflater();
269 inflater.inflate(R.menu.main_menu, menu);
273 /** Called when the menu item is selected */
275 public boolean onOptionsItemSelected(MenuItem item) {
276 switch (item.getItemId()) {
281 Log.w(TAG, "authorize requested");
282 startActivity(new Intent(this, Authorize.class));
285 Log.w(TAG, "settings requested");
286 startActivity(new Intent(this, WhereAmIprefs.class));
289 return super.onOptionsItemSelected(item);