Skip to content

Commit

Permalink
Code refactor and deleted unused code.
Browse files Browse the repository at this point in the history
  • Loading branch information
JuanDesi committed Apr 25, 2015
1 parent 640867c commit 94ab7de
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 116 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
}
Expand All @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,114 +23,71 @@
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<MessageApi.SendMessageResult>() {
@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<NodeApi.GetConnectedNodesResult>() {
@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);
}
}
});
}

@Override
public void onConnectionSuspended(int i) {
message1Button.setEnabled(false);
message2Button.setEnabled(false);
punchButton.setEnabled(false);
}
}).addApi(Wearable.API).build();
}
Expand Down Expand Up @@ -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();
}
}
10 changes: 1 addition & 9 deletions wear/src/main/res/layout/main_activity.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,11 @@
android:orientation="horizontal">

<Button
android:id="@+id/message1Button"
android:id="@+id/punchButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="false"
android:text="@string/send_message1"
android:layout_gravity="center" />

<Button
android:id="@+id/message2Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="false"
android:text="@string/send_message2"
android:layout_gravity="center_vertical" />

</LinearLayout>

0 comments on commit 94ab7de

Please sign in to comment.