Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.kisscool.android.garage_opener.app;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.AsyncTask;
- import android.os.Bundle;
- import android.support.wearable.activity.ConfirmationActivity;
- import android.support.wearable.view.WatchViewStub;
- import android.util.Log;
- import android.view.View;
- import android.widget.ImageButton;
- import com.google.android.gms.common.api.GoogleApiClient;
- import com.google.android.gms.wearable.MessageApi;
- import com.google.android.gms.wearable.Node;
- import com.google.android.gms.wearable.NodeApi;
- import com.google.android.gms.wearable.Wearable;
- import java.util.Collection;
- import java.util.LinkedList;
- import java.util.List;
- public class WearActivity extends Activity {
- private ImageButton openButton;
- private ImageButton closeButton;
- GoogleApiClient googleApiClient;
- private static final String START_ACTIVITY_PATH = "/start-activity";
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_wear);
- WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
- stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
- @Override
- public void onLayoutInflated(WatchViewStub watchViewStub) {
- openButton = (ImageButton) findViewById(R.id.openButton);
- closeButton = (ImageButton) findViewById(R.id.closeButton);
- openButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Log.d("GARAGE_CONTROL_WEAR", "Button open clicked.");
- sendCmd("open");
- }
- });
- closeButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Log.d("GARAGE_CONTROL_WEAR", "Button close clicked.");
- sendCmd("close");
- }
- });
- }
- });
- }
- private List<Node> getNodes() {
- Collection<String> results = new LinkedList<>();
- if (googleApiClient == null) {
- GoogleApiClient.Builder builder = new GoogleApiClient.Builder(this);
- builder.addApi(Wearable.API);
- googleApiClient = builder.build();
- googleApiClient.connect();
- }
- NodeApi.GetConnectedNodesResult nodes =
- Wearable.NodeApi.getConnectedNodes(googleApiClient).await();
- return nodes.getNodes();
- }
- private void sendCmd(String cmd) {
- AsyncTask<String, Void, Boolean> task = new AsyncTask<String, Void, Boolean>() {
- private String cmd;
- @Override
- protected Boolean doInBackground(String... params) {
- cmd = params[0];
- boolean atLeastOneHasSuccess = false;
- for (Node node : getNodes()) {
- MessageApi.SendMessageResult result = Wearable.MessageApi.sendMessage(
- googleApiClient, node.getId(), START_ACTIVITY_PATH, params[0].getBytes()
- ).await();
- atLeastOneHasSuccess |= result.getStatus().isSuccess();
- }
- return atLeastOneHasSuccess;
- }
- @Override
- protected void onPostExecute(Boolean isSuccess) {
- if (isSuccess) {
- Intent intent = new Intent(WearActivity.this, ConfirmationActivity.class);
- intent.putExtra(ConfirmationActivity.EXTRA_ANIMATION_TYPE,
- ConfirmationActivity.SUCCESS_ANIMATION);
- intent.putExtra(ConfirmationActivity.EXTRA_MESSAGE, cmd);
- startActivity(intent);
- }
- }
- };
- task.execute(cmd);
- }
- @Override
- protected void onDestroy() {
- super.onDestroy();
- if (googleApiClient != null) {
- googleApiClient.disconnect();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement