// Copyright (c) 2014 Eugene Zhuk.
// Use of this source code is governed by the MIT license that can be found
// in the LICENSE file.

package com.ezhuk.wear;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.wearable.view.WatchViewStub;
import android.view.View;
import android.widget.Button;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.wearable.Asset;
import com.google.android.gms.wearable.Wearable;

import java.io.ByteArrayOutputStream;

import static com.ezhuk.wear.NotificationUtils.*;
import static com.ezhuk.wear.DataUtils.*;


public class MainActivity extends Activity {
    private static final String TAG = "Wear.MainActivity";
    public static final String MESSAGE_PATH = "/message";
    public static final String DATA_PATH = "/data";

    private GoogleApiClient mGoogleApiClient;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
        stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
            @Override
            public void onLayoutInflated(WatchViewStub stub) {
                Button buttonShow = (Button) stub.findViewById(R.id.button_show);
                buttonShow.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        showNotifications();
                        moveTaskToBack(true);
                    }
                });

                Button buttonSendMsg = (Button) stub.findViewById(R.id.button_send_msg);
                buttonSendMsg.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        sendMessage(mGoogleApiClient, MESSAGE_PATH,
                                getString(R.string.message_text));
                    }
                });

                Button buttonSendData = (Button) stub.findViewById(R.id.button_send_data);
                buttonSendData.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
                                R.drawable.background);
                        final ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
                        bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteStream);
                        sendData(mGoogleApiClient, DATA_PATH,
                                Asset.createFromBytes(byteStream.toByteArray()));
                    }
                });
            }
        });

        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(new ConnectionCallbacks())
                .addOnConnectionFailedListener(new ConnectionFailedListener())
                .addApi(Wearable.API)
                .build();
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (null != mGoogleApiClient && !mGoogleApiClient.isConnected()) {
            mGoogleApiClient.connect();
        }
    }

    @Override
    protected void onStop() {
        if (null != mGoogleApiClient && mGoogleApiClient.isConnected()) {
            mGoogleApiClient.disconnect();
        }
        super.onStop();
    }

    private class ConnectionCallbacks implements
            GoogleApiClient.ConnectionCallbacks {
        @Override
        public void onConnected(Bundle bundle) {
            // empty
        }

        @Override
        public void onConnectionSuspended(int i) {
            // empty
        }
    }

    private class ConnectionFailedListener implements
            GoogleApiClient.OnConnectionFailedListener {
        @Override
        public void onConnectionFailed(ConnectionResult result) {
            // empty
        }
    }

    private void showNotifications() {
        showNotification(this);
        showNotificationNoIcon(this);
        showNotificationMinPriority(this);
        showNotificationBigTextStyle(this, "Sample big text.");
        showNotificationBigPictureStyle(this,
                BitmapFactory.decodeResource(getResources(), R.drawable.background));
        showNotificationInboxStyle(this);
        showNotificationWithPages(this);
        showNotificationWithAction(this);
        showNotificationWithInputForPrimaryAction(this);
        showNotificationWithInputForSecondaryAction(this);
        showGroupNotifications(this, "Group");
    }
}

@@ -183,16 +183,47 @@
 tivity;%0A
+import android.content.Intent;%0A
 import a
@@ -246,16 +246,16 @@
 Bitmap;%0A
-
 import a
@@ -311,16 +311,56 @@
 Bundle;%0A
+import android.speech.RecognizerIntent;%0A
 import a
@@ -706,16 +706,44 @@
 tStream;
+%0Aimport java.util.ArrayList;
 %0A%0Aimport
@@ -931,37 +931,38 @@
 Activity%22;%0A    p
-ublic
+rivate
  static final St
@@ -998,21 +998,22 @@
 %22;%0A    p
-ublic
+rivate
  static 
@@ -1045,16 +1045,70 @@
 %22/data%22;
+%0A    private static final int SPEECH_REQUEST_CODE = 1;
 %0A%0A    pr
@@ -2224,115 +2224,28 @@
    s
-endMessage(mGoogleApiClient, MESSAGE_PATH,%0A                                getString(R.string.message_text)
+howSpeechRecognizer(
 );%0A 
@@ -4267,32 +4267,32 @@
        // empty%0A
-
         %7D%0A    %7D%0A
@@ -4284,32 +4284,851 @@
        %7D%0A    %7D%0A%0A
+    private void showSpeechRecognizer() %7B%0A        Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);%0A        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,%0A                RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);%0A        startActivityForResult(intent, SPEECH_REQUEST_CODE);%0A    %7D%0A%0A    @Override%0A    protected void onActivityResult(int requestCode, int resultCode, Intent data) %7B%0A        if (SPEECH_REQUEST_CODE == requestCode && RESULT_OK == resultCode) %7B%0A            ArrayList%3CString%3E results = data%0A                    .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);%0A            if (0 %3C results.size()) %7B%0A                sendMessage(mGoogleApiClient, MESSAGE_PATH, results.get(0));%0A            %7D%0A        %7D%0A%0A        super.onActivityResult(requestCode, resultCode, data);%0A    %7D%0A%0A
     private void

Use speech recognizer to send custom messages.