wear sending confirmation requests

This commit is contained in:
AdrianLxM 2017-02-08 11:53:26 +01:00
parent d1a3645be7
commit 8e9d73e3a3
4 changed files with 112 additions and 8 deletions

View file

@ -42,7 +42,10 @@ public class WearFragment extends Fragment implements FragmentBase {
view.findViewById(R.id.wear_opensettings).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
getPlugin(getContext()).openSettings();
//TODO: revert after debugging!
//getPlugin(getContext()).openSettings();
getPlugin(getContext()).requestActionConfirmation("Titel", "bla bla", "action 1 string");
}
});

View file

@ -163,6 +163,15 @@ public class WearPlugin implements PluginBase {
}
public void requestActionConfirmation(String title, String message, String actionstring){
Intent intent = new Intent(ctx, WatchUpdaterService.class).setAction(WatchUpdaterService.ACTION_SEND_ACTIONCONFIRMATIONREQUEST);
intent.putExtra("title", title);
intent.putExtra("message", message);
intent.putExtra("actionstring", actionstring);
ctx.startService(intent);
}
public static boolean isEnabled() {
return fragmentEnabled;
}

View file

@ -45,16 +45,21 @@ public class WatchUpdaterService extends WearableListenerService implements
public static final String ACTION_SEND_STATUS = WatchUpdaterService.class.getName().concat(".SendStatus");
public static final String ACTION_SEND_BASALS = WatchUpdaterService.class.getName().concat(".SendBasals");
public static final String ACTION_SEND_BOLUSPROGRESS = WatchUpdaterService.class.getName().concat(".BolusProgress");
public static final String ACTION_SEND_ACTIONCONFIRMATIONREQUEST = WatchUpdaterService.class.getName().concat(".ActionConfirmationRequest");
private GoogleApiClient googleApiClient;
public static final String WEARABLE_DATA_PATH = "/nightscout_watch_data";
public static final String WEARABLE_RESEND_PATH = "/nightscout_watch_data_resend";
private static final String WEARABLE_CANCELBOLUS_PATH = "/nightscout_watch_cancel_bolus";
public static final String WEARABLE_CONFIRM_ACTIONSTRING_PATH = "/nightscout_watch_confirmactionstring";
public static final String WEARABLE_INITIATE_ACTIONSTRING_PATH = "/nightscout_watch_initiateactionstring";
private static final String OPEN_SETTINGS_PATH = "/openwearsettings";
private static final String NEW_STATUS_PATH = "/sendstatustowear";
public static final String BASAL_DATA_PATH = "/nightscout_watch_basal";
public static final String BOLUS_PROGRESS_PATH = "/nightscout_watch_bolusprogress";
public static final String ACTION_CONFIRMATION_REQUEST_PATH = "/nightscout_watch_actionconfirmationrequest";
@ -121,7 +126,13 @@ public class WatchUpdaterService extends WearableListenerService implements
sendBasals();
} else if (ACTION_SEND_BOLUSPROGRESS.equals(action)){
sendBolusProgress(intent.getIntExtra("progresspercent", 0), intent.hasExtra("progressstatus")?intent.getStringExtra("progressstatus"):"");
} else {
} else if (ACTION_SEND_ACTIONCONFIRMATIONREQUEST.equals(action)){
String title = intent.getStringExtra("title");
String message = intent.getStringExtra("message");
String actionstring = intent.getStringExtra("actionstring");
sendActionConfirmationRequest(title, message, actionstring);
}
else {
sendData();
}
} else {
@ -148,6 +159,18 @@ public class WatchUpdaterService extends WearableListenerService implements
if (event != null && event.getPath().equals(WEARABLE_CANCELBOLUS_PATH)) {
cancelBolus();
}
if (event != null && event.getPath().equals(WEARABLE_INITIATE_ACTIONSTRING_PATH)) {
String actionstring = new String(event.getData());
ToastUtils.showToastInUiThread(this, "INITIATE: " + actionstring);
//TODO: watch initiated action
}
if (event != null && event.getPath().equals(WEARABLE_CONFIRM_ACTIONSTRING_PATH)) {
String actionstring = new String(event.getData());
ToastUtils.showToastInUiThread(this, "CONFIRM: " + actionstring);
//TODO: watch confirmed action
}
}
}
@ -454,6 +477,23 @@ public class WatchUpdaterService extends WearableListenerService implements
}
}
private void sendActionConfirmationRequest(String title, String message, String actionstring) {
if (googleApiClient.isConnected()) {
PutDataMapRequest dataMapRequest = PutDataMapRequest.create(ACTION_CONFIRMATION_REQUEST_PATH);
//unique content
dataMapRequest.getDataMap().putDouble("timestamp", System.currentTimeMillis());
dataMapRequest.getDataMap().putString("actionConfirmationRequest", "actionConfirmationRequest");
dataMapRequest.getDataMap().putString("title", title);
dataMapRequest.getDataMap().putString("message", message);
dataMapRequest.getDataMap().putString("actionstring", actionstring);
PutDataRequest putDataRequest = dataMapRequest.asPutDataRequest();
Wearable.DataApi.putDataItem(googleApiClient, putDataRequest);
} else {
Log.e("ActionConfirmationRequest", "No connection to wearable available!");
}
}
private void sendStatus() {
if (googleApiClient.isConnected()) {

View file

@ -32,11 +32,15 @@ public class ListenerService extends WearableListenerService implements GoogleAp
private static final String WEARABLE_DATA_PATH = "/nightscout_watch_data";
private static final String WEARABLE_RESEND_PATH = "/nightscout_watch_data_resend";
private static final String WEARABLE_CANCELBOLUS_PATH = "/nightscout_watch_cancel_bolus";
public static final String WEARABLE_CONFIRM_ACTIONSTRING_PATH = "/nightscout_watch_confirmactionstring";
public static final String WEARABLE_INITIATE_ACTIONSTRING_PATH = "/nightscout_watch_initiateactionstring";
private static final String OPEN_SETTINGS = "/openwearsettings";
private static final String NEW_STATUS_PATH = "/sendstatustowear";
public static final String BASAL_DATA_PATH = "/nightscout_watch_basal";
public static final String BOLUS_PROGRESS_PATH = "/nightscout_watch_bolusprogress";
public static final String ACTION_CONFIRMATION_REQUEST_PATH = "/nightscout_watch_actionconfirmationrequest";
public static final int BOLUS_PROGRESS_NOTIF_ID = 001;
public static final int CONFIRM_NOTIF_ID = 002;
@ -86,11 +90,11 @@ public class ListenerService extends WearableListenerService implements GoogleAp
@Override
protected Void doInBackground(Void... params) {
if (googleApiClient.isConnected()) {
NodeApi.GetConnectedNodesResult nodes =
Wearable.NodeApi.getConnectedNodes(googleApiClient).await();
for (Node node : nodes.getNodes()) {
Wearable.MessageApi.sendMessage(googleApiClient, node.getId(), WEARABLE_CANCELBOLUS_PATH, null);
}
NodeApi.GetConnectedNodesResult nodes =
Wearable.NodeApi.getConnectedNodes(googleApiClient).await();
for (Node node : nodes.getNodes()) {
Wearable.MessageApi.sendMessage(googleApiClient, node.getId(), WEARABLE_CANCELBOLUS_PATH, null);
}
} else {
googleApiClient.blockingConnect(15, TimeUnit.SECONDS);
@ -107,6 +111,37 @@ public class ListenerService extends WearableListenerService implements GoogleAp
}
}
public class MessageActionTask extends AsyncTask<Void, Void, Void> {
Context mContext;
String mActionstring;
String mMessagePath;
MessageActionTask(Context context, String messagePath, String actionstring) {
mContext = context;
mActionstring = actionstring;
mMessagePath = messagePath;
}
@Override
protected Void doInBackground(Void... params) {
if (googleApiClient.isConnected()) {
NodeApi.GetConnectedNodesResult nodes =
Wearable.NodeApi.getConnectedNodes(googleApiClient).await();
for (Node node : nodes.getNodes()) {
Wearable.MessageApi.sendMessage(googleApiClient, node.getId(), mMessagePath, mActionstring.getBytes());
}
} else {
googleApiClient.blockingConnect(15, TimeUnit.SECONDS);
if (googleApiClient.isConnected()) {
//TODO: copy send code
}
}
return null;
}
}
public void requestData() {
new DataRequester(this).execute();
}
@ -115,6 +150,14 @@ public class ListenerService extends WearableListenerService implements GoogleAp
new BolusCancelTask(this).execute();
}
private void sendConfirmActionstring(String actionstring) {
new MessageActionTask(this, WEARABLE_CONFIRM_ACTIONSTRING_PATH, actionstring).execute();
}
private void sendInitiateActionstring(String actionstring) {
new MessageActionTask(this, WEARABLE_INITIATE_ACTIONSTRING_PATH, actionstring).execute();
}
public void googleApiConnect() {
googleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
@ -150,12 +193,15 @@ public class ListenerService extends WearableListenerService implements GoogleAp
NotificationManagerCompat.from(ListenerService.this);
notificationManager.cancel(CONFIRM_NOTIF_ID);
String actionstring = intent.getStringExtra("actionstring");
sendConfirmActionstring(actionstring);
//TODO: send confirmation string to phone
}
return START_STICKY;
}
@Override
public void onDataChanged(DataEventBuffer dataEvents) {
@ -175,7 +221,12 @@ public class ListenerService extends WearableListenerService implements GoogleAp
int progress = DataMapItem.fromDataItem(event.getDataItem()).getDataMap().getInt("progresspercent", 0);
String status = DataMapItem.fromDataItem(event.getDataItem()).getDataMap().getString("progressstatus", "");
showBolusProgress(progress, status);
} else if (path.equals(NEW_STATUS_PATH)) {
} else if (path.equals(ACTION_CONFIRMATION_REQUEST_PATH)) {
String title = DataMapItem.fromDataItem(event.getDataItem()).getDataMap().getString("title");
String message = DataMapItem.fromDataItem(event.getDataItem()).getDataMap().getString("message");
String actionstring = DataMapItem.fromDataItem(event.getDataItem()).getDataMap().getString("actionstring");
showConfirmationDialog(title, message, actionstring);
}else if (path.equals(NEW_STATUS_PATH)) {
dataMap = DataMapItem.fromDataItem(event.getDataItem()).getDataMap();
Intent messageIntent = new Intent();
messageIntent.setAction(Intent.ACTION_SEND);
@ -236,6 +287,7 @@ public class ListenerService extends WearableListenerService implements GoogleAp
Intent actionIntent = new Intent(this, ListenerService.class);
actionIntent.setAction(ACTION_CONFIRMATION);
actionIntent.putExtra("actionstring", actionstring);
PendingIntent actionPendingIntent = PendingIntent.getService(this, 0, actionIntent, 0);;
long[] vibratePattern = new long[]{0, 100, 50, 100, 50};