package com.twolf.stressball11; import android.graphics.Color; import android.os.AsyncTask; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.TextView; import com.github.mikephil.charting.charts.LineChart; import com.github.mikephil.charting.data.Entry; import com.github.mikephil.charting.data.LineData; import com.github.mikephil.charting.data.LineDataSet; import org.json.JSONArray; 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.ArrayList; import java.util.concurrent.ExecutionException; /************************************************************************************************* * This class is a visual representation of the heart data along with a text box to show you * sleep, steps, and resting heart rate. Every service if it notifies the user will bring them to * this activity. * The graph package was made by PhilJay and found at: * https://github.com/PhilJay/MPAndroidChart/wiki * I am currently using a previous version he has sense updated and is no longer compatible with * how I fill the graph. * -Thomas Wolf */ public class HeartData extends AppCompatActivity { String strUrl="http://compsci02.snc.edu/cs460/2017/wolftd/oauth2/active.json";//used in asynch TextView txtView; public int[] myData= new int[6]; public int [] heartData; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_heart_data); txtView = (TextView) findViewById(R.id.textFitbit); try { //WAIT SO THE ARRAY CAN BE FILLED WITH HEART DATA String result =new getData().execute().get(); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } final int color = Color.argb(255,255,0,0); final int blue = Color.argb(255,0,0,255); final LineChart lineChart = (LineChart) findViewById(R.id.chart); final ArrayList entries = new ArrayList(); for(int i=0; i labels = new ArrayList(); for(int i=0; i { /* This time the async process will have to go through all three of my json files to pull all relevant information to be displayed for the user. During post execute it will write to the textview filling in all information the user should need to see about their daily activities. The heart data will be filled when this thread finishes and its array has been filled. */ @Override protected void onPreExecute() { super.onPreExecute(); } @Override protected void onPostExecute(String s) { super.onPostExecute(s); txtView.setText(""); txtView.append("Steps: "+myData[0]+'\n'); txtView.append("Sleep in minuets: "+myData[1]+'\n'); int t=myData[1]; int hours = t/60; int min = t%60; txtView.append("Converted sleep: "+hours+"hr "+min+ "min \n"); txtView.append("Resting Heart Rate: "+myData[3]+'\n'); } @Override protected String doInBackground(String... params) { try { URL url= new URL(strUrl); HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.connect(); 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(); JSONObject parentObject = new JSONObject(finalJson); JSONObject stepps = parentObject.getJSONObject("summary"); int steps =stepps.getInt("steps"); myData[0]=steps; int restingHeart = stepps.getInt("restingHeartRate"); myData[3]=restingHeart; con.disconnect(); url= new URL("http://compsci02.snc.edu/cs460/2017/wolftd/oauth2/sleep.json"); con = (HttpURLConnection) url.openConnection(); con.connect(); stream = con.getInputStream(); reader = new BufferedReader(new InputStreamReader(stream)); sb = new StringBuffer(); line=""; while ((line =reader.readLine())!=null) { sb.append(line); } finalJson =sb.toString(); parentObject = new JSONObject(finalJson); JSONObject sleepJson = parentObject.getJSONObject("summary"); int sleep =sleepJson.getInt("totalMinutesAsleep"); myData[1]=sleep; con.disconnect(); url= new URL("http://compsci02.snc.edu/cs460/2017/wolftd/oauth2/heartrate.json"); con = (HttpURLConnection) url.openConnection(); con.connect(); stream = con.getInputStream(); reader = new BufferedReader(new InputStreamReader(stream)); sb = new StringBuffer(); line=""; while ((line =reader.readLine())!=null) { sb.append(line); } finalJson =sb.toString(); parentObject = new JSONObject(finalJson); JSONObject heartJson= parentObject.getJSONObject("activities-heart-intraday"); JSONArray heartArray=heartJson.getJSONArray("dataset"); heartData=new int[heartArray.length()]; for(int i=0; i