package com.twolf.stressball11; import android.app.AlarmManager; import android.app.PendingIntent; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.os.SystemClock; import android.preference.PreferenceManager; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private PendingIntent HeartPendingIntent, SleepPendingIntent,StepsPendingIntent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); /****************************************************************************************** * On create will seet the service alarms 1 time once and store the variable in the app preferences. So the next time the app is lunched it will not set the alarms again. Helpful code: www.youtube.com/watch?v=gm5n_hRIR-c && www.youtube.com/watch?v=tyVaPHv-RGo setHeart Alarm currently blocked from starting so anyone downloading app can see it run with last loaded fitbit data. If they try to run oAuth at this point it will return nothing and fail due to the fact I am not wearing the fitBit anymore. ******************************************************************************************/ SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); SharedPreferences.Editor editor = preferences.edit(); int i = preferences.getInt("numberoflaunches", 1); if (i < 2) { //setHeartAlarm(); //setting the alarm for Heart notification setSleepAlarm(); //setting the alarm for Sleep notification setStepsAlarm(); //setting the alarm for Steps notification i++; editor.putInt("numberoflaunches", i); editor.apply(); Toast.makeText(this, "Stressball Alarms Now Set", Toast.LENGTH_LONG).show(); } } public void onHeartClick(View v) { Intent i=new Intent(MainActivity.this, HeartData.class); startActivity(i);// starts the graph activity to see heart data and fitbit data } public void onBreathClick(View v) { Intent i=new Intent(MainActivity.this, Breath.class); startActivity(i);// opens the breathing acitivty } public void onLogClick(View v) { Intent i=new Intent(MainActivity.this, Journal.class); startActivity(i);// opens the jounral activity } public void onMusicClick (View v) { Intent i=new Intent(MainActivity.this, MusicPlayer.class); startActivity(i);// opens the media player } /************************************************************************************************* * The three alarms below are currently set for testing purpose using firs time. So as soon as the app starts it will wait sometime before triggering the alarm. When the app is fully functional use the calendar to set a specific time of the day you would want the service to go off. Use the calendar for sleep and steps * Sleep should be a 24 hour alarm signaling once a day * Steps should be set for every hour to remind you to move if you have not *Heart should be checked no more that every 20 min as fitbit only gives heart data every 15 min. No real time access to hear data **************************************************************************************************/ private void setHeartAlarm() { Intent checkHeart = new Intent(MainActivity.this, HeartService.class); AlarmManager alarmManager =(AlarmManager)getSystemService(ALARM_SERVICE); HeartPendingIntent = PendingIntent.getService(MainActivity.this,0,checkHeart, PendingIntent.FLAG_UPDATE_CURRENT); /*Calendar calendar= Calendar.getInstance(); calendar.set(Calendar.HOUR, 12); calendar.set(Calendar.MINUTE, 03); calendar.set(Calendar.AM_PM, Calendar.PM); */ long firstTime = SystemClock.elapsedRealtime(); firstTime +=10*1000;//5 second delay is minimum delay you can use alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,firstTime,1000*60*1, HeartPendingIntent); // repeat every 20 min } private void setSleepAlarm () { Intent checkSleep = new Intent(MainActivity.this, SleepService.class); AlarmManager alarmManager =(AlarmManager)getSystemService(ALARM_SERVICE); SleepPendingIntent = PendingIntent.getService(MainActivity.this,0, checkSleep,PendingIntent.FLAG_UPDATE_CURRENT); /*Calendar calendar= Calendar.getInstance(); calendar.set(Calendar.HOUR, 12); calendar.set(Calendar.MINUTE, 03); calendar.set(Calendar.AM_PM, Calendar.PM); */ long firstTime = SystemClock.elapsedRealtime(); firstTime +=20*1000;//5 second delay is minimum delay you can use alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,firstTime,1000*60*3, SleepPendingIntent); // every 60 min } private void setStepsAlarm () { Intent checkSleep = new Intent(MainActivity.this, StepsService.class); AlarmManager alarmManager =(AlarmManager)getSystemService(ALARM_SERVICE); StepsPendingIntent = PendingIntent.getService(MainActivity.this,0,checkSleep, PendingIntent.FLAG_UPDATE_CURRENT); /*Calendar calendar= Calendar.getInstance(); calendar.set(Calendar.HOUR, 12); calendar.set(Calendar.MINUTE, 03); calendar.set(Calendar.AM_PM, Calendar.PM); */ long firstTime = SystemClock.elapsedRealtime(); firstTime +=10*1000;//5 second delay is minimum delay you can use alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,firstTime,1000*60, StepsPendingIntent); // every 60 min } /********************************************************************************************* * The Connect to fitbit button when pressed will force a log on to the fitbit servers and run index.php to update all jsons manually in case the alarm service did not work or you want to test the connection to fit bit. * In the future this button should be removed and replaced with a settings button. Currently * going to comment this out so when new user downloads app they can run the app with data after * my capstone oAuth2 will fail to return data as I will not be wearing the same fit bit most * likely *********************************************************************************************/ /* public void contactFitbit (View v) { String urlString="http://compsci02.snc.edu/cs460/2017/wolftd/" + "oauth2/index.php"; Intent intent=new Intent(Intent.ACTION_VIEW, Uri.parse(urlString)); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setPackage("com.android.chrome"); try{ MainActivity.this.startActivity(intent); } catch (ActivityNotFoundException ex) { Toast.makeText(MainActivity.this,"GOOGLE CRHOME NEEDS TO BE INSTALLED", Toast.LENGTH_LONG).show(); } } */ }