108
Android Twitter App and C2DM By Rohit Ghatol Architect @ QuickOffice

Android Twitter and C2DM Explained

Embed Size (px)

DESCRIPTION

Presentation given on TechGig for Android Twitter Reference App and C2DM.

Citation preview

Page 1: Android Twitter and C2DM Explained

Android Twitter Appand C2DM

By Rohit GhatolArchitect @ QuickOffice

Page 2: Android Twitter and C2DM Explained

Speaker Introduction

Page 3: Android Twitter and C2DM Explained

Speaker Introduction

• Rohit Ghatol• Architect @ QuickOffice• Proj Mgr @ Synerzip• Founder TechNext • Author “Beginning PhoneGap”

@Apress• Technical Speaker and Corporate

Trainer

Page 4: Android Twitter and C2DM Explained

Why Twitter App and C2DM in the same talk?

Page 5: Android Twitter and C2DM Explained

Poll Vs Push

Page 6: Android Twitter and C2DM Explained

Twitter App Demo

http://code.google.com/p/DroidTwitt

Page 8: Android Twitter and C2DM Explained

Twitter Requirements

OAuth AuthenticationFetch Tweets

Send TweetsBackground Sync

Page 9: Android Twitter and C2DM Explained

Revisit Android Building Blocks in Short

Page 10: Android Twitter and C2DM Explained

Activity LifeCycle

onCreate()

onDestroy()

onStart()

onStop()

onResume()

onPause()

CompleteLifeCycle

VisibleLifeCycle

Foreground VisibleLifeCycle

Page 11: Android Twitter and C2DM Explained

Calling Service

Activity

Service

void onStartCommand(Intent intent,…){}

Activity

Service

startService(intent)

bindService(intent)

foo();bar();

void foo(){}int bar(){}

Page 12: Android Twitter and C2DM Explained

Broadcast ReceiversApp 1

Android OS

App 2

Your App

Custom Event 1

Custom Event 2

Battery Low

RoamingCall

Network Change

Interested in any of these Events.

Page 13: Android Twitter and C2DM Explained

Overall Architecture

Page 14: Android Twitter and C2DM Explained
Page 15: Android Twitter and C2DM Explained

OAuth Authentication

Page 16: Android Twitter and C2DM Explained
Page 17: Android Twitter and C2DM Explained

First Time Launch

Page 18: Android Twitter and C2DM Explained
Page 19: Android Twitter and C2DM Explained

Asking Service To Fetch Tweets

Page 20: Android Twitter and C2DM Explained
Page 21: Android Twitter and C2DM Explained

Triggering Service every n minutes

Page 22: Android Twitter and C2DM Explained
Page 23: Android Twitter and C2DM Explained

Phone Boot Event

Page 24: Android Twitter and C2DM Explained
Page 25: Android Twitter and C2DM Explained

Battery Low Event

Page 26: Android Twitter and C2DM Explained
Page 27: Android Twitter and C2DM Explained

OAuth

Short Introduction to OAuth

Page 28: Android Twitter and C2DM Explained

http://fotofast.com

username

password

Login Cancel

http://fotofast.comWelcome Rohit

Picasa Flickr

Print Photo

Page 29: Android Twitter and C2DM Explained

http://fotofast.com

username

password

Login Cancel

Welcome Rohit

Enter Credentials for Picasa

If I go to a restaurant, do I give my ATM Card and Pin to the waiter to pay my bills?

Then Why should I give my Picasa Credentials to fotofast.com?

Page 30: Android Twitter and C2DM Explained

Take 2, Action

Page 31: Android Twitter and C2DM Explained

http://fotofast.com

username

password

Login Cancel

http://fotofast.comWelcome Rohit

Picasa Flickr

Print Photo

Page 32: Android Twitter and C2DM Explained

http://picasa.com

username

password

Login Cancel

Welcome to Picasa

Look Ma its Picasa itself asking for password!

http://picasa.com

Choose Album to Share with FotoFast

1 2

4 5

3

6

Page 33: Android Twitter and C2DM Explained

http://fotofast.comWelcome Rohit

Picasa Albums

Choose photo to Print Photo

Look Ma! I am able to print picasa photos from FotoFast

Page 34: Android Twitter and C2DM Explained

Twitter OAuth in Android

Page 35: Android Twitter and C2DM Explained

About OAuth

• The way OAuth works for Web is Browser redirects between the 2 sites

• Browser first shows FotoFast, which redirects you to Picasa to authenticate and approve sharing data with FotoFast

• On proper Authentication and Approval, Browser takes you back to FotoFast.com

Page 36: Android Twitter and C2DM Explained

How does the same work for Android Applications?

Page 37: Android Twitter and C2DM Explained

Steps of Twitter OAuth

Page 38: Android Twitter and C2DM Explained

Step 1 : Register with Twitter for OAuth Token

Page 39: Android Twitter and C2DM Explained
Page 40: Android Twitter and C2DM Explained

Step 2: Register your Activity to handle url “DroidTwit://twitt”

Page 41: Android Twitter and C2DM Explained

<activity android:name=".OAuthLogin" android:label="@string/app_name”android:launchMode="singleTask"><intent-filter>

<action android:name="android.intent.action.VIEW"></action><category

android:name="android.intent.category.DEFAULT"></category><category

android:name="android.intent.category.BROWSABLE"></category><data android:scheme="DroidTwit" android:host="twitt"></data>

</intent-filter></activity>

Page 42: Android Twitter and C2DM Explained

Step 3: Create a OAuth URL and ask browser to show it

OAuth URL also contains CallBack URL you URL can redirect back to

your activity

Page 43: Android Twitter and C2DM Explained

public static final String CALLBACK_URL ="DroidTwitt://twitt";

public void buttonClick(){OAuthSignpostClient client = new OAuthSignpostClient

(TWITTER_KEY, TWITTER_SECRET,CALLBACK_URL);

final URI twitterUrl = client.authorizeUrl();

//Start BrowserstartActivity(new Intent(Intent.ACTION_VIEW,

Uri.parse(twitterUrl.toString())));}

Page 44: Android Twitter and C2DM Explained

Step 4: Handle Response from Browser after OAuth

Page 45: Android Twitter and C2DM Explained

@Overrideprotected void onNewIntent(final Intent intent) {

super.onNewIntent(intent);final Uri uri = intent.getData();if ((uri != null) && uri.toString().startsWith(CALLBACK_URL)) {verifier = uri.getQueryParameter("oauth_verifier");

client.setAuthorizationCode(verifier);

accessTokenAndSecret = client.getAccessToken();

Log.e("NewIntent", "Access token: " + accessTokenAndSecret[0]);Log.e("NewIntent", "Token secret: " + accessTokenAndSecret[1]);}

}

Page 46: Android Twitter and C2DM Explained
Page 48: Android Twitter and C2DM Explained

Fetching From Twitter API

Page 49: Android Twitter and C2DM Explained

Guidelines

• Make the fetch module SynchronousList<Tweets> getLatestTweets();

• Either call directly or through Services

• Use AsyncTask on UI to avoid ANR

Page 51: Android Twitter and C2DM Explained

Need for List Adapter

Entry 1

1

2

3

4

List<Tweet>

Tweets

Something else

Data that can varyList View functionality is same

Layout can vary

Adapter Layer

Page 52: Android Twitter and C2DM Explained

public class MyAdapter extends BaseAdapter{

public int getCount(){ }

public Object getItem(int position){ }

public long getItemId(int position){ }

public View getView(int position,Convert view,..){ }}

Page 53: Android Twitter and C2DM Explained

List View Explained

Entry 1

Entry 2

Entry 3

Entry 4

1

2

3

4

1 Plank No.

Plank

Page 54: Android Twitter and C2DM Explained

List View Explained

Entry 1

Entry 2

Entry 3

Entry 4

1

2

3

4

1 Plank No.

Scroll

Entry 51

Plank 1 Reused when it pops out the top while scrolling

Page 55: Android Twitter and C2DM Explained

Thumbnails in ListView

Common Problem and How to solve it

Page 56: Android Twitter and C2DM Explained

Why is thumbnail is such a problem in ListView?

Entry 1

Entry 2

Entry 3

Entry 4

Thread 1

Thread 2

Threads to fetch image asynchronously

1

2

3

4

Thread 3

Thread 4

Page 57: Android Twitter and C2DM Explained

Why is thumbnail is such a problem in ListView?

Entry 5

Entry 6

Entry 7

Entry 8

Thread 1 Now Thread 1 returns image one and sets it on Plank 1, but Plank 1 should show image from Entry 5.

Wrong images are shown for Entry 5, till Thread 5 returns.

And for each scroll, again images are fetched

1

2

3

4

Thread 5

Page 58: Android Twitter and C2DM Explained

Solution

Entry 1

Entry 2

Entry 3

Entry 4

1

2

3

4Blocking Queue

Thread 1

Local Image Cache

Thread reads from Blocking Queue, checks the Image Cache, if Image not there, fetches it dumps in Image Cache and gives it to the Image View (If position is valid)

While Scrolling, we add entries in Blocking Queue

Page 59: Android Twitter and C2DM Explained

Class Diagram<<ImageLoader>>

void queue(String url, ImageView imageView);

<<ImageCache>>

void cache(String url, Bitmap bitmap);

BitMap get(String url);

PhotoToLoad

private String url;private ImageView imageView;

Uses BlockingQueue<PhotoToLoad> and has a thread which keeps running on the

BlockingQueue, to either fetch image from cache or from internet

Page 61: Android Twitter and C2DM Explained

Using Services

Lets see Code Demos for this

Complete Detailed Video of this is available on http://code.google.com/p/droidtwit/wiki/AndroidServiceTutorial

Page 62: Android Twitter and C2DM Explained

Using Alarm Manager

Lets see Code Demo

Page 63: Android Twitter and C2DM Explained

Listener Battery Event

Lets see Code Demo

Page 64: Android Twitter and C2DM Explained

C2DM

Page 65: Android Twitter and C2DM Explained

What is C2DM?

Cloud To Device Messaging

Page 66: Android Twitter and C2DM Explained

What is C2DM?

• Messages send from Server to Device via Google

• Small Messages only meant to tell the client, that server has new information

• Optimized and uses same channel as Gmail, Calendar and other google apps

Page 67: Android Twitter and C2DM Explained

Why C2DM?

Page 68: Android Twitter and C2DM Explained

Drawbacks of DroidTwitt

Page 69: Android Twitter and C2DM Explained

Battery Drain

• Application launching every 5/10/15 mins

• Not sure if we will get new information, blindly trying

• Way to optimize is using AlarmManager.setInExactRepeating(), but that only helps so much

Page 70: Android Twitter and C2DM Explained

Data Usage

• Polling means more use of Data

• If Server is not optimized, then it always sends a chunk of stale data, more bandwidth

• Ways to optimize is ask server to send data on top of what the client already has (say use the timestamp), but that only helps so much

Page 71: Android Twitter and C2DM Explained

Lag

• Notification using Polling will always have a Lag

• It ends up being a balancing act between Saving Battery and Freshness of the data

Page 72: Android Twitter and C2DM Explained

Traffic (Server Side)

• Every device, Every n minutes bombarding the Server

• Businesses won’t mind traffic as long it is adding real value. But is this real value?

Page 73: Android Twitter and C2DM Explained

Load (Server Side)

• Hitting Database blindly when getting traffic

• Servers need to optimized to know whether the polling requests will repeat very n minutes and they only hit database if they have change.

Page 74: Android Twitter and C2DM Explained

C2DM Players

Page 75: Android Twitter and C2DM Explained

Google Cloud

ApplicationServer

But Remember a User can have more than one Device!

Page 76: Android Twitter and C2DM Explained

C2DM Flow of Events

Page 77: Android Twitter and C2DM Explained

Google Cloud

ApplicationServer

Step 1: App Server goes Authentication with Google C2DM Cloud and gets a security token

Security TokenAppServer123

Page 78: Android Twitter and C2DM Explained

Google Cloud

ApplicationServer

Step 2: Device 1 Registers itself to Google C2DM Cloud, gets an Registration Id

Security TokenAppServer123

Page 79: Android Twitter and C2DM Explained

Google Cloud

ApplicationServer

Step 3: Device 1 tells App Server that its C2DM Reg Id is XYZ

Device Info Reg IdRohit’s Phone XYZ

XYZ

Security Token

AppServer123

Page 80: Android Twitter and C2DM Explained

Google Cloud

ApplicationServer

Device Info Reg IdRohit’s Phone XYZ

Step 4: App Server sends message to C2DM intended for Device with Reg Id XYZ

Security TokenAppServer123

Page 81: Android Twitter and C2DM Explained

Google Cloud

ApplicationServer

Device Info Reg IdRohit’s Phone XYZ

Step 6: Google C2DM Cloud Service sends Intent to Device with Reg id XYZ

Security TokenAppServer123

Page 82: Android Twitter and C2DM Explained

Google Cloud

ApplicationServer

Device Info Reg IdRohit’s Phone XYZ

Step 7: Device fetches data from App Server to process

Security TokenAppServer123

Page 83: Android Twitter and C2DM Explained

C2DM Demo

Lets see the Demo

Page 84: Android Twitter and C2DM Explained
Page 85: Android Twitter and C2DM Explained
Page 86: Android Twitter and C2DM Explained
Page 87: Android Twitter and C2DM Explained
Page 88: Android Twitter and C2DM Explained
Page 89: Android Twitter and C2DM Explained
Page 90: Android Twitter and C2DM Explained
Page 91: Android Twitter and C2DM Explained

Steps for C2DM

Page 92: Android Twitter and C2DM Explained

Step 1: Create AVD for Emulator

• Create a AVD With Google API 8 (not Android API 8)

Page 93: Android Twitter and C2DM Explained

Step 1: Create AVD for Emulator

• Go to Settings and Add a Google Account

Page 94: Android Twitter and C2DM Explained

Step 2: Server Side Authentication//Create URL for Google Authentication for C2DMStringBuilder builder = new StringBuilder();//Has to be C2DM Registered Email Addressbuilder.append("Email=").append(email);builder.append("&Passwd=").append(password);builder.append("&accountType=GOOGLE");builder.append("&source=MyLittleExample");builder.append("&service=ac2dm");

byte[] data = builder.toString().getBytes();

Server Side Code

One Time

Page 95: Android Twitter and C2DM Explained

Step 2: Server Side Authentication// Setup the Http PostURL url = new URL("https://www.google.com/accounts/ClientLogin");HttpURLConnection con = (HttpURLConnection) url.openConnection();……con.setRequestMethod("POST");con.setRequestProperty("Content-Type”, "application/x-www-form-urlencoded");

con.setRequestProperty("Content-Length", Integer.toString(data.length));

Server Side Code

One Time

Page 96: Android Twitter and C2DM Explained

Step 2: Server Side Authentication// Issue the HTTP POST requestOutputStream output = con.getOutputStream();output.write(data);output.close();

//Read Auth Token ResponseBufferedReader reader = new BufferedReader(new InputStreamReader(

con.getInputStream()));String line = null, auth_key = null;while ((line = reader.readLine()) != null) {

if (line.startsWith("Auth=")) {auth_key = line.substring(5);

}}

Server Side Code

One Time

Page 97: Android Twitter and C2DM Explained

Step 3: Android Manifest<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="com.sparklytix.factoreal" android:versionCode="1"android:versionName="1.0"><uses-sdk android:minSdkVersion="8" /><permission

android:name="com.sparklytix.factoreal.permission.C2D_MESSAGE"android:protectionLevel="signature" />

<uses-permissionandroid:name="com.sparklytix.factoreal.permission.C2D_MESSAGE" />

<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

<uses-permission android:name="android.permission.INTERNET" />

Android Code

Page 98: Android Twitter and C2DM Explained

Step 3: Android Manifest<receiver android:name=".receivers.C2DMBroadCastReceiver”

android:permission="com.google.android.c2dm.permission.SEND"> <!-- Receive the actual message -->

<intent-filter><action

android:name="com.google.android.c2dm.intent.RECEIVE" /><category android:name="com.sparklytix.factoreal" />

</intent-filter><!-- Receive the registration id --><intent-filter>

<action android:name="com.google.android.c2dm.intent.REGISTRATION" />

<category android:name="com.sparklytix.factoreal" /></intent-filter>

</receiver>

Android Code

Page 99: Android Twitter and C2DM Explained

Step 4: Send C2DM Device Reg Reqpublic void register(Context context, String sender) {

Intent intent = new Intent("com.google.android.c2dm.intent.REGISTER"); //app is this application itself

intent.putExtra("app",PendingIntent.getBroadcast(context, 0, new

Intent(), 0)); //sender is typical gmail account on the device

intent.putExtra("sender”,sender);context.startService(intent);

}

Android Code

Page 100: Android Twitter and C2DM Explained

Step 5: Get Device Reg Idpublic void onReceive(Context context, Intent intent) { if(“com.google.android.c2dm.intent.REGISTRATION”.equals(intent.getAction()){

final String registrationId = intent.getStringExtra("registration_id");String error = intent.getStringExtra("error");if (intent.getStringExtra("error") != null) {

// Registration failed, should try again later.} else if (intent.getStringExtra("unregistered") != null) {

//Remove Reg Id from Our Application Server} else if (registrationId != null) {

//Send Reg Id to Our Application Server}

}}

Android Code

Broadcast Receiver

Page 101: Android Twitter and C2DM Explained

Step 6: Send Message from ServerStringBuilder postDataBuilder = new StringBuilder();postDataBuilder.append(“registration_id”).append("=”).append

(registrationId);postDataBuilder.append("&").append(“collapse_key”).append("=")

.append("0");postDataBuilder.append("&").append("data.payload").append("=")

.append(URLEncoder.encode(message, UTF8));byte[] postData = postDataBuilder.toString().getBytes(UTF8);

Server Side Code

Page 102: Android Twitter and C2DM Explained

Step 6: Send Message from ServerURL url = new URL("https://android.clients.google.com/c2dm/send");HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();....conn.setRequestMethod("POST");conn.setRequestProperty("Content-Type“,"application/x-www-form-urlencoded;charset=UTF-8");conn.setRequestProperty("Content-Length",Integer.toString(postData.length));conn.setRequestProperty("Authorization", "GoogleLogin auth="

+ auth_token);OutputStream out = conn.getOutputStream();out.write(postData);out.close();

Server Side Code

Page 103: Android Twitter and C2DM Explained

Step 7: Receive Message at Android

public void onReceive(Context context, Intent intent) { if(“com.google.android.c2dm.intent.RECEIVE”.equals(intent.getAction()){

String payload = intent.getStringExtra("payload");//Create a Notification and fire it

}}

Android Code

Broadcast Receiver

Page 104: Android Twitter and C2DM Explained

Conclusion

• C2DM is an excellent way to– Reduce Data Usage and Save Battery on the

Device– And also reduce load and network contingency on

the Server– Also when device goes offline, Our App Server

does not have to queue, Google C2DM queues itself

• Definitely a better option than Polling

Page 106: Android Twitter and C2DM Explained

Code Projects referred

• Droid Twitt Project - http://code.google.com/p/droidtwitt

• Android Code Examples – http://code.google.com/p/droidtwitt

• Listing Thumbnails in ListView - http://code.google.com/p/feedreader/

• C2DM Server and Android App - http://code.google.com/p/android-c2dm-reference-impl/

Page 107: Android Twitter and C2DM Explained

Q & A