SlideShare a Scribd company logo
1 of 55
Download to read offline
DroidCon Montréal
2015
ANDROID ONYOUR SLEEVE
1
DroidCon Montréal
2015
2
Building your Super Wearable App
• You've got an idea for the next super app?
• But don't know where to start with Android Wear
development?
• Let's spend some time stretching those Android Wear
development muscles.
DroidCon Montréal
2015
3
What is Android Wear?
• An Android platform for your wrist that can run native apps.
• A stream of cards that you can swipe through and interact with.
• These are notifications delivered from your phone, or displayed directly by
apps on the watch.
• Sensors like step counters, barometer, heart-rate sensor, and accelerometer.
• Customizable watch faces, and the ability to design your own faces.
• A communication channel between your watch and your phone.
• A set of custom components to make application development for the watch
easier.
DroidCon Montréal
2015
4
Types of Android Wear Applications
• Extended Notifications in Phone Apps
• Notifications appear in the watch stream and can be customized with
background images, pages, and layout modifications.
• Phone App with Embedded Wear App
• More powerful and flexible: a full, native,Android app is delivered to the Wear
device which can take full advantage of the platform.
DroidCon Montréal
2015
5
First Wear Wonder: Enhanced Notifications
• Notifications can be extended for wear using the
Android Support Library and the
NotificationCompat.Builder class.
• The NotificationCompat.WearableExtender class provides
properties that Android Wear uses to change the
appearance and behaviour of the notifications on the
watch.
DroidCon Montréal
2015
6
Extending Notifications: Step One
dependencies {

compile 'com.android.support:support-v4:22.0.0'

}
Add the Android Support library to your dependencies in your
App's build.gradle file.
DroidCon Montréal
2015
7
Extending Notifications: Step Two
NotificationCompat.Builder mBuilder =

new NotificationCompat.Builder(this)

.setSmallIcon(R.mipmap.ic_launcher)

.setContentTitle("My notification")

.setContentText("When in danger,nWhen in doubt,nRun in circles,nScream and shout!");



int mNotificationId = 1;

NotificationManager mNotifyMgr =

(NotificationManager) getSystemService(NOTIFICATION_SERVICE);

mNotifyMgr.notify(mNotificationId, mBuilder.build());
Modify your notification building code to extend it for Android
Wear.
DroidCon Montréal
2015
int mNotificationId = 1;

NotificationManager mNotifyMgr =

(NotificationManager) getSystemService(NOTIFICATION_SERVICE);

mNotifyMgr.notify(mNotificationId, mBuilder.build());
8
Extending Notifications: Step Two
NotificationCompat.Builder mBuilder =

new NotificationCompat.Builder(this)

.setSmallIcon(R.mipmap.ic_launcher)

.setContentTitle("My notification")

.setContentText("When in danger,nWhen in doubt,nRun in circles,nScream and shout!");
DroidCon Montréal
2015
int mNotificationId = 1;

NotificationManager mNotifyMgr =

(NotificationManager) getSystemService(NOTIFICATION_SERVICE);

mNotifyMgr.notify(mNotificationId, mBuilder.build());
8
Extending Notifications: Step Two
NotificationCompat.Builder mBuilder =

new NotificationCompat.Builder(this)

.setSmallIcon(R.mipmap.ic_launcher)

.setContentTitle("My notification")

.setContentText("When in danger,nWhen in doubt,nRun in circles,nScream and shout!");
NotificationCompat.WearableExtender wearableExtender = new NotificationCompat.WearableExtender()

.setGravity(Gravity.TOP)

.setHintScreenTimeout(NotificationCompat.WearableExtender.SCREEN_TIMEOUT_LONG);

mBuilder.extend(wearableExtender);
DroidCon Montréal
2015
9
Extending Notifications:Wearable Only Actions
• Adding wearable-only actions can be done by adding one or more actions 

to the wearable extender.
// Create an intent for the action

Intent actionIntent = new Intent(this, LoveItActivity.class);

PendingIntent actionPendingIntent =

PendingIntent.getActivity(this, 0, actionIntent,

PendingIntent.FLAG_UPDATE_CURRENT);
// Create the action

NotificationCompat.Action action =

new NotificationCompat.Action.Builder(R.drawable.ic_love,

getString(R.string.ic_love_label), actionPendingIntent)

.build();
NotificationCompat.WearableExtender wearableExtender = new NotificationCompat.WearableExtender()

.setGravity(Gravity.TOP)

.setHintScreenTimeout(NotificationCompat.WearableExtender.SCREEN_TIMEOUT_LONG);
.addAction(action)
DroidCon Montréal
2015
10
Extending Notifications: Adding Pages
• The WearableExtender provides the ability to add pages to a

notification that can be swiped through.
wearableExtender.addPage(

new NotificationCompat.Builder(this)

.extend(new NotificationCompat.WearableExtender()

.setBackground(BitmapFactory.decodeResource(getResources(), R.drawable.punk))

.setHintAvoidBackgroundClipping(true)

.setHintShowBackgroundOnly(true))

.build());

Source code for this sample app can be found at: http://goo.gl/Kxucib
DroidCon Montréal
2015
11
Extending Notifications: Fake Demo
• In lieu of super demo hardware, you instead receive a fake demo.
• The notification is sent, and on the Watch, the following pages are displayed.
DroidCon Montréal
2015
12
Native App Development
• A native Android Wear app will always be bundled
with an app that's installed from Google Play on the
Phone.
• Android Studio has templates to get you started.
• The Google Play Services Wearable library bridges the
gap between the phone and the watch.
DroidCon Montréal
2015
13
Wear Development Ideas
• Wear Apps can fire their own Notifications.Those notifications will not

be shown on the phone.
• It can be very helpful to share a bit of code between a Wear app and it's
Mobile App counterpart through a shared Android Library.
• The Google Play Services Wearable library provides data synchronization and
message passing, but only passes byte arrays. Libraries can make this much
easier, such as the Courier library by Denley Bihari, which will be discussed later.
• A watch isn't just for watch faces although you can make those as well.
DroidCon Montréal
2015
14
Getting Started With Wear Development
• Android Studio provides a Wear module template.This can be added into your
app, and with a little Gradle Glue, you've added a wear extension to your app.
DroidCon Montréal
2015
15
Finishing the Template Wizard
• Choosing the Display Notification activity leads to some customization.
• Hitting finish, creates a project with

two modules: mobile, and wear.
• The wear module contains two activities 

and a broadcast receiver.
MyStub

BroadcastActivity
MyPostNotification

Receiver
MyDisplay

Activity
Creates NotificationBroadcasts Intent
DroidCon Montréal
2015
16
Running the Wear App During Development
• You're going to want to run the app, and debug it, during development.
• If you have a device with a charging dock that doubles as a USB bridge, 

you can develop rapidly with the device on the dock.
• Otherwise you'll need to use Debugging over Bluetooth or make your own
cable.
DroidCon Montréal
2015
17
Enabling Debugging over Bluetooth
• First, on your Wear device you'll have to
enable developer settings by tapping on
the Settings > About > Build Number a few
times.
• Then in Settings > Developer Settings enable
ADB debugging and Debug over Bluetooth.
DroidCon Montréal
2015
18
Enabling Debugging over Bluetooth
• Next, in the Android Wear app on your
phone, open settings and turn on the
Debugging over Bluetooth
• Initially you will likely see host is
disconnected, so you will need to run
some ADB commands:
• adb forward tcp:4444 localabstract:/adb-hub
• adb connect localhost:4444
DroidCon Montréal
2015
19
The RestYou Already Know
• Your Android Wear device will show up
in Android Studio as another Android
device you can deploy to, and debug on.
• Transfer to the device over Bluetooth
takes quite a while, but it is conveniently
devoid of wires.
• Choose the wear module from the
configurations chooser, hit the run
button, and the app will run on your
watch.
DroidCon Montréal
2015
19
The RestYou Already Know
• Your Android Wear device will show up
in Android Studio as another Android
device you can deploy to, and debug on.
• Transfer to the device over Bluetooth
takes quite a while, but it is conveniently
devoid of wires.
• Choose the wear module from the
configurations chooser, hit the run
button, and the app will run on your
watch.
DroidCon Montréal
2015
20
Gradle Glue and Matryoshka Apps
• The Wear app's dependencies in
build.gradle look like this:
dependencies {

compile 'com.google.android.support:wearable:1.1.0'

compile 'com.google.android.gms:play-services-wearable:6.5.87'

}
• The Mobile app's dependencies

look like this:
dependencies {

wearApp project(':wear')

compile 'com.google.android.gms:play-services:+'

}



• Notice the special wearApp project statement.This is how the wear app

is embedded into the mobile app.Apps inside apps. Like Russian Dolls.
• Only a signed, release build, will automatically deploy the wear app to a

watch. Debug builds do not auto deploy their wear apps.
DroidCon Montréal
2015
21
Mobile and Wear app Symbiosis
• A Wear app is not very interesting without a Mobile app partnered with it.
• The Mobile app and Wear app share data, and can send messages to each
other.
• Let's quickly go through the Android Wear API for sharing data and sending
messages.
DroidCon Montréal
2015
22
Sharing Data and Sending Messages
• It all starts with the GoogleApiClient and the Wearable.API.
// In onCreate
GoogleApiClient mGoogleApiClient = new GoogleApiClient.Builder(this)

.addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {

@Override

public void onConnected(Bundle connectionHint) {

Log.d(TAG, "onConnected: " + connectionHint);

// Now you can use the Data Layer API

}

@Override

public void onConnectionSuspended(int cause) {

Log.d(TAG, "onConnectionSuspended: " + cause);

}

})

.addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {

@Override

public void onConnectionFailed(ConnectionResult result) {

Log.d(TAG, "onConnectionFailed: " + result);

}

})

// Request access only to the Wearable API

.addApi(Wearable.API)

.build();

// In onResume

mGoogleApiClient.connect();

DroidCon Montréal
2015
23
Syncing Data Items
• A Data Item has a Path and a Payload
• A Path is a unique string that starts with a forward slash. Don't use
underscores.
• A Payload is a byte array limited to 100kb.You must serialize the data
yourself. Flexible, but requires some assembly.
DroidCon Montréal
2015
24
Syncing Data Items
• Google recommends using the DataMap class instead. It allows data to

be accessed as key-value pairs. It's used as follows:
DroidCon Montréal
2015
25
Receiving Data Changes and Messages
• For background receipt of Data Item changes, implement a subclass of
WearableListenerService and override some of the methods.
• onMessageReceived - for listening for messages.
• onDataChanged - for detecting Data Item changes.
• onPeerConnected
• onPeerDisconnected
• For Foreground receipt of messages and Data Item changes, you can
implement the DataListener and MessageListener interfaces and register with either
the DataApi, or the MessageApi.
DroidCon Montréal
2015
26
An Even Better Way
• Use the Courier library written by Denley Bihari!
• It wraps up Wearable.DataApi and Wearable.MessageApi into a simple and clean
delivery service for Android Wear.
• Provides an Annotation processor that simplifies serializing objects, and sending
them as either Data Items or Messages.
DroidCon Montréal
2015
27
Courier
• Imagine you want to deliver a message from your phone to a Wear Device
public class Message {

public String mSender;

public String mMessageText;

public long mTimeStamp;



public Message() {

}



public Message(String sender, String messageText, long timeStamp) {

mSender = sender;

mMessageText = messageText;

mTimeStamp = timeStamp;

}

}

import me.denley.courier.Deliverable;



@Deliverable

DroidCon Montréal
2015
28
Courier
• Then in the Activity you want to send from, register with Courier to do the
work:
public class MainActivity extends Activity {



@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Courier.startReceiving(this);

}



@Override

protected void onDestroy() {

Courier.stopReceiving(this);

super.onDestroy();

}



public void onSaySomethingButtonClicked(View button) {

Courier.deliverMessage(this, "/message", "From Mobile!");



Message message = new Message("Me", getRandomPhrase(), System.currentTimeMillis());

Courier.deliverData(this, "/message/data", message);

}

}
DroidCon Montréal
2015
29
Receiving Is Just as Easy
• To Receive data, the code is remarkably similar:
public class ContentActivity extends Activity {



Message mMessage;



@Override

protected void onCreate(Bundle savedInstanceState) {

Courier.startReceiving(this);

}



@Override

protected void onDestroy() {

Courier.stopReceiving(this);

super.onDestroy();

}



private void updateMessage() {

mTextView.setText(mMessage.mMessageText);

}



@ReceiveData("/message/data")

void onMessageChanged(Message message) {

mMessage = message;

updateMessage();

}

}

DroidCon Montréal
2015
30
Courier's Packager
• Receiving messages using a WearableListenerService is possible with Courier's
Packager class doing the deserialization. Handles Serializable and classes marked
with Deliverable annotations.
public class ListenerService extends WearableListenerService {



@Override

public void onMessageReceived(MessageEvent messageEvent) {



if (messageEvent.getPath().equals("/message")) {

String message = Packager.unpack(messageEvent.getData(), String.class);



Intent i = new Intent();

i.setAction("org.nsdev.wearableapp.SHOW_NOTIFICATION");

i.putExtra(MyPostNotificationReceiver.CONTENT_KEY, message);

sendBroadcast(i);

}



super.onMessageReceived(messageEvent);

}

}

DroidCon Montréal
2015
31
Take a Look At Some Code
• While developing this section of the talk, I wrote some code that demonstrates
the details, and the steps of arriving at the final result can be seen in the
commits.
• Check it out at http://goo.gl/mQsFA1
• Check out the Courier library at: http://goo.gl/qAI4wu
• Thanks to Denley Bihari for permission to talk about his project.
DroidCon Montréal
2015
32
Wear UI Classes forYour Toolbelt
• You have a bit of an idea of the mechanics of writing
wear applications, and how to get data to and from
your wear device now, let's look at the UIToolkit.
• Wear UI classes are designed to be easy to use
without a lot of fine touch control.
• The use of cards and swipe gestures is encouraged.
• Applications should be composed of the following
simple building blocks.
DroidCon Montréal
2015
33
Wear UI Patterns
DroidCon Montréal
2015
33
Wear UI Patterns
DroidCon Montréal
2015
33
Wear UI Patterns
• Cards - CardFragment, CardFrame,
CardScrollView
DroidCon Montréal
2015
33
Wear UI Patterns
• Cards - CardFragment, CardFrame,
CardScrollView
• Selection Lists - WearableListView
DroidCon Montréal
2015
33
Wear UI Patterns
• Cards - CardFragment, CardFrame,
CardScrollView
• Selection Lists - WearableListView
• 2D Pickers - GridViewPager
DroidCon Montréal
2015
33
Wear UI Patterns
• Cards - CardFragment, CardFrame,
CardScrollView
• Selection Lists - WearableListView
• 2D Pickers - GridViewPager
• Countdown Pickers -
DelayedConfirmationView
DroidCon Montréal
2015
33
Wear UI Patterns
• Cards - CardFragment, CardFrame,
CardScrollView
• Selection Lists - WearableListView
• 2D Pickers - GridViewPager
• Countdown Pickers -
DelayedConfirmationView
• Long Press to Dismiss -
DismissOverlayView
DroidCon Montréal
2015
34
Round, Square and Butterknife
• You will be needing to use the WatchViewStub to make layouts that work both in
square and round devices.
• The appropriate layout will be chosen automatically.
• Let's go through the layout files and show an example of binding a WatchViewStub
to layouts.
DroidCon Montréal
2015
35
Round, Square and Butterknife
<?xml version="1.0" encoding="utf-8"?>

<android.support.wearable.view.WatchViewStub

xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:id="@+id/watch_view_stub"

android:layout_width="match_parent"

android:layout_height="match_parent"

app:rectLayout="@layout/rect_activity_content"

app:roundLayout="@layout/round_activity_content"

tools:context="org.nsdev.wearableapp.ContentActivity"

tools:deviceIds="wear">

</android.support.wearable.view.WatchViewStub>

activity_content.xml
DroidCon Montréal
2015
36
Round, Square and Butterknife
rect_activity_content.xml
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

tools:context="org.nsdev.wearableapp.ContentActivity"

tools:deviceIds="wear_square">



<TextView

android:id="@+id/text"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/hello_square"/>



<android.support.wearable.view.DismissOverlayView

android:id="@+id/dismiss_overlay"

android:layout_width="match_parent"

android:layout_height="match_parent"

/>



</LinearLayout>
DroidCon Montréal
2015
37
Round, Square and Butterknife
round_activity_content.xml
<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context="org.nsdev.wearableapp.ContentActivity"

tools:deviceIds="wear_round">



<TextView

android:id="@+id/text"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerHorizontal="true"

android:layout_centerVertical="true"

android:textAppearance="@style/TextAppearance.Wearable.Large"

android:textColor="#00F"

android:text="@string/hello_round"/>



<android.support.wearable.view.DismissOverlayView

android:id="@+id/dismiss_overlay"

android:layout_width="match_parent"

android:layout_height="match_parent"

/>



</RelativeLayout>
DroidCon Montréal
2015
38
Round, Square and Butterknife
ContentActivity.java
public class ContentActivity extends Activity {



Message mMessage;



@InjectView(R.id.watch_view_stub)

WatchViewStub mStub;



@Optional

@InjectView(value = R.id.text)

TextView mTextView;



@Optional

@InjectView(R.id.dismiss_overlay)

DismissOverlayView mDismissOverlayView;



private GestureDetector mDetector;
DroidCon Montréal
2015
39
Round, Square and Butterknife
ContentActivity.java

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_content);

ButterKnife.inject(this);



mStub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {

@Override

public void onLayoutInflated(WatchViewStub stub) {

ButterKnife.inject(ContentActivity.this, mStub);



mDismissOverlayView.setIntroText(R.string.dismiss_intro_text);

mDismissOverlayView.showIntroIfNecessary();



mDetector = new GestureDetector(ContentActivity.this,
new GestureDetector.SimpleOnGestureListener() {

@Override

public void onLongPress(MotionEvent e) {

mDismissOverlayView.show();

}

});

}

});

}

DroidCon Montréal
2015
40
Round, Square and Butterknife
ContentActivity.java
@Override

public boolean onTouchEvent(MotionEvent event) {

return mDetector.onTouchEvent(event) || super.onTouchEvent(event);

}
• Let's do something a little interesting with all of this...
• We'll detect a couple more gestures.
DroidCon Montréal
2015
41
Adding Interesting Gestures
ContentActivity.java
mDetector = new GestureDetector(ContentActivity.this, new GestureDetector.SimpleOnGestureListener() {

@Override

public void onLongPress(MotionEvent e) {

mDismissOverlayView.show();

}X

});

DroidCon Montréal
2015
42
Adding Interesting Gestures
ContentActivity.java
mDetector = new GestureDetector(ContentActivity.this, new GestureDetector.SimpleOnGestureListener() {

@Override

public void onLongPress(MotionEvent e) {

mDismissOverlayView.show();

}X

























});

DroidCon Montréal
2015
43
Adding Interesting Gestures
ContentActivity.java
mDetector = new GestureDetector(ContentActivity.this, new GestureDetector.SimpleOnGestureListener() {

@Override

public void onLongPress(MotionEvent e) {

mDismissOverlayView.show();

}X



@Override

public boolean onSingleTapConfirmed(MotionEvent e) {

Courier.deliverMessage(ContentActivity.this, "/keynote/command", "next");

return true;

}



@Override

public boolean onDoubleTap(MotionEvent e) {

Courier.deliverMessage(ContentActivity.this, "/keynote/command", "previous");

return true;

}

});



DroidCon Montréal
2015
44
Mobile App Modifications
MainActivity.java
@ReceiveMessages("/keynote/command")

public void onKeynoteMessage(String message) {

try {

runCommand(message);

} catch (Exception e) {

e.printStackTrace();

}

}



private final OkHttpClient client = new OkHttpClient();



public void runCommand(final String command) throws Exception {

Request request = new Request.Builder()

.get()

.url(String.format("http://%s/json/%s", mServerHost.getText().toString(), command))

.build();



client.newCall(request).enqueue(new Callback() {



@Override

public void onFailure(Request request, IOException e) {

e.printStackTrace();

}



DroidCon Montréal
2015
45
Mobile App Modifications
MainActivity.java
@Override

public void onResponse(Response response) throws IOException {

if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);



try {

JSONObject responseJson = new JSONObject(response.body().string());



String slide = responseJson.optString("slide");

String build = responseJson.optString("build");



Message message = new Message("Me", "Slide: " + slide + "nBuild: " + build,
System.currentTimeMillis());

Courier.deliverData(MainActivity.this, "/message/data", message);



} catch (JSONException e) {

e.printStackTrace();

}

}

});

}





DroidCon Montréal
2015
46
Notable Wear Libraries and Tools
• By Denley Bihari: Courier, WearPreferenceActivity, and
WearPrefs
• Jimu Labs Mirror and Sandbox - jimulabs.com
• Everything Square Does, ever!
• Everything Jake Wharton Does, ever!
DroidCon Montréal
2015
CONFIDENTIAL INFORMATION - NOT FOR DISTRIBUTION.
THANK YOU :)
47
neal.sanche@rnp.io
@thorinside
Art: Mary Sanche, @thoughtsupnorth

More Related Content

What's hot

What's hot (6)

Bringing Mobile Vision to Wearables
Bringing Mobile Vision to Wearables Bringing Mobile Vision to Wearables
Bringing Mobile Vision to Wearables
 
Android wear notes
Android wear notesAndroid wear notes
Android wear notes
 
Break Timer: Android-wear introduction and application case-study
Break Timer: Android-wear introduction and application case-studyBreak Timer: Android-wear introduction and application case-study
Break Timer: Android-wear introduction and application case-study
 
Android Wearable App
Android Wearable AppAndroid Wearable App
Android Wearable App
 
How to create android applications
How to create android applicationsHow to create android applications
How to create android applications
 
Android Development Tutorial V3
Android Development Tutorial   V3Android Development Tutorial   V3
Android Development Tutorial V3
 

Viewers also liked

Viewers also liked (18)

Bureau of Non-Formal Education(BNFE)
Bureau of Non-Formal Education(BNFE)Bureau of Non-Formal Education(BNFE)
Bureau of Non-Formal Education(BNFE)
 
Cida
CidaCida
Cida
 
Folk school n clc
Folk school n clcFolk school n clc
Folk school n clc
 
C.V. darshana
C.V.  darshanaC.V.  darshana
C.V. darshana
 
An international model of world class education
An international model of world class educationAn international model of world class education
An international model of world class education
 
Income generating program
Income generating programIncome generating program
Income generating program
 
Individual interest promotion programme-: ATLP-CE
Individual interest promotion programme-: ATLP-CEIndividual interest promotion programme-: ATLP-CE
Individual interest promotion programme-: ATLP-CE
 
Ple y symbaloo
Ple y symbaloo Ple y symbaloo
Ple y symbaloo
 
Community learning center
Community learning centerCommunity learning center
Community learning center
 
ilginççç
ilginçççilginççç
ilginççç
 
Brac ib norway
Brac ib norwayBrac ib norway
Brac ib norway
 
Action Aid In Bangladesh
Action Aid In BangladeshAction Aid In Bangladesh
Action Aid In Bangladesh
 
Canibalia
CanibaliaCanibalia
Canibalia
 
Equivalency Programme
Equivalency Programme   Equivalency Programme
Equivalency Programme
 
Room to read
Room to readRoom to read
Room to read
 
Education in australia
Education in australiaEducation in australia
Education in australia
 
Olacab ppt
Olacab pptOlacab ppt
Olacab ppt
 
Diseño
DiseñoDiseño
Diseño
 

Similar to Android On Your Sleeve - DroidCon Montreal 2015

Similar to Android On Your Sleeve - DroidCon Montreal 2015 (20)

Android Wear - Manuel Vicente Vivo
Android Wear - Manuel Vicente VivoAndroid Wear - Manuel Vicente Vivo
Android Wear - Manuel Vicente Vivo
 
Google IO Devbytes 2014
Google IO Devbytes 2014Google IO Devbytes 2014
Google IO Devbytes 2014
 
Discover Android Wear
Discover Android WearDiscover Android Wear
Discover Android Wear
 
Build your own remote control. Droidcon greece 2016
Build your own remote control. Droidcon greece 2016Build your own remote control. Droidcon greece 2016
Build your own remote control. Droidcon greece 2016
 
Android application developement
Android application developementAndroid application developement
Android application developement
 
Android class provider in mumbai
Android class provider in mumbaiAndroid class provider in mumbai
Android class provider in mumbai
 
Londroid meetup
Londroid meetupLondroid meetup
Londroid meetup
 
Introduction to Android - Mobile Fest Singapore 2009
Introduction to Android - Mobile Fest Singapore 2009Introduction to Android - Mobile Fest Singapore 2009
Introduction to Android - Mobile Fest Singapore 2009
 
Android Interview Questions
Android Interview QuestionsAndroid Interview Questions
Android Interview Questions
 
Android interview questions
Android interview questionsAndroid interview questions
Android interview questions
 
June 2014 - Android wear
June 2014 - Android wearJune 2014 - Android wear
June 2014 - Android wear
 
Android wear notes
Android wear notesAndroid wear notes
Android wear notes
 
android phone ppt
android phone pptandroid phone ppt
android phone ppt
 
Getting Started with Android - OSSPAC 2009
Getting Started with Android - OSSPAC 2009Getting Started with Android - OSSPAC 2009
Getting Started with Android - OSSPAC 2009
 
001-Mobile Application.pptx
001-Mobile Application.pptx001-Mobile Application.pptx
001-Mobile Application.pptx
 
Native vs Hybrid - Options to develop your mobile application
Native vs Hybrid - Options to develop your mobile applicationNative vs Hybrid - Options to develop your mobile application
Native vs Hybrid - Options to develop your mobile application
 
NTXISSACSC3 - Manage Mobile Security Incidents like A Boss by Ismail Guneydas
NTXISSACSC3 - Manage Mobile Security Incidents like A Boss by Ismail Guneydas NTXISSACSC3 - Manage Mobile Security Incidents like A Boss by Ismail Guneydas
NTXISSACSC3 - Manage Mobile Security Incidents like A Boss by Ismail Guneydas
 
Turn your smartphone into a sensor.pptx
Turn your smartphone into a sensor.pptxTurn your smartphone into a sensor.pptx
Turn your smartphone into a sensor.pptx
 
Real-Time Applications with SignalR and Vue by Girish Sonewane.docx.pdf
Real-Time Applications with SignalR and Vue by Girish Sonewane.docx.pdfReal-Time Applications with SignalR and Vue by Girish Sonewane.docx.pdf
Real-Time Applications with SignalR and Vue by Girish Sonewane.docx.pdf
 
Android 130923124440-phpapp01
Android 130923124440-phpapp01Android 130923124440-phpapp01
Android 130923124440-phpapp01
 

Recently uploaded

Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...
Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...
Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...
Lisi Hocke
 

Recently uploaded (20)

Automate your OpenSIPS config tests - OpenSIPS Summit 2024
Automate your OpenSIPS config tests - OpenSIPS Summit 2024Automate your OpenSIPS config tests - OpenSIPS Summit 2024
Automate your OpenSIPS config tests - OpenSIPS Summit 2024
 
Abortion Clinic Pretoria ](+27832195400*)[ Abortion Clinic Near Me ● Abortion...
Abortion Clinic Pretoria ](+27832195400*)[ Abortion Clinic Near Me ● Abortion...Abortion Clinic Pretoria ](+27832195400*)[ Abortion Clinic Near Me ● Abortion...
Abortion Clinic Pretoria ](+27832195400*)[ Abortion Clinic Near Me ● Abortion...
 
GraphSummit Milan - Neo4j: The Art of the Possible with Graph
GraphSummit Milan - Neo4j: The Art of the Possible with GraphGraphSummit Milan - Neo4j: The Art of the Possible with Graph
GraphSummit Milan - Neo4j: The Art of the Possible with Graph
 
Abortion Pill Prices Jozini ](+27832195400*)[ 🏥 Women's Abortion Clinic in Jo...
Abortion Pill Prices Jozini ](+27832195400*)[ 🏥 Women's Abortion Clinic in Jo...Abortion Pill Prices Jozini ](+27832195400*)[ 🏥 Women's Abortion Clinic in Jo...
Abortion Pill Prices Jozini ](+27832195400*)[ 🏥 Women's Abortion Clinic in Jo...
 
BusinessGPT - Security and Governance for Generative AI
BusinessGPT  - Security and Governance for Generative AIBusinessGPT  - Security and Governance for Generative AI
BusinessGPT - Security and Governance for Generative AI
 
[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypse
[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypse[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypse
[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypse
 
Workshop: Enabling GenAI Breakthroughs with Knowledge Graphs - GraphSummit Milan
Workshop: Enabling GenAI Breakthroughs with Knowledge Graphs - GraphSummit MilanWorkshop: Enabling GenAI Breakthroughs with Knowledge Graphs - GraphSummit Milan
Workshop: Enabling GenAI Breakthroughs with Knowledge Graphs - GraphSummit Milan
 
Food Delivery Business App Development Guide 2024
Food Delivery Business App Development Guide 2024Food Delivery Business App Development Guide 2024
Food Delivery Business App Development Guide 2024
 
Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...
Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...
Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...
 
CERVED e Neo4j su una nuvola, migrazione ed evoluzione di un grafo mission cr...
CERVED e Neo4j su una nuvola, migrazione ed evoluzione di un grafo mission cr...CERVED e Neo4j su una nuvola, migrazione ed evoluzione di un grafo mission cr...
CERVED e Neo4j su una nuvola, migrazione ed evoluzione di un grafo mission cr...
 
Modern binary build systems - PyCon 2024
Modern binary build systems - PyCon 2024Modern binary build systems - PyCon 2024
Modern binary build systems - PyCon 2024
 
[GRCPP] Introduction to concepts (C++20)
[GRCPP] Introduction to concepts (C++20)[GRCPP] Introduction to concepts (C++20)
[GRCPP] Introduction to concepts (C++20)
 
Workshop - Architecting Innovative Graph Applications- GraphSummit Milan
Workshop -  Architecting Innovative Graph Applications- GraphSummit MilanWorkshop -  Architecting Innovative Graph Applications- GraphSummit Milan
Workshop - Architecting Innovative Graph Applications- GraphSummit Milan
 
GraphSummit Milan - Visione e roadmap del prodotto Neo4j
GraphSummit Milan - Visione e roadmap del prodotto Neo4jGraphSummit Milan - Visione e roadmap del prodotto Neo4j
GraphSummit Milan - Visione e roadmap del prodotto Neo4j
 
Abortion Clinic In Pretoria ](+27832195400*)[ 🏥 Safe Abortion Pills in Pretor...
Abortion Clinic In Pretoria ](+27832195400*)[ 🏥 Safe Abortion Pills in Pretor...Abortion Clinic In Pretoria ](+27832195400*)[ 🏥 Safe Abortion Pills in Pretor...
Abortion Clinic In Pretoria ](+27832195400*)[ 🏥 Safe Abortion Pills in Pretor...
 
Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...
Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...
Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...
 
Encryption Recap: A Refresher on Key Concepts
Encryption Recap: A Refresher on Key ConceptsEncryption Recap: A Refresher on Key Concepts
Encryption Recap: A Refresher on Key Concepts
 
From Theory to Practice: Utilizing SpiraPlan's REST API
From Theory to Practice: Utilizing SpiraPlan's REST APIFrom Theory to Practice: Utilizing SpiraPlan's REST API
From Theory to Practice: Utilizing SpiraPlan's REST API
 
Evolving Data Governance for the Real-time Streaming and AI Era
Evolving Data Governance for the Real-time Streaming and AI EraEvolving Data Governance for the Real-time Streaming and AI Era
Evolving Data Governance for the Real-time Streaming and AI Era
 
Lessons Learned from Building a Serverless Notifications System.pdf
Lessons Learned from Building a Serverless Notifications System.pdfLessons Learned from Building a Serverless Notifications System.pdf
Lessons Learned from Building a Serverless Notifications System.pdf
 

Android On Your Sleeve - DroidCon Montreal 2015

  • 2. DroidCon Montréal 2015 2 Building your Super Wearable App • You've got an idea for the next super app? • But don't know where to start with Android Wear development? • Let's spend some time stretching those Android Wear development muscles.
  • 3. DroidCon Montréal 2015 3 What is Android Wear? • An Android platform for your wrist that can run native apps. • A stream of cards that you can swipe through and interact with. • These are notifications delivered from your phone, or displayed directly by apps on the watch. • Sensors like step counters, barometer, heart-rate sensor, and accelerometer. • Customizable watch faces, and the ability to design your own faces. • A communication channel between your watch and your phone. • A set of custom components to make application development for the watch easier.
  • 4. DroidCon Montréal 2015 4 Types of Android Wear Applications • Extended Notifications in Phone Apps • Notifications appear in the watch stream and can be customized with background images, pages, and layout modifications. • Phone App with Embedded Wear App • More powerful and flexible: a full, native,Android app is delivered to the Wear device which can take full advantage of the platform.
  • 5. DroidCon Montréal 2015 5 First Wear Wonder: Enhanced Notifications • Notifications can be extended for wear using the Android Support Library and the NotificationCompat.Builder class. • The NotificationCompat.WearableExtender class provides properties that Android Wear uses to change the appearance and behaviour of the notifications on the watch.
  • 6. DroidCon Montréal 2015 6 Extending Notifications: Step One dependencies {
 compile 'com.android.support:support-v4:22.0.0'
 } Add the Android Support library to your dependencies in your App's build.gradle file.
  • 7. DroidCon Montréal 2015 7 Extending Notifications: Step Two NotificationCompat.Builder mBuilder =
 new NotificationCompat.Builder(this)
 .setSmallIcon(R.mipmap.ic_launcher)
 .setContentTitle("My notification")
 .setContentText("When in danger,nWhen in doubt,nRun in circles,nScream and shout!");
 
 int mNotificationId = 1;
 NotificationManager mNotifyMgr =
 (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
 mNotifyMgr.notify(mNotificationId, mBuilder.build()); Modify your notification building code to extend it for Android Wear.
  • 8. DroidCon Montréal 2015 int mNotificationId = 1;
 NotificationManager mNotifyMgr =
 (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
 mNotifyMgr.notify(mNotificationId, mBuilder.build()); 8 Extending Notifications: Step Two NotificationCompat.Builder mBuilder =
 new NotificationCompat.Builder(this)
 .setSmallIcon(R.mipmap.ic_launcher)
 .setContentTitle("My notification")
 .setContentText("When in danger,nWhen in doubt,nRun in circles,nScream and shout!");
  • 9. DroidCon Montréal 2015 int mNotificationId = 1;
 NotificationManager mNotifyMgr =
 (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
 mNotifyMgr.notify(mNotificationId, mBuilder.build()); 8 Extending Notifications: Step Two NotificationCompat.Builder mBuilder =
 new NotificationCompat.Builder(this)
 .setSmallIcon(R.mipmap.ic_launcher)
 .setContentTitle("My notification")
 .setContentText("When in danger,nWhen in doubt,nRun in circles,nScream and shout!"); NotificationCompat.WearableExtender wearableExtender = new NotificationCompat.WearableExtender()
 .setGravity(Gravity.TOP)
 .setHintScreenTimeout(NotificationCompat.WearableExtender.SCREEN_TIMEOUT_LONG);
 mBuilder.extend(wearableExtender);
  • 10. DroidCon Montréal 2015 9 Extending Notifications:Wearable Only Actions • Adding wearable-only actions can be done by adding one or more actions 
 to the wearable extender. // Create an intent for the action
 Intent actionIntent = new Intent(this, LoveItActivity.class);
 PendingIntent actionPendingIntent =
 PendingIntent.getActivity(this, 0, actionIntent,
 PendingIntent.FLAG_UPDATE_CURRENT); // Create the action
 NotificationCompat.Action action =
 new NotificationCompat.Action.Builder(R.drawable.ic_love,
 getString(R.string.ic_love_label), actionPendingIntent)
 .build(); NotificationCompat.WearableExtender wearableExtender = new NotificationCompat.WearableExtender()
 .setGravity(Gravity.TOP)
 .setHintScreenTimeout(NotificationCompat.WearableExtender.SCREEN_TIMEOUT_LONG); .addAction(action)
  • 11. DroidCon Montréal 2015 10 Extending Notifications: Adding Pages • The WearableExtender provides the ability to add pages to a
 notification that can be swiped through. wearableExtender.addPage(
 new NotificationCompat.Builder(this)
 .extend(new NotificationCompat.WearableExtender()
 .setBackground(BitmapFactory.decodeResource(getResources(), R.drawable.punk))
 .setHintAvoidBackgroundClipping(true)
 .setHintShowBackgroundOnly(true))
 .build());
 Source code for this sample app can be found at: http://goo.gl/Kxucib
  • 12. DroidCon Montréal 2015 11 Extending Notifications: Fake Demo • In lieu of super demo hardware, you instead receive a fake demo. • The notification is sent, and on the Watch, the following pages are displayed.
  • 13. DroidCon Montréal 2015 12 Native App Development • A native Android Wear app will always be bundled with an app that's installed from Google Play on the Phone. • Android Studio has templates to get you started. • The Google Play Services Wearable library bridges the gap between the phone and the watch.
  • 14. DroidCon Montréal 2015 13 Wear Development Ideas • Wear Apps can fire their own Notifications.Those notifications will not
 be shown on the phone. • It can be very helpful to share a bit of code between a Wear app and it's Mobile App counterpart through a shared Android Library. • The Google Play Services Wearable library provides data synchronization and message passing, but only passes byte arrays. Libraries can make this much easier, such as the Courier library by Denley Bihari, which will be discussed later. • A watch isn't just for watch faces although you can make those as well.
  • 15. DroidCon Montréal 2015 14 Getting Started With Wear Development • Android Studio provides a Wear module template.This can be added into your app, and with a little Gradle Glue, you've added a wear extension to your app.
  • 16. DroidCon Montréal 2015 15 Finishing the Template Wizard • Choosing the Display Notification activity leads to some customization. • Hitting finish, creates a project with
 two modules: mobile, and wear. • The wear module contains two activities 
 and a broadcast receiver. MyStub
 BroadcastActivity MyPostNotification
 Receiver MyDisplay
 Activity Creates NotificationBroadcasts Intent
  • 17. DroidCon Montréal 2015 16 Running the Wear App During Development • You're going to want to run the app, and debug it, during development. • If you have a device with a charging dock that doubles as a USB bridge, 
 you can develop rapidly with the device on the dock. • Otherwise you'll need to use Debugging over Bluetooth or make your own cable.
  • 18. DroidCon Montréal 2015 17 Enabling Debugging over Bluetooth • First, on your Wear device you'll have to enable developer settings by tapping on the Settings > About > Build Number a few times. • Then in Settings > Developer Settings enable ADB debugging and Debug over Bluetooth.
  • 19. DroidCon Montréal 2015 18 Enabling Debugging over Bluetooth • Next, in the Android Wear app on your phone, open settings and turn on the Debugging over Bluetooth • Initially you will likely see host is disconnected, so you will need to run some ADB commands: • adb forward tcp:4444 localabstract:/adb-hub • adb connect localhost:4444
  • 20. DroidCon Montréal 2015 19 The RestYou Already Know • Your Android Wear device will show up in Android Studio as another Android device you can deploy to, and debug on. • Transfer to the device over Bluetooth takes quite a while, but it is conveniently devoid of wires. • Choose the wear module from the configurations chooser, hit the run button, and the app will run on your watch.
  • 21. DroidCon Montréal 2015 19 The RestYou Already Know • Your Android Wear device will show up in Android Studio as another Android device you can deploy to, and debug on. • Transfer to the device over Bluetooth takes quite a while, but it is conveniently devoid of wires. • Choose the wear module from the configurations chooser, hit the run button, and the app will run on your watch.
  • 22. DroidCon Montréal 2015 20 Gradle Glue and Matryoshka Apps • The Wear app's dependencies in build.gradle look like this: dependencies {
 compile 'com.google.android.support:wearable:1.1.0'
 compile 'com.google.android.gms:play-services-wearable:6.5.87'
 } • The Mobile app's dependencies
 look like this: dependencies {
 wearApp project(':wear')
 compile 'com.google.android.gms:play-services:+'
 }
 
 • Notice the special wearApp project statement.This is how the wear app
 is embedded into the mobile app.Apps inside apps. Like Russian Dolls. • Only a signed, release build, will automatically deploy the wear app to a
 watch. Debug builds do not auto deploy their wear apps.
  • 23. DroidCon Montréal 2015 21 Mobile and Wear app Symbiosis • A Wear app is not very interesting without a Mobile app partnered with it. • The Mobile app and Wear app share data, and can send messages to each other. • Let's quickly go through the Android Wear API for sharing data and sending messages.
  • 24. DroidCon Montréal 2015 22 Sharing Data and Sending Messages • It all starts with the GoogleApiClient and the Wearable.API. // In onCreate GoogleApiClient mGoogleApiClient = new GoogleApiClient.Builder(this)
 .addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
 @Override
 public void onConnected(Bundle connectionHint) {
 Log.d(TAG, "onConnected: " + connectionHint);
 // Now you can use the Data Layer API
 }
 @Override
 public void onConnectionSuspended(int cause) {
 Log.d(TAG, "onConnectionSuspended: " + cause);
 }
 })
 .addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
 @Override
 public void onConnectionFailed(ConnectionResult result) {
 Log.d(TAG, "onConnectionFailed: " + result);
 }
 })
 // Request access only to the Wearable API
 .addApi(Wearable.API)
 .build();
 // In onResume
 mGoogleApiClient.connect();

  • 25. DroidCon Montréal 2015 23 Syncing Data Items • A Data Item has a Path and a Payload • A Path is a unique string that starts with a forward slash. Don't use underscores. • A Payload is a byte array limited to 100kb.You must serialize the data yourself. Flexible, but requires some assembly.
  • 26. DroidCon Montréal 2015 24 Syncing Data Items • Google recommends using the DataMap class instead. It allows data to
 be accessed as key-value pairs. It's used as follows:
  • 27. DroidCon Montréal 2015 25 Receiving Data Changes and Messages • For background receipt of Data Item changes, implement a subclass of WearableListenerService and override some of the methods. • onMessageReceived - for listening for messages. • onDataChanged - for detecting Data Item changes. • onPeerConnected • onPeerDisconnected • For Foreground receipt of messages and Data Item changes, you can implement the DataListener and MessageListener interfaces and register with either the DataApi, or the MessageApi.
  • 28. DroidCon Montréal 2015 26 An Even Better Way • Use the Courier library written by Denley Bihari! • It wraps up Wearable.DataApi and Wearable.MessageApi into a simple and clean delivery service for Android Wear. • Provides an Annotation processor that simplifies serializing objects, and sending them as either Data Items or Messages.
  • 29. DroidCon Montréal 2015 27 Courier • Imagine you want to deliver a message from your phone to a Wear Device public class Message {
 public String mSender;
 public String mMessageText;
 public long mTimeStamp;
 
 public Message() {
 }
 
 public Message(String sender, String messageText, long timeStamp) {
 mSender = sender;
 mMessageText = messageText;
 mTimeStamp = timeStamp;
 }
 }
 import me.denley.courier.Deliverable;
 
 @Deliverable

  • 30. DroidCon Montréal 2015 28 Courier • Then in the Activity you want to send from, register with Courier to do the work: public class MainActivity extends Activity {
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 Courier.startReceiving(this);
 }
 
 @Override
 protected void onDestroy() {
 Courier.stopReceiving(this);
 super.onDestroy();
 }
 
 public void onSaySomethingButtonClicked(View button) {
 Courier.deliverMessage(this, "/message", "From Mobile!");
 
 Message message = new Message("Me", getRandomPhrase(), System.currentTimeMillis());
 Courier.deliverData(this, "/message/data", message);
 }
 }
  • 31. DroidCon Montréal 2015 29 Receiving Is Just as Easy • To Receive data, the code is remarkably similar: public class ContentActivity extends Activity {
 
 Message mMessage;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 Courier.startReceiving(this);
 }
 
 @Override
 protected void onDestroy() {
 Courier.stopReceiving(this);
 super.onDestroy();
 }
 
 private void updateMessage() {
 mTextView.setText(mMessage.mMessageText);
 }
 
 @ReceiveData("/message/data")
 void onMessageChanged(Message message) {
 mMessage = message;
 updateMessage();
 }
 }

  • 32. DroidCon Montréal 2015 30 Courier's Packager • Receiving messages using a WearableListenerService is possible with Courier's Packager class doing the deserialization. Handles Serializable and classes marked with Deliverable annotations. public class ListenerService extends WearableListenerService {
 
 @Override
 public void onMessageReceived(MessageEvent messageEvent) {
 
 if (messageEvent.getPath().equals("/message")) {
 String message = Packager.unpack(messageEvent.getData(), String.class);
 
 Intent i = new Intent();
 i.setAction("org.nsdev.wearableapp.SHOW_NOTIFICATION");
 i.putExtra(MyPostNotificationReceiver.CONTENT_KEY, message);
 sendBroadcast(i);
 }
 
 super.onMessageReceived(messageEvent);
 }
 }

  • 33. DroidCon Montréal 2015 31 Take a Look At Some Code • While developing this section of the talk, I wrote some code that demonstrates the details, and the steps of arriving at the final result can be seen in the commits. • Check it out at http://goo.gl/mQsFA1 • Check out the Courier library at: http://goo.gl/qAI4wu • Thanks to Denley Bihari for permission to talk about his project.
  • 34. DroidCon Montréal 2015 32 Wear UI Classes forYour Toolbelt • You have a bit of an idea of the mechanics of writing wear applications, and how to get data to and from your wear device now, let's look at the UIToolkit. • Wear UI classes are designed to be easy to use without a lot of fine touch control. • The use of cards and swipe gestures is encouraged. • Applications should be composed of the following simple building blocks.
  • 37. DroidCon Montréal 2015 33 Wear UI Patterns • Cards - CardFragment, CardFrame, CardScrollView
  • 38. DroidCon Montréal 2015 33 Wear UI Patterns • Cards - CardFragment, CardFrame, CardScrollView • Selection Lists - WearableListView
  • 39. DroidCon Montréal 2015 33 Wear UI Patterns • Cards - CardFragment, CardFrame, CardScrollView • Selection Lists - WearableListView • 2D Pickers - GridViewPager
  • 40. DroidCon Montréal 2015 33 Wear UI Patterns • Cards - CardFragment, CardFrame, CardScrollView • Selection Lists - WearableListView • 2D Pickers - GridViewPager • Countdown Pickers - DelayedConfirmationView
  • 41. DroidCon Montréal 2015 33 Wear UI Patterns • Cards - CardFragment, CardFrame, CardScrollView • Selection Lists - WearableListView • 2D Pickers - GridViewPager • Countdown Pickers - DelayedConfirmationView • Long Press to Dismiss - DismissOverlayView
  • 42. DroidCon Montréal 2015 34 Round, Square and Butterknife • You will be needing to use the WatchViewStub to make layouts that work both in square and round devices. • The appropriate layout will be chosen automatically. • Let's go through the layout files and show an example of binding a WatchViewStub to layouts.
  • 43. DroidCon Montréal 2015 35 Round, Square and Butterknife <?xml version="1.0" encoding="utf-8"?>
 <android.support.wearable.view.WatchViewStub
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 xmlns:tools="http://schemas.android.com/tools"
 android:id="@+id/watch_view_stub"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 app:rectLayout="@layout/rect_activity_content"
 app:roundLayout="@layout/round_activity_content"
 tools:context="org.nsdev.wearableapp.ContentActivity"
 tools:deviceIds="wear">
 </android.support.wearable.view.WatchViewStub>
 activity_content.xml
  • 44. DroidCon Montréal 2015 36 Round, Square and Butterknife rect_activity_content.xml <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical"
 tools:context="org.nsdev.wearableapp.ContentActivity"
 tools:deviceIds="wear_square">
 
 <TextView
 android:id="@+id/text"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="@string/hello_square"/>
 
 <android.support.wearable.view.DismissOverlayView
 android:id="@+id/dismiss_overlay"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 />
 
 </LinearLayout>
  • 45. DroidCon Montréal 2015 37 Round, Square and Butterknife round_activity_content.xml <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context="org.nsdev.wearableapp.ContentActivity"
 tools:deviceIds="wear_round">
 
 <TextView
 android:id="@+id/text"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 android:textAppearance="@style/TextAppearance.Wearable.Large"
 android:textColor="#00F"
 android:text="@string/hello_round"/>
 
 <android.support.wearable.view.DismissOverlayView
 android:id="@+id/dismiss_overlay"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 />
 
 </RelativeLayout>
  • 46. DroidCon Montréal 2015 38 Round, Square and Butterknife ContentActivity.java public class ContentActivity extends Activity {
 
 Message mMessage;
 
 @InjectView(R.id.watch_view_stub)
 WatchViewStub mStub;
 
 @Optional
 @InjectView(value = R.id.text)
 TextView mTextView;
 
 @Optional
 @InjectView(R.id.dismiss_overlay)
 DismissOverlayView mDismissOverlayView;
 
 private GestureDetector mDetector;
  • 47. DroidCon Montréal 2015 39 Round, Square and Butterknife ContentActivity.java
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_content);
 ButterKnife.inject(this);
 
 mStub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
 @Override
 public void onLayoutInflated(WatchViewStub stub) {
 ButterKnife.inject(ContentActivity.this, mStub);
 
 mDismissOverlayView.setIntroText(R.string.dismiss_intro_text);
 mDismissOverlayView.showIntroIfNecessary();
 
 mDetector = new GestureDetector(ContentActivity.this, new GestureDetector.SimpleOnGestureListener() {
 @Override
 public void onLongPress(MotionEvent e) {
 mDismissOverlayView.show();
 }
 });
 }
 });
 }

  • 48. DroidCon Montréal 2015 40 Round, Square and Butterknife ContentActivity.java @Override
 public boolean onTouchEvent(MotionEvent event) {
 return mDetector.onTouchEvent(event) || super.onTouchEvent(event);
 } • Let's do something a little interesting with all of this... • We'll detect a couple more gestures.
  • 49. DroidCon Montréal 2015 41 Adding Interesting Gestures ContentActivity.java mDetector = new GestureDetector(ContentActivity.this, new GestureDetector.SimpleOnGestureListener() {
 @Override
 public void onLongPress(MotionEvent e) {
 mDismissOverlayView.show();
 }X
 });

  • 50. DroidCon Montréal 2015 42 Adding Interesting Gestures ContentActivity.java mDetector = new GestureDetector(ContentActivity.this, new GestureDetector.SimpleOnGestureListener() {
 @Override
 public void onLongPress(MotionEvent e) {
 mDismissOverlayView.show();
 }X
 
 
 
 
 
 
 
 
 
 
 
 
 });

  • 51. DroidCon Montréal 2015 43 Adding Interesting Gestures ContentActivity.java mDetector = new GestureDetector(ContentActivity.this, new GestureDetector.SimpleOnGestureListener() {
 @Override
 public void onLongPress(MotionEvent e) {
 mDismissOverlayView.show();
 }X
 
 @Override
 public boolean onSingleTapConfirmed(MotionEvent e) {
 Courier.deliverMessage(ContentActivity.this, "/keynote/command", "next");
 return true;
 }
 
 @Override
 public boolean onDoubleTap(MotionEvent e) {
 Courier.deliverMessage(ContentActivity.this, "/keynote/command", "previous");
 return true;
 }
 });
 

  • 52. DroidCon Montréal 2015 44 Mobile App Modifications MainActivity.java @ReceiveMessages("/keynote/command")
 public void onKeynoteMessage(String message) {
 try {
 runCommand(message);
 } catch (Exception e) {
 e.printStackTrace();
 }
 }
 
 private final OkHttpClient client = new OkHttpClient();
 
 public void runCommand(final String command) throws Exception {
 Request request = new Request.Builder()
 .get()
 .url(String.format("http://%s/json/%s", mServerHost.getText().toString(), command))
 .build();
 
 client.newCall(request).enqueue(new Callback() {
 
 @Override
 public void onFailure(Request request, IOException e) {
 e.printStackTrace();
 }
 

  • 53. DroidCon Montréal 2015 45 Mobile App Modifications MainActivity.java @Override
 public void onResponse(Response response) throws IOException {
 if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
 
 try {
 JSONObject responseJson = new JSONObject(response.body().string());
 
 String slide = responseJson.optString("slide");
 String build = responseJson.optString("build");
 
 Message message = new Message("Me", "Slide: " + slide + "nBuild: " + build, System.currentTimeMillis());
 Courier.deliverData(MainActivity.this, "/message/data", message);
 
 } catch (JSONException e) {
 e.printStackTrace();
 }
 }
 });
 }
 
 

  • 54. DroidCon Montréal 2015 46 Notable Wear Libraries and Tools • By Denley Bihari: Courier, WearPreferenceActivity, and WearPrefs • Jimu Labs Mirror and Sandbox - jimulabs.com • Everything Square Does, ever! • Everything Jake Wharton Does, ever!
  • 55. DroidCon Montréal 2015 CONFIDENTIAL INFORMATION - NOT FOR DISTRIBUTION. THANK YOU :) 47 neal.sanche@rnp.io @thorinside Art: Mary Sanche, @thoughtsupnorth