How to Launch Glassware from the IDE

2014-08-04 18.37.55-2Launching Glassware by hand sucks. Here’s how to get Eclipse or Android Studio to do it automatically.

You might assume that every time you deploy your Glassware, you have to launch it by voice or menu. It’s an annoying waste of time, and it puts this unnerving warning in the DDMS console:

Screen Cap  2014-08-04 at 5.33.10 PMWhat’s going on here? Eclipse and Android Studio seek out a MAIN Activity that responds to the LAUNCHER intent to automatically launch the app when you deploy it. Glass doesn’t have a launcher, and none of the sample apps have an Activity that could be considered Main, so the IDE farts out the warning and you have to manually launch your app like a chump. Every goddamn time.

In this tutorial, we’ll add a dummy Activity so the IDE will automatically launch the app. As a bonus, we can preload a fake voice result to test apps without having to run test cases through Google’s ultra-sketchy speech-to-text servers.

This tutorial assumes you already have a functional GDK app open in Eclipse. I’m demonstrating on one of my apps, Voidstar Quest, namespace com.voidstar.glass.quest.

Step 1: Add an Activity to your project.

Right-click on your app’s namespace under the src folder and hit New –> Class. Give it a Classy name and make sure it inherits from android.app.Activity. I called mine DebugLauncherActivity.

Screen Cap  2014-08-04 at 5.47.35 PM

Step 2: Add the Activity and intent filter to your manifest.

Open your AndroidManifest.xml and find some nice whitespace inside the application element. Add the below Activity element, making sure to match android:name to your app’s namespace and the name of your new Activity.

<activity
    android:name="com.voidstar.glass.quest.DebugLauncherActivity"
    android:label="@string/title_activity_debug_launcher" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

The intent-filter is the important bit – that’s what Eclipse and Android Studio look for when deploying the app. The android:label is irrelevant, and only nerds who use Launchy will ever see it.

Step 3: Make the Activity launch your Glassware.

Open up the new Activity and override OnCreate():

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Intent intent = new Intent(
        getBaseContext(), QuestService.class);
    // Uncomment to spoof voice results:
    //addSpoofedResultsExtra(intent, "foobar"); 
    startService(intent);
    finish();
}

Of course, if your Glassware’s entry point is an Activity, call startActivity(intent) instead. My entry point is a Service named QuestService, so modify that line to match your app.

We’ll use the commented-out line in Step 4, Spoofing Voice Results. If you don’t need to do so, you’re done! Hit F3 and let the convenience flow through you.

Optional Step 4: Spoofing Voice Results

If your Voice Trigger has a Prompt, testing is a pain in the ass and can’t be expedited through this method. Instead, I directly inject Results into that intent. From the handler’s perspective, it’s identical to the voice prompt. From your perspective, it’s a hell of a lot easier to debug. Uncomment the line above, and add this to the Activity:

public void addSpoofedResultsExtra(Intent intent, String results) {
    ArrayList<String> payload = new ArrayList<String>();
    payload.add(results);
    intent.putStringArrayListExtra(
    RecognizerIntent.EXTRA_RESULTS, payload);
}

Anything passed as the results parameter will be identical to the results returned by voice, making testing reproducible and FAST!!

Conclusion!

Simply adding a LAUNCHER intent-filter to your Glassware allows the IDE to start your app after deployment, speeding up your testing cycle and making tests more reproducible. I highly recommend it.

A warning: Users who have Launchy or mad adb skillz will be able to access this Activity, so make sure you remove it for release! Also remember that this will not automatically turn your Glass on. You may want to put a momentary Wakelock into the code to force Glass to turn on the display.

Thanks for reading, and may you be a highly productive cyborg.

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s