package com.doubleencore.sample.wear.ongoingnotification;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

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.InputStream;
import java.util.concurrent.TimeUnit;

/**
 * Created by carlos on 7/15/14.
 */
public class NotificationActivity extends Activity {
    private static final String TAG = NotificationActivity.class.getSimpleName();

    public static final String EXTRA_TITLE = "title";
    public static final String EXTRA_IMAGE = "image";

    private ImageView mImageView;
    private TextView mTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notification);

        mImageView = (ImageView) findViewById(R.id.image_view);
        mTextView = (TextView) findViewById(R.id.text_view);

        Intent intent = getIntent();
        if (intent != null) {
            mTextView.setText(intent.getStringExtra(EXTRA_TITLE));

            final Asset asset = intent.getParcelableExtra(EXTRA_IMAGE);

            loadBitmapFromAsset(asset, mImageView);
        }
    }

    private void loadBitmapFromAsset(final Asset asset, final ImageView target) {
        if (asset == null) {
            throw new IllegalArgumentException("Asset must be non-null");
        }

        new AsyncTask<Asset, Void, Bitmap>() {
            @Override
            protected Bitmap doInBackground(Asset... assets) {
                GoogleApiClient googleApiClient = new GoogleApiClient.Builder(NotificationActivity.this)
                        .addApi(Wearable.API)
                        .build();
                ConnectionResult result =
                        googleApiClient.blockingConnect(
                                1000, TimeUnit.MILLISECONDS);
                if (!result.isSuccess()) {
                    return null;
                }

                // convert asset into a file descriptor and block until it's ready
                InputStream assetInputStream = Wearable.DataApi.getFdForAsset(
                        googleApiClient, assets[0]).await().getInputStream();
                googleApiClient.disconnect();

                if (assetInputStream == null) {
                    Log.w(TAG, "Requested an unknown Asset.");
                    return null;
                }

                // decode the stream into a bitmap
                return BitmapFactory.decodeStream(assetInputStream);
            }

            @Override
            protected void onPostExecute(Bitmap bitmap) {
                if (bitmap != null) {
                    target.setImageBitmap(bitmap);
                }
            }
        }.execute(asset);
    }

}
@@ -183,16 +183,47 @@
 actory;%0A
+import android.graphics.Color;%0A
 import a
@@ -243,16 +243,16 @@
 ncTask;%0A
-
 import a
@@ -294,16 +294,42 @@
 il.Log;%0A
+import android.view.View;%0A
 import a
@@ -384,16 +384,45 @@
 extView;
+%0Aimport android.widget.Toast;
 %0A%0Aimport
@@ -652,16 +652,41 @@
 Stream;%0A
+import java.util.Random;%0A
 import j
@@ -1067,16 +1067,35 @@
 extView;
+%0A    Random random;
 %0A%0A    @O
@@ -1642,24 +1642,24 @@
 ImageView);%0A
-
         %7D%0A  
@@ -1651,24 +1651,332 @@
 );%0A        %7D
+%0A%0A        random = new Random();%0A        mTextView.setOnClickListener(new View.OnClickListener() %7B%0A            @Override%0A            public void onClick(View v) %7B%0A                mTextView.setTextColor(Color.argb(255, random.nextInt(256), random.nextInt(256), random.nextInt(256)));%0A            %7D%0A        %7D);
 %0A    %7D%0A%0A    

Add click listener to notification activity