Skip to content

Commit 032aa18

Browse files
committed
wakeup service alarm corrected; misc debugging
1 parent 9ebd180 commit 032aa18

File tree

4 files changed

+122
-170
lines changed

4 files changed

+122
-170
lines changed

src/edu/fsu/cs/contextprovider/ContextExpandableListActivity.java

Lines changed: 67 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,11 @@
4141
import org.xml.sax.SAXException;
4242
import org.xml.sax.XMLReader;
4343

44+
import android.app.AlarmManager;
4445
import android.app.AlertDialog;
4546
import android.app.Dialog;
4647
import android.app.ExpandableListActivity;
48+
import android.app.PendingIntent;
4749
import android.content.ComponentName;
4850
import android.content.Context;
4951
import android.content.Intent;
@@ -58,6 +60,7 @@
5860
import android.os.Handler;
5961
import android.os.IBinder;
6062
import android.os.RemoteException;
63+
import android.os.SystemClock;
6164
import android.preference.PreferenceManager;
6265
import android.speech.tts.TextToSpeech;
6366
import android.text.ClipboardManager;
@@ -87,15 +90,14 @@
8790
import edu.fsu.cs.contextprovider.sensor.AccelerometerService;
8891
import edu.fsu.cs.contextprovider.sensor.LightService;
8992

90-
public class ContextExpandableListActivity extends ExpandableListActivity
91-
implements OnChildClickListener, TextToSpeech.OnInitListener {
93+
public class ContextExpandableListActivity extends ExpandableListActivity implements OnChildClickListener, TextToSpeech.OnInitListener {
9294
private static final String TAG = "ContextExpandableListActivity";
9395

9496
private static final String NAME = "NAME";
9597
private static final String VALUE = "VALUE";
9698

9799
private ExpandableListAdapter mAdapter;
98-
private static Timer timer = new Timer();
100+
private static Timer timer = new Timer();
99101

100102
// private static final int MENU_ADD_ID = Menu.FIRST + 2;
101103
// private static final int MENU_SHARE_ID = Menu.FIRST + 3;
@@ -139,6 +141,7 @@ public class ContextExpandableListActivity extends ExpandableListActivity
139141
// debug
140142
private boolean ttsEnabled;
141143
private boolean shakeEnabled;
144+
SharedPreferences prefs;
142145

143146
public static boolean running = false;
144147
public static TextToSpeech tts;
@@ -170,24 +173,36 @@ public void onServiceDisconnected(ComponentName name) {
170173
public void onCreate(Bundle savedInstanceState) {
171174
super.onCreate(savedInstanceState);
172175

173-
getPrefs();
176+
prefs = getSharedPreferences(ContextConstants.CONTEXT_PREFS, MODE_WORLD_READABLE);
177+
178+
if (!prefs.contains(ContextConstants.PREFS_FIRST_RUN)) {
179+
180+
SharedPreferences.Editor prefsEditor = prefs.edit();
181+
prefsEditor.putBoolean(ContextConstants.PREFS_FIRST_RUN, false);
182+
prefsEditor.commit();
183+
184+
// startup the primary context service (if just installed)
185+
startService(new Intent(this, ContextService.class));
186+
187+
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
188+
Intent intent = new Intent(getBaseContext(), edu.fsu.cs.contextprovider.wakeup.WakeupAlarmReceiver.class);
189+
PendingIntent pi = PendingIntent.getBroadcast(getBaseContext(), 0, intent, 0);
190+
manager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 10000, accuracyPopupPeriod * 1000, pi);
191+
192+
}
174193

175-
// startup the primary context service (if just installed)
176-
startService(new Intent(this, ContextService.class));
194+
getPrefs();
177195

178196
tts = new TextToSpeech(this, this);
179197

180198
running = true;
181199

182-
Intent intent = null;
183-
184200
if (clip == null) {
185201
clip = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
186202
}
187203

188204
/* Start ContextProviderService */
189-
bindService(new Intent(this, ContextProviderService.class), conn,
190-
Context.BIND_AUTO_CREATE);
205+
bindService(new Intent(this, ContextProviderService.class), conn, Context.BIND_AUTO_CREATE);
191206

192207
if (locationEnabled) {
193208
refreshLocation();
@@ -212,25 +227,19 @@ public void onCreate(Bundle savedInstanceState) {
212227
refreshDerived();
213228
}
214229

215-
mAdapter = new SimpleExpandableListAdapter(this, groupData,
216-
android.R.layout.simple_expandable_list_item_1, new String[] {
217-
NAME, VALUE }, new int[] { android.R.id.text1,
218-
android.R.id.text2 }, childData,
219-
android.R.layout.simple_expandable_list_item_2, new String[] {
220-
NAME, VALUE }, new int[] { android.R.id.text1,
221-
android.R.id.text2 });
230+
mAdapter = new SimpleExpandableListAdapter(this, groupData, android.R.layout.simple_expandable_list_item_1, new String[] { NAME, VALUE }, new int[] {
231+
android.R.id.text1, android.R.id.text2 }, childData, android.R.layout.simple_expandable_list_item_2, new String[] { NAME, VALUE }, new int[] {
232+
android.R.id.text1, android.R.id.text2 });
222233

223234
setListAdapter(mAdapter);
224235

225-
226-
227-
Handler handler = new Handler();
228-
handler.postDelayed(new Runnable() {
229-
public void run() {
230-
refresh();
231-
}
232-
}, 1000);
233-
236+
Handler handler = new Handler();
237+
handler.postDelayed(new Runnable() {
238+
public void run() {
239+
refresh();
240+
}
241+
}, 1000);
242+
234243
}
235244

236245
public void onInit(int status) {
@@ -239,16 +248,14 @@ public void onInit(int status) {
239248
tts.setLanguage(loc);
240249
}
241250
if (ttsEnabled)
242-
tts.speak("Text to Speach Initialized", TextToSpeech.QUEUE_FLUSH,
243-
null);
251+
tts.speak("Text to Speach Initialized", TextToSpeech.QUEUE_FLUSH, null);
244252
}
245253

246254
private void getPrefs() {
247255

248256
// SharedPreferences prefs =
249257
// PreferenceManager.getDefaultSharedPreferences(this);
250-
SharedPreferences prefs = getSharedPreferences(
251-
ContextConstants.CONTEXT_PREFS, MODE_WORLD_READABLE);
258+
prefs = getSharedPreferences(ContextConstants.CONTEXT_PREFS, MODE_WORLD_READABLE);
252259

253260
// general
254261
// startupEnabled = prefs.getBoolean("PREFS_STARTUP_ENABLED", true);
@@ -356,13 +363,11 @@ private void refreshMovement() {
356363
curChildMap = new HashMap<String, String>();
357364
movement.add(curChildMap);
358365
curChildMap.put(NAME, ContextConstants.MOVEMENT_STEP_COUNT);
359-
curChildMap.put(VALUE, String.valueOf(AccelerometerService
360-
.getStepCount()));
366+
curChildMap.put(VALUE, String.valueOf(AccelerometerService.getStepCount()));
361367
curChildMap = new HashMap<String, String>();
362368
movement.add(curChildMap);
363369
curChildMap.put(NAME, ContextConstants.MOVEMENT_LAST_STEP);
364-
curChildMap.put(VALUE, String.valueOf(AccelerometerService
365-
.getStepTimestamp()));
370+
curChildMap.put(VALUE, String.valueOf(AccelerometerService.getStepTimestamp()));
366371

367372
childData.add(movement);
368373
}
@@ -377,8 +382,7 @@ private void refreshProximity() {
377382
proximityMap.put(VALUE, "Proximity");
378383
List<Map<String, String>> proximity = new ArrayList<Map<String, String>>();
379384

380-
SharedPreferences pref = getSharedPreferences(
381-
ContextConstants.PREFS_ADDRESS, 0);
385+
SharedPreferences pref = getSharedPreferences(ContextConstants.PREFS_ADDRESS, 0);
382386
Map<String, String> list = (Map<String, String>) pref.getAll();
383387
for (Map.Entry<String, String> entry : list.entrySet()) {
384388
Map<String, String> curChildMap = new HashMap<String, String>();
@@ -484,18 +488,15 @@ private void refreshSystem() {
484488
curChildMap = new HashMap<String, String>();
485489
system.add(curChildMap);
486490
curChildMap.put(NAME, ContextConstants.SYSTEM_PLUGGED);
487-
curChildMap
488-
.put(VALUE, String.valueOf(SystemMonitor.isBatteryPlugged()));
491+
curChildMap.put(VALUE, String.valueOf(SystemMonitor.isBatteryPlugged()));
489492
curChildMap = new HashMap<String, String>();
490493
system.add(curChildMap);
491494
curChildMap.put(NAME, ContextConstants.SYSTEM_LAST_PLUGGED);
492-
curChildMap.put(VALUE, String.valueOf(SystemMonitor
493-
.getBatteryLastPlugged()));
495+
curChildMap.put(VALUE, String.valueOf(SystemMonitor.getBatteryLastPlugged()));
494496
curChildMap = new HashMap<String, String>();
495497
system.add(curChildMap);
496498
curChildMap.put(NAME, ContextConstants.SYSTEM_LAST_PRESENT);
497-
curChildMap.put(VALUE, String.valueOf(SystemMonitor
498-
.getUserLastPresent()));
499+
curChildMap.put(VALUE, String.valueOf(SystemMonitor.getUserLastPresent()));
499500
curChildMap = new HashMap<String, String>();
500501
system.add(curChildMap);
501502
curChildMap.put(NAME, ContextConstants.SYSTEM_WIFI_SSID);
@@ -553,20 +554,13 @@ public boolean onCreateOptionsMenu(Menu menu) {
553554

554555
// menu.add(Menu.NONE, MENU_ADD_ID, Menu.NONE,
555556
// "Add").setIcon(android.R.drawable.ic_menu_add).setAlphabeticShortcut('a');
556-
menu.add(Menu.NONE, MENU_SET_HOME_ID, Menu.NONE, "Set Home").setIcon(
557-
android.R.drawable.ic_menu_compass).setAlphabeticShortcut('h');
558-
menu.add(Menu.NONE, MENU_SET_WORK_ID, Menu.NONE, "Set Work").setIcon(
559-
android.R.drawable.ic_menu_compass).setAlphabeticShortcut('w');
560-
menu.add(Menu.NONE, MENU_REFRESH_ID, Menu.NONE, "Refresh").setIcon(
561-
android.R.drawable.ic_menu_rotate).setAlphabeticShortcut('r');
557+
menu.add(Menu.NONE, MENU_SET_HOME_ID, Menu.NONE, "Set Home").setIcon(android.R.drawable.ic_menu_compass).setAlphabeticShortcut('h');
558+
menu.add(Menu.NONE, MENU_SET_WORK_ID, Menu.NONE, "Set Work").setIcon(android.R.drawable.ic_menu_compass).setAlphabeticShortcut('w');
559+
menu.add(Menu.NONE, MENU_REFRESH_ID, Menu.NONE, "Refresh").setIcon(android.R.drawable.ic_menu_rotate).setAlphabeticShortcut('r');
562560
// menu.add(Menu.NONE, MENU_SHARE_ID, Menu.NONE,
563561
// "Share").setIcon(android.R.drawable.ic_menu_share).setAlphabeticShortcut('s');
564-
menu.add(Menu.NONE, MENU_PREFS_ID, Menu.NONE, "Prefs").setIcon(
565-
android.R.drawable.ic_menu_preferences).setAlphabeticShortcut(
566-
'p');
567-
menu.add(Menu.NONE, MENU_ABOUT_ID, Menu.NONE, "About").setIcon(
568-
android.R.drawable.ic_menu_info_details).setAlphabeticShortcut(
569-
'i');
562+
menu.add(Menu.NONE, MENU_PREFS_ID, Menu.NONE, "Prefs").setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('p');
563+
menu.add(Menu.NONE, MENU_ABOUT_ID, Menu.NONE, "About").setIcon(android.R.drawable.ic_menu_info_details).setAlphabeticShortcut('i');
570564

571565
return (super.onCreateOptionsMenu(menu));
572566
}
@@ -579,18 +573,14 @@ public boolean onOptionsItemSelected(MenuItem item) {
579573
refresh();
580574
return true;
581575
case MENU_SET_HOME_ID:
582-
intent = new Intent(getApplicationContext(),
583-
edu.fsu.cs.contextprovider.map.AddPlaceMapActivity.class);
584-
intent.putExtra(ContextConstants.PLACE_REQUEST_ID,
585-
ContextConstants.SET_HOME_REQUEST);
576+
intent = new Intent(getApplicationContext(), edu.fsu.cs.contextprovider.map.AddPlaceMapActivity.class);
577+
intent.putExtra(ContextConstants.PLACE_REQUEST_ID, ContextConstants.SET_HOME_REQUEST);
586578
startActivityForResult(intent, ContextConstants.SET_HOME_REQUEST);
587579
// refresh();
588580
return true;
589581
case MENU_SET_WORK_ID:
590-
intent = new Intent(getApplicationContext(),
591-
edu.fsu.cs.contextprovider.map.AddPlaceMapActivity.class);
592-
intent.putExtra(ContextConstants.PLACE_REQUEST_ID,
593-
ContextConstants.SET_WORK_REQUEST);
582+
intent = new Intent(getApplicationContext(), edu.fsu.cs.contextprovider.map.AddPlaceMapActivity.class);
583+
intent.putExtra(ContextConstants.PLACE_REQUEST_ID, ContextConstants.SET_WORK_REQUEST);
594584
startActivityForResult(intent, ContextConstants.SET_WORK_REQUEST);
595585

596586
// startActivityForResult(new Intent(getApplicationContext(),
@@ -649,18 +639,15 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
649639
}
650640

651641
@Override
652-
public void onCreateContextMenu(ContextMenu menu, View v,
653-
ContextMenu.ContextMenuInfo menuInfo) {
654-
menu.add(Menu.NONE, CONTEXT_DELETE_ID, Menu.NONE, "Delete").setIcon(
655-
android.R.drawable.ic_menu_delete).setAlphabeticShortcut('d');
642+
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
643+
menu.add(Menu.NONE, CONTEXT_DELETE_ID, Menu.NONE, "Delete").setIcon(android.R.drawable.ic_menu_delete).setAlphabeticShortcut('d');
656644
}
657645

658646
@Override
659647
public boolean onContextItemSelected(MenuItem item) {
660648
switch (item.getItemId()) {
661649
case CONTEXT_DELETE_ID:
662-
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item
663-
.getMenuInfo();
650+
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
664651
return true;
665652

666653
}
@@ -694,8 +681,7 @@ public String toString() {
694681
}
695682

696683
@Override
697-
public boolean onChildClick(ExpandableListView parent, View v,
698-
int groupPosition, int childPosition, long id) {
684+
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
699685
String copiedString = null;
700686
Context context = getApplicationContext();
701687
List<Map<String, String>> category = childData.get(groupPosition);
@@ -713,11 +699,9 @@ public boolean onChildClick(ExpandableListView parent, View v,
713699
copiedString = name;
714700
}
715701

716-
Toast.makeText(context, "Copied: [" + copiedString + "]",
717-
Toast.LENGTH_SHORT).show();
702+
Toast.makeText(context, "Copied: [" + copiedString + "]", Toast.LENGTH_SHORT).show();
718703
clip.setText(copiedString);
719-
Log.i("LIST", "Name [" + name + "] | Value [" + value + "] | id [" + id
720-
+ "]");
704+
Log.i("LIST", "Name [" + name + "] | Value [" + value + "] | id [" + id + "]");
721705

722706
return true;
723707
}
@@ -775,22 +759,17 @@ protected Dialog onCreateDialog(int id) {
775759
}
776760

777761
private AlertDialog getAboutBox() {
778-
String title = getString(R.string.app_name) + " build "
779-
+ getVersion(this);
762+
String title = getString(R.string.app_name) + " build " + getVersion(this);
780763

781-
return new AlertDialog.Builder(ContextExpandableListActivity.this)
782-
.setTitle(title).setView(
783-
View.inflate(this, R.layout.about, null)).setIcon(
784-
R.drawable.context64).setPositiveButton("OK", null)
785-
.create();
764+
return new AlertDialog.Builder(ContextExpandableListActivity.this).setTitle(title).setView(View.inflate(this, R.layout.about, null)).setIcon(
765+
R.drawable.context64).setPositiveButton("OK", null).create();
786766

787767
}
788768

789769
public static String getVersion(Context context) {
790770
String version = "1.0";
791771
try {
792-
PackageInfo pi = context.getPackageManager().getPackageInfo(
793-
context.getPackageName(), 0);
772+
PackageInfo pi = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
794773
version = pi.versionName;
795774
} catch (PackageManager.NameNotFoundException e) {
796775
Log.e(TAG, "Package name not found", e);
@@ -799,19 +778,16 @@ public static String getVersion(Context context) {
799778
}
800779

801780
public void exportToFile() throws IOException {
802-
String path = Environment.getExternalStorageDirectory() + "/"
803-
+ CSV_FILENAME;
781+
String path = Environment.getExternalStorageDirectory() + "/" + CSV_FILENAME;
804782

805783
File file = new File(path);
806784
file.createNewFile();
807785

808786
if (!file.isFile()) {
809-
throw new IllegalArgumentException("Should not be a directory: "
810-
+ file);
787+
throw new IllegalArgumentException("Should not be a directory: " + file);
811788
}
812789
if (!file.canWrite()) {
813-
throw new IllegalArgumentException("File cannot be written: "
814-
+ file);
790+
throw new IllegalArgumentException("File cannot be written: " + file);
815791
}
816792

817793
Writer output = new BufferedWriter(new FileWriter(file));
@@ -833,14 +809,12 @@ public void exportToFile() throws IOException {
833809

834810
output.close();
835811

836-
Toast.makeText(this, String.format("Saved", path), Toast.LENGTH_LONG)
837-
.show();
812+
Toast.makeText(this, String.format("Saved", path), Toast.LENGTH_LONG).show();
838813

839814
Intent shareIntent = new Intent(Intent.ACTION_SEND);
840815
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + path));
841816
shareIntent.setType("text/plain");
842-
startActivity(Intent.createChooser(shareIntent,
843-
"Share Context Using..."));
817+
startActivity(Intent.createChooser(shareIntent, "Share Context Using..."));
844818
}
845819

846820
}

0 commit comments

Comments
 (0)