The Glass Class - Tutorial 4 - GDK-Live Cards

33
The Glass Class: GDK Live Cards July 7 th 11 th 2014 Mark Billinghurst, Gun Lee HIT Lab NZ University of Canterbury

description

Tutorial 4: GDK - Live Cards The Glass Class at HIT Lab NZ Learn how to program and develop for Google Glass. https://www.youtube.com/watch?v=lnMKRpmtV-o&list=PLsIGb72j1WOlLFoJqkhyugDv-juTEAtas http://arforglass.org http://www.hitlabnz.org

Transcript of The Glass Class - Tutorial 4 - GDK-Live Cards

Page 1: The Glass Class - Tutorial 4 - GDK-Live Cards

The Glass Class:

GDK – Live Cards

July 7th – 11th 2014

Mark Billinghurst, Gun Lee

HIT Lab NZ

University of Canterbury

Page 2: The Glass Class - Tutorial 4 - GDK-Live Cards

An Introduction to

Glassware Development

GDK – Live Cards

Gun Lee

* Images in the slides are from variety of sources,

including http://developer.android.com and http://developers.google.com/glass

Page 3: The Glass Class - Tutorial 4 - GDK-Live Cards

Live cards - Native Glassware sitting on timeline

Page 4: The Glass Class - Tutorial 4 - GDK-Live Cards

Live Cards

https://developers.google.com/glass/design/patterns

Page 5: The Glass Class - Tutorial 4 - GDK-Live Cards

Creating a Live Card Project

Start with normal Android Application Project

(same as Immersions)

Minimum and Target SDK Versions: 19

Compile with: GDK Preview

Theme: None (allows the Glass theme to be applied.)

Your main activity is now used for menu

Rename MainActivity to MenuActivity

Remove launcher/main intent filters from manifest

Add Service as the main component

Page 6: The Glass Class - Tutorial 4 - GDK-Live Cards

Creating a Live Card Project

Add the main service component in the manifest

(next to activity) with voice trigger intent filter <service

android:name=“org.hitlabnz.wailc.LiveCardMainService"

android:label="@string/app_name"

android:enabled="true"

android:exported="true“ >

<intent-filter>

<action android:name="com.google.android.glass.action.VOICE_TRIGGER" />

</intent-filter>

<meta-data

android:name="com.google.android.glass.VoiceTrigger"

android:resource="@xml/voice_trigger" />

</service>

Page 7: The Glass Class - Tutorial 4 - GDK-Live Cards

Creating a Live Card Project

Create the main service Java class

File > New > Class

Then in the dialog

- Give a name to your custom class

- Assign android.app.Service as the super class

public class LiveCardMainService extends Service {

@Override

public IBinder onBind(Intent arg0) {

return null;

}

}

Page 8: The Glass Class - Tutorial 4 - GDK-Live Cards

Implement Live Card Service

onStartCommand()

Called when service is started

Create a live card and publish it

If a live card already exists, show it

onDestroy()

Called when service is destroyed

Unpublish the live card

Page 9: The Glass Class - Tutorial 4 - GDK-Live Cards

Implement Live Card Service public int onStartCommand(Intent intent, int flags, int startId) {

if (mLiveCard == null) {

// create a live card

mLiveCard = new LiveCard(getApplicationContext(),

"SampleLiveCard");

... Setup rendering views & menu activity of the live card ...

// publish it

mLiveCard.publish(LiveCard.PublishMode.REVEAL);

// Use PublishMode.SILENT to not go to the live card

} else {

// show the existing live card

mLiveCard.navigate();

}

return Service.START_STICKY; // keep running

}

Page 10: The Glass Class - Tutorial 4 - GDK-Live Cards

Implement Live Card Service

Unpublish the live card in onDestroy()

@Override

public void onDestroy() {

if (mLiveCard != null) {

if(mLiveCard.isPublished())

mLiveCard.unpublish();

mLiveCard = null;

}

super.onDestroy();

}

Page 11: The Glass Class - Tutorial 4 - GDK-Live Cards

Live Card Rendering

Low-Frequency Update

< 1 fps

Simpler to implement

Limited set of type of

views supported

High-Frequency Update

> 1 fps

More code to implement

More flexible

https://developers.google.com/glass/develop/gdk/ui/live-cards

Page 12: The Glass Class - Tutorial 4 - GDK-Live Cards

Implement Low-Freq. Rendering

Live Card Setup

Update RemoteViews

mRemoteViews =

new RemoteViews(getPackageName(), R.layout.livecard)

mLiveCard.setViews(mRemoteViews);

* Layout can contain TextView, ImageView, Linear/Relative Layouts,

ProgressBar, etc.

http://developer.android.com/reference/android/widget/RemoteViews.html

https://developers.google.com/glass/develop/gdk/ui/live-cards

https://developers.google.com/glass/develop/gdk/reference/com/google/android/glass/timeline/LiveCard

// update the subviews of the RemoteViews object

mRemoteViews.setTextViewText(R.id.textview1, “New Text”);

// trigger udpate

mLiveCard.setViews(mRemoteViews);

Page 13: The Glass Class - Tutorial 4 - GDK-Live Cards

Implement High-Freq. Rendering

Live Card Setup

Draw view in DirectRendering callback Subclass of SurfaceHolder.Callback

- void renderingPaused(SurfaceHolder holder, boolean paused)

Get SurfaceHolder in surfaceCreated()

Run a thread for drawing

- Obtain a Canvas from the SurfaceHolder to draw on it

then release it when done

DirectRendering callback; // initialized somewhere else

mLiveCard.setDirectRenderingEnabled(true);

mLiveCard.getSurfaceHolder().addCallback(callback);

https://developers.google.com/glass/develop/gdk/ui/live-cards

https://developers.google.com/glass/develop/gdk/reference/com/google/android/glass/timeline/LiveCard

Page 14: The Glass Class - Tutorial 4 - GDK-Live Cards

Implement Live Card Menu Setup

Make Live Card to start Menu Activity

when tapped on

// create intent to start MenuActivity

Intent menuIntent = new Intent(this, MenuActivity.class);

// ask live card to issue that intent when tapped on

mLiveCard.setAction(PendingIntent.getActivity(this, 0, menuIntent, 0));

https://developers.google.com/glass/develop/gdk/ui/live-cards

https://developers.google.com/glass/develop/gdk/reference/com/google/android/glass/timeline/LiveCard

Page 15: The Glass Class - Tutorial 4 - GDK-Live Cards

Implement Menu Activity

Menu activity should have no content

Remove method onCreate()

At least one menu item to close the Live Card

Override onOptionsItemSelected() and call

stopService(new Intent(context, MyLiveCard.class))

The menu activity should immediately show

menu after resumed, and finish when the menu

is closed

Override onAttachedToWindow() and call

openOptionsMenu()

Override onOptionsmenuClosed() and call finish()

https://developers.google.com/glass/develop/gdk/ui/live-card-menus

Page 16: The Glass Class - Tutorial 4 - GDK-Live Cards

Implement Menu Activity

Add style to make it transparent

res/values/styles.xml

Assign the style to the menu activity

component in the manifest

<style name="MenuTheme" parent="@android:style/Theme.DeviceDefault">

<item name="android:windowBackground">@android:color/transparent</item>

<item name="android:colorBackgroundCacheHint">@null</item>

<item name="android:windowIsTranslucent">true</item>

<item name="android:windowAnimationStyle">@null</item>

</style>

<activity …

android:theme="@style/MenuTheme" >

https://developers.google.com/glass/develop/gdk/ui/live-card-menus

Page 17: The Glass Class - Tutorial 4 - GDK-Live Cards

Live Demo

- Live Card with Counter

Page 18: The Glass Class - Tutorial 4 - GDK-Live Cards

Location

Setup LocationManager

Location providers

- Glass might have different providers!

• Network, gps, remote

- Choose provider by criteria

Setup listener and start listening

Stop listening when done

Add permission to the manifest file

https://developers.google.com/glass/develop/gdk/location-sensors/index

http://developer.android.com/guide/topics/location/index.html

Page 19: The Glass Class - Tutorial 4 - GDK-Live Cards

Location

Setup LocationManager and Start Listening // get location manager

locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

// get location providers by criteria

Criteria criteria = new Criteria();

criteria.setAccuracy(Criteria.ACCURACY_COARSE);

locationProviders = locationManager.getProviders(criteria, true /* enabledOnly */);

// start listening (request update to listener for each provider)

for (String provider : locationProviders) {

locationManager.requestLocationUpdates(provider,

1000, // update every 1 sec (1000 millisec)

50, // update every 50 meters

locationListener);

}

Page 20: The Glass Class - Tutorial 4 - GDK-Live Cards

Location

Setup Listener LocationListener locationListener = new LocationListener() {

@Override

public void onLocationChanged(Location location) {

// use your location information here

}

@Override

public void onProviderDisabled(String provider) {}

@Override

public void onProviderEnabled(String provider) {}

@Override

public void onStatusChanged(String provider, int status, Bundle extras) {}

};

Page 21: The Glass Class - Tutorial 4 - GDK-Live Cards

Location

Stop listening when done

Add permission to the manifest file

locationManager.removeUpdates(locationListener);

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

* Or use ACCESS_COARSE_LOCATION if you don’t need more accurate providers

Page 22: The Glass Class - Tutorial 4 - GDK-Live Cards

Live Demo

- Location

Page 23: The Glass Class - Tutorial 4 - GDK-Live Cards

Sensors

Sensors on available Glass

Accelerometer, gyroscope, magnetic field,

Gravity, linear acceleration, rotation vector

Light

Motion sensors located on the optic pod

Be aware of battery life

Only use when necessary

Update frequency

https://developers.google.com/glass/develop/gdk/location-sensors/index

Page 24: The Glass Class - Tutorial 4 - GDK-Live Cards

Sensors

Setup SensorManager

Get access to wanted type of sensor

Various types of sensors

- Accelerometer, gyroscope, magnetic field,

- Gravity, linear acceleration, rotation vector

- Light

Setup listener and start listening

Stop listening when done

https://developers.google.com/glass/develop/gdk/location-sensors/index

http://developer.android.com/guide/topics/sensors/sensors_overview.html

Page 25: The Glass Class - Tutorial 4 - GDK-Live Cards

Sensors

Setup SensorManager

http://developer.android.com/guide/topics/sensors/sensors_overview.html

// setup sensor manager

SensorManager sensorManager =

(SensorManager)getSystemService(Context.SENSOR_SERVICE);

// get gravity sensor

Sensor gravitySensor =

sensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY);

// start listening

if(gravitySensor != null)

sensorManager.registerListener(gravitySensorListener,

gravitySensor,

SensorManager.SENSOR_DELAY_NORMAL);

Page 26: The Glass Class - Tutorial 4 - GDK-Live Cards

Sensors

Setup Listener SensorEventListener gravitySensorListener = new SensorEventListener() {

@Override

public void onSensorChanged(SensorEvent event) {

float[] gravityValue = event.values;

}

@Override

public void onAccuracyChanged(Sensor sensor, int accuracy) {}

};

http://developer.android.com/guide/topics/sensors/sensors_motion.html

http://developer.android.com/guide/topics/sensors/sensors_overview.html

Page 27: The Glass Class - Tutorial 4 - GDK-Live Cards

Sensors

Stop listening when done

http://developer.android.com/guide/topics/sensors/sensors_overview.html

sensorManager.unregisterListener(gravitySensorListener);

Page 28: The Glass Class - Tutorial 4 - GDK-Live Cards

Live Demo

- Sensors

Page 29: The Glass Class - Tutorial 4 - GDK-Live Cards

Richer Live Cards

https://developers.google.com/glass/design/patterns

Page 30: The Glass Class - Tutorial 4 - GDK-Live Cards

Sample Apps in SDK

File > New Project > Android Sample

Project

Choose build target to GDK

Stopwatch

Timer

Compass

Page 31: The Glass Class - Tutorial 4 - GDK-Live Cards

GDK

Glass Development Kit

Android 4.4.2 + Glass specific APIs

Sneak peek preview

Look for more components in Android SDK

http://developer.android.com

Page 32: The Glass Class - Tutorial 4 - GDK-Live Cards

Summary

Use Mirror API if you need ...

Use GDK if you need ...

Or use both

Page 33: The Glass Class - Tutorial 4 - GDK-Live Cards

More Information

Website

https://developers.google.com/glass

http://arforglass.org

http://www.hitlabnz.org

Gun Lee

[email protected]

Mark Billinghurst

[email protected]