package com.twolf.stressball11; import android.app.NotificationManager; import android.app.PendingIntent; import android.app.Service; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; 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; /********************************************************************************************* *Once the steps service is activated after initial startup of the app the steps service should go * off every hour and check the steps updated in the array from the json file. If the person has * not moved in the last hour/ or has not moved enough depending on setup of the app, the user will * be notified to get up and move a little as it is not good to be sitting for to long. * * 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 * https://www.youtube.com/watch?v=4asV24BnsXQ **** more json parsing * DR. McVey ****json parsing */ public class StepsService extends Service { int myData=0; public StepsService() { } @Override public IBinder onBind(Intent intent) { return null; } /*********************************************************************************** * Function Name:oncreate() * Purpose: This function first starts the async process and get the step data from the json * After it comes back it will open up shared preferences of the app and if this is the first * time doing so it will simply store the data. The next time it runs it will compare previous * data to current data and if not enough steps have been reached then the app will notify the * user that he/she needs to walk some more. * Usage: this function is automatically called when the service starts. */ @Override public void onCreate() { try { String result = new getJsonData().execute().get();//Wait for thread to finish before // Check the array } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } //ONLY THIS APP SHOULD BE ABLE TO ACCESS USER DATA SO MAKE MODE PRIVATE SharedPreferences preferences = getSharedPreferences("stepsFile", Context.MODE_PRIVATE); int prevSteps = preferences.getInt("stepsData",1);// if first time then steps will be 1 if(prevSteps==1) { SharedPreferences.Editor editor = preferences.edit(); editor.putInt("stepsData", myData ); editor.apply(); } else if(prevSteps<=myData)// compare previous steps to current steps { 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(StepsService.this) .setSmallIcon(R.drawable.icon) .setContentTitle("Walk More") .setContentText("walk around to reduce stress") .setTicker("Steps detection") .setContentIntent(pendingIntent) .setSound(sound) .setAutoCancel(true); NM.notify(1, notificationCompat.build());//Posts to the screen my notification } else { SharedPreferences.Editor editor = preferences.edit(); editor.putInt("stepsData", myData ); editor.apply(); } stopSelf();// use this to stop the service so the alarm can reset } @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(StepsService.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 { //New thread to go and read the json file from comp sci server. @Override protected void onPreExecute() { super.onPreExecute(); } @Override protected void onPostExecute(String s) { super.onPostExecute(s); } @Override protected String doInBackground(String... params) { try { //Connect to active json file URL url= new URL("http://compsci02.snc.edu/cs460/2017/wolftd/oauth2/active.json"); HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.connect(); //create buffer and read from the json file 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();// buffer into string JSONObject parentObject = new JSONObject(finalJson); //turn string into json object JSONObject stepps = parentObject.getJSONObject("summary");// grab the summary from json int steps =stepps.getInt("steps"); // get data from summary object myData =steps; con.disconnect(); } catch(IOException e) { } catch (JSONException e) { e.printStackTrace(); } return null; } } }