Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

fetch and store userinfo associated with provider

  • Loading branch information...
commit 6dbbb9f54d67e851d975b91ab62ba13f53648cc3 1 parent f87eefa
@herestomwiththeweather authored
View
17 src/org/opensourcecurrency/hack/ConstantsUsers.java
@@ -0,0 +1,17 @@
+package org.opensourcecurrency.hack;
+
+import static android.provider.BaseColumns._ID;
+import android.provider.BaseColumns;
+
+public interface ConstantsUsers extends BaseColumns {
+ public static final String USERS_TABLE_NAME = "users";
+ // columns in the assets table
+ public static final String USER_PROVIDER_ID = "provider_id";
+ public static final String USER_URL = "url";
+ public static final String USER_WEBSITE_URL = "website_url";
+ public static final String USER_PICTURE_URL = "picture_url";
+ public static final String USER_NAME = "name";
+ public static final String USER_EMAIL = "email";
+ public static final String USER_USER_ID = "user_id";
+ public static final String USER_CREATED_AT = "created_at";
+}
View
82 src/org/opensourcecurrency/hack/Profile.java
@@ -1,12 +1,92 @@
package org.opensourcecurrency.hack;
+import java.net.URI;
+
+import org.apache.http.client.methods.HttpGet;
+import org.json.JSONException;
+import org.json.JSONObject;
+
import android.app.Activity;
+import android.app.ProgressDialog;
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
import android.os.Bundle;
+import android.util.Log;
+import android.view.Gravity;
+import android.widget.Toast;
public class Profile extends Activity {
+
+ private static final String PROFILE_ACTION = "org.opensourcecurrency.hack.PROFILE";
+ private static final String TAG = "OpenTransact";
+ private ProviderData providers;
+ private ProgressDialog progress;
+
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
- setContentView(R.layout.profile);
+ setContentView(R.layout.profile);
+
+ providers = new ProviderData(this);
+ Asset asset = providers.getCurrentAsset(this);
+ Provider provider = asset.getProvider();
+
+ User user = provider.getUser();
+ if(null == user) {
+ provider.getUserInfo(this, PROFILE_ACTION);
+ progress = ProgressDialog.show(this, "Fetching profile", "Waiting...", true);
+ } else {
+ Toast toast = Toast.makeText(this, user.email, Toast.LENGTH_LONG);
+ toast.setGravity(Gravity.CENTER, 0, 0);
+ toast.show();
+ }
+ }
+
+ @Override
+ public void onResume() {
+ super.onResume();
+ registerReceiver(receiver, new IntentFilter(PROFILE_ACTION));
}
+
+ @Override
+ public void onPause() {
+ super.onPause();
+ unregisterReceiver(receiver);
+ }
+
+ private BroadcastReceiver receiver = new BroadcastReceiver() {
+ @Override
+ public void onReceive(Context context, Intent intent) {
+ Log.d(TAG,"onReceive called (Profile)!");
+ if(progress != null) {
+ progress.dismiss();
+ }
+ String response = intent.getStringExtra(RestTask.HTTP_RESPONSE);
+ Log.d(TAG,"response(Profile): "+response);
+
+ if(null == response) {
+ Toast toast = Toast.makeText(context, "Error connecting to provider!", Toast.LENGTH_LONG);
+ toast.setGravity(Gravity.CENTER, 0, 0);
+ toast.show();
+ return;
+ }
+
+ try {
+ JSONObject profile_response = new JSONObject(response);
+ String email = profile_response.getString("email");
+ Log.d(TAG,"adding email: " + email);
+ providers = new ProviderData(context);
+ Asset asset = providers.getCurrentAsset(context);
+ Provider provider = asset.getProvider();
+ provider.addUser(profile_response);
+ } catch (JSONException e) {
+ e.printStackTrace();
+ }
+
+ finish();
+
+ }
+ };
}
View
41 src/org/opensourcecurrency/hack/Provider.java
@@ -2,8 +2,15 @@
import static org.opensourcecurrency.hack.ConstantsAssets.ASSET_PROVIDER_ID;
+import java.net.URI;
import java.util.ArrayList;
+import org.apache.http.client.methods.HttpGet;
+import org.json.JSONException;
+import org.json.JSONObject;
+
+import android.content.Context;
+
public class Provider {
public Integer providerId;
public String providerName;
@@ -51,6 +58,36 @@ public void addAsset(String name, String url) {
m_providers.addAsset(providerId,name,url);
}
+ public void getUserInfo(Context context, String action) {
+ String access_token = getAccessToken();
+
+ String about_user="/user_info";
+
+ try {
+ HttpGet userinfoRequest = new HttpGet(new URI(providerUrl + about_user));
+ userinfoRequest.setHeader("Accept","application/json");
+ userinfoRequest.setHeader("Authorization","Bearer " + access_token);
+ RestTask task = new RestTask(context, action);
+ task.execute(userinfoRequest);
+ } catch(Exception e) {
+ e.printStackTrace();
+ }
+ }
+
+ public void addUser(JSONObject r) {
+ try {
+ String name = r.getString("name");
+ String email = r.getString("email");
+ String url = r.getString("profile");
+ String website_url = r.getString("website");
+ String picture_url = r.getString("picture");
+ String user_id = r.getString("user_id");
+ m_providers.addUser(providerId,name,email,url,website_url,picture_url,user_id);
+ } catch (JSONException e) {
+ e.printStackTrace();
+ }
+ }
+
public ArrayList<Asset> getAssets() {
ArrayList<Asset> assets;
@@ -62,6 +99,10 @@ public void addAsset(String name, String url) {
return assets;
}
+
+ public User getUser() {
+ return m_providers.getUser(providerId);
+ }
public String getAccessToken() {
// get first unexpired access token associated with this provider
View
87 src/org/opensourcecurrency/hack/ProviderData.java
@@ -19,6 +19,16 @@
import static org.opensourcecurrency.hack.ConstantsAssets.ASSET_NAME;
import static org.opensourcecurrency.hack.ConstantsAssets.ASSET_CREATED_AT;
+import static org.opensourcecurrency.hack.ConstantsUsers.USERS_TABLE_NAME;
+import static org.opensourcecurrency.hack.ConstantsUsers.USER_PROVIDER_ID;
+import static org.opensourcecurrency.hack.ConstantsUsers.USER_URL;
+import static org.opensourcecurrency.hack.ConstantsUsers.USER_WEBSITE_URL;
+import static org.opensourcecurrency.hack.ConstantsUsers.USER_PICTURE_URL;
+import static org.opensourcecurrency.hack.ConstantsUsers.USER_NAME;
+import static org.opensourcecurrency.hack.ConstantsUsers.USER_EMAIL;
+import static org.opensourcecurrency.hack.ConstantsUsers.USER_USER_ID;
+import static org.opensourcecurrency.hack.ConstantsUsers.USER_CREATED_AT;
+
import static org.opensourcecurrency.hack.ConstantsAccessTokens.ACCESS_TOKENS_TABLE_NAME;
import static org.opensourcecurrency.hack.ConstantsAccessTokens.ACCESS_TOKEN_PROVIDER_ID;
import static org.opensourcecurrency.hack.ConstantsAccessTokens.REFRESH_TOKEN_ID;
@@ -52,6 +62,7 @@
private static String[] FROM = { _ID, NAME,PROVIDER_URL,REDIRECT_URL,CLIENT_ID,CLIENT_SECRET,PROVIDER_CREATED_AT };
private static String[] TOKENS_FROM = { _ID, ACCESS_TOKEN_PROVIDER_ID,REFRESH_TOKEN_ID,ACCESS_TOKEN,ACCESS_TOKEN_EXPIRES_AT,ACCESS_TOKEN_CREATED_AT };
private static String[] ASSETS_FROM = { _ID, ASSET_PROVIDER_ID, ASSET_URL, ASSET_NAME, ASSET_CREATED_AT };
+ private static String[] USERS_FROM = { _ID, USER_PROVIDER_ID, USER_URL, USER_WEBSITE_URL, USER_PICTURE_URL, USER_NAME, USER_EMAIL, USER_USER_ID, USER_CREATED_AT };
public ProviderData(Context ctx) {
super(ctx, DATABASE_NAME, null, DATABASE_VERSION);
@@ -69,6 +80,16 @@ public void onCreate(SQLiteDatabase db) {
+ " TEXT NOT NULL, " + CLIENT_SECRET
+ " TEXT NOT NULL," + PROVIDER_CREATED_AT + " DATE);");
+ db.execSQL("CREATE TABLE " + USERS_TABLE_NAME + " (" + _ID
+ + " INTEGER PRIMARY KEY AUTOINCREMENT, " + USER_PROVIDER_ID
+ + " INTEGER, " + USER_URL
+ + " TEXT NOT NULL," + USER_WEBSITE_URL
+ + " TEXT NOT NULL," + USER_PICTURE_URL
+ + " TEXT NOT NULL," + USER_NAME
+ + " TEXT NOT NULL," + USER_EMAIL
+ + " TEXT NOT NULL," + USER_USER_ID
+ + " TEXT NOT NULL," + USER_CREATED_AT + " DATE);");
+
db.execSQL("CREATE TABLE " + ASSETS_TABLE_NAME + " (" + _ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + ASSET_PROVIDER_ID
+ " INTEGER, " + ASSET_URL
@@ -222,6 +243,50 @@ public Provider addProvider(String name, String provider_url, String redirect_ur
return resultList;
}
+ public ArrayList<User> getUsers(String selection, String [] selectionArgs) {
+ Log.d(TAG,"XXX ProviderData#getUsers");
+ SQLiteDatabase db = getReadableDatabase();
+ ArrayList<User> resultList = new ArrayList<User>();
+
+ Cursor cursor = db.query(USERS_TABLE_NAME, USERS_FROM, selection, selectionArgs, null, null, null);
+ Log.d(TAG,"XXX ProviderData#getUsers count: " + cursor.getCount());
+ while (cursor.moveToNext())
+ {
+ try {
+ User ob = new User();
+ ob.setId(cursor.getInt(cursor.getColumnIndex(_ID)));
+ ob.setEmail(cursor.getString(cursor.getColumnIndex(USER_EMAIL)));
+ resultList.add(ob);
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+
+ cursor.close();
+ db.close();
+
+ return resultList;
+ }
+
+ public User getUser(Integer provider_id) {
+ Log.d(TAG,"XXX getUser: " + provider_id);
+
+ User user;
+ ArrayList<User> users;
+
+ try {
+ users = getUsers(USER_PROVIDER_ID + " = ?", new String[] {provider_id.toString()});
+ } finally {
+ close();
+ }
+ if(0 == users.size()) {
+ user = null;
+ } else {
+ user = users.get(0);
+ }
+ return user;
+ }
+
public AccessToken getAccessToken(Integer provider_id) {
Log.d(TAG,"XXX getAccessToken: " + provider_id);
@@ -290,6 +355,28 @@ public void addAccessToken(Integer provider_id, String access_token, Integer exp
return resultList;
}
+ public void addUser(Integer provider_id, String name, String email, String url, String website_url, String picture_url, String user_id) {
+ Log.d(TAG,"ProviderData#addUser name: " + name);
+ Log.d(TAG,"ProviderData#addUser email: " + email);
+
+ SQLiteDatabase db = getWritableDatabase();
+ ContentValues values = new ContentValues();
+
+ values.put(USER_PROVIDER_ID, provider_id);
+ values.put(USER_NAME, name);
+ values.put(USER_EMAIL, email);
+ values.put(ASSET_URL, url);
+ values.put(USER_WEBSITE_URL, website_url);
+ values.put(USER_PICTURE_URL, picture_url);
+ values.put(USER_USER_ID, user_id);
+
+ Date now = new Date();
+ SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+ values.put(USER_CREATED_AT, dateFormat.format(now));
+
+ db.insertOrThrow(USERS_TABLE_NAME, null, values);
+ }
+
public void addAsset(Integer provider_id, String name, String url) {
Log.d(TAG,"ProviderData#addAsset name: " + name);
Log.d(TAG,"ProviderData#addAsset url: " + url);
View
14 src/org/opensourcecurrency/hack/User.java
@@ -0,0 +1,14 @@
+package org.opensourcecurrency.hack;
+
+public class User {
+ public Integer userId;
+ public String email;
+
+ public void setId(Integer id) {
+ userId = id;
+ }
+
+ public void setEmail(String email_addr) {
+ email = email_addr;
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.