Search code examples
google-glassgoogle-gdk

Displaying Multiple Images on a Single Google Glass Live Card


I'm creating a live card app that recieves PNGs from a php script running on my server in response to a request from scanning QR codes. At the moment, I simply replace the image on my Live card with the PNG I recieve from the server, but I would like to recieve and display multiple images from the server with each request.

Is there an approved way to show multiple images on a live card? I was thinking there may be a possiblity of generating a menu full of images that simply closed itself when clicked, but it seems like there might be a better alternative.

This is my code at the moment:

Current Code

import com.google.android.glass.timeline.LiveCard;
import com.google.android.glass.timeline.LiveCard.PublishMode;

import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Binder;
import android.os.IBinder;
import android.util.Base64;
import android.widget.RemoteViews;

public class iotSplashScreen extends Service {

    private static final String LIVE_CARD_TAG = "iotSplashScreen";
    private LiveCard mLiveCard;
    private RemoteViews mLiveCardView;

    public class iotBinder extends Binder {
        public void changeImage(String change) {
            try {
                byte[] bob = Base64.decode(change, Base64.DEFAULT);
                Bitmap bitmap = BitmapFactory.decodeByteArray(bob, 0, bob.length);
                if(bitmap != null) {
                    mLiveCardView.setImageViewBitmap(R.id.image_view_id, bitmap);
                    mLiveCard.setViews(mLiveCardView);
                }
                else
                {
                    System.out.println("Daaang, dat bitmap was null doe");
                }
            }
            catch (IllegalArgumentException e)
            {
                System.out.println("Base64 had an issues: " + e);
                System.out.println(change);
            }
            catch (NullPointerException e)
            {
                System.out.println("Null Pointer: " + e);
            }
        }
    }

    private final iotBinder mBinder = new iotBinder();

    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (mLiveCard == null) {
            mLiveCard = new LiveCard(this, LIVE_CARD_TAG);

            mLiveCardView = new RemoteViews(getPackageName(), R.layout.iot_splash_screen);
            mLiveCard.setViews(mLiveCardView);

            // Display the options menu when the live card is tapped.
            Intent menuIntent = new Intent(this, LiveCardMenuActivity.class);
            mLiveCard.setAction(PendingIntent.getActivity(this, 0, menuIntent, 0));
            mLiveCard.publish(PublishMode.REVEAL);
        } else {
            mLiveCard.navigate();
        }
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        if (mLiveCard != null && mLiveCard.isPublished()) {
            mLiveCard.unpublish();
            mLiveCard = null;
        }
        super.onDestroy();
    }
}

Solution

  • Simply add more ImageViews, either in your layout file (iot_splash_screen) or programmatically.

    With the resource IDs of your ImageViews, you can call setImageViewResource on each one.

    Make sure that you are setting these images before calling setViews on your Live Card.