Thursday, September 4, 2014

Wearable Computing & Android

Recently there has been a computing revolution that has effected many industries such as medical, automotive, and defense industries, the "Wearable Revolution".

However wearable computers are not new, such as consumer digital watches, electronic helmet displays for pilots , wearable computers are becoming more powerful and consumer oriented.

Almost every major electronic manufacturer has a wearable device line such as Samsung,Sony,Apple,Google etc. Even companies that are not in the electronics game such as Nike & Nissan have created a space for themselves in this industry.

Below is a table of some major companies and their associated platforms:

One can easily see that Android OS is a predominant environment in this sector of computing, and it shouldn't be a surprise given the fact that Android is already successful mobile platform, after all mobile devices paved the way for wearable devices!

Now that we have established Android as a legitimate platform for wearable application development, lets turn our attention learning to develop apps for Google Glasses.

Some prerequisites prior knowledge of Java & mobile programming.
Setting up your computer for Android Development

Download  latest Java JDK & setup JAVA_HOME & PATH environment variables
Install eclipse IDE for Android ( IDE + ADT + Android SDK bundle)


Please check if java jdk & path has been setup correctly on your computer ,by going to command prompt and typing javac -version, which should give you the jdk version. 


Introduction to Android Development 

This section will introduce the developer to the Android platform & development environment.

What is Android ...

 Android is Linux based open source operating system designed primarily for mobile development.
Android is developed in private by Google until the latest changes and updates are ready to be released, at which point the source code is made available publicly.
Android allows application development via a customized Java packages that run on DVK (Dalvik Virtual Machine) , not Java Virtual Machine, not Java byte code
Java code is compiled to .dex files not .class files
Basically one uses the Java language/syntax to develop Android applications however you don’t require the JVM , or JRE
You can program for  device specific native development via the Android NDK (C/C++) , not recommended unless your functionality is hardware dependent.

Installation of Android Tools
To develop apps for Android devices, you use a set of tools that are included in the Android SDK.
Android Tools include
  Download all the SDK, Tools, and instructions  at   (http://developer.android.com/sdk/index.html)
You can either add Android plugin to your IDE (Eclipse) or get the Eclipse/Android bundle ( the bundle is optimized , so get a fresh installation)
1)ADT Plugin  (Gadgets) - The Accessory Development Kit (ADK) is a reference implementation for hardware manufacturers and hobbyists to use as a starting point for building accessories for Android
2) Android SDK – The Android SDK is composed of modular packages that you can download separately using the Android SDK Manager.

2) Emulator – emulates actual mobile phones via software

Components of Android SDK Explained




Eclipse: Now that you have Installed everything , lets create a project 



Open Eclipse , Go to global menu, File -> New -> Android Application Project 

Development:Lets create a project




Click through the Application icon, initial activity, project workspace wizards...

Creating the first Activity , GUI start


Activity – “An activity is a single, focused thing that the user can do. Almost all activities interact with the user,
so the Activity class takes care of creating a window for you in which you can place your UI with setContentView(View).”

Creating the first Activity, continued


Specify the name of your first activity window
The name of the layout used to build the Activities GUI.
In Android development the layout is an xml file that describes the view object that is
 used to render and provide GUI capabilities (backgrounds, buttons, etc.)
In Android GUI development one has the choice of building GUIs either via xml or Java code.
For rapid app development,  use of GUI builders use the xml approach.

Now lets test run the project 



Right click on your project , select run as , than select Android project

If this is the first time you might get some configuration errors , don’t panic  

One of the most common configuration errors


When attempting to run a Android project  the user is trying to test their code on an emulated device.
A virtual device most be created & selected.
Virtual device creation & management is achieved via the AVD (Android Device Manager) 

Creating a virtual device via the AVD


<android_installation_dir>\sdk\tools\android.bat, create a shortcut on your desktop
Tools - > Manage AVDs
Select New to create the virtual mobile device
After the creation is comlete press Start, than go to your app, right click, Run as -> android application, you should see your app start in a emulated Android  mobile device

Clearing App Cache, for testing 

In development you might persist data that you might later want erased for testing, this is how:

In development you might persist data that you might later want erased for testing, this is how:

Window, Open Perspective, Select the DDMS perspective , data/your.apps.package/data/, find the file that you persist to and delete it.

Android Framework: Activities,Intent,Services,Broadcast, receiver, Content providers

Activity – “An activity is a single, focused thing that the user can do. Almost all activities interact with the user,  so the Activity class takes care of creating a window for you in which you can place your UI with setContentView(View).”
A)You can just think of an activity as almost a GUI FORM.
B ) You can transfer control (call to display) another activity via Intents (Inter process communications)
import android.app.Activity;
public class ApplicationLifeCycleTest extends Activity {
onCreate(Bundle savedInstanceState)
onDestroy()
onPause()
onRestart()
onResume()
onStart()
onStop()
}
C) Activities must be registered in the manifest.xml ( see the next slide)
For details : http://developer.android.com/reference/android/app/Activity.html

The Manifest File:  /YourAppname/AndroidManifest.xml

What  is  the Manifest file : http://developer.android.com/guide/topics/manifest/manifest-intro.html

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.testapp"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.yourPackage.ApplicationLifeCycleTest "
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

Android Framework : Intent

Intent – One way operations can be performed via asynchronous message,.
A)Activities can be launched via Intents.
Code snippet:
import android.content.Intent;
Intent i = new Intent(view.getContext(), ClassThatExtendsActivity.class);
startActivity(i);
For details : http://developer.android.com/reference/android/content/Intent.html

Android Framework : Services

Servicesplatform services (window manager) & custom services are programs and modules that run in the background
You can create faceless applications/tasks that run on the device without user interaction using Services.
Apps can use existing services or create new services
Use Case for Background Service
For example you can have a background services that based on your GPS location (if available) or cell tower meta data ( downtown LA tower) queries available “sale offers” via HTTP , and if there are good sales launches your application displaying store & sale information.
http://developer.android.com/guide/components/services.html

Android Framework : Broadcast
Broadcast receiver
A BroadcastReceiver is a custom listener that listens for system  or local transmitted messages.
Broadcast receivers do not display a user interface.
They register with the system by means of a filter acting as a key.
A broadcast receiver can respond by either launching a specific activity or using the notification services , to launch a notification to the user
For details : http://developer.android.com/reference/android/content/BroadcastReceiver.html

Android Framework: Content Provider

Content provider
A content provider is a data-centric service that makes persistent datasets possible & accessible to Android applications.
You can use content providers to CRUD: contacts, pictures, messages, audio files, emails.
You can use content providers to access small RDMS such as SQLite database.
Content provider :
http://developer.android.com/guide/topics/providers/content-providers.html

Understanding the life cycle “The Life Cycle App”

onCreate(Bundle savedInstanceState)
onDestroy()
onPause()
onRestart()
onResume()
onStart()
onStop()
For details  on activity life cycle see : http://developer.android.com/training/basics/activity-lifecycle/index.html

Google Glass Development

 Official Google Glass site :
Google Glass GDK Quick Start
https://developers.google.com/glass/develop/gdk/quick-start

1)Glass Development Kit Preview  & USB Driver –
Go to your Android SDK Manager (YoutAndroidInstallationPath\sdk\tools\android.bat) , Run android.bat file & create shortcut for later use
A )Select to download all API 19 & related packages
B) Navigate to Extras , and select to download Google USB Driver
See below for visual guide.



2) Enable USB debugging –
a)Turn on Google Glass
b)If Google Glass is not sync with MyGlass app, Go to https://glass.google.com/setup and follow instructions on how sync Google Glass with your computer & phone, this is crucial , than come back  to the slides 

2 – c) To go your Google Glass & enable USB debugging continued
When you see the “ok glass” screen , swipe back using your finger 


Then continue to swipe until you see  the “Settings” screen, Than tap Glass to select, swipe again to navigate to settings.

Once you have navigated to “Device info:” , tap Glass , and swipe forward until you see “Turn on debug” , tap Glass to turn on debug mode. 


Now you computer should be able to see the Glass via USB connection & you should be able to push access Android OS (via abd command prompt)  & push your “.apk” code to Glass device. Either via Eclipse or adb.

Believe it or not were still were still configuring your computers usb connectivity with Google Glass !
2- d) Disable driver signature enforcement for Windows 7 & 8
    Reboot & Press F8 - Using advanced start up options and there is an option
to turn off  driver signature enforcement “Disable Driver Signature Enforcement
USB Configuration is done.

3) Make sure your able to access Glass via “Android Debug Bridge” adb
Go to command prompt
a) Run Android SDK Manager uninstall Google USB Driver & re-install it again
b) Go to Control Panel , Device Manager , than select portable devices , and select Glass device , right click , select Update Driver , Browse the computer for driver, go to YourAndroidInstallationPath\sdk\extras\google\usb_driver, Select driver
c) Stop adb (adb kill-server) than  start  adb (adb start-server) , issue “adb devices”
If you don’t see your device repeat steps 2 & 3 until you do!!!

5) In order to check if your able to deploy code to the Glass, Import some GDK samples with the File > New Project > Android Sample Project menu.
a)Select build target
b)Select Glass Development Kit 


Select sample app to import, than select Finish. 
6) a) In Eclipse in the Project Explorer view , Select the Imported Project, right click & clean & build, if there are conflicts resolve them
a)Right click built project , Select Run As, Select Android Application
c)In the Android Device Chooser Select your Android Device to deploy to which is the Google Glass , Wait for deployment to finish

 If you cannot see your Google Glass device than follow these steps, else go back to steps 2 & 3.
7) Put on your Google Glass or use MyGlass navigate to Time screen, tap the Glass, you should see the Glassware that we side loaded, if not swipe right until you do else repeat step 6. 

GDK vs SDK & Mirror API


When developing Glassware the developer currently has these 3 different UI components to base their application on Static cards, Live cards, and Immersions.
The timeline is the primary interface that  is displayed to the user when using Google Glass , and is comprised of 640 × 360 pixel cards.  The timeline exposes access to live and static cards, voice commands, and ability to launch Glassware.
1)Cards
a)Static Card
For more details: https://developers.google.com/glass/design/ui
a)Live Card
For more details: https://developers.google.com/glass/develop/gdk/live-cards
2)
 Immersions:
For more details:
https://developers.google.com/glass/develop/gdk/immersions
I think the most important UI components are static cards & Immersions because they represent both ends of the spectrum from little functionality via Static cards & full functionality via Immersion (which is really a full screen Activity).
Lets start with creating a new Glassware and create a simple Static card that displays the most recent picture taken & stored in your device gallery.
Steps:
1)Start Eclipse IDE
2)File, New , Other, Android Application Project , Follow the wizard with default settings & name your project RecentPicsCollage
3)Right click the project , select properties, select Android tab , Set Project Build Target to Google Development Kit version that you have downloaded
See screenshot below:
Set Project Target To GDK

Lets Cleanup The Project Before Starting to Code 
1)The Eclipse wizard creates some extraneous code & themes that are geared towards mobile applications rather than Glass,  we need to more all that code in order for Glassware UI to appear correctly.
Steps:
1)Open the AndroidManifest.xml file remove everything associated with theme, fragments until it looks like this:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.recentpicscollage"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name">
        <activity
            android:name="com.example.recentpicscollage.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

Remove All Auto generated code in the Activity 
package com.example.recentpicscollage;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

}

}

Ok lets start to design before we code
Someone wise said “Failing to plan is planning to fail”

Our application will :
listen to the background events and invoke its logic when a new picture is taken
The app will retrieve the 3 most recent pics & create a collage
Split the screen to 3 rectangles & resize the images to fit & display 

Ok lets create our events listener(BroadcastReciever

1)Lets add permission to read external storage  AND
2)Lets add the BroadcastReceiver to our AndroidManifest.xml:
Your AndroiidManifest should look like this : 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.recentpicscollage"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name="com.example.recentpicscollage.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <!-- broadcast receiver that listens actions/event associated with NEW PICTURE -->
        <receiver
            android:name=".CameraReciver"
            android:enabled="true" >
            <intent-filter>
                <action android:name="com.android.camera.NEW_PICTURE" />
                <action android:name="android.hardware.action.NEW_PICTURE" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="image/*" />
            </intent-filter>
        </receiver>
    </application>
    <!-- permission to read SD card -->
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
</manifest>

Code Updates Coming Soon...