package com.twolf.stressball11; import android.app.NotificationManager; import android.app.PendingIntent; import android.app.Service; import android.content.Intent; import android.media.RingtoneManager; import android.net.Uri; import android.os.AsyncTask; import android.os.IBinder; import android.support.v4.app.NotificationCompat; import android.widget.Toast; import org.json.JSONException; import org.json.JSONObject; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.util.concurrent.ExecutionException; /********************************************************************************************* * The sleep service is runs currently every couple of minutes for testing using the calendar * and in the future a settings page the user can input how many hours of sleep is good for them * and when they usually wake up. Using the calendar then would run the alarm every 24 hours and * a couple hours after the person has woken up. This class runs the json parser and compares sleep * time to how much sleep is needed in minutes. * * All services if they notify the user send the user to heartData class to visually see the data * * -Thomas Wolf * * help from: https://www.youtube.com/watch?v=tyVaPHv-RGo ****setting up a service * https://developer.android.com/reference/android/util/JsonReader.html ****json parsing */ public class SleepService extends Service { int myData=0; public SleepService() { } @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() /***************************************************************************** * Function onCreate() * Purpose: when service starts it first updates its array to gather sleep information * if sleep is bellow what is needed notifies user stress may be high due to lack of sleep * Usage: called automatically when service starts */ { try { String result = new getJsonData().execute().get(); // forces a wait. Wong move until // Async has returned a result } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } if(myData<360) // standard set to 6 hours of sleep { final Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); final NotificationManager NM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); Intent intent = new Intent(this.getApplicationContext(), HeartData.class); // where to go when clicked on final PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0); NotificationCompat.Builder notificationCompat = new NotificationCompat.Builder(SleepService.this) .setSmallIcon(R.drawable.icon) .setContentTitle("Lack of Sleep") .setContentText("Stress levels may be high today") .setTicker("Sleep detection") .setContentIntent(pendingIntent) .setSound(sound) .setAutoCancel(true); NM.notify(1, notificationCompat.build());//Posts to the screen my notification } stopSelf();//Need to call this so the alarm can reset otherwise it will continue in the //backgound. } @Override public void onDestroy() { /*********************************************************************************************** * Function Name: onDestroy() * Purpose: Destroys the notification * Usage: Call this function when you want notification to be destroyed ***********************************************************************************************/ super.onDestroy(); Toast.makeText(this,"in desotry", Toast.LENGTH_LONG).show(); return; } @Override public void onStart(Intent intent, int startId) { /*********************************************************************************************** * Function Name: onStart(Intent intent, int startID) * Purpose: Creates the notification * Usage: Call this function when you want creating notification process to begin ***********************************************************************************************/ super.onCreate(); Toast.makeText(SleepService.this, "Service started", Toast.LENGTH_LONG).show(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { /*********************************************************************************************** * Function Name: onStartCommand(Intent intent, int flags, int startId) * Purpose: The system invokes this method by calling startService() when another component * (such as an activity) requests that the service be started. When this method executes, * the service is started and can run in the background indefinitely * * help from https://developer.android.com/guide/components/services.html ***********************************************************************************************/ // TODO do something useful return Service.START_NOT_STICKY;//https://developer.android.com/reference/android/app/Service.html Only while needed; } public class getJsonData extends AsyncTask { /* Updates array using json files */ @Override protected void onPreExecute() { super.onPreExecute(); } @Override protected void onPostExecute(String s) { super.onPostExecute(s); } @Override protected String doInBackground(String... params) { try { //connect to sleep json URL url= new URL("http://compsci02.snc.edu/cs460/2017/wolftd/oauth2/sleep.json"); HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.connect(); //Grab the data InputStream stream = con.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(stream)); StringBuffer sb = new StringBuffer(); String line=""; while ((line =reader.readLine())!=null) { sb.append(line); } String finalJson =sb.toString(); // turn json to string JSONObject parentObject = new JSONObject(finalJson);// create json parent object JSONObject sleepJson = parentObject.getJSONObject("summary");// grabs specific object int sleep =sleepJson.getInt("totalMinutesAsleep");//grabs data at object myData =sleep; con.disconnect(); } catch(IOException e) { } catch (JSONException e) { e.printStackTrace(); } return null; } } }