Advanced dev tips for the Android Wear

You first Android Wear app is finally complete. A working notification system, a couple of custom wear activities and an exciting voice input
control. Now what?
In this session, you’ll learn about some of the advanced Android Wear programming guidelines, code optimizations, useful community libraries, best UI patterns seen so far, brilliant watch faces, pitfalls to avoid and other “real world” Android Wear tips’nd tricks.


(Droidcon Turin, 9th April 2015)

The second screen world in the Google Cast era

TVs are the biggest, most beautiful screen in people’s living rooms. Google Cast is a technology that enables true multi-screen experiences for the users. Integrating Google Cast into existing applications is simple, and we’re going to cover the SDK and resources available to make your application Cast enabled really easily. Android, iOS and Web. Possibilities? Endless: not only casting video or audio, but also games where the TV becomes the new and high-tech game board or a variety of other apps to enjoy with friends, sitting together on the couch.


(Codemotion Roma 2015)

Environmental variables, API key and secret, BuildConfig and Android Studio

You wanna create an Android app that uses Twittet APIs, so yo need an API key and an API secrets only you and your apps know. Because you need these values inside you app, it’s easy and quick to write them down directly in your source code. But when you commit this code to GitHub (or any other public repo), practically you’re telling your secret keys to entire world. Seems uncommon? Unfortunately, not so much! Same for Dropbox SDK.

One simple way to avoid this bad practice is to store your values inside an environmental variable, so only your machine knows it, then read this values in some way and inject them in your code at build time.
Let’s see how to do that using Android Studio, Gradle, and BuildConfig.

First, we need to create these environmental vars. In Linux and Mac, create or edit the file ~/.gradle/gradle.properties (pay attention to the actual Gradle User Home directory position) and add some values:

#define your secret values
TwitterConsumerKeyProp=xxxxxx66666666634333333ddddddTwitterConsumerSecretProp=3nkl3sds3skmslSDF394asdk39dmasd

Second, in your module’s build.gradle file, add these lines

apply plugin: 'com.android.application'
 
//Add these lines
def TWITTER_CONSUMER_KEY = '"' + TwitterConsumerKeyProp + '"' ?: '"Define Twitter Consumer key"';def TWITTER_CONSUMER_SECRET = '"' + TwitterConsumerSecretProp + '"' ?: '"Define Twitter Consumer secret"';
 
android.buildTypes.each { type ->
    type.buildConfigField 'String', 'TWITTER_CONSUMER_KEY', TWITTER_CONSUMER_KEY    type.buildConfigField 'String', 'TWITTER_CONSUMER_SECRET', TWITTER_CONSUMER_SECRET
}

Please note the “TwitterConsumerKeyProp” and “TwitterConsumerSecretProp” have to be the same in both Gradle settings file and Gradle build file.
Finally, to use these values in your code, filled at runtime by Gradle in the build script for you, simply use:

ConfigurationBuilder cb = new ConfigurationBuilder()
    .setDebugEnabled(BuildConfig.DEBUG)
    .setApplicationOnlyAuthEnabled(true)
    .setOAuthConsumerKey(BuildConfig.TWITTER_CONSUMER_KEY)    .setOAuthConsumerSecret(BuildConfig.TWITTER_CONSUMER_SECRET);

That’s all, then it’s up to you how to create more elaborated configurations. For example, you can have different values based on different android.buildTypes types, or the gradle settings file in a common network folder used by the entire team or…

Fastboot mode for Android Wear devices (G Watch, Gear Live, SmartWatch 3)

Putting Android devices in Recovery mode / Fastboot, when you don’t have access to adb, is an art. And Android Wear, where you have only one button (the power button), adds even more entropy to the whole process.

So let’s gather the different procedures for the different watches.

LG G Watch

  • Find the reset button: it is on the back of the device, near the pins to charge it.
  • Press the button using a paperclip, a SIM ejector or your fantasy
  • Wait for LG logo to appears on screen
  • While the logo is showing, swipe from the top left-hand corner to the the bottom right-hand corner in one, diagonal swipe.
  • From the Fastboot menu, you can launch the Recovery Mode
  • Video

 

LG G Watch R

  • Turn off the watch and then turn it on normally
  • Wait for LG logo to appears on screen
  • While the logo is showing, swipe from the top left-hand corner to the the bottom right-hand corner in one, diagonal swipe, pressing and holding the power button
  • Alternatively, if the swipe doesn’t work, instead of swiping start tapping the screen with two fingers, one on the top left (10) and one on the bottom right (4), pressing and holding the power button
  • From the Fastboot menu, you can launch the Recovery Mode
  • Video

 

Samsumg Gear Live

  • Turn off the watch and then turn it on normally
  • Wait for Samsung logo to appears on screen
  • While the logo is showing, swipe from the top left-hand corner to the the bottom right-hand corner in one, diagonal swipe, pressing and holding the power button
  • The bootloader appears
  • Select the Fastboot or Recovery mode swiping downward, and swipe to the right to confirm the selection.

 

Sony SmartWatch 3

  • Detach any USB cable connected to the watch
  • Turn it off holding the power button and keep holding the button until the watch vibrates three times and then switches it off
  • Press the power button and keep holding. A blue screen that prompts to insert an USB cable will display
  • Double tap the power button
  • A black menu will display, with on-screen instructions
  • Move to Fastboot or Recovery Mode pressing the power button, once a time
  • Select Fastboot (or Recovery) by double pressing the power button

 

For extreme modding, there is also a third mode available, the Download mode.