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
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
The Glass Class - Tutorial 4 - GDK-Live Cards
1. The Glass Class:
GDK – Live Cards
July 7th – 11th 2014
Mark Billinghurst, Gun Lee
HIT Lab NZ
University of Canterbury
2. 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
5. 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
6. 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>
7. 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;
}
}
8. 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
9. 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
}
10. 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();
}
11. 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
12. 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);
13. 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
14. 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
15. 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
16. 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
18. 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
19. 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);
}
20. 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) {}
};
21. 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
23. 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
24. 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