-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(wear): add free message client client and receiver (WIP)
- Loading branch information
Showing
8 changed files
with
219 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
wear-app/app/src/main/java/org/nativescript/demo/freemessage/FreeMessage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
wear-app/app/src/main/java/org/nativescript/demo/freemessage/FreeMessageClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
wear-app/app/src/main/java/org/nativescript/demo/freemessage/FreeMessageHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
wear-app/app/src/main/java/org/nativescript/demo/freemessage/FreeMessageProtocol.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
wear-app/app/src/main/java/org/nativescript/demo/freemessage/ReceivedMessage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 + | ||
'}'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters