the glass class - tutorial 4 - gdk-live cards
Post on 28-Jan-2015
110 Views
Preview:
DESCRIPTION
TRANSCRIPT
The Glass Class:
GDK – Live Cards
July 7th – 11th 2014
Mark Billinghurst, Gun Lee
HIT Lab NZ
University of Canterbury
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
Live cards - Native Glassware sitting on timeline
Live Cards
https://developers.google.com/glass/design/patterns
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
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>
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;
}
}
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
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
}
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();
}
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
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);
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
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
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
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
Live Demo
- Live Card with Counter
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
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);
}
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) {}
};
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
Live Demo
- Location
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
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
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);
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
Sensors
Stop listening when done
http://developer.android.com/guide/topics/sensors/sensors_overview.html
sensorManager.unregisterListener(gravitySensorListener);
Live Demo
- Sensors
Richer Live Cards
https://developers.google.com/glass/design/patterns
Sample Apps in SDK
File > New Project > Android Sample
Project
Choose build target to GDK
Stopwatch
Timer
Compass
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
Summary
Use Mirror API if you need ...
Use GDK if you need ...
Or use both
More Information
Website
https://developers.google.com/glass
http://arforglass.org
http://www.hitlabnz.org
Gun Lee
gun.lee@hitlabnz.org
Mark Billinghurst
mark.billinghurst@hitlabnz.org
top related