@Override public void handleMessage(Message msg) { String action = ""; Long requested = 0L; String dataString = ""; Bundle data = new Bundle(); Toast.makeText(MainApp.instance(), "START", Toast.LENGTH_LONG).show(); try { data = msg.getData(); action = data.getString("ACTION"); Log.d("RECEIVED: ACTION", action); requested = data.getLong("DATE_REQUESTED", 0); Log.d("RECEIVED: DATE", requested.toString()); dataString = data.getString("DATA"); Log.d("RECEIVED: DATA", dataString); } catch (Exception e) { e.printStackTrace(); // TODO: 16/01/2016 Issue getting treatment details from HAPPs msg } finally { // Toast.makeText(MainApp.instance(), action, Toast.LENGTH_LONG).show(); switch (action) { case "TEST_MSG": Resources appR = MainApp.instance().getResources(); CharSequence txt = appR.getText( appR.getIdentifier("app_name", "string", MainApp.instance().getPackageName())); Toast.makeText( MainApp.instance(), txt + ": HAPP has connected successfully. ", Toast.LENGTH_LONG) .show(); break; case "temp_basal": case "cancel_temp_basal": ObjectToSync basalSync = new Gson().fromJson(dataString, ObjectToSync.class); Basal basal = new Basal(); basal.rate = basalSync.value1; basal.ratePercent = Integer.getInteger(basalSync.value2, 0); basal.duration = Integer.getInteger(basalSync.value3, 0); basal.start_time = basalSync.requested; basal.action = basalSync.action; basal.been_set = false; basal.happ_int_id = basalSync.happ_integration_id; basal.auth_code = basalSync.integrationSecretCode; basal.state = "received"; basal.save(); // We have now saved the requested treatments from HAPP to our local DB, now action them connect_to_HAPP(); actionBasal(); break; case "bolus_delivery": Type type = new TypeToken<List<ObjectToSync>>() {}.getType(); List<ObjectToSync> bolusList = new Gson().fromJson(dataString, type); for (ObjectToSync newBolus : bolusList) { try { Treatment newTreatment = new Treatment(); newTreatment.type = newBolus.value3; newTreatment.date_requested = newBolus.requested.getTime(); newTreatment.value = newBolus.value1; newTreatment.delivered = false; newTreatment.happ_int_id = newBolus.happ_integration_id; newTreatment.auth_code = newBolus.integrationSecretCode; newTreatment.state = "received"; newTreatment.save(); } catch (Exception e) { e.printStackTrace(); // TODO: 16/01/2016 Issue getting treatment details } } // We have now saved the requested treatments from HAPP to our local DB, now action them connect_to_HAPP(); actionTreatments(); break; } } }