diff --git a/.classpath b/.classpath
index 94e92eb..2dd540a 100644
--- a/.classpath
+++ b/.classpath
@@ -1,18 +1,14 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/.gitignore b/.gitignore
index 0e96cd3..970ae35 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,5 @@
/bin
/gen
/assets
+/target
+
diff --git a/.project b/.project
index d0e396c..1a96ea7 100644
--- a/.project
+++ b/.project
@@ -1,33 +1,39 @@
-
-
- AndroidTwitterGoogleApiJavaClient
-
-
-
-
-
- com.android.ide.eclipse.adt.ResourceManagerBuilder
-
-
-
-
- com.android.ide.eclipse.adt.PreCompilerBuilder
-
-
-
-
- org.eclipse.jdt.core.javabuilder
-
-
-
-
- com.android.ide.eclipse.adt.ApkBuilder
-
-
-
-
-
- com.android.ide.eclipse.adt.AndroidNature
- org.eclipse.jdt.core.javanature
-
-
+
+
+ AndroidTwitterGoogleApiJavaClient
+
+
+
+
+
+ com.android.ide.eclipse.adt.ResourceManagerBuilder
+
+
+
+
+ com.android.ide.eclipse.adt.PreCompilerBuilder
+
+
+
+
+ org.eclipse.jdt.core.javabuilder
+
+
+
+
+ com.android.ide.eclipse.adt.ApkBuilder
+
+
+
+
+ org.eclipse.m2e.core.maven2Builder
+
+
+
+
+
+ org.eclipse.m2e.core.maven2Nature
+ com.android.ide.eclipse.adt.AndroidNature
+ org.eclipse.jdt.core.javanature
+
+
diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs
new file mode 100644
index 0000000..99f26c0
--- /dev/null
+++ b/.settings/org.eclipse.core.resources.prefs
@@ -0,0 +1,2 @@
+eclipse.preferences.version=1
+encoding/=UTF-8
diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs
new file mode 100644
index 0000000..abec6ca
--- /dev/null
+++ b/.settings/org.eclipse.jdt.core.prefs
@@ -0,0 +1,5 @@
+eclipse.preferences.version=1
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5
+org.eclipse.jdt.core.compiler.compliance=1.5
+org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
+org.eclipse.jdt.core.compiler.source=1.5
diff --git a/.settings/org.eclipse.m2e.core.prefs b/.settings/org.eclipse.m2e.core.prefs
new file mode 100644
index 0000000..f897a7f
--- /dev/null
+++ b/.settings/org.eclipse.m2e.core.prefs
@@ -0,0 +1,4 @@
+activeProfiles=
+eclipse.preferences.version=1
+resolveWorkspaceProjects=true
+version=1
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index b1d4848..2cc22ff 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -3,11 +3,19 @@
package="com.ecs.sample"
android:versionCode="1"
android:versionName="1.0">
-
+
+
-
+
+
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..a91b366
--- /dev/null
+++ b/README.md
@@ -0,0 +1,108 @@
+##Introduction
+
+##Software requirements
+
+
+##Project setup
+
+### m2e-android plugin
+
+This project is built using the [m2e-android plugin](http://rgladwell.github.io/m2e-android/index.html) to handle its external dependencies.
+
+When using Eclipse ADT, it assumes that the following components are installed :
+
+- Eclipse Market Client
+- [m2e-android plugin](http://rgladwell.github.io/m2e-android/index.html)
+- [maven-android-sdk-deployer](https://github.com/mosabua/)
+
+### Eclipse MarketPlace
+
+If you don't have the Eclipse Market Client installed, you can install it by clicking on
+
+```Help → Install new Software → Switch to the Juno Repository → General Purpose Tools → Marketplace Client```
+
+
+
+Once you have the Eclipse Market Client installed, you can proceed to install the m2e-android plugin
+
+
+
+```Help -> Eclipse Marketplace... and search for "android m2e".```
+
+
+
+More instructions can be found on the [m2e-android plugin](http://rgladwell.github.io/m2e-android/index.html) site.
+
+### maven-android-sdk-deployer
+
+Clone the [maven-android-sdk-deployer](https://github.com/mosabua/maven-android-sdk-deployer) and execute mvn install. This will install the required projects into your local maven repository.
+
+### Environment setup
+
+Make sure you have your ANDROID_HOME variable pointing to your SDK
+
+
+
+### Clone and import the project
+
+Clone the project on your filesystem
+
+ git clone git@github.com:ddewaele/AndroidTwitterGoogleApiJavaClient.git
+ Cloning into AndroidTwitterGoogleApiJavaClient...
+ remote: Reusing existing pack: 145, done.
+ remote: Counting objects: 11, done.
+ remote: Compressing objects: 100% (8/8), done.
+ remote: Total 156 (delta 0), reused 0 (delta 0)
+ Receiving objects: 100% (156/156), 529.74 KiB | 483 KiB/s, done.
+ Resolving deltas: 100% (33/33), done.
+
+Import the project in Eclipse ADT.
+
+When importing the project, select ```Existing Maven Projects```.
+
+
+
+Navigate to the place where you cloned the repository (ex: /Users/ddewaele/Projects/AndroidTwitterGoogleApiJavaClient) and click Finish.
+
+
+
+After that, Eclipse ADT and the Maven M2E plugin should import your project and setup all the dependencies.
+
+
+
+
+### Twitter OAuth application and keys
+
+As this application allows you to interact with Twitter, you need to go to to the [Twitter Developer page](dev.twitter.com) to [register a new application](https://apps.twitter.com/app/new).
+
+Here are a couple of steps you need to follow
+
+- Register a new application in the Twitter dev console
+
+
+
+- Verify that the application was created succesfully.
+
+
+
+- Set the correct permissions
+
+
+
+- Copy the key and secret from the app.
+
+
+
+- Put the key and secret in the [Constants file](https://github.com/ddewaele/AndroidTwitterGoogleApiJavaClient/blob/master/src/com/ecs/sample/Constants.java)
+
+Replace the placeholders below with the actual KEY and SECRET
+
+ public static final String API_KEY = "PUT YOUR TWITTER API KEY HERE";
+ public static final String API_SECRET= "PUT YOUR TWITTER API SECRET HERE";
+
+
+Launch the application and you should see the following screens.
+
+
+
+
diff --git a/default.properties b/default.properties
deleted file mode 100644
index b74c488..0000000
--- a/default.properties
+++ /dev/null
@@ -1,11 +0,0 @@
-# This file is automatically generated by Android Tools.
-# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
-#
-# This file must be checked in Version Control Systems.
-#
-# To customize properties used by the Ant build system use,
-# "build.properties", and override values to adapt the script to your
-# project structure.
-
-# Project target.
-target=android-4
diff --git a/lib/twitter4j-core-2.1.11.jar b/lib/twitter4j-core-2.1.11.jar
deleted file mode 100644
index b0fb214..0000000
Binary files a/lib/twitter4j-core-2.1.11.jar and /dev/null differ
diff --git a/pom.xml b/pom.xml
new file mode 100644
index 0000000..973baa9
--- /dev/null
+++ b/pom.xml
@@ -0,0 +1,86 @@
+
+
+ 4.0.0
+ com.ecs
+ com.ecs.android.twitter.ouath
+ 0.0.1-SNAPSHOT
+ apk
+ com.ecs.android.twitter.ouath
+
+
+ UTF-8
+ 4.1.1.4
+ 3.6.0
+ 1.18.0-rc
+ UTF-8
+
+
+
+
+ com.google.android
+ android
+ ${platform.version}
+ provided
+
+
+ xpp3
+ xpp3
+
+
+
+
+
+ com.google.api-client
+ google-api-client
+ ${com.google.api-client.version}
+
+
+
+ com.google.http-client
+ google-http-client-jackson2
+ ${com.google.api-client.version}
+
+
+ xpp3
+ xpp3
+
+
+
+
+
+ org.twitter4j
+ twitter4j-core
+ 4.0.1
+
+
+
+
+
+
+
+ ${project.artifactId}
+
+
+
+ com.jayway.maven.plugins.android.generation2
+ android-maven-plugin
+ ${android.plugin.version}
+ true
+
+
+
+
+
+ com.jayway.maven.plugins.android.generation2
+ android-maven-plugin
+
+
+ 16
+ /Users/davydewaele/Soft/adt-bundle-mac-x86_64-20130522/sdk
+
+
+
+
+
+
diff --git a/project.properties b/project.properties
new file mode 100644
index 0000000..a3ee5ab
--- /dev/null
+++ b/project.properties
@@ -0,0 +1,14 @@
+# This file is automatically generated by Android Tools.
+# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
+#
+# This file must be checked in Version Control Systems.
+#
+# To customize properties used by the Ant build system edit
+# "ant.properties", and override values to adapt the script to your
+# project structure.
+#
+# To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
+#proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
+
+# Project target.
+target=android-17
diff --git a/res/drawable-hdpi/ic_launcher.png b/res/drawable-hdpi/ic_launcher.png
new file mode 100644
index 0000000..288b665
Binary files /dev/null and b/res/drawable-hdpi/ic_launcher.png differ
diff --git a/res/drawable-hdpi/icon.png b/res/drawable-hdpi/icon.png
deleted file mode 100644
index 8074c4c..0000000
Binary files a/res/drawable-hdpi/icon.png and /dev/null differ
diff --git a/res/drawable-ldpi/icon.png b/res/drawable-ldpi/icon.png
deleted file mode 100644
index 1095584..0000000
Binary files a/res/drawable-ldpi/icon.png and /dev/null differ
diff --git a/res/drawable-mdpi/ic_launcher.png b/res/drawable-mdpi/ic_launcher.png
new file mode 100644
index 0000000..6ae570b
Binary files /dev/null and b/res/drawable-mdpi/ic_launcher.png differ
diff --git a/res/drawable-mdpi/icon.png b/res/drawable-mdpi/icon.png
deleted file mode 100644
index a07c69f..0000000
Binary files a/res/drawable-mdpi/icon.png and /dev/null differ
diff --git a/res/drawable-xhdpi/ic_launcher.png b/res/drawable-xhdpi/ic_launcher.png
new file mode 100644
index 0000000..d4fb7cd
Binary files /dev/null and b/res/drawable-xhdpi/ic_launcher.png differ
diff --git a/res/drawable-xxhdpi/ic_launcher.png b/res/drawable-xxhdpi/ic_launcher.png
new file mode 100644
index 0000000..85a6081
Binary files /dev/null and b/res/drawable-xxhdpi/ic_launcher.png differ
diff --git a/res/layout/login.xml b/res/layout/login.xml
new file mode 100644
index 0000000..b5578f3
--- /dev/null
+++ b/res/layout/login.xml
@@ -0,0 +1,23 @@
+
+
+
+
+
+
+
+
diff --git a/res/layout/main.xml b/res/layout/main.xml
index b5578f3..9dabfd2 100644
--- a/res/layout/main.xml
+++ b/res/layout/main.xml
@@ -4,7 +4,8 @@
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
-
@@ -12,12 +13,11 @@
-
-
+ android:text="Clear Credentials"/>
+
+
+
diff --git a/res/layout/tweet_row.xml b/res/layout/tweet_row.xml
new file mode 100644
index 0000000..d03ef73
--- /dev/null
+++ b/res/layout/tweet_row.xml
@@ -0,0 +1,15 @@
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/res/values-sw600dp/dimens.xml b/res/values-sw600dp/dimens.xml
new file mode 100644
index 0000000..1ba777d
--- /dev/null
+++ b/res/values-sw600dp/dimens.xml
@@ -0,0 +1,8 @@
+
+
+
+
+
\ No newline at end of file
diff --git a/res/values-sw720dp-land/dimens.xml b/res/values-sw720dp-land/dimens.xml
new file mode 100644
index 0000000..eee741a
--- /dev/null
+++ b/res/values-sw720dp-land/dimens.xml
@@ -0,0 +1,9 @@
+
+
+
+ 128dp
+
+
\ No newline at end of file
diff --git a/res/values-v11/styles.xml b/res/values-v11/styles.xml
new file mode 100644
index 0000000..541752f
--- /dev/null
+++ b/res/values-v11/styles.xml
@@ -0,0 +1,11 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/res/values-v14/styles.xml b/res/values-v14/styles.xml
new file mode 100644
index 0000000..f20e015
--- /dev/null
+++ b/res/values-v14/styles.xml
@@ -0,0 +1,12 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/res/values/dimens.xml b/res/values/dimens.xml
new file mode 100644
index 0000000..a6dd140
--- /dev/null
+++ b/res/values/dimens.xml
@@ -0,0 +1,7 @@
+
+
+
+ 16dp
+ 16dp
+
+
\ No newline at end of file
diff --git a/res/values/styles.xml b/res/values/styles.xml
new file mode 100644
index 0000000..4a10ca4
--- /dev/null
+++ b/res/values/styles.xml
@@ -0,0 +1,20 @@
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/src/com/ecs/sample/AndroidTwitterGoogleApiJavaClientActivity.java b/src/com/ecs/sample/AndroidTwitterGoogleApiJavaClientActivity.java
index be9ef91..bd0814b 100644
--- a/src/com/ecs/sample/AndroidTwitterGoogleApiJavaClientActivity.java
+++ b/src/com/ecs/sample/AndroidTwitterGoogleApiJavaClientActivity.java
@@ -1,14 +1,20 @@
package com.ecs.sample;
-import java.util.Date;
+import java.util.ArrayList;
+import java.util.List;
+import twitter4j.Status;
+import twitter4j.ResponseList;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
+import android.net.Uri;
+import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;
+import android.widget.ListView;
import android.widget.TextView;
import com.ecs.sample.store.SharedPreferencesCredentialStore;
@@ -17,10 +23,14 @@ public class AndroidTwitterGoogleApiJavaClientActivity extends Activity {
private SharedPreferences prefs;
private TextView textView;
+ private HomeTimelineAdapter adapter;
+ private ListView listview;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
+ listview = (ListView) findViewById(R.id.listview);
+
this.prefs = PreferenceManager.getDefaultSharedPreferences(this);
Button launchOauth = (Button) findViewById(R.id.btn_launch_oauth);
@@ -49,19 +59,45 @@ public void onClick(View v) {
private void clearCredentials() {
new SharedPreferencesCredentialStore(prefs).clearCredentials();
}
-
+
/**
* Performs an authorized API call.
*/
private void performApiCall() {
- try {
- String tweet = "Tweet sent at " + new Date();
- TwitterUtils.sendTweet(prefs, tweet);
- textView.setText(tweet);
- } catch (Exception ex) {
- ex.printStackTrace();
- textView.setText("Error occured : " + ex.getMessage());
+ new ApiCallExecutor().execute();
+ }
+
+ private class ApiCallExecutor extends AsyncTask {
+
+ @Override
+ protected Void doInBackground(Uri...params) {
+
+ try {
+// String tweet = "Tweet sent at " + new Date();
+// TwitterUtils.sendTweet(prefs, tweet);
+ ResponseList homeTimeline = TwitterUtils.getHomeTimeline(prefs);
+ List statusList = new ArrayList();
+ for (twitter4j.Status status : homeTimeline) {
+ statusList.add(status);
+ }
+ adapter = new HomeTimelineAdapter(AndroidTwitterGoogleApiJavaClientActivity.this,R.layout.tweet_row,statusList);
+
+ } catch (Exception ex) {
+ adapter = new HomeTimelineAdapter(AndroidTwitterGoogleApiJavaClientActivity.this,R.layout.tweet_row,new ArrayList());
+ ex.printStackTrace();
+ }
+
+ return null;
+ }
+
+ @Override
+ protected void onPostExecute(Void result) {
+ super.onPostExecute(result);
+ listview.setAdapter(adapter);
+ adapter.notifyDataSetChanged();
}
+
}
+
}
diff --git a/src/com/ecs/sample/Constants.java b/src/com/ecs/sample/Constants.java
index b5ee644..3af80eb 100644
--- a/src/com/ecs/sample/Constants.java
+++ b/src/com/ecs/sample/Constants.java
@@ -2,12 +2,14 @@
public class Constants {
- public static final String CONSUMER_KEY = "PUT YOUR TWITTER OAUTH CONSUMER KEY HERE";
- public static final String CONSUMER_SECRET= "PUT YOUR TWITTER OAUTH CONSUMER SECRET HERE";
+ public static final String TAG = "AndroidTwitterOauth1";
- public static final String REQUEST_URL = "http://api.twitter.com/oauth/request_token";
- public static final String ACCESS_URL = "http://api.twitter.com/oauth/access_token";
- public static final String AUTHORIZE_URL = "http://api.twitter.com/oauth/authorize";
+ public static final String API_KEY = "PUT YOUR TWITTER API KEY HERE";
+ public static final String API_SECRET= "PUT YOUR TWITTER API SECRET HERE";
+
+ public static final String REQUEST_URL = "https://api.twitter.com/oauth/request_token";
+ public static final String ACCESS_URL = "https://api.twitter.com/oauth/access_token";
+ public static final String AUTHORIZE_URL = "https://api.twitter.com/oauth/authorize";
public static final String OAUTH_CALLBACK_URL = "http://localhost";
}
diff --git a/src/com/ecs/sample/HomeTimelineAdapter.java b/src/com/ecs/sample/HomeTimelineAdapter.java
new file mode 100644
index 0000000..afa297c
--- /dev/null
+++ b/src/com/ecs/sample/HomeTimelineAdapter.java
@@ -0,0 +1,56 @@
+package com.ecs.sample;
+
+import java.util.List;
+
+import twitter4j.Status;
+import android.content.Context;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.ArrayAdapter;
+import android.widget.TextView;
+
+public class HomeTimelineAdapter extends ArrayAdapter {
+
+ private LayoutInflater inflater;
+ private int tvrId;
+ private List listItems;
+
+ public HomeTimelineAdapter(Context context, int textViewResourceId, List objects) {
+ super(context, textViewResourceId, objects);
+ inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
+ tvrId = textViewResourceId;
+ listItems = objects;
+ }
+
+ @Override
+ public View getView(int position, View convertView, ViewGroup parent){
+ try {
+ ViewHolder holder;
+ Status item = listItems.get(position);
+ if(convertView == null) {
+ convertView = inflater.inflate(tvrId, null);
+
+ holder = new ViewHolder();
+ holder.text = (TextView)convertView.findViewById(R.id.user_text);
+
+ convertView.setTag(holder);
+ } else {
+ holder = (ViewHolder)convertView.getTag();
+ }
+
+ holder.text.setText(item.getText());
+
+ } catch(Exception e) {
+ e.printStackTrace();
+ }
+ return convertView;
+ }
+
+ private static class ViewHolder {
+
+ public TextView text;
+
+ }
+
+}
\ No newline at end of file
diff --git a/src/com/ecs/sample/OAuthAccessTokenActivity.java b/src/com/ecs/sample/OAuthAccessTokenActivity.java
index bedb0b5..f6696bd 100644
--- a/src/com/ecs/sample/OAuthAccessTokenActivity.java
+++ b/src/com/ecs/sample/OAuthAccessTokenActivity.java
@@ -2,10 +2,13 @@
import java.io.IOException;
+import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
+import android.net.Uri;
+import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
@@ -23,12 +26,15 @@
import com.google.api.client.auth.oauth.OAuthHmacSigner;
import com.google.api.client.http.apache.ApacheHttpTransport;
+@SuppressLint("SetJavaScriptEnabled")
public class OAuthAccessTokenActivity extends Activity {
final String TAG = getClass().getName();
private SharedPreferences prefs;
+ private boolean handled;
+
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@@ -36,95 +42,169 @@ public void onCreate(Bundle savedInstanceState) {
this.prefs = PreferenceManager.getDefaultSharedPreferences(this);
}
+ private WebView webview;
+
@Override
protected void onResume() {
super.onResume();
- WebView webview = new WebView(this);
+ webview = new WebView(this);
webview.getSettings().setJavaScriptEnabled(true);
webview.setVisibility(View.VISIBLE);
setContentView(webview);
- Log.i(TAG, "Retrieving request token from Google servers");
+ handled=false;
+
+ new PreProcessToken().execute();
+
+ }
+
+ private class PreProcessToken extends AsyncTask {
+
+ final OAuthHmacSigner signer = new OAuthHmacSigner();
+ private String authorizationUrl;
+
+ @Override
+ protected Void doInBackground(Uri...params) {
+
+ try {
+
+ signer.clientSharedSecret = Constants.API_SECRET;
+
+ OAuthGetTemporaryToken temporaryToken = new OAuthGetTemporaryToken(Constants.REQUEST_URL);
+ temporaryToken.transport = new ApacheHttpTransport();
+ temporaryToken.signer = signer;
+ temporaryToken.consumerKey = Constants.API_KEY;
+ temporaryToken.callback = Constants.OAUTH_CALLBACK_URL;
+
+ OAuthCredentialsResponse tempCredentials = temporaryToken.execute();
+ signer.tokenSharedSecret = tempCredentials.tokenSecret;
+
+ OAuthAuthorizeTemporaryTokenUrl authorizeUrl = new OAuthAuthorizeTemporaryTokenUrl(Constants.AUTHORIZE_URL);
+ authorizeUrl.temporaryToken = tempCredentials.token;
+ authorizationUrl = authorizeUrl.build();
+
+ Log.i(Constants.TAG, "Using authorizationUrl = " + authorizationUrl);
+
+ handled=false;
+ } catch (Exception ex) {
+ ex.printStackTrace();
+ }
+
+ return null;
+ }
+
+
+ /**
+ * When we're done and we've retrieved either a valid token or an error from the server,
+ * we'll return to our original activity
+ */
+ @Override
+ protected void onPostExecute(Void result) {
+
+ Log.i(TAG, "Retrieving request token from Google servers");
- try {
-
- final OAuthHmacSigner signer = new OAuthHmacSigner();
- signer.clientSharedSecret = Constants.CONSUMER_SECRET;
-
- OAuthGetTemporaryToken temporaryToken = new OAuthGetTemporaryToken(Constants.REQUEST_URL);
- temporaryToken.transport = new ApacheHttpTransport();
- temporaryToken.signer = signer;
- temporaryToken.consumerKey = Constants.CONSUMER_KEY;
- temporaryToken.callback = Constants.OAUTH_CALLBACK_URL;
-
- OAuthCredentialsResponse tempCredentials = temporaryToken.execute();
- signer.tokenSharedSecret = tempCredentials.tokenSecret;
-
- OAuthAuthorizeTemporaryTokenUrl authorizeUrl = new OAuthAuthorizeTemporaryTokenUrl(Constants.AUTHORIZE_URL);
- authorizeUrl.temporaryToken = tempCredentials.token;
- String authorizationUrl = authorizeUrl.build();
-
-
- /* WebViewClient must be set BEFORE calling loadUrl! */
webview.setWebViewClient(new WebViewClient() {
-
+
@Override
public void onPageStarted(WebView view, String url,Bitmap bitmap) {
- System.out.println("onPageStarted : " + url);
+ Log.i(Constants.TAG, "onPageStarted : " + url + " handled = " + handled);
}
@Override
- public void onPageFinished(WebView view, String url) {
-
- if (url.startsWith(Constants.OAUTH_CALLBACK_URL)) {
- try {
-
- if (url.indexOf("oauth_token=")!=-1) {
-
- String requestToken = extractParamFromUrl(url,"oauth_token");
- String verifier= extractParamFromUrl(url,"oauth_verifier");
-
- signer.clientSharedSecret = Constants.CONSUMER_SECRET;
-
- OAuthGetAccessToken accessToken = new OAuthGetAccessToken(Constants.ACCESS_URL);
- accessToken.transport = new ApacheHttpTransport();
- accessToken.temporaryToken = requestToken;
- accessToken.signer = signer;
- accessToken.consumerKey = Constants.CONSUMER_KEY;
- accessToken.verifier = verifier;
-
- OAuthCredentialsResponse credentials = accessToken.execute();
- signer.tokenSharedSecret = credentials.tokenSecret;
-
- CredentialStore credentialStore = new SharedPreferencesCredentialStore(prefs);
- credentialStore.write(new String[] {credentials.token,credentials.tokenSecret});
- view.setVisibility(View.INVISIBLE);
- startActivity(new Intent(OAuthAccessTokenActivity.this,AndroidTwitterGoogleApiJavaClientActivity.class));
- } else if (url.indexOf("error=")!=-1) {
- view.setVisibility(View.INVISIBLE);
- new SharedPreferencesCredentialStore(prefs).clearCredentials();
- startActivity(new Intent(OAuthAccessTokenActivity.this,AndroidTwitterGoogleApiJavaClientActivity.class));
- }
-
- } catch (IOException e) {
- e.printStackTrace();
- }
-
- }
- System.out.println("onPageFinished : " + url);
-
+ public void onPageFinished(final WebView view, final String url) {
+ Log.i(Constants.TAG, "onPageFinished : " + url + " handled = " + handled);
+
+ if (url.startsWith(Constants.OAUTH_CALLBACK_URL)) {
+ if (url.indexOf("oauth_token=")!=-1) {
+ webview.setVisibility(View.INVISIBLE);
+
+ if (!handled) {
+ new ProcessToken(url,signer).execute();
+ }
+ } else {
+ webview.setVisibility(View.VISIBLE);
+ }
+ }
}
- private String extractParamFromUrl(String url,String paramName) {
- String queryString = url.substring(url.indexOf("?", 0)+1,url.length());
- QueryStringParser queryStringParser = new QueryStringParser(queryString);
- return queryStringParser.getQueryParamValue(paramName);
- }
-
+
});
webview.loadUrl(authorizationUrl);
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
+
+ }
+
+ }
+
+ private class ProcessToken extends AsyncTask {
+
+ String url;
+ private OAuthHmacSigner signer;
+
+ public ProcessToken(String url,OAuthHmacSigner signer) {
+ this.url=url;
+ this.signer = signer;
+ }
+
+ @Override
+ protected Void doInBackground(Uri...params) {
+
+ Log.i(Constants.TAG, "doInbackground called with url " + url);
+ if (url.startsWith(Constants.OAUTH_CALLBACK_URL) && !handled) {
+ try {
+
+ if (url.indexOf("oauth_token=")!=-1) {
+ handled=true;
+ String requestToken = extractParamFromUrl(url,"oauth_token");
+ String verifier= extractParamFromUrl(url,"oauth_verifier");
+
+ signer.clientSharedSecret = Constants.API_SECRET;
+
+ OAuthGetAccessToken accessToken = new OAuthGetAccessToken(Constants.ACCESS_URL);
+ accessToken.transport = new ApacheHttpTransport();
+ accessToken.temporaryToken = requestToken;
+ accessToken.signer = signer;
+ accessToken.consumerKey = Constants.API_KEY;
+ accessToken.verifier = verifier;
+
+ OAuthCredentialsResponse credentials = accessToken.execute();
+ signer.tokenSharedSecret = credentials.tokenSecret;
+
+ CredentialStore credentialStore = new SharedPreferencesCredentialStore(prefs);
+ credentialStore.write(new String[] {credentials.token,credentials.tokenSecret});
+
+ } else if (url.indexOf("error=")!=-1) {
+ new SharedPreferencesCredentialStore(prefs).clearCredentials();
+ }
+
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+
+ }
+ return null;
+ }
+
+ private String extractParamFromUrl(String url,String paramName) {
+ String queryString = url.substring(url.indexOf("?", 0)+1,url.length());
+ QueryStringParser queryStringParser = new QueryStringParser(queryString);
+ return queryStringParser.getQueryParamValue(paramName);
+ }
+
+ @Override
+ protected void onPreExecute() {
+
+ }
+
+ /**
+ * When we're done and we've retrieved either a valid token or an error from the server,
+ * we'll return to our original activity
+ */
+ @Override
+ protected void onPostExecute(Void result) {
+ Log.i(Constants.TAG," ++++++++++++ Starting mainscreen again");
+ startActivity(new Intent(OAuthAccessTokenActivity.this,AndroidTwitterGoogleApiJavaClientActivity.class));
+ finish();
+ }
+
+ }
}
diff --git a/src/com/ecs/sample/TwitterUtils.java b/src/com/ecs/sample/TwitterUtils.java
index a366627..ddce4ba 100644
--- a/src/com/ecs/sample/TwitterUtils.java
+++ b/src/com/ecs/sample/TwitterUtils.java
@@ -1,13 +1,15 @@
package com.ecs.sample;
-import com.ecs.sample.store.SharedPreferencesCredentialStore;
-
+import twitter4j.ResponseList;
+import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
-import twitter4j.http.AccessToken;
+import twitter4j.auth.AccessToken;
import android.content.SharedPreferences;
+import com.ecs.sample.store.SharedPreferencesCredentialStore;
+
public class TwitterUtils {
public static boolean isAuthenticated(SharedPreferences prefs) {
@@ -15,7 +17,7 @@ public static boolean isAuthenticated(SharedPreferences prefs) {
String[] tokens = new SharedPreferencesCredentialStore(prefs).read();
AccessToken a = new AccessToken(tokens[0],tokens[1]);
Twitter twitter = new TwitterFactory().getInstance();
- twitter.setOAuthConsumer(Constants.CONSUMER_KEY, Constants.CONSUMER_SECRET);
+ twitter.setOAuthConsumer(Constants.API_KEY, Constants.API_SECRET);
twitter.setOAuthAccessToken(a);
try {
@@ -26,11 +28,21 @@ public static boolean isAuthenticated(SharedPreferences prefs) {
}
}
+ public static ResponseList getHomeTimeline(SharedPreferences prefs) throws Exception {
+ String[] tokens = new SharedPreferencesCredentialStore(prefs).read();
+ AccessToken a = new AccessToken(tokens[0],tokens[1]);
+ Twitter twitter = new TwitterFactory().getInstance();
+ twitter.setOAuthConsumer(Constants.API_KEY, Constants.API_SECRET);
+ twitter.setOAuthAccessToken(a);
+ ResponseList homeTimeline = twitter.getHomeTimeline();
+ return homeTimeline;
+ }
+
public static void sendTweet(SharedPreferences prefs,String msg) throws Exception {
String[] tokens = new SharedPreferencesCredentialStore(prefs).read();
AccessToken a = new AccessToken(tokens[0],tokens[1]);
Twitter twitter = new TwitterFactory().getInstance();
- twitter.setOAuthConsumer(Constants.CONSUMER_KEY, Constants.CONSUMER_SECRET);
+ twitter.setOAuthConsumer(Constants.API_KEY, Constants.API_SECRET);
twitter.setOAuthAccessToken(a);
twitter.updateStatus(msg);
}
diff --git a/src/com/ecs/sample/store/SharedPreferencesCredentialStore.java b/src/com/ecs/sample/store/SharedPreferencesCredentialStore.java
index 32d125e..614114d 100644
--- a/src/com/ecs/sample/store/SharedPreferencesCredentialStore.java
+++ b/src/com/ecs/sample/store/SharedPreferencesCredentialStore.java
@@ -14,7 +14,6 @@ public SharedPreferencesCredentialStore(SharedPreferences prefs) {
this.prefs = prefs;
}
- @Override
public String[] read() {
String[] tokens = new String[2];
tokens[0]=prefs.getString(TOKEN, "");
@@ -22,7 +21,6 @@ public String[] read() {
return tokens;
}
- @Override
public void write(String[] tokens) {
Editor editor = prefs.edit();
editor.putString(TOKEN,tokens[0]);
@@ -30,7 +28,6 @@ public void write(String[] tokens) {
editor.commit();
}
- @Override
public void clearCredentials() {
Editor editor = prefs.edit();
editor.remove(TOKEN);