Presentation is loading. Please wait.

Presentation is loading. Please wait.

Building Twitter App Rohit Ghatol. About Me Rohit Ghatol 2.Project 3.Certified Scrum Master 4.Author “Beginning.

Similar presentations


Presentation on theme: "Building Twitter App Rohit Ghatol. About Me Rohit Ghatol 2.Project 3.Certified Scrum Master 4.Author “Beginning."— Presentation transcript:

1 Building Twitter App Rohit Ghatol

2 About Me Rohit Ghatol 1.Architect @QuickOffice 2.Project Mgr @Synerzip 3.Certified Scrum Master 4.Author “Beginning PhoneGap” @ApressBeginning PhoneGap 5.Founder TechNext Pune (Pune Developer Community)TechNext LinkedIn Profile LinkedIn Profile

3 Topics – Part 1 Twitter App Demo Twitter RestFul API & Java Library Twitter OAuth Coding the List Activity Understanding AsyncTask Issue with Icons in ListView 3

4 Topics – Part 2 Coding different Types of Services Coding Alarm & Notification Manager Coding Broadcast Receiver Coding SQLite DB Drawbacks of Twitter App C2DM to Rescue Understanding C2DM and short Demo 4

5 Building Twitter App – Part 1 5

6 Twitter App Demo http://code.google.com/p/DroidTwitt

7 Youtube Video Demo http://www.youtube.com/user/rohitssghatol# p/u/13/XhCZpxWDtx0 http://www.youtube.com/user/rohitssghatol# p/u/13/XhCZpxWDtx0

8 Twitter Requirements OAuth Authentication Fetch Tweets Send Tweets Background Sync

9 Overall Architecture

10

11 OAuth Authentication

12

13 First Time Launch

14

15 Asking Service To Fetch Tweets

16

17 Triggering Service every n minutes

18

19 Phone Boot Event

20

21 Battery Low Event

22

23 OAuth Short Introduction to OAuth

24 http://fotofast.com username password Login Cancel http://fotofast.com Welcome Rohit PicasaFlickr Print Photo

25 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?

26 Take 2, Action

27 http://fotofast.com username password Login Cancel http://fotofast.com Welcome Rohit PicasaFlickr Print Photo

28 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 1 2 2 4 4 5 5 3 3 6 6

29 http://fotofast.com Welcome Rohit Picasa Albums Choose photo to Print Photo Look Ma! I am able to print picasa photos from FotoFast

30 Twitter OAuth in Android

31 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

32 How does the same work for Android Applications?

33 Steps of Twitter OAuth

34 Step 1 : Register with Twitter for OAuth Token

35

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

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

38 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

39 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 Browser startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(twitterUrl.toString()))); }

40 Step 4: Handle Response from Browser after OAuth

41 @Override protected 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]); }

42

43 For more details on OAuth in Android read http://blog.copyninja.info/2010/09/a ndroid-oauth-authentication- with.html

44 Fetching From Twitter API

45 Guidelines Make the fetch module Synchronous List getLatestTweets(); Either call directly or through Services Use AsyncTask on UI to avoid ANR

46 ListView Showing Tweets Lets see a some code for this. http://code.google.com/p/droidtwit/wiki/AndroidListActivityAsyncTaskTutorial

47 Need for List Adapter Entry 1 1 2 3 4 List Tweets Something else Data that can vary List View functionality is same Layout can vary Adapter Layer

48 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,..){ }

49 List View Explained Entry 1Entry 2Entry 3 Entry 4 1 2 3 4 1 Plank No. Plank

50 List View Explained Entry 1 Entry 2 Entry 3 Entry 4 1 2 3 4 1 Plank No. Scroll Entry 5 1 Plank 1 Reused when it pops out the top while scrolling

51 Thumbnails in ListView Common Problem and How to solve it

52 Why is thumbnail is such a problem in ListView? Entry 1Entry 2Entry 3Entry 4 Thread 1 Thread 2 Threads to fetch image asynchronously 1 2 3 4 Thread 3 Thread 4

53 Why is thumbnail such a problem in ListView? Entry 5Entry 6Entry 7Entry 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

54 Solution Entry 1Entry 2Entry 3Entry 4 1 2 3 4 Blocking 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

55 Step 0 1 2 3 4 Blocking Queue Thread 1 Local Image Cache Continuous Thread started while(true){ PhotoToLoad task = queue.take(); //Fetch photo from internet //Put photo in cache and set on Image View } Thread is blocked on queue.take()

56 Step 1 ….. Entry 1 1 2 3 4 PhotoToLoad Blocking Queue Thread 1 Local Image Cache Adapter’s getView() is called Thread now unblocked Continuous Thread started while(true){ PhotoToLoad task = queue.take(); //Fetch photo from internet //Put photo in cache and set on Image View }

57 Step 1 Entry 1 1 2 3 4 PhotoToLoad Blocking Queue Thread 1 Local Image Cache Adapter’s getView() is called Thread now unblocked Continuous Thread started while(true){ PhotoToLoad task = queue.take(); //Fetch photo from internet //Put photo in cache and set on Image View }

58 Step 0 3 4 Blocking Queue Thread 1 Local Image Cache Continuous Thread started while(true){ PhotoToLoad task = queue.take(); //Fetch photo from internet //Put photo in cache and set on Image View } Thread is blocked on queue.take() Entry 1 Entry 2 1 2

59 Class Diagram > void queue(String url, ImageView imageView); > void cache(String url, Bitmap bitmap); BitMap get(String url); PhotoToLoad private String url; private ImageView imageView; Uses BlockingQueue and has a thread which keeps running on the BlockingQueue, to either fetch image from cache or from internet

60 Complete Example The complete example of the QueuedImageLoader can be found http://code.google.com/p/feedreader/source/ browse/trunk/FifaLatestNews/src/com/latestn ews/cache/QueuedImageLoader.java http://code.google.com/p/feedreader/source/ browse/trunk/FifaLatestNews/src/com/latestn ews/cache/QueuedImageLoader.java

61 Building Twitter App – Part 2 61

62 Using Services Lets see Code Demos for this Complete Detailed Video of this is available on http://code.google.com/p/droidtwit/wiki/AndroidServiceTutorial http://code.google.com/p/droidtwit/wiki/AndroidServiceTutorial

63 Types of Services Main Types – Start Service – Bind Service (using Primitives) – Bind Service (using complex types) – Local Service 63

64 Using Alarm Manager Lets see Code Demo

65 Listener Battery Event Lets see Code Demo

66 SQLite Database Demo Lets see Code Demo

67 Pull Vs Server Push Twitter App is an example of writing Pull Application Pull Applications have some drawbacks 67

68 Drawbacks of DroidTwitt

69 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

70 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

71 Lag Notification using Polling will always have a Lag It ends up being a balancing act between Saving Battery and Freshness of the data

72 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?

73 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.

74 C2DM Players

75 Google Cloud Application Server Application Server But Remember a User can have more than one Device!

76 Android C2DM to Rescue

77 Google Cloud Application Server Application Server Step 1: App Server goes Authentication with Google C2DM Cloud and gets a security token Security Token AppServer123

78 Google Cloud Application Server Application Server Step 2: Device 1 Registers itself to Google C2DM Cloud, gets an Registration Id Security Token AppServer123

79 Google Cloud Application Server Application Server Step 3: Device 1 tells App Server that its C2DM Reg Id is XYZ Device InfoReg Id Rohit’s PhoneXYZ Security Token AppServer123

80 Google Cloud Application Server Application Server Device InfoReg Id Rohit’s PhoneXYZ Step 4: App Server sends message to C2DM intended for Device with Reg Id XYZ Security Token AppServer123

81 Google Cloud Application Server Application Server Device InfoReg Id Rohit’s PhoneXYZ Step 6: Google C2DM Cloud Service sends Intent to Device with Reg id XYZ Security Token AppServer123

82 Google Cloud Application Server Application Server Device InfoReg Id Rohit’s PhoneXYZ Step 7: Device fetches data from App Server to process Security Token AppServer123

83 C2DM Demo Lets see the Demo

84

85

86

87

88

89

90

91 Q & A 91

92 More about Me Twitter - http://twitter.com/#!/rohitghatolhttp://twitter.com/#!/rohitghatol TechGig - http://www.techgig.com/rohitghatolhttp://www.techgig.com/rohitghatol LinkedIn - http://www.linkedin.com/in/rohitghatol http://www.linkedin.com/in/rohitghatol Presentations - www.slideshare.net/rohitsghatol/ www.slideshare.net/rohitsghatol/ YouTube Tutorials - http://www.youtube.com/user/rohitssghatol http://www.youtube.com/user/rohitssghatol


Download ppt "Building Twitter App Rohit Ghatol. About Me Rohit Ghatol 2.Project 3.Certified Scrum Master 4.Author “Beginning."

Similar presentations


Ads by Google