diff --git a/mobile/src/main/java/com/example/android/wearablemessageapiexample/MobileActivity.java b/mobile/src/main/java/com/example/android/wearablemessageapiexample/MobileActivity.java index 318adc2..85f4f3d 100644 --- a/mobile/src/main/java/com/example/android/wearablemessageapiexample/MobileActivity.java +++ b/mobile/src/main/java/com/example/android/wearablemessageapiexample/MobileActivity.java @@ -51,7 +51,6 @@ public class MobileActivity extends Activity { private final String COMMAND_PATH = "/command"; private GoogleApiClient apiClient; - private NodeApi.NodeListener nodeListener; private MessageApi.MessageListener messageListener; private String command; @@ -60,33 +59,67 @@ protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_activity); - // Create MessageListener that receives messages sent from a wearable - messageListener = new MessageApi.MessageListener() { + messageListener = messageListenerFactory(); + + apiClient = apiClientFactory(); + } + + @Override + protected void onResume() { + super.onResume(); + + // Check is Google Play Services available + int connectionResult = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext()); + + if (connectionResult != ConnectionResult.SUCCESS) { + // Google Play Services is NOT available. Show appropriate error dialog + GooglePlayServicesUtil.showErrorDialogFragment(connectionResult, this, 0, new DialogInterface.OnCancelListener() { + @Override + public void onCancel(DialogInterface dialog) { + finish(); + } + }); + } else { + apiClient.connect(); + } + } + + @Override + protected void onPause() { + // Unregister Node and Message listeners, disconnect GoogleApiClient and disable buttons + Wearable.NodeApi.removeListener(apiClient, null); + Wearable.MessageApi.removeListener(apiClient, messageListener); + apiClient.disconnect(); + super.onPause(); + } + + // Create MessageListener that receives messages sent from a wearable + private MessageApi.MessageListener messageListenerFactory(){ + return new MessageApi.MessageListener() { @Override public void onMessageReceived(MessageEvent messageEvent) { if (messageEvent.getPath().equals(COMMAND_PATH)) { command = new String(messageEvent.getData()); - - HttpClient httpclient = new DefaultHttpClient(); try { - httpclient.execute(new HttpGet()); - }catch (IOException e) { + httpclient.execute(new HttpGet(BASE_URL + command)); + } catch (IOException e) { e.printStackTrace(); } - } } }; + } - // Create GoogleApiClient - apiClient = new GoogleApiClient.Builder(getApplicationContext()).addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() { + // Create GoogleApiClient + private GoogleApiClient apiClientFactory(){ + return new GoogleApiClient.Builder(getApplicationContext()).addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() { @Override public void onConnected(Bundle bundle) { // Register Node and Message listeners - Wearable.NodeApi.addListener(apiClient, nodeListener); + Wearable.NodeApi.addListener(apiClient, null); Wearable.MessageApi.addListener(apiClient, messageListener); // If there is a connected node, get it's id that is used when sending messages } @@ -102,33 +135,4 @@ public void onConnectionFailed(ConnectionResult connectionResult) { } }).addApi(Wearable.API).build(); } - - @Override - protected void onResume() { - super.onResume(); - - // Check is Google Play Services available - int connectionResult = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext()); - - if (connectionResult != ConnectionResult.SUCCESS) { - // Google Play Services is NOT available. Show appropriate error dialog - GooglePlayServicesUtil.showErrorDialogFragment(connectionResult, this, 0, new DialogInterface.OnCancelListener() { - @Override - public void onCancel(DialogInterface dialog) { - finish(); - } - }); - } else { - apiClient.connect(); - } - } - - @Override - protected void onPause() { - // Unregister Node and Message listeners, disconnect GoogleApiClient and disable buttons - Wearable.NodeApi.removeListener(apiClient, nodeListener); - Wearable.MessageApi.removeListener(apiClient, messageListener); - apiClient.disconnect(); - super.onPause(); - } } diff --git a/wear/src/main/java/com/example/android/wearablemessageapiexample/WearActivity.java b/wear/src/main/java/com/example/android/wearablemessageapiexample/WearActivity.java index d23e1dc..183a282 100644 --- a/wear/src/main/java/com/example/android/wearablemessageapiexample/WearActivity.java +++ b/wear/src/main/java/com/example/android/wearablemessageapiexample/WearActivity.java @@ -23,105 +23,63 @@ import android.os.Handler; import android.support.wearable.activity.ConfirmationActivity; import android.view.View; -import android.widget.EditText; import com.google.android.gms.common.ConnectionResult; import com.google.android.gms.common.GooglePlayServicesUtil; import com.google.android.gms.common.api.GoogleApiClient; import com.google.android.gms.common.api.ResultCallback; import com.google.android.gms.wearable.MessageApi; -import com.google.android.gms.wearable.MessageEvent; import com.google.android.gms.wearable.Node; import com.google.android.gms.wearable.NodeApi; import com.google.android.gms.wearable.Wearable; -import java.util.Stack; - public class WearActivity extends Activity { - private final String MESSAGE1_PATH = "/command"; + private final String COMMAND_PATH = "/command"; private GoogleApiClient apiClient; - private View message1Button; - private View message2Button; - private NodeApi.NodeListener nodeListener; - private MessageApi.MessageListener messageListener; + private View punchButton; private String remoteNodeId; - private Handler handler; private String command; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_activity); - command = "lala"; - handler = new Handler(); - message1Button = findViewById(R.id.message1Button); - message2Button = findViewById(R.id.message2Button); + command = getCommand(); + + punchButton = findViewById(R.id.punchButton); - // Set message1Button onClickListener to send message 1 - message1Button.setOnClickListener(new View.OnClickListener() { + // Set the button onClickListener to send the message + punchButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - Wearable.MessageApi.sendMessage(apiClient, remoteNodeId, MESSAGE1_PATH, command.getBytes()).setResultCallback(new ResultCallback() { - @Override - public void onResult(MessageApi.SendMessageResult sendMessageResult) { - - } - }); + Wearable.MessageApi.sendMessage(apiClient, remoteNodeId, COMMAND_PATH, command.getBytes()); } }); + apiClient = apiClientFactory(); + } - // Create NodeListener that enables buttons when a node is connected and disables buttons when a node is disconnected - nodeListener = new NodeApi.NodeListener() { - @Override - public void onPeerConnected(Node node) { - remoteNodeId = node.getId(); - handler.post(new Runnable() { - @Override - public void run() { - message1Button.setEnabled(true); - message2Button.setEnabled(true); - } - }); - Intent intent = new Intent(getApplicationContext(), ConfirmationActivity.class); - intent.putExtra(ConfirmationActivity.EXTRA_ANIMATION_TYPE, ConfirmationActivity.SUCCESS_ANIMATION); - intent.putExtra(ConfirmationActivity.EXTRA_MESSAGE, getString(R.string.peer_connected)); - startActivity(intent); - } - - @Override - public void onPeerDisconnected(Node node) { - handler.post(new Runnable() { - @Override - public void run() { - message1Button.setEnabled(false); - message2Button.setEnabled(false); - } - }); - Intent intent = new Intent(getApplicationContext(), ConfirmationActivity.class); - intent.putExtra(ConfirmationActivity.EXTRA_ANIMATION_TYPE, ConfirmationActivity.FAILURE_ANIMATION); - intent.putExtra(ConfirmationActivity.EXTRA_MESSAGE, getString(R.string.peer_disconnected)); - startActivity(intent); - } - }; + // Returns the command using the voice input + private String getCommand() { + return "Left_Kick"; + } - // Create GoogleApiClient - apiClient = new GoogleApiClient.Builder(getApplicationContext()).addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() { + // Create GoogleApiClient + private GoogleApiClient apiClientFactory() { + return new GoogleApiClient.Builder(getApplicationContext()).addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() { @Override public void onConnected(Bundle bundle) { // Register Node and Message listeners - Wearable.NodeApi.addListener(apiClient, nodeListener); - Wearable.MessageApi.addListener(apiClient, messageListener); + Wearable.NodeApi.addListener(apiClient, null); // If there is a connected node, get it's id that is used when sending messages Wearable.NodeApi.getConnectedNodes(apiClient).setResultCallback(new ResultCallback() { @Override public void onResult(NodeApi.GetConnectedNodesResult getConnectedNodesResult) { if (getConnectedNodesResult.getStatus().isSuccess() && getConnectedNodesResult.getNodes().size() > 0) { remoteNodeId = getConnectedNodesResult.getNodes().get(0).getId(); - message1Button.setEnabled(true); - message2Button.setEnabled(true); + punchButton.setEnabled(true); } } }); @@ -129,8 +87,7 @@ public void onResult(NodeApi.GetConnectedNodesResult getConnectedNodesResult) { @Override public void onConnectionSuspended(int i) { - message1Button.setEnabled(false); - message2Button.setEnabled(false); + punchButton.setEnabled(false); } }).addApi(Wearable.API).build(); } @@ -158,11 +115,9 @@ public void onCancel(DialogInterface dialog) { @Override protected void onPause() { // Unregister Node and Message listeners, disconnect GoogleApiClient and disable buttons - Wearable.NodeApi.removeListener(apiClient, nodeListener); - Wearable.MessageApi.removeListener(apiClient, messageListener); + Wearable.NodeApi.removeListener(apiClient, null); apiClient.disconnect(); - message1Button.setEnabled(false); - message2Button.setEnabled(false); + punchButton.setEnabled(false); super.onPause(); } } diff --git a/wear/src/main/res/layout/main_activity.xml b/wear/src/main/res/layout/main_activity.xml index 8c620df..d11581e 100644 --- a/wear/src/main/res/layout/main_activity.xml +++ b/wear/src/main/res/layout/main_activity.xml @@ -21,19 +21,11 @@ android:orientation="horizontal">