Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Tweet feed added

  • Loading branch information...
commit 88bb1b8e943951b1ca75f71c57aa1e786de3e5f1 1 parent a136ef0
authored March 15, 2013
8  AndroidManifest.xml
@@ -25,13 +25,15 @@
25 25
         </activity>
26 26
         <activity android:name=".twitter.TwitterSignInActivity" />
27 27
         <activity android:name=".twitter.TwitterAccountActivity" />
28  
-        <activity android:name=".twitter.TweetActivity" >
  28
+        <activity android:name=".twitter.TweetsFeedActivity" >
29 29
             <intent-filter>
30 30
                 <action android:name="android.intent.action.MAIN" />
31 31
 
32 32
                 <category android:name="android.intent.category.LAUNCHER" />
33 33
             </intent-filter>
34 34
         </activity>
  35
+        <activity android:name=".twitter.TweetActivity" >
  36
+        </activity>
35 37
         <activity android:name=".home.AddAcount" />
36 38
         <activity
37 39
             android:name=".home.AboutActivity"
@@ -62,6 +64,10 @@
62 64
                 <action android:name="com.codeskraps.lolo.BROADCAST_RECEIVER" />
63 65
             </intent-filter>
64 66
         </receiver>
  67
+
  68
+        <service
  69
+            android:name=".twitter.TwitterService"
  70
+            android:exported="false" />
65 71
     </application>
66 72
 
67 73
 </manifest>
BIN  res/drawable-hdpi/action_settings.png
BIN  res/drawable-hdpi/content_picture.png
BIN  res/drawable-hdpi/social_send_now.png
BIN  res/drawable-mdpi/action_settings.png
BIN  res/drawable-mdpi/content_picture.png
BIN  res/drawable-mdpi/social_send_now.png
BIN  res/drawable-xhdpi/action_settings.png
BIN  res/drawable-xhdpi/content_picture.png
BIN  res/drawable-xhdpi/social_send_now.png
38  res/layout/row_tweet.xml
... ...
@@ -0,0 +1,38 @@
  1
+<?xml version="1.0" encoding="utf-8"?>
  2
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3
+    android:layout_width="match_parent"
  4
+    android:layout_height="match_parent"
  5
+    android:orientation="horizontal" >
  6
+
  7
+    <ImageView
  8
+        android:id="@+id/tws_row_img_user"
  9
+        android:layout_width="40dp"
  10
+        android:layout_height="40dp"
  11
+        android:layout_marginBottom="5dp"
  12
+        android:layout_marginLeft="10dp"
  13
+        android:layout_marginRight="5dp"
  14
+        android:layout_marginTop="5dp"
  15
+        android:contentDescription="@string/contentDescription" />
  16
+
  17
+    <LinearLayout
  18
+        android:layout_width="wrap_content"
  19
+        android:layout_height="wrap_content"
  20
+        android:orientation="vertical" >
  21
+
  22
+        <TextView
  23
+            android:id="@+id/tws_row_txt_user"
  24
+            android:layout_width="match_parent"
  25
+            android:layout_height="wrap_content"
  26
+            android:layout_marginBottom="5dp"
  27
+            android:layout_marginRight="10dp"
  28
+            android:textIsSelectable="false" />
  29
+
  30
+        <TextView
  31
+            android:id="@+id/tws_row_txt_text"
  32
+            android:layout_width="match_parent"
  33
+            android:layout_height="wrap_content"
  34
+            android:layout_marginRight="10dp"
  35
+            android:textIsSelectable="false" />
  36
+    </LinearLayout>
  37
+
  38
+</LinearLayout>
17  res/layout/tweets.xml
... ...
@@ -0,0 +1,17 @@
  1
+<?xml version="1.0" encoding="utf-8"?>
  2
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3
+    android:layout_width="match_parent"
  4
+    android:layout_height="match_parent"
  5
+    android:orientation="vertical" >
  6
+
  7
+    <View
  8
+        android:layout_width="match_parent"
  9
+        android:layout_height="1dp"
  10
+        android:background="#FF0000" />
  11
+
  12
+    <ListView
  13
+        android:id="@+id/tws_lst"
  14
+        android:layout_width="match_parent"
  15
+        android:layout_height="match_parent" />
  16
+
  17
+</LinearLayout>
6  res/menu/activity_main.xml
... ...
@@ -1,7 +1,13 @@
1 1
 <menu xmlns:android="http://schemas.android.com/apk/res/android" >
2 2
 
3 3
     <item
  4
+        android:id="@+id/men_tweet"
  5
+        android:icon="@drawable/social_send_now"
  6
+        android:showAsAction="always"
  7
+        android:title="@string/men_tweet"/>
  8
+    <item
4 9
         android:id="@+id/men_settings"
  10
+        android:icon="@drawable/action_settings"
5 11
         android:showAsAction="always"
6 12
         android:title="@string/men_setting"/>
7 13
 
5  res/values/strings.xml
@@ -17,6 +17,7 @@
17 17
 
18 18
     <string-array name="OnClick_entries">
19 19
         <item>Do nothing</item>
  20
+        <item>Open tweets feed</item>
20 21
         <item>Update lo-lo status</item>
21 22
         <item>Open widget settings</item>
22 23
         <item>Open URL</item>
@@ -26,6 +27,7 @@
26 27
         <item>1</item>
27 28
         <item>2</item>
28 29
         <item>3</item>
  30
+        <item>4</item>
29 31
     </string-array>
30 32
 
31 33
     <string name="prefsURL_title">URL</string>
@@ -67,7 +69,7 @@
67 69
     <string name="no_site_error">Sorry, could not connect to the WordPress site. Please try again later.</string>
68 70
 
69 71
     <!-- Twitter Accont -->
70  
-    <string name="twi_help">You got to sign in to Twitter using OAth and allow access to 091 Labs lo-lo app. Once you do it will give a code, come here and enter the code you are given.</string>
  72
+    <string name="twi_help">First sign into Twitter using OAth and allow access to 091 Labs lo-lo app. Once you do it will give a code, come here and enter the code you were given.</string>
71 73
     <string name="twi_stepone">Step One</string>
72 74
     <string name="twi_steptwo">Setp Two</string>
73 75
     <string name="twi_goto">Go to Twitter</string>
@@ -86,6 +88,7 @@
86 88
 
87 89
     <!-- Menu -->
88 90
     <string name="men_setting">Settings</string>
  91
+    <string name="men_tweet">Tweet</string>
89 92
 
90 93
     <!-- About -->
91 94
     <string name="about_title">091 Labs lo-lo</string>
15  src/com/codeskraps/lolo/home/LoloApp.java
@@ -3,15 +3,20 @@
3 3
 import org.acra.ACRA;
4 4
 import org.acra.annotation.ReportsCrashes;
5 5
 
  6
+import com.codeskraps.lolo.twitter.TwitterService;
  7
+
6 8
 import android.app.Application;
  9
+import android.content.Intent;
7 10
 
8 11
 @ReportsCrashes(formKey = "dC1lU3BVVGh4ejlQSTJta05pNXBPQVE6MQ")
9 12
 public class LoloApp extends Application {
10 13
 
11 14
 	@Override
12  
-    public void onCreate() {
13  
-        // The following line triggers the initialization of ACRA
14  
-        ACRA.init(this);
15  
-        super.onCreate();
16  
-    }
  15
+	public void onCreate() {
  16
+		// The following line triggers the initialization of ACRA
  17
+		ACRA.init(this);
  18
+		super.onCreate();
  19
+
  20
+		startService(new Intent(this, TwitterService.class));
  21
+	}
17 22
 }
27  src/com/codeskraps/lolo/home/UpdateWidgetReceiver.java
@@ -76,36 +76,11 @@ public void onReceive(Context context, Intent intent) {
76 76
 		public void run() {
77 77
 			try {
78 78
 				lolo = Utils.getLolo();
79  
-				// loloRSS =
80  
-				// Utils.getRSSFedd(prefs.getString(Constants.RSS_TITLE, " "));
81  
-				// Log.d(TAG, "loloRSS" + loloRSS[0]);
82  
-				// if (loloRSS != null) {
83  
-				// SharedPreferences.Editor editor = prefs.edit();
84  
-				// editor.putString(Constants.RSS_TITLE, loloRSS[0]);
85  
-				// editor.commit();
86  
-				// }
87  
-
88  
-				// @formatter:off
89  
-//			} catch (SAXException e) {
90  
-//				if (BuildConfig.DEBUG) Log.e(TAG, (e == null) ? "SAXException" : e.getMessage());
91  
-			} catch (UnsupportedEncodingException e) {
92  
-				if (BuildConfig.DEBUG) Log.e(TAG, (e == null) ? "UnsupportedEncodingException" : e.getMessage());
93  
-			} catch (ClientProtocolException e) {
94  
-				if (BuildConfig.DEBUG) Log.e(TAG, (e == null) ? "ClientProtocolException" : e.getMessage());
95  
-			} catch (IOException e) {
96  
-				if (BuildConfig.DEBUG) Log.e(TAG, (e == null) ? "IOException" : e.getMessage());
97  
-			} catch (IllegalArgumentException e) {
98  
-				if (BuildConfig.DEBUG) Log.e(TAG, (e == null) ? "IllegalArgumentException" : e.getMessage());
99  
-			} catch (NullPointerException e) {
100  
-				if (BuildConfig.DEBUG) Log.e(TAG, (e == null) ? "NullPointerException" : e.getMessage());
101  
-			} catch (JSONException e) {
102  
-				if (BuildConfig.DEBUG) Log.e(TAG, (e == null) ? "JSONException" : e.getMessage());
103 79
 			} catch (Exception e) {
104  
-				if (BuildConfig.DEBUG) Log.e(TAG, (e == null) ? "Exception" : e.getMessage());
  80
+				Log.i(TAG, "Handled:" + e, e);
105 81
 			} finally {
106 82
 				handler.post(new MyRunnable());
107 83
 			}
108  
-			// @formatter:off
109 84
 		}
110 85
 	}
111 86
 
121  src/com/codeskraps/lolo/misc/ImageHelper.java
... ...
@@ -0,0 +1,121 @@
  1
+package com.codeskraps.lolo.misc;
  2
+
  3
+import java.io.InputStream;
  4
+import java.lang.ref.WeakReference;
  5
+import java.net.HttpURLConnection;
  6
+import java.net.URL;
  7
+
  8
+import com.codeskraps.lolo.R;
  9
+
  10
+import android.content.res.Resources;
  11
+import android.graphics.Bitmap;
  12
+import android.graphics.BitmapFactory;
  13
+import android.graphics.drawable.BitmapDrawable;
  14
+import android.graphics.drawable.Drawable;
  15
+import android.os.AsyncTask;
  16
+import android.util.Log;
  17
+import android.widget.ImageView;
  18
+
  19
+public class ImageHelper {
  20
+	private static final String TAG = ImageHelper.class.getSimpleName();
  21
+
  22
+	public static void loadBitmap(Resources res, String url, ImageView imageView) {
  23
+		if (cancelPotentialWork(url, imageView)) {
  24
+			final BitmapWorkerTask task = new BitmapWorkerTask(imageView);
  25
+			Bitmap mPlaceHolderBitmap = BitmapFactory.decodeResource(res,
  26
+					R.drawable.content_picture);
  27
+			final AsyncDrawable asyncDrawable = new AsyncDrawable(res, mPlaceHolderBitmap, task);
  28
+			imageView.setImageDrawable(asyncDrawable);
  29
+			task.execute(url);
  30
+
  31
+		}
  32
+	}
  33
+
  34
+	private static BitmapWorkerTask getBitmapWorkerTask(ImageView imageView) {
  35
+		if (imageView != null) {
  36
+			final Drawable drawable = imageView.getDrawable();
  37
+			if (drawable instanceof AsyncDrawable) {
  38
+				final AsyncDrawable asyncDrawable = (AsyncDrawable) drawable;
  39
+				return asyncDrawable.getBitmapWorkerTask();
  40
+			}
  41
+		}
  42
+		return null;
  43
+	}
  44
+
  45
+	private static boolean cancelPotentialWork(String url, ImageView imageView) {
  46
+		final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView);
  47
+
  48
+		if (bitmapWorkerTask != null) {
  49
+			final String bitmapData = bitmapWorkerTask.data;
  50
+			if (bitmapData.equals(url) == false) {
  51
+				// Cancel previous task
  52
+				bitmapWorkerTask.cancel(true);
  53
+			} else {
  54
+				// The same work is already in progress
  55
+				return false;
  56
+			}
  57
+		}
  58
+		// No task associated with the ImageView, or an existing task was
  59
+		// cancelled
  60
+		return true;
  61
+	}
  62
+
  63
+	private static class AsyncDrawable extends BitmapDrawable {
  64
+		private final WeakReference<BitmapWorkerTask> bitmapWorkerTaskReference;
  65
+
  66
+		public AsyncDrawable(Resources res, Bitmap bitmap, BitmapWorkerTask bitmapWorkerTask) {
  67
+			super(res, bitmap);
  68
+			bitmapWorkerTaskReference = new WeakReference<BitmapWorkerTask>(bitmapWorkerTask);
  69
+		}
  70
+
  71
+		public BitmapWorkerTask getBitmapWorkerTask() {
  72
+			return bitmapWorkerTaskReference.get();
  73
+		}
  74
+	}
  75
+
  76
+	private static class BitmapWorkerTask extends AsyncTask<String, Void, Bitmap> {
  77
+		private final WeakReference<ImageView> imageViewReference;
  78
+		private String data = "";
  79
+
  80
+		public BitmapWorkerTask(ImageView imageView) {
  81
+			// Use a WeakReference to ensure the ImageView can be garbage
  82
+			// collected
  83
+			imageViewReference = new WeakReference<ImageView>(imageView);
  84
+		}
  85
+
  86
+		// Decode image in background.
  87
+		@Override
  88
+		protected Bitmap doInBackground(String... params) {
  89
+			data = params[0];
  90
+			try {
  91
+				URL url = new URL(data);
  92
+				HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  93
+				conn.setDoInput(true);
  94
+				conn.connect();
  95
+				InputStream is = conn.getInputStream();
  96
+				return BitmapFactory.decodeStream(is);
  97
+
  98
+			} catch (Exception e) {
  99
+				Log.i(TAG, "Handled: image - " + e.getMessage(), e);
  100
+			}
  101
+
  102
+			return null;
  103
+		}
  104
+
  105
+		// Once complete, see if ImageView is still around and set bitmap.
  106
+		@Override
  107
+		protected void onPostExecute(Bitmap bitmap) {
  108
+			if (isCancelled()) {
  109
+				bitmap = null;
  110
+			}
  111
+
  112
+			if (imageViewReference != null && bitmap != null) {
  113
+				final ImageView imageView = imageViewReference.get();
  114
+				final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView);
  115
+				if (this == bitmapWorkerTask && imageView != null) {
  116
+					imageView.setImageBitmap(bitmap);
  117
+				}
  118
+			}
  119
+		}
  120
+	}
  121
+}
12  src/com/codeskraps/lolo/misc/Utils.java
@@ -52,6 +52,8 @@
52 52
 import com.codeskraps.lolo.R;
53 53
 import com.codeskraps.lolo.home.PrefsActivity;
54 54
 import com.codeskraps.lolo.misc.Constants.LOLO;
  55
+import com.codeskraps.lolo.twitter.TweetActivity;
  56
+import com.codeskraps.lolo.twitter.TweetsFeedActivity;
55 57
 
56 58
 public class Utils {
57 59
 	private static final String TAG = Utils.class.getSimpleName();
@@ -111,19 +113,25 @@ public static PendingIntent getOnTouchIntent(Context context) {
111 113
 			break;
112 114
 
113 115
 		case 1:
  116
+			intent = new Intent(context, TweetsFeedActivity.class);
  117
+			pendingIntent = PendingIntent.getActivity(context, 0, intent,
  118
+					PendingIntent.FLAG_UPDATE_CURRENT);
  119
+			break;
  120
+
  121
+		case 2:
114 122
 			intent = new Intent();
115 123
 			intent.setAction(Constants.BROADCAST_RECEIVER);
116 124
 			pendingIntent = PendingIntent.getBroadcast(context, 0, intent,
117 125
 					PendingIntent.FLAG_UPDATE_CURRENT);
118 126
 			break;
119 127
 
120  
-		case 2:
  128
+		case 3:
121 129
 			intent = new Intent(context, PrefsActivity.class);
122 130
 			pendingIntent = PendingIntent.getActivity(context, 0, intent,
123 131
 					PendingIntent.FLAG_UPDATE_CURRENT);
124 132
 			break;
125 133
 
126  
-		case 3:
  134
+		case 4:
127 135
 			intent = new Intent(Intent.ACTION_VIEW);
128 136
 			String url = prefs.getString(Constants.EURL,
129 137
 					context.getString(R.string.prefsURL_default));
14  src/com/codeskraps/lolo/twitter/TweetActivity.java
@@ -9,13 +9,11 @@
9 9
 import org.scribe.oauth.OAuthService;
10 10
 
11 11
 import android.app.Activity;
12  
-import android.content.Intent;
13 12
 import android.content.SharedPreferences;
14 13
 import android.os.AsyncTask;
15 14
 import android.os.Bundle;
16 15
 import android.preference.PreferenceManager;
17 16
 import android.util.Log;
18  
-import android.view.Menu;
19 17
 import android.view.MenuItem;
20 18
 import android.view.View;
21 19
 import android.view.View.OnClickListener;
@@ -25,7 +23,6 @@
25 23
 import android.widget.TextView;
26 24
 
27 25
 import com.codeskraps.lolo.R;
28  
-import com.codeskraps.lolo.home.PrefsActivity;
29 26
 import com.codeskraps.lolo.misc.Constants;
30 27
 
31 28
 public class TweetActivity extends Activity implements OnClickListener {
@@ -42,6 +39,7 @@ protected void onCreate(Bundle savedInstanceState) {
42 39
 		service = new ServiceBuilder().provider(TwitterApi.class).apiKey("Z4lEh5rSu0rV3fXt37gw7A")
43 40
 				.apiSecret("vLLTqO311ZhlVXhl1GaB72DnIwdCOPwzeozNRWy3I").build();
44 41
 		requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
  42
+		getActionBar().setDisplayHomeAsUpEnabled(true);
45 43
 
46 44
 		setContentView(R.layout.tweet);
47 45
 
@@ -105,16 +103,10 @@ protected void onPostExecute(String result) {
105 103
 	}
106 104
 
107 105
 	@Override
108  
-	public boolean onCreateOptionsMenu(Menu menu) {
109  
-		getMenuInflater().inflate(R.menu.activity_main, menu);
110  
-		return true;
111  
-	}
112  
-
113  
-	@Override
114 106
 	public boolean onOptionsItemSelected(MenuItem item) {
115 107
 		switch (item.getItemId()) {
116  
-		case R.id.men_settings:
117  
-			startActivity(new Intent(this, PrefsActivity.class));
  108
+		case android.R.id.home:
  109
+			finish();
118 110
 			break;
119 111
 		}
120 112
 		return true;
203  src/com/codeskraps/lolo/twitter/TweetsFeedActivity.java
... ...
@@ -0,0 +1,203 @@
  1
+package com.codeskraps.lolo.twitter;
  2
+
  3
+import java.lang.reflect.Type;
  4
+import java.util.HashMap;
  5
+import java.util.List;
  6
+import java.util.Map;
  7
+
  8
+import org.scribe.builder.ServiceBuilder;
  9
+import org.scribe.builder.api.TwitterApi;
  10
+import org.scribe.model.OAuthRequest;
  11
+import org.scribe.model.Token;
  12
+import org.scribe.model.Verb;
  13
+import org.scribe.oauth.OAuthService;
  14
+
  15
+import android.app.Activity;
  16
+import android.content.Context;
  17
+import android.content.Intent;
  18
+import android.content.SharedPreferences;
  19
+import android.os.AsyncTask;
  20
+import android.os.Bundle;
  21
+import android.preference.PreferenceManager;
  22
+import android.util.Log;
  23
+import android.view.LayoutInflater;
  24
+import android.view.Menu;
  25
+import android.view.MenuItem;
  26
+import android.view.View;
  27
+import android.view.ViewGroup;
  28
+import android.view.Window;
  29
+import android.widget.BaseAdapter;
  30
+import android.widget.ImageView;
  31
+import android.widget.ListView;
  32
+import android.widget.TextView;
  33
+
  34
+import com.codeskraps.lolo.R;
  35
+import com.codeskraps.lolo.home.PrefsActivity;
  36
+import com.codeskraps.lolo.misc.Constants;
  37
+import com.codeskraps.lolo.misc.ImageHelper;
  38
+import com.google.gson.Gson;
  39
+import com.google.gson.reflect.TypeToken;
  40
+
  41
+public class TweetsFeedActivity extends Activity {
  42
+	private static final String TAG = TweetsFeedActivity.class.getSimpleName();
  43
+	private static final String RESOURCE_URL = "https://api.twitter.com/1.1/statuses/home_timeline.json";
  44
+
  45
+	private OAuthService service = null;
  46
+	private TweetsAdapter adapter = null;
  47
+
  48
+	@Override
  49
+	protected void onCreate(Bundle savedInstanceState) {
  50
+		super.onCreate(savedInstanceState);
  51
+		requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
  52
+		service = new ServiceBuilder().provider(TwitterApi.class).apiKey("Z4lEh5rSu0rV3fXt37gw7A")
  53
+				.apiSecret("vLLTqO311ZhlVXhl1GaB72DnIwdCOPwzeozNRWy3I").build();
  54
+
  55
+		setContentView(R.layout.tweets);
  56
+
  57
+		adapter = new TweetsAdapter(this);
  58
+
  59
+		ListView lstTweets = (ListView) findViewById(R.id.tws_lst);
  60
+		lstTweets.setAdapter(adapter);
  61
+	}
  62
+
  63
+	@Override
  64
+	protected void onResume() {
  65
+		super.onResume();
  66
+		new GetTimeline().execute();
  67
+	}
  68
+
  69
+	@Override
  70
+	public boolean onCreateOptionsMenu(Menu menu) {
  71
+		getMenuInflater().inflate(R.menu.activity_main, menu);
  72
+		return true;
  73
+	}
  74
+
  75
+	@Override
  76
+	public boolean onOptionsItemSelected(MenuItem item) {
  77
+		switch (item.getItemId()) {
  78
+		case R.id.men_tweet:
  79
+			startActivity(new Intent(this, TweetActivity.class));
  80
+			break;
  81
+		case R.id.men_settings:
  82
+			startActivity(new Intent(this, PrefsActivity.class));
  83
+			break;
  84
+		}
  85
+		return true;
  86
+	}
  87
+
  88
+	private class GetTimeline extends AsyncTask<Void, Void, String> {
  89
+
  90
+		@Override
  91
+		protected void onPreExecute() {
  92
+			super.onPreExecute();
  93
+			setProgressBarIndeterminateVisibility(Boolean.TRUE);
  94
+		}
  95
+
  96
+		@Override
  97
+		protected String doInBackground(Void... params) {
  98
+			try {
  99
+				SharedPreferences prefs = PreferenceManager
  100
+						.getDefaultSharedPreferences(getApplication());
  101
+				String token = prefs.getString(Constants.ACCESS_TOKEN, null);
  102
+				String secret = prefs.getString(Constants.ACCESS_SECRET, null);
  103
+
  104
+				Token accessToken = new Token(token, secret);
  105
+
  106
+				OAuthRequest request = new OAuthRequest(Verb.GET, RESOURCE_URL);
  107
+				service.signRequest(accessToken, request);
  108
+				return request.send().getBody();
  109
+
  110
+			} catch (Exception e) {
  111
+				Log.i(TAG, "Handled: GetTimeline - " + e, e);
  112
+			}
  113
+			return null;
  114
+		}
  115
+
  116
+		@Override
  117
+		protected void onPostExecute(String result) {
  118
+			super.onPostExecute(result);
  119
+			new Gson();
  120
+
  121
+			setProgressBarIndeterminateVisibility(Boolean.FALSE);
  122
+
  123
+			if (result != null) {
  124
+				try {
  125
+					Log.v(TAG, result);
  126
+					Type collectionType = new TypeToken<List<Map<String, Object>>>() {}.getType();
  127
+					List<Map<String, Object>> tweets = new Gson().fromJson(result, collectionType);
  128
+					adapter.setTweets(tweets);
  129
+					adapter.notifyDataSetChanged();
  130
+				} catch (Exception e) {
  131
+					Log.i(TAG, "Handle: onPostExecute - " + e, e);
  132
+					Log.i(TAG, "Handle: result:" + result);
  133
+				}
  134
+			}
  135
+		}
  136
+	}
  137
+
  138
+	private class TweetsAdapter extends BaseAdapter {
  139
+		private List<Map<String, Object>> tweets = null;
  140
+		private LayoutInflater mInflater = null;
  141
+
  142
+		public TweetsAdapter(Context context) {
  143
+			mInflater = LayoutInflater.from(context);
  144
+		}
  145
+
  146
+		public void setTweets(List<Map<String, Object>> tweets) {
  147
+			this.tweets = tweets;
  148
+		}
  149
+
  150
+		@Override
  151
+		public int getCount() {
  152
+			if (tweets == null || tweets.isEmpty()) return 0;
  153
+			return tweets.size();
  154
+		}
  155
+
  156
+		@Override
  157
+		public Object getItem(int position) {
  158
+			return tweets.get(position);
  159
+		}
  160
+
  161
+		@Override
  162
+		public long getItemId(int position) {
  163
+			return position;
  164
+		}
  165
+
  166
+		@Override
  167
+		public View getView(int position, View convertView, ViewGroup parent) {
  168
+			ViewHolder vHolder = null;
  169
+
  170
+			if (convertView != null) vHolder = (ViewHolder) convertView.getTag();
  171
+			else {
  172
+				convertView = (View) mInflater.inflate(R.layout.row_tweet, null);
  173
+
  174
+				vHolder = new ViewHolder();
  175
+				vHolder.ImgUser = (ImageView) convertView.findViewById(R.id.tws_row_img_user);
  176
+				vHolder.text = (TextView) convertView.findViewById(R.id.tws_row_txt_text);
  177
+				vHolder.txtUser = (TextView) convertView.findViewById(R.id.tws_row_txt_user);
  178
+
  179
+				convertView.setTag(vHolder);
  180
+				convertView.setId(position);
  181
+			}
  182
+
  183
+			try {
  184
+				Map<String, Object> tweet = tweets.get(position);
  185
+				Map<String, Object> user = (Map<String, Object>) tweet.get("user");
  186
+				
  187
+				String url = (String) user.get("profile_image_url");
  188
+				ImageHelper.loadBitmap(getResources(), url, vHolder.ImgUser);
  189
+				vHolder.txtUser.setText((String) user.get("name"));
  190
+				vHolder.text.setText((String) tweet.get("text"));
  191
+			} catch (Exception e) {
  192
+				Log.i(TAG, "Handled: " + e, e);
  193
+			}
  194
+			return convertView;
  195
+		}
  196
+	}
  197
+
  198
+	public static class ViewHolder {
  199
+		ImageView ImgUser;
  200
+		TextView text;
  201
+		TextView txtUser;
  202
+	}
  203
+}
86  src/com/codeskraps/lolo/twitter/TwitterService.java
... ...
@@ -0,0 +1,86 @@
  1
+package com.codeskraps.lolo.twitter;
  2
+
  3
+import java.lang.reflect.Type;
  4
+import java.util.List;
  5
+import java.util.Map;
  6
+
  7
+import org.scribe.builder.ServiceBuilder;
  8
+import org.scribe.builder.api.TwitterApi;
  9
+import org.scribe.model.OAuthRequest;
  10
+import org.scribe.model.Token;
  11
+import org.scribe.model.Verb;
  12
+import org.scribe.oauth.OAuthService;
  13
+
  14
+import android.app.IntentService;
  15
+import android.content.Intent;
  16
+import android.content.SharedPreferences;
  17
+import android.os.AsyncTask;
  18
+import android.preference.PreferenceManager;
  19
+import android.util.Log;
  20
+
  21
+import com.codeskraps.lolo.misc.Constants;
  22
+import com.google.gson.Gson;
  23
+import com.google.gson.reflect.TypeToken;
  24
+
  25
+public class TwitterService extends IntentService {
  26
+	private static final String TAG = TwitterService.class.getSimpleName();
  27
+	private static final String RESOURCE_URL = "https://api.twitter.com/1.1/statuses/home_timeline.json";
  28
+
  29
+	private OAuthService service = null;
  30
+
  31
+	public TwitterService() {
  32
+		super(TAG);
  33
+	}
  34
+
  35
+	public TwitterService(String name) {
  36
+		super(name);
  37
+	}
  38
+
  39
+	@Override
  40
+	protected void onHandleIntent(Intent intent) {
  41
+		new GetTimeline().execute();
  42
+	}
  43
+
  44
+	private class GetTimeline extends AsyncTask<Void, Void, String> {
  45
+
  46
+		@Override
  47
+		protected String doInBackground(Void... params) {
  48
+			try {
  49
+				service = new ServiceBuilder().provider(TwitterApi.class)
  50
+						.apiKey("Z4lEh5rSu0rV3fXt37gw7A")
  51
+						.apiSecret("vLLTqO311ZhlVXhl1GaB72DnIwdCOPwzeozNRWy3I").build();
  52
+
  53
+				SharedPreferences prefs = PreferenceManager
  54
+						.getDefaultSharedPreferences(getApplication());
  55
+				String token = prefs.getString(Constants.ACCESS_TOKEN, null);
  56
+				String secret = prefs.getString(Constants.ACCESS_SECRET, null);
  57
+
  58
+				Token accessToken = new Token(token, secret);
  59
+
  60
+				OAuthRequest request = new OAuthRequest(Verb.GET, RESOURCE_URL);
  61
+				service.signRequest(accessToken, request);
  62
+				return request.send().getBody();
  63
+
  64
+			} catch (Exception e) {
  65
+				Log.i(TAG, "Handled: GetTimeline - " + e, e);
  66
+			}
  67
+			return null;
  68
+		}
  69
+
  70
+		@Override
  71
+		protected void onPostExecute(String result) {
  72
+			super.onPostExecute(result);
  73
+			new Gson();
  74
+
  75
+			if (result != null) {
  76
+				Log.v(TAG, result);
  77
+				Type collectionType = new TypeToken<List<Map<String, Object>>>() {}.getType();
  78
+				List<Map<String, Object>> tweets = new Gson().fromJson(result, collectionType);
  79
+				for (Map m : tweets) {
  80
+					String blah = (String) m.get("text");
  81
+					Log.d(TAG, blah);
  82
+				}
  83
+			}
  84
+		}
  85
+	}
  86
+}

0 notes on commit 88bb1b8

Please sign in to comment.
Something went wrong with that request. Please try again.