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.LastLocation;
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;
41 private CredentialStore store;
43 private class UpdateTarget {
45 private Oracle updater;
46 private BgUpdate task;
48 private class BgUpdate extends AsyncTask<Void, Void, String> {
50 protected String doInBackground(Void... params) {
51 Log.w(TAG, "BgUpdate " + updater + " starting");
52 return updater.getResult();
56 protected void onPostExecute(String str) {
57 Log.w(TAG, "BgUpdate callback executing");
60 if (runningtasks <= 0) {
61 boolean wifion = wifiman.setWifiEnabled(false);
62 Log.w(TAG, "disabling wifi result " + wifion);
65 tvs.setText(R.string.lasttry);
66 tvs.append(tm.format(" %d/%m/%Y %H:%M:%S"));
71 protected void onCancelled() {
72 Log.w(TAG, "BgUpdate callback cancelled");
77 public UpdateTarget(TextView tv, Oracle updater) {
79 this.updater = updater;
82 public void launch() {
83 tv.setText(R.string.updating);
84 task = new BgUpdate();
88 public void cancel() {
92 private UpdateTarget[] ut;
94 private TextView tvt, tvd, tvs;
95 private Resources res;
96 private String[] month;
97 private String[] wday;
99 private Handler mHandler = new Handler();
101 private Runnable updateClock = new Runnable () {
103 long now = System.currentTimeMillis();
104 Time tm = new Time();
106 tvt.setText(tm.format("%H:%M"));
109 long next = tm.toMillis(false);
110 mHandler.postDelayed(this, next-now+1);
114 private Runnable updateCal = new Runnable () {
116 long now = System.currentTimeMillis();
117 Time tm = new Time();
124 tm.set(now + 86400000);
128 long next = tm.toMillis(false);
129 mHandler.postDelayed(this, next-now+1);
133 private Runnable updateInfo = new Runnable () {
135 Log.w(TAG, "updateInfo starting");
137 IntentFilter intentFilter =
138 new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE");
139 registerReceiver(connChanged,intentFilter);
140 connChangedRegistered = true;
141 boolean wifion = wifiman.setWifiEnabled(true);
142 Log.w(TAG, "enabling wifi result " + wifion);
144 for (int i = 0; i < ut.length; i++) {
149 mHandler.postDelayed(resetInfo, updatedelay);
150 mHandler.postDelayed(this, 60000);
154 private Runnable resetInfo = new Runnable () {
156 Log.w(TAG, "resetInfo starting");
157 if (connChangedRegistered) {
158 unregisterReceiver(connChanged);
159 connChangedRegistered = false;
161 if (runningtasks > 0) {
162 for (int i = 0; i < ut.length; i++) {
165 Time tm = new Time();
167 tvs.setText(R.string.failtry);
168 tvs.append(tm.format(" %d/%m/%Y %H:%M:%S"));
171 boolean wifion = wifiman.setWifiEnabled(false);
172 Log.w(TAG, "disabling wifi result " + wifion);
177 private boolean connChangedRegistered = false;
178 private final BroadcastReceiver connChanged = new BroadcastReceiver() {
180 public void onReceive(Context context, Intent intent) {
181 ConnectivityManager cm = (ConnectivityManager)context.
182 getSystemService(Context.CONNECTIVITY_SERVICE);
183 NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
184 boolean isConnected = activeNetwork != null &&
185 activeNetwork.isConnectedOrConnecting();
186 Log.w(TAG, "Connectivity changed to " + isConnected);
188 for (int i = 0; i < ut.length; i++) {
196 /** Called when the activity is first created. */
198 public void onCreate(Bundle savedInstanceState) {
199 super.onCreate(savedInstanceState);
200 prefs = PreferenceManager.getDefaultSharedPreferences(this);
201 store = new CredentialStore(prefs);
202 wifiman = (WifiManager)getSystemService(Context.WIFI_SERVICE);
203 requestWindowFeature(Window.FEATURE_NO_TITLE);
204 getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
205 setContentView(R.layout.main);
206 res = getResources();
207 month = res.getStringArray(R.array.month);
208 wday = res.getStringArray(R.array.wday);
209 tvt = (TextView)findViewById(R.id.time);
210 tvd = (TextView)findViewById(R.id.date);
211 tvs = (TextView)findViewById(R.id.timestamp);
212 ut = new UpdateTarget[] {
213 new UpdateTarget((TextView)findViewById(R.id.location),
214 new PhoneLog(store)),
215 new UpdateTarget((TextView)findViewById(R.id.phonecall),
216 new LastLocation(store))
218 Log.w(TAG, "created UI, about to start update task");
219 mHandler.post(updateClock);
220 mHandler.post(updateCal);
221 mHandler.post(updateInfo);
222 Log.w(TAG, "created UI, update task created");
225 /** Called when reactivated */
227 public void onResume() {
229 boolean fullscreen = prefs.getBoolean("fullscreen", false);
230 managewifi = prefs.getBoolean("managewifi", false);
231 updatedelay = Long.parseLong(prefs.getString("updateperiod", "1200000"));
233 getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
234 getWindow().clearFlags(WindowManager.LayoutParams.
235 FLAG_FORCE_NOT_FULLSCREEN);
237 getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
241 /** Called when put to background */
243 public void onPause() {
245 Log.w(TAG, "going background");
248 /** Called when the activity is destroyed. */
250 public void onDestroy() {
252 Log.w(TAG, "going down");
253 mHandler.removeCallbacks(updateClock);
254 mHandler.removeCallbacks(updateCal);
255 mHandler.removeCallbacks(updateInfo);
256 if (connChangedRegistered) {
257 unregisterReceiver(connChanged);
258 connChangedRegistered = false;
260 boolean wifion = wifiman.setWifiEnabled(false);
261 Log.w(TAG, "disabling wifi result " + wifion);
264 /** Called when the menu is activated. */
266 public boolean onCreateOptionsMenu(Menu menu) {
267 MenuInflater inflater = getMenuInflater();
268 inflater.inflate(R.menu.main_menu, menu);
272 /** Called when the menu item is selected */
274 public boolean onOptionsItemSelected(MenuItem item) {
275 switch (item.getItemId()) {
280 Log.w(TAG, "authorize requested");
281 startActivity(new Intent(this, Authorize.class));
284 Log.w(TAG, "settings requested");
285 startActivity(new Intent(this, WhereAmIprefs.class));
288 return super.onOptionsItemSelected(item);