use of com.aware.ui.esms.ESM_Question in project aware-client by denzilferreira.
the class ESM method processFlow.
private static void processFlow(Context context, String current_answer) {
if (Aware.DEBUG) {
Log.d(ESM.TAG, "Current answer: " + current_answer);
}
try {
// Check flow
Cursor last_esm = context.getContentResolver().query(ESM_Data.CONTENT_URI, null, ESM_Data.STATUS + "=" + ESM.STATUS_ANSWERED, null, ESM_Data.TIMESTAMP + " DESC LIMIT 1");
if (last_esm != null && last_esm.moveToFirst()) {
JSONObject esm_question = new JSONObject(last_esm.getString(last_esm.getColumnIndex(ESM_Data.JSON)));
ESM_Question esm = new ESMFactory().getESM(esm_question.getInt(ESM_Question.esm_type), esm_question, last_esm.getInt(last_esm.getColumnIndex(ESM_Data._ID)));
// Set as branched the flow rules that are not triggered
JSONArray flows = esm.getFlows();
for (int i = 0; i < flows.length(); i++) {
JSONObject flow = flows.getJSONObject(i);
String flowAnswer = flow.getString(ESM_Question.flow_user_answer);
JSONObject nextESM = flow.getJSONObject(ESM_Question.flow_next_esm).getJSONObject(EXTRA_ESM);
if (flowAnswer.equals(current_answer)) {
if (Aware.DEBUG)
Log.d(ESM.TAG, "Following next question: " + nextESM);
// Queued ESM
ContentValues rowData = new ContentValues();
// fixed issue with synching and support ordering of esms by timestamp
rowData.put(ESM_Data.TIMESTAMP, System.currentTimeMillis());
rowData.put(ESM_Data.DEVICE_ID, Aware.getSetting(context, Aware_Preferences.DEVICE_ID));
rowData.put(ESM_Data.JSON, nextESM.toString());
// optional, defaults to 0
rowData.put(ESM_Data.EXPIRATION_THRESHOLD, nextESM.optInt(ESM_Data.EXPIRATION_THRESHOLD));
// optional, defaults to 0
rowData.put(ESM_Data.NOTIFICATION_TIMEOUT, nextESM.optInt(ESM_Data.NOTIFICATION_TIMEOUT));
rowData.put(ESM_Data.STATUS, ESM.STATUS_NEW);
// optional, defaults to ""
rowData.put(ESM_Data.TRIGGER, nextESM.optString(ESM_Data.TRIGGER));
context.getContentResolver().insert(ESM_Data.CONTENT_URI, rowData);
} else {
if (Aware.DEBUG)
Log.d(ESM.TAG, "Branched split: " + flowAnswer + " Skipping: " + nextESM);
// Branched ESM
ContentValues rowData = new ContentValues();
// fixed issue with synching and support ordering of esms by timestamp
rowData.put(ESM_Data.TIMESTAMP, System.currentTimeMillis());
rowData.put(ESM_Data.DEVICE_ID, Aware.getSetting(context, Aware_Preferences.DEVICE_ID));
rowData.put(ESM_Data.JSON, nextESM.toString());
// optional, defaults to 0
rowData.put(ESM_Data.EXPIRATION_THRESHOLD, nextESM.optInt(ESM_Data.EXPIRATION_THRESHOLD));
// optional, defaults to 0
rowData.put(ESM_Data.NOTIFICATION_TIMEOUT, nextESM.optInt(ESM_Data.NOTIFICATION_TIMEOUT));
rowData.put(ESM_Data.STATUS, ESM.STATUS_BRANCHED);
// optional, defaults to ""
rowData.put(ESM_Data.TRIGGER, nextESM.optString(ESM_Data.TRIGGER));
context.getContentResolver().insert(ESM_Data.CONTENT_URI, rowData);
}
}
}
if (last_esm != null && !last_esm.isClosed())
last_esm.close();
} catch (JSONException e) {
e.printStackTrace();
}
}
use of com.aware.ui.esms.ESM_Question in project aware-client by denzilferreira.
the class ESM_Queue method onResume.
@Override
protected void onResume() {
super.onResume();
if (getQueueSize(getApplicationContext()) == 0)
finish();
try {
FragmentManager fragmentManager = getSupportFragmentManager();
Cursor current_esm;
if (ESM.isESMVisible(getApplicationContext())) {
current_esm = getContentResolver().query(ESM_Data.CONTENT_URI, null, ESM_Data.STATUS + "=" + ESM.STATUS_VISIBLE, null, ESM_Data.TIMESTAMP + " ASC LIMIT 1");
} else {
current_esm = getContentResolver().query(ESM_Data.CONTENT_URI, null, ESM_Data.STATUS + "=" + ESM.STATUS_NEW, null, ESM_Data.TIMESTAMP + " ASC LIMIT 1");
}
if (current_esm != null && current_esm.moveToFirst()) {
int _id = current_esm.getInt(current_esm.getColumnIndex(ESM_Data._ID));
// Fixed: set the esm as VISIBLE, to avoid displaying the same ESM twice due to changes in orientation
ContentValues update_state = new ContentValues();
update_state.put(ESM_Data.STATUS, ESM.STATUS_VISIBLE);
getContentResolver().update(ESM_Data.CONTENT_URI, update_state, ESM_Data._ID + "=" + _id, null);
// --
// Load esm question JSON from database
JSONObject esm_question = new JSONObject(current_esm.getString(current_esm.getColumnIndex(ESM_Data.JSON)));
ESM_Question esm = esmFactory.getESM(esm_question.getInt(ESM_Question.esm_type), esm_question, current_esm.getInt(current_esm.getColumnIndex(ESM_Data._ID)));
if (esm != null) {
esm.show(fragmentManager, TAG);
}
}
if (current_esm != null && !current_esm.isClosed())
current_esm.close();
} catch (JSONException e) {
e.printStackTrace();
}
}
use of com.aware.ui.esms.ESM_Question in project aware-client by denzilferreira.
the class ESM method queueESM.
/**
* Queue an ESM queue, but allowing trials
*
* @param context
* @param queue
*/
public static void queueESM(Context context, String queue, boolean isTrial) {
try {
JSONArray esms = new JSONArray(queue);
long esm_timestamp = System.currentTimeMillis();
boolean is_persistent = false;
for (int i = 0; i < esms.length(); i++) {
JSONObject esm = esms.getJSONObject(i).getJSONObject(EXTRA_ESM);
if (i == 0) {
// we check the first ESM item in the queue to see whether any current queue items need to be removed
if (esm.optBoolean("esm_replace_queue")) {
// clear current queue
if (Aware.DEBUG)
Log.d(TAG, "Clearing ESM queue before adding new ESM to queue");
// Remove notification
if (mNotificationManager == null)
mNotificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
mNotificationManager.cancel(ESM.ESM_NOTIFICATION_ID);
// Clear queue
Cursor esm_cursor = context.getContentResolver().query(ESM_Data.CONTENT_URI, null, ESM_Data.STATUS + " IN (" + ESM.STATUS_NEW + "," + ESM.STATUS_VISIBLE + ")", null, null);
if (esm_cursor != null && esm_cursor.moveToFirst()) {
do {
ContentValues rowData = new ContentValues();
rowData.put(ESM_Data.ANSWER_TIMESTAMP, System.currentTimeMillis());
rowData.put(ESM_Data.STATUS, ESM.STATUS_REPLACED);
context.getContentResolver().update(ESM_Data.CONTENT_URI, rowData, ESM_Data._ID + "=" + esm_cursor.getInt(esm_cursor.getColumnIndex(ESM_Data._ID)), null);
} while (esm_cursor.moveToNext());
}
if (esm_cursor != null && !esm_cursor.isClosed())
esm_cursor.close();
}
}
ContentValues rowData = new ContentValues();
// fix issue with synching and support ordering
rowData.put(ESM_Data.TIMESTAMP, esm_timestamp + i);
rowData.put(ESM_Data.DEVICE_ID, Aware.getSetting(context, Aware_Preferences.DEVICE_ID));
rowData.put(ESM_Data.JSON, esm.toString());
// optional, defaults to 0
rowData.put(ESM_Data.EXPIRATION_THRESHOLD, esm.optInt(ESM_Data.EXPIRATION_THRESHOLD));
// optional, defaults to 0
rowData.put(ESM_Data.NOTIFICATION_TIMEOUT, esm.optInt(ESM_Data.NOTIFICATION_TIMEOUT));
rowData.put(ESM_Data.STATUS, ESM.STATUS_NEW);
// we use this TRIAL trigger to remove trials from database at the end of the trial
rowData.put(ESM_Data.TRIGGER, isTrial ? "TRIAL" : esm.optString(ESM_Data.TRIGGER));
if (i == 0 && (rowData.getAsInteger(ESM_Data.EXPIRATION_THRESHOLD) == 0 || rowData.getAsInteger(ESM_Data.NOTIFICATION_TIMEOUT) > 0)) {
is_persistent = true;
}
try {
context.getContentResolver().insert(ESM_Data.CONTENT_URI, rowData);
if (Aware.DEBUG)
Log.d(TAG, "ESM: " + rowData.toString());
} catch (SQLiteException e) {
if (Aware.DEBUG)
Log.d(TAG, e.getMessage());
}
}
if (is_persistent) {
// show notification
Cursor pendingESM = context.getContentResolver().query(ESM_Data.CONTENT_URI, null, ESM_Data.STATUS + "=" + ESM.STATUS_NEW, null, ESM_Data.TIMESTAMP + " ASC LIMIT 1");
if (pendingESM != null && pendingESM.moveToFirst()) {
// Set the timer if there is a notification timeout
int notification_timeout = pendingESM.getInt(pendingESM.getColumnIndex(ESM_Data.NOTIFICATION_TIMEOUT));
if (notification_timeout > 0) {
try {
ESM_Question question = new ESM_Question().rebuild(new JSONObject(pendingESM.getString(pendingESM.getColumnIndex(ESM_Data.JSON))));
esm_notif_expire = new ESMNotificationTimeout(context, System.currentTimeMillis(), notification_timeout, question.getNotificationRetry(), pendingESM.getInt(pendingESM.getColumnIndex(ESM_Data._ID)));
esm_notif_expire.execute();
} catch (JSONException e) {
e.printStackTrace();
}
}
}
if (pendingESM != null && !pendingESM.isClosed())
pendingESM.close();
// Show notification
notifyESM(context, true);
} else {
// show ESM immediately
Intent intent_ESM = new Intent(context, ESM_Queue.class);
intent_ESM.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent_ESM);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
use of com.aware.ui.esms.ESM_Question in project aware-client by denzilferreira.
the class ESM method processAppIntegration.
private static void processAppIntegration(Context context) {
try {
ESM_Question esm = null;
Cursor last_esm = context.getContentResolver().query(ESM_Data.CONTENT_URI, null, ESM_Data.STATUS + "=" + ESM.STATUS_ANSWERED, null, ESM_Data.TIMESTAMP + " DESC LIMIT 1");
if (last_esm != null && last_esm.moveToFirst()) {
JSONObject esm_question = new JSONObject(last_esm.getString(last_esm.getColumnIndex(ESM_Data.JSON)));
esm = new ESMFactory().getESM(esm_question.getInt(ESM_Question.esm_type), esm_question, last_esm.getInt(last_esm.getColumnIndex(ESM_Data._ID)));
}
if (last_esm != null && !last_esm.isClosed())
last_esm.close();
if (esm != null && esm.getAppIntegration().length() > 0) {
try {
Intent integration = new Intent(Intent.ACTION_VIEW);
integration.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
integration.setData(Uri.parse(esm.getAppIntegration()));
context.startActivity(integration);
} catch (ActivityNotFoundException e) {
Toast.makeText(context, "No application to handle: " + esm.getAppIntegration(), Toast.LENGTH_LONG).show();
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
Aggregations