-
Notifications
You must be signed in to change notification settings - Fork 0
Hospitalization #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ynnadkrap
wants to merge
13
commits into
master
Choose a base branch
from
danny-feat-hospital-tracker
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
0f89563
Create geofences for nearby hospitals
ynnadkrap 2f1984a
Implement removing geofences, timer, and reboot receiver
ynnadkrap eacb41b
Refactor service logic
ynnadkrap e11f1ae
Clean up
ynnadkrap 1f978a3
Remove test locations
ynnadkrap 7ad252a
Refactor hospitalization logic
ynnadkrap e5d9722
Minor fix
ynnadkrap 2dc1ca7
Minor changes
ynnadkrap cdfe793
Change survey language and other minor changes
ynnadkrap 937a319
Minor fixes
ynnadkrap 7359cfb
Update README.md
ynnadkrap cff447b
Update README.md
ynnadkrap 4df0848
Update README.md
ynnadkrap File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,15 @@ | ||
| # android | ||
| <b>Testing Hospitalization</b> | ||
| <br> | ||
| <br> | ||
| 1. You can add custom 'hospitals' (locations that will trigger the survey) in addHospitalForDebugging() of HospitalizationService.java. There should already be 1 custom hospital, so you can just follow that format to add as many as you'd like. | ||
| <br><br> | ||
| 2. You'll probably want to change the trigger times so you're not sitting around for 5 hours. You'll want to change <b>Constants.GEOFENCE_LOITER_TIME_MILLIS</b> (amount of time to mark user as hospitalized) and <b>Constants.SURVEY_TRIGGER_MILLIS</b> (amount of time to wait after user exits hospital before displaying survey) in Constants.java. | ||
| <br><br> | ||
| 3. Install the app on your device once you've set the above variables. | ||
| 3a. If this is NOT a fresh install (i.e. you previously installed the app and decided to change some variables and re-install), you'll need to press the 'RESTART SERVICE' button after installation. | ||
| <br><br> | ||
| 4. Assuming you've placed a hospital where your current location is, wait for <b>GEOFENCE_LOITER_TIME_MILLIS</b> (I would wait an extra minute or two to account for the geofence setup time). This will mark the user as hospitalized (you will NOT receive any indication of this on your phone, but it will display in the Android Studio logs). | ||
| <br><br> | ||
| 5. To mimic a user exiting a location, I use a third-party application called "Mock Locations" (https://play.google.com/store/apps/details?id=ru.gavrikov.mocklocations&hl=en). You need to enable 'Allow mock locations' in your phone's developer options (Settings -> Developer Options). Follow the app's instructions to mimic leaving the 'hospital'. | ||
| <br><br> | ||
| 6. Wait for <b>SURVEY_TRIGGER_MILLIS</b> and you should receive a notification that upon clicking will take you to the app and display a survey. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| package org.healtheheartstudy; | ||
|
|
||
| import android.app.AlarmManager; | ||
| import android.app.PendingIntent; | ||
| import android.content.Context; | ||
| import android.content.Intent; | ||
| import android.os.SystemClock; | ||
|
|
||
| import java.text.SimpleDateFormat; | ||
| import java.util.Calendar; | ||
|
|
||
| /** | ||
| * AlarmManager wrapper | ||
| */ | ||
| public class AlarmHelper { | ||
|
|
||
| private Context context; | ||
| private Intent intent; | ||
|
|
||
| public AlarmHelper(Context context, String intentAction) { | ||
| this.context = context; | ||
| intent = new Intent(context, CustomBroadcastReceiver.class); | ||
| intent.setAction(intentAction); | ||
| } | ||
|
|
||
| public void putExtra(String key, String value) { | ||
| intent.putExtra(key, value); | ||
| } | ||
|
|
||
| public void set(long triggerAtMillis) { | ||
| PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0); | ||
| AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); | ||
| am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + triggerAtMillis, pi); | ||
| } | ||
|
|
||
| public void setRepeating(long triggerAtMillis, long intervalMillis) { | ||
| PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0); | ||
| AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); | ||
| am.setRepeating( | ||
| AlarmManager.ELAPSED_REALTIME_WAKEUP, | ||
| SystemClock.elapsedRealtime() + triggerAtMillis, | ||
| intervalMillis, | ||
| pi); | ||
| } | ||
|
|
||
| // Returns a date formatted in mm-dd-yyyy (e.g. 05-26-1992) | ||
| public static String getCurrentDate() { | ||
| Calendar c = Calendar.getInstance(); | ||
| SimpleDateFormat df = new SimpleDateFormat("MM-dd-yyyy"); | ||
| return df.format(c.getTime()); | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| package org.healtheheartstudy; | ||
|
|
||
| /** | ||
| * Created by dannypark on 7/1/15. | ||
| */ | ||
| public class Constants { | ||
|
|
||
| public static final String KEY_FIRST_APP_OPEN = "first_app_open"; | ||
| public static final String KEY_SERVICE_ACTION = "service_action"; | ||
| public static final String KEY_HOSPITAL_NAME = "hospital_name"; | ||
| public static final String KEY_HOSPITAL_LAT = "hospital_lat"; | ||
| public static final String KEY_HOSPITAL_LNG = "hospital_lng"; | ||
| public static final String KEY_TRANSITION_TYPE = "transition_type"; | ||
| public static final String KEY_PREV_USER_LAT = "prev_user_lat"; | ||
| public static final String KEY_PREV_USER_LNG = "prev_user_lng"; | ||
| public static final String KEY_DATE = "date"; | ||
| public static final String KEY_PERSISTENT_SURVEY_HOSPITAL = "persistent_hospital_name"; | ||
| public static final String KEY_PERSISTENT_SURVEY_DATE = "persistent_date"; | ||
|
|
||
| public static final String ACTION_CREATE_GEOFENCES = "service_create_geofences"; | ||
| public static final String ACTION_UPDATE_TRANSITION_TYPE = "update_transition_type"; | ||
| public static final String ACTION_SURVEY_ALARM = "survey_alarm"; | ||
| public static final String ACTION_CHECK_LOCATION = "check_location"; | ||
|
|
||
| public static final long ONE_HOUR_MILLIS = 1000 * 60 * 60; | ||
| public static final long ONE_DAY_MILLIS = ONE_HOUR_MILLIS * 24; | ||
| public static final int TWO_MINUTES_MILLIS = 1000 * 60 * 2; | ||
|
|
||
| public static final long SURVEY_TRIGGER_MILLIS = ONE_HOUR_MILLIS; | ||
|
|
||
| public static final int GEOFENCE_RADIUS_METERS = 100; | ||
| public static final int GEOFENCE_LOITER_TIME_MILLIS = 1000 * 60 * 60 * 4; | ||
|
|
||
| } | ||
85 changes: 85 additions & 0 deletions
85
app/src/main/java/org/healtheheartstudy/CustomBroadcastReceiver.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| package org.healtheheartstudy; | ||
|
|
||
| import android.app.PendingIntent; | ||
| import android.content.BroadcastReceiver; | ||
| import android.content.Context; | ||
| import android.content.Intent; | ||
| import android.content.SharedPreferences; | ||
| import android.support.v4.app.NotificationCompat; | ||
| import android.support.v4.app.NotificationManagerCompat; | ||
|
|
||
| import com.securepreferences.SecurePreferences; | ||
|
|
||
| import timber.log.Timber; | ||
|
|
||
| /** | ||
| * CustomBroadcastReceiver handles three events. The first is BOOT_COMPLETED, which is when the | ||
| * device boots up. During this event, we need to recreate all geofences because they don't | ||
| * persist when the device shuts off. The second event is prompted 1-hour after a user | ||
| * leaves a hospital (after dwelling at a hospital for 4 hours). During this event, we need to | ||
| * display a notification that asks the user a few questions about their hospital visit. The | ||
| * third event is prompted by a daily alarm that launches our service to check if we need to | ||
| * find new hospitals because the user moved a significant distance. | ||
| */ | ||
| public class CustomBroadcastReceiver extends BroadcastReceiver { | ||
|
|
||
| @Override | ||
| public void onReceive(Context context, Intent intent) { | ||
| String action = intent.getAction(); | ||
| if (action != null) { | ||
| if (action.equals(Intent.ACTION_BOOT_COMPLETED)) { | ||
| Timber.d("BOOT COMPLETED"); | ||
| // Create geofences | ||
| Intent serviceIntent = new Intent(context, HospitalizationService.class); | ||
| serviceIntent.putExtra(Constants.KEY_SERVICE_ACTION, Constants.ACTION_CREATE_GEOFENCES); | ||
| context.startService(serviceIntent); | ||
|
|
||
| // Start alarm to check user's location every day | ||
| AlarmHelper ah = new AlarmHelper(context, Constants.ACTION_CHECK_LOCATION); | ||
| ah.setRepeating(Constants.ONE_DAY_MILLIS, Constants.ONE_DAY_MILLIS); | ||
| } else if (action.equals(Constants.ACTION_SURVEY_ALARM)) { | ||
| Timber.d("ALARM TRIGGERED FOR SURVEY"); | ||
| String hospitalName = intent.getStringExtra(Constants.KEY_HOSPITAL_NAME); | ||
| buildNotification(context, hospitalName); | ||
| } else if (action.equals(Constants.ACTION_CHECK_LOCATION)) { | ||
| Timber.d("ALARM TRIGGERED FOR CHECK LOCATION"); | ||
| Intent serviceIntent = new Intent(context, HospitalizationService.class); | ||
| serviceIntent.putExtra(Constants.KEY_SERVICE_ACTION, action); | ||
| context.startService(serviceIntent); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private void buildNotification(Context context, String hospitalName) { | ||
| // Need to store this in SharedPreferences in case user clears notification | ||
| String date = AlarmHelper.getCurrentDate(); | ||
| SharedPreferences prefs = new SecurePreferences(context.getApplicationContext()); | ||
| SharedPreferences.Editor editor = prefs.edit(); | ||
| editor.putString(Constants.KEY_PERSISTENT_SURVEY_HOSPITAL, hospitalName); | ||
| editor.putString(Constants.KEY_PERSISTENT_SURVEY_DATE, date); | ||
| editor.apply(); | ||
|
|
||
| Intent displayIntent = new Intent(context, MainActivity.class); | ||
| displayIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); | ||
| displayIntent.putExtra(Constants.KEY_HOSPITAL_NAME, hospitalName); | ||
| displayIntent.putExtra(Constants.KEY_DATE, date); | ||
| PendingIntent displayPendingIntent = PendingIntent.getActivity( | ||
| context, | ||
| 0, | ||
| displayIntent, | ||
| PendingIntent.FLAG_UPDATE_CURRENT | ||
| ); | ||
|
|
||
| NotificationCompat.Builder notifBuilder = new NotificationCompat.Builder(context) | ||
| .setSmallIcon(R.drawable.ic_media_pause) | ||
| .setContentTitle("Health eHeart Study") | ||
| .setContentText("We noticed that you're near a hospital and would like you to fill out a survey.") | ||
| .setContentIntent(displayPendingIntent) | ||
| .setAutoCancel(true) | ||
| .setVibrate(new long[]{500, 500, 500, 500}); | ||
|
|
||
| NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context); | ||
| notificationManager.notify(1, notifBuilder.build()); | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't it better to set the value instead the operation?