Google Glass from a developers perspective

0

During the last two weeks I have had the opportunity to work on a proof of concept that uses the Google Glass. In this post I will share my experiences with this device from a developers perspective.

Probably everyone has heard about the Google Glass, a wearable computer developed by Google X. The Google Glass has already opened up new possibilities and raised numerous concerns. This post will not be about any of that. This post will focus solely on developing for Google Glass; what are the things that you have to keep in mind and what is so special about developing for Google Glass. If you are more interested in the other topics mentioned above, this post is not for you.

Google Glass PoC

Demo of voice controll manual with Google Glass as a support tool for professional engineers

What is Google Glass?

From a developers perspective Google Glass is nothing more than the next Android device, accept from the fact that it is not the next device. The internals of the Google Glass are very similar to a high-end smartphone from 2011, although the newest version of the Google Glass has two gigabytes of RAM instead of the one gigabyte mentioned in the previous mentioned link. The Google Glass has all the usual components, such as a camera, wifi and bluetooth. What makes the Google Glass so very interesting for developers is not the hardware on its own, but the entire package including the Glass Development Kit (GDK) and the guidelines provided by Google for developing for the Google Glass.

What is so special about the GDK?

As mentioned before, Glass is nothing more than the next Android device, at least from a developers perspective. This means that you could develop your applications using the standard tools. However, Google has created an add-on to the standard Android Developer Tools (ADT), the GDK. This toolkit makes it very easy to create applications that adhere to the design guidelines provided by Google. For instance, creating a scrollable list of views (or cards as they are called) can be easily done by creating your own implementation of the abstract class CardScrollAdapter, after which you can use this adapter in the a CardScrollView. An example implementation of the CardScrollAdapter is given below:

package nl.amis.blog.adapters;

import java.util.ArrayList;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;

import nl.amis.blog.CardData;
import nl.amis.blog.CardSet;
import com.google.android.glass.app.Card;
import com.google.android.glass.widget.CardScrollAdapter;

public class CardSetScrollAdapter extends CardScrollAdapter
{

private ArrayList cardCache;
private final Context context;
private CardSet cardSet = null;

public CardSetScrollAdapter(final Context context) {
cardCache = new ArrayList();
this.context = context;
}

public CardSetScrollAdapter(final Context context, final CardSet cardSet) {
this(context);
this.fillCardCache(cardSet);
}

public synchronized void fillCardCache(final CardSet cardSet)
{
this.cardSet = cardSet;
refreshAllCards();
}

private synchronized void refreshAllCards()
{
cardCache = new ArrayList();
int size = this.cardSet.size();

CardData cardData;
for (int index = 0; index < size; index++)
{
cardData = this.cardSet.getCard(index);

this.setCard(cardData);
}

this.notifyDataSetChanged();
}

private synchronized void setCard(CardData cardData)
{
int index = this.cardCache.size();
this.setCard(index, cardData);
}

private synchronized void setCard(int index, CardData cardData)
{
Card card = null;

switch (cardData.type)
{
case TEXT:
String text = cardData.getData();

card = new Card(context);
card.setText(text);

break;
case IMAGE:
// Do something interesting

break;
}

if (index == cardCache.size())
{
cardCache.add(card);
} else
{
cardCache.set(index, card);
}

}

@Override
public synchronized int getPosition(Object item)
{
return cardCache.indexOf(item);
}

@Override
public synchronized int getCount()
{
return cardCache.size();
}

@Override
public int getViewTypeCount()
{
return Card.getViewTypeCount();
}

@Override
public int getItemViewType(int position)
{
return cardCache.get(position).getItemViewType();
}

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
return cardCache.get(position).getView(convertView, parent);
}

@Override
public Object getItem(int position)
{
return cardCache.get(position);
}

}

It can be as easy as that! In this sample, the objects of type CardData are simple data containers and the CardSet is a collection of these data containers. Naturally, it is possible to have different implementations of these CardScrollAdapters. For instance, we made an adapter that got dynamically instantiated and loaded images from remote locations lazily. These lazily loaded images should be shown fullscreen on the glass. The default implementation for this would be something like this (replace comment on line 73):

case IMAGE:
Bitmap image = cardData.getData(this);

card = new Card(context);
card.addImage(image);
card.setImageLayout( ImageLayout.FULL);
break;

The needed imports would be:

import android.graphics.Bitmap;
import com.google.android.glass.app.Card.ImageLayout;

Another cool and easy feature of the GDK is adding contextual voice commands to your application (also known as the “ok glass” menu). It is extremely easy to implement and works very well. It is even possible to define your own voice commands, as long as you use your application has the development permission (please take note: Google does not allow applications that have this permission in their AndroidManifest.xml in the store). What is also very remarkable is the quality of the speech recognition. The custom voice commands are recognized even in noisy environments, as long as the background noise does not consist of human speech.

How to remove gradient from fullscreen image

Of course, there are always little nuisances, especially with new products. One of those little nuisances is the ImageLayout.FULL. It does not only render the image fullscreen, it also adds a gradient to the image, as can be seen in the image below. In the left image, you can clearly see the default gradient added to the image while using the default option for displaying fullscreen images.

On the right, you can see the original image. Currently, there is no Card without this annoying gradient. To fix this problem, we had to go back to ADT and render the image using an ImageView:

ImageView iv = new ImageView(context);
iv.setImageBitmap( image );
iv.setPadding(0, 0, 0, 0);
iv.setAdjustViewBounds(true);
iv.setScaleType(ScaleType.CENTER);

Of course, this is not a big “hack”, if it can be called a hack at all, but it is still annoying that it is not possible to use the provided classes for displaying fullscreen images without a gradient. Furthermore, we need to make more changes to the CardSetScrollAdapter to handle everything correctly. For instance, the method getItemViewType should be adapted to handle this new kind of card correctly.

Also the contextual voice input has some little it’s annoying parts. It is not possible to have the “ok glass” text hidden while still listening for the voice command (or at least, not when using the contextual menu as it is intended), nor can it be repositioned. This means that there will always be the text “ok glass” hovering at the bottom of your screen, ensuring that you lose some of that precious little screen real estate you have available.

Last but not least, there is no possibility to use an emulator for Glass. Google does not provide this option and probably will not provide it in the near feature. The supposed reason for this is that it is important to actually test and design your application on the device itself. Although it is very annoying at the beginning, I think in the end it does help you create a better experience.

In conclusion

It is very easy to develop for Google Glass and Google provides some very useful guidelines to help you get started with this new format. The extra implementations in the GDK are very useful and makes developing for Glass a breeze. Of course, there are some things that could be done differently to make developing for Google Glass even more enjoyable, but none of these form real big problems. Besides, everything about Google Glass is still heavily under development, so these little “annoyances” might be gone when you have finished reading this post.

Share.

About Author

Leave a Reply