Skip to content

Commit

Permalink
feat(wear): add free message client client and receiver (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
matey97 committed Jun 27, 2022
1 parent 7e99bbd commit 6a87315
Show file tree
Hide file tree
Showing 8 changed files with 219 additions and 0 deletions.
1 change: 1 addition & 0 deletions wear-app/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,6 @@ dependencies {
implementation 'androidx.recyclerview:recyclerview:1.2.0'
implementation 'androidx.wear:wear:1.0.0'
implementation 'com.google.android.gms:play-services-location:17.0.0'
implementation 'com.google.code.gson:gson:2.9.0'
compileOnly 'com.google.android.wearable:wearable:2.8.1'
}
4 changes: 4 additions & 0 deletions wear-app/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@
android:host="*"
android:pathPattern="/.*/stop"
android:scheme="wear" />
<data
android:host="*"
android:pathPattern="/free-message.*"
android:scheme="wear" />
</intent-filter>
</service>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package org.nativescript.demo.freemessage;

import com.google.gson.Gson;

public class FreeMessage {
private String message;
private FreeMessage inResponseTo;

public FreeMessage(String message) {
this.message = message;
}

public FreeMessage(String message, FreeMessage inResponseTo) {
this.message = message;
this.inResponseTo = inResponseTo;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

public FreeMessage getInResponseTo() {
return inResponseTo;
}

public void setInResponseTo(FreeMessage inResponseTo) {
this.inResponseTo = inResponseTo;
}

@Override
public String toString() {
return "FreeMessage{" +
"message='" + message + '\'' +
", inResponseTo=" + inResponseTo +
'}';
}

public static String encodeFreeMessage(FreeMessage freeMessage) {
return new Gson().toJson(freeMessage);
}

public static FreeMessage decodeFreeMessage(String stringMessage) {
return new Gson().fromJson(stringMessage, FreeMessage.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.nativescript.demo.freemessage;

import android.content.Context;
import android.util.Log;

import androidx.annotation.NonNull;

import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.android.gms.tasks.Tasks;
import com.google.android.gms.wearable.CapabilityClient;
import com.google.android.gms.wearable.CapabilityInfo;
import com.google.android.gms.wearable.MessageClient;
import com.google.android.gms.wearable.Node;
import com.google.android.gms.wearable.Wearable;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.util.Set;

public class FreeMessageClient {

private CapabilityClient capabilityClient;
private MessageClient messageClient;
private FreeMessageProtocol protocol;

public FreeMessageClient(Context context) {
this.capabilityClient = Wearable.getCapabilityClient(context);
this.messageClient = Wearable.getMessageClient(context);
this.protocol = FreeMessageProtocol.getProtocol();
}

public void send(FreeMessage freeMessage) {
capabilityClient.getCapability("main-node", CapabilityClient.FILTER_ALL)
.addOnSuccessListener(capabilityInfo -> {
Set<Node> mainNodes = capabilityInfo.getNodes();
if (mainNodes.isEmpty())
return;

Node mainNode = mainNodes.iterator().next();
String jsonString = FreeMessage.encodeFreeMessage(freeMessage);
messageClient.sendMessage(mainNode.getId(), this.protocol.getWithoutResponsePath(), jsonString.getBytes());
})
.addOnFailureListener(e -> Log.d("Failure", e.getMessage()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.nativescript.demo.freemessage;

import android.content.Context;
import android.util.Log;

import com.google.android.gms.wearable.MessageEvent;

public class FreeMessageHandler {

private Context context;
private FreeMessageProtocol protocol;

public FreeMessageHandler(Context context) {
this.context = context;
this.protocol = FreeMessageProtocol.getProtocol();
}

public void handleMessage(MessageEvent event) {
String path = event.getPath();

if (!path.equals(this.protocol.getExpectingResponsePath())
&& !path.equals(this.protocol.getWithoutResponsePath())) {
return;
}

if (event.getData() == null) {
return;
}

String sourceNodeId = event.getSourceNodeId();
String encodedMessage = new String(event.getData());

FreeMessage message = FreeMessage.decodeFreeMessage(encodedMessage);
ReceivedMessage receivedMessage = new ReceivedMessage(
sourceNodeId,
message,
path.equals(this.protocol.getExpectingResponsePath())
);
Log.d("FreeMessageHandler", receivedMessage.toString());

// TODO: Listener callback
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.nativescript.demo.freemessage;

public class FreeMessageProtocol {
private String withoutResponsePath;
private String expectingResponsePath;

public FreeMessageProtocol(String withoutResponsePath, String expectingResponsePath) {
this.withoutResponsePath = withoutResponsePath;
this.expectingResponsePath = expectingResponsePath;
}

public String getWithoutResponsePath() {
return withoutResponsePath;
}

public String getExpectingResponsePath() {
return expectingResponsePath;
}

public static FreeMessageProtocol getProtocol() {
return new FreeMessageProtocol(
"free-message-no-response",
"free-message-expecting-response"
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package org.nativescript.demo.freemessage;

public class ReceivedMessage {
private String senderNodeId;
private FreeMessage freeMessage;
private boolean requiresResponse;

public ReceivedMessage(String senderNodeId, FreeMessage freeMessage, boolean requiresResponse) {
this.senderNodeId = senderNodeId;
this.freeMessage = freeMessage;
this.requiresResponse = requiresResponse;
}

public String getSenderNodeId() {
return senderNodeId;
}

public void setSenderNodeId(String senderNodeId) {
this.senderNodeId = senderNodeId;
}

public FreeMessage getFreeMessage() {
return freeMessage;
}

public void setFreeMessage(FreeMessage freeMessage) {
this.freeMessage = freeMessage;
}

public boolean isRequiresResponse() {
return requiresResponse;
}

public void setRequiresResponse(boolean requiresResponse) {
this.requiresResponse = requiresResponse;
}

@Override
public String toString() {
return "ReceivedMessage{" +
"senderNodeId='" + senderNodeId + '\'' +
", freeMessage=" + freeMessage +
", requiresResponse=" + requiresResponse +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.google.android.gms.wearable.WearableListenerService;

import org.nativescript.demo.capabilities.CapabilityAdvertisementHandler;
import org.nativescript.demo.freemessage.FreeMessageHandler;
import org.nativescript.demo.messaging.handlers.AccelerometerMessagingHandler;
import org.nativescript.demo.messaging.handlers.GyroscopeMessagingHandler;
import org.nativescript.demo.messaging.handlers.HeartRateMessagingHandler;
Expand All @@ -29,6 +30,8 @@ public void onMessageReceived(MessageEvent event) {
new HeartRateMessagingHandler(this).handleMessage(event);
} else if (path.contains("location")) {
new LocationMessagingHandler(this).handleMessage(event);
} else if (path.contains("free-message")) {
new FreeMessageHandler(this).handleMessage(event);
}
}
}

0 comments on commit 6a87315

Please sign in to comment.