Week Seven

We’ve officially completed week seven of our REU summer program! This week was short, but productive. We took of Monday for the Forth of July, but got to work on Tuesday. This week’s big task was finishing up syncing the databases. Here is a description of what I did this week:

Tuesday
Tuesday I continued working trying to get the sync adapter working. Last week I was able to log into the IU server and run PHPscripts that I had written. These scripts took a JSON object that contained the rows of the table and inserted it into a MySQL database. Now, I needed to make sure that the Android application was correctly accessing this server and running the scripts. I had already written the majority of the sync adapter from the tutorial I had found, but my sync adapter wasn’t sending the tables in the correct JSON object. So I spent the day working on this. I was able to find some forums online that showed how JSON objects could be sent to a server. These were very helpful, and so I started by making a test program. I quickly developed a simple program that sent a table to a server. The thing that differed from the Android Developer tutorial was my SyncAdapter.java class. Instead I rewrote the onPerformSync() method so that the JSON object was directly sent to the server there. You can view the code below. I finally had a good understating of how the sync adapter should be functioning, and I began to implement the same ideas into the ESM application.

import android.accounts.Account;
import android.content.AbstractThreadedSyncAdapter;
import android.content.ContentProviderClient;
import android.content.Context;
import android.content.SyncResult;
import android.os.Bundle;
import android.util.Log;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by saurabh on 12/24/2015.
 */
public class SyncAdapter extends AbstractThreadedSyncAdapter {

    public SyncAdapter(Context context, boolean autoInitialize) {
        super(context, autoInitialize);
    }

    String url = "http://cgi.sice.indiana.edu/~demensch/stressdiary/sync.php";

    @Override
    public void onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider, SyncResult syncResult) {

        Log.i("SyncAdapter","Synchronization started");

        UserTableOperations uop = new UserTableOperations(getContext());

        final ArrayList<Object> tableList = uop.getUserTableContents();
        for(int i = 0; i < tableList.size(); i++)
        {
            Log.i("tableList", tableList.get(i).toString());
        }
        Map<String, String> params = new HashMap<String, String>();
        String JSON;
        Gson gson = new Gson();

        JSON = gson.toJson(tableList); //Could take a while.
        Log.d("OnPerformSync", "the request json is " + JSON);
        params.put("tableName", "user_table");
        params.put("tableRows", JSON);
        params.put("syncMode", "CS");
        CustomVolleyGsonRequest req = new CustomVolleyGsonRequest(url, Object.class, params, new Response.Listener<Object>() {
            @Override
            public void onResponse(Object response) {

                if (response instanceof String) {
                    String result = (String) response;
                    Log.d("Volley", "Response:" + result);
                } else {
                    Log.i("OnPerformSync", "Yet to be implemented. Parse Server Response"); //TODO: Take care of this.
                }
                // uop.setSyncFlag(tableName);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

                Log.e("Volley", "Error in getting the response");
//                Log.e("Volley", error.getLocalizedMessage());

            }
        });
        SingletonVolley.getInstance(getContext()).addToRequestQueue(req);

    }
}

Wednesday
Wednesday I finally synced the databases! I finished up implementing the ideas I had developed in the sync adapter test program into my ESM application, and it finally worked! Once I knew that the sync adapter was working I needed to make sure that the scripts were functioning exactly how I wanted them to.After a little work, I had the scripts adding rows to the MySQL database when the row was new and updating when it was not. I also added a UserID column to each table in the MySQL database. The researchers will then be able to query the database for the data from each participant. The UserID is currently the model number on their phone, but I may need to do some more search to make sure  that this number is unique.  Once this was complete, I wanted to do some testing on different devices. I because by testing the application on different emulators. This went well and the sync adapter was functioning correctly on each emulator. Then I decided to start testing the application on physical devices. I began with my phone, a Samsung Galaxy S7. After testing the application a few times, I quickly realized that the sync adapter was never being created on my phone. This was a problem, so I started doing some research about how the sync adapter functioned physical devices. I tried a few things but didn’t find a solution that seemed to work. So I called it a day, and decided I would start with this tomorrow.

Thursday
Thursday I began with fix the problem I was having with the sync adapter on my phone. I looked online some more for some answer as to why the program would work on an emulator by not a psychical device. I found a few things, but none of it seemed to work. Then I came across a forum that discussed how the sync adapter needs syncing to be activated on the psychical device. After some research, I found an article describing how unlike most phones the Samsung Galaxy has syncing turned off. So I asked to burrow Aislinn’s Motorola phone, and the sync adapter worked on the first try! Although this was exciting, it could be very possible that a participant may have a Samsung phone. Thus, we need to look more into how we can instruct these participants on how to turn syncing on. Overall, it was exciting that everything with the sync adapter was finally working. Aislinn spent the remainder of the day working on ProHealth Deliverables and catching up on some small things we had neglected during the beginning of the week. We were able to get everything done, and it was a very productive day.

Although the list of thing we completed isn’t very long this week, the things we did complete were very essential to us finishing the project on time. Now that the databases are functioning properly, we just need to complete the Alarm algorithm and the Microsoft Band tile. Both of these will be challenging, but I am confident that we will finish them. We also finished a few other tasks this week. Here is a decription of each one:

Lightning Talk
Raise if you have ever been stressed?
Now raise your hand if you have ever worn some kind of wearable technology?
Well, what if wearable technology had the potential to detect stress in real time.

This summer I am working on a research project to try and determine whether the Microsoft Band 2 could be used to detect stress in real time. To do this, I am developing an Android application that will ask users survey questions about their stress levels while collecting data from the Band. The surveys will be given to the users based on an algorithm that selects a time based on the user’s heart rate. This application will then be used as a tool by researchers to study the correlation of the Band’s output and user’s stress levels. Thus, allow us to be one step closer to answering the question: does wearable technology have the potential to detect stress real time.

Story Board for Video

  1. Introduction
    1. Overview of ESM StressDiary Application
  2. Demo (Walk Through Application)
    1. Show Application’s Connection to Microsoft Band 2
    2. Heart Rate Alarms
    3. Surveys
      1. Survey on Microsoft Band Tile
      2. Pending Surveys on Android Phone
    4.  Databases
      1. Local SQLite Databse
      2. phpMyAdmin MySQL Database
      3. Sync Adapter
  3.  Future Works
    1. Overview the Use of the Application in the New Mothers Study