Advertisement
the-technoholik

garage_opener/wear/WearActivity.java

Nov 15th, 2014
333
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.20 KB | None | 0 0
  1. package com.kisscool.android.garage_opener.app;
  2.  
  3. import android.app.Activity;
  4. import android.content.Intent;
  5. import android.os.AsyncTask;
  6. import android.os.Bundle;
  7. import android.support.wearable.activity.ConfirmationActivity;
  8. import android.support.wearable.view.WatchViewStub;
  9. import android.util.Log;
  10. import android.view.View;
  11. import android.widget.ImageButton;
  12. import com.google.android.gms.common.api.GoogleApiClient;
  13. import com.google.android.gms.wearable.MessageApi;
  14. import com.google.android.gms.wearable.Node;
  15. import com.google.android.gms.wearable.NodeApi;
  16. import com.google.android.gms.wearable.Wearable;
  17.  
  18. import java.util.Collection;
  19. import java.util.LinkedList;
  20. import java.util.List;
  21.  
  22. public class WearActivity extends Activity {
  23.     private ImageButton openButton;
  24.     private ImageButton closeButton;
  25.  
  26.     GoogleApiClient googleApiClient;
  27.     private static final String START_ACTIVITY_PATH = "/start-activity";
  28.  
  29.     @Override
  30.     protected void onCreate(Bundle savedInstanceState) {
  31.         super.onCreate(savedInstanceState);
  32.         setContentView(R.layout.activity_wear);
  33.  
  34.         WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
  35.         stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
  36.             @Override
  37.             public void onLayoutInflated(WatchViewStub watchViewStub) {
  38.                 openButton = (ImageButton) findViewById(R.id.openButton);
  39.                 closeButton = (ImageButton) findViewById(R.id.closeButton);
  40.  
  41.                 openButton.setOnClickListener(new View.OnClickListener() {
  42.                     @Override
  43.                     public void onClick(View v) {
  44.                         Log.d("GARAGE_CONTROL_WEAR", "Button open clicked.");
  45.                         sendCmd("open");
  46.                     }
  47.                 });
  48.  
  49.  
  50.                 closeButton.setOnClickListener(new View.OnClickListener() {
  51.                     @Override
  52.                     public void onClick(View v) {
  53.                         Log.d("GARAGE_CONTROL_WEAR", "Button close clicked.");
  54.                         sendCmd("close");
  55.                     }
  56.                 });
  57.             }
  58.         });
  59.     }
  60.  
  61.     private List<Node> getNodes() {
  62.         Collection<String> results = new LinkedList<>();
  63.  
  64.         if (googleApiClient == null) {
  65.             GoogleApiClient.Builder builder = new GoogleApiClient.Builder(this);
  66.             builder.addApi(Wearable.API);
  67.             googleApiClient = builder.build();
  68.             googleApiClient.connect();
  69.         }
  70.  
  71.         NodeApi.GetConnectedNodesResult nodes =
  72.                 Wearable.NodeApi.getConnectedNodes(googleApiClient).await();
  73.         return nodes.getNodes();
  74.     }
  75.  
  76.     private void sendCmd(String cmd) {
  77.         AsyncTask<String, Void, Boolean> task = new AsyncTask<String, Void, Boolean>() {
  78.             private String cmd;
  79.  
  80.             @Override
  81.             protected Boolean doInBackground(String... params) {
  82.                 cmd = params[0];
  83.  
  84.                 boolean atLeastOneHasSuccess = false;
  85.                 for (Node node : getNodes()) {
  86.                     MessageApi.SendMessageResult result = Wearable.MessageApi.sendMessage(
  87.                             googleApiClient, node.getId(), START_ACTIVITY_PATH, params[0].getBytes()
  88.                     ).await();
  89.                     atLeastOneHasSuccess |= result.getStatus().isSuccess();
  90.                 }
  91.                 return atLeastOneHasSuccess;
  92.             }
  93.  
  94.             @Override
  95.             protected void onPostExecute(Boolean isSuccess) {
  96.                 if (isSuccess) {
  97.                     Intent intent = new Intent(WearActivity.this, ConfirmationActivity.class);
  98.                     intent.putExtra(ConfirmationActivity.EXTRA_ANIMATION_TYPE,
  99.                             ConfirmationActivity.SUCCESS_ANIMATION);
  100.                     intent.putExtra(ConfirmationActivity.EXTRA_MESSAGE, cmd);
  101.                     startActivity(intent);
  102.                 }
  103.             }
  104.         };
  105.         task.execute(cmd);
  106.     }
  107.  
  108.     @Override
  109.     protected void onDestroy() {
  110.         super.onDestroy();
  111.         if (googleApiClient != null) {
  112.             googleApiClient.disconnect();
  113.         }
  114.     }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement