Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

fetch provider related data from sqlite database

  • Loading branch information...
commit 7d3fcf9aa567228376ff9a79e4c09eb2dcd4e81d 1 parent 61131de
@herestomwiththeweather authored
View
78 src/org/opensourcecurrency/hack/Cyberwire.java
@@ -15,6 +15,7 @@
import org.json.JSONObject;
import org.opensourcecurrency.hack.RestTask;
import org.opensourcecurrency.hack.WebViewActivity;
+import org.opensourcecurrency.hack.Provider;
import android.app.Activity;
import android.app.ProgressDialog;
@@ -26,6 +27,7 @@
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
+import android.database.Cursor;
import android.util.Log;
import android.view.Gravity;
@@ -34,20 +36,28 @@
import android.view.MenuItem;
import android.widget.Toast;
+import static org.opensourcecurrency.hack.ConstantsProviders.PROVIDER_URL;
+
+
public class Cyberwire extends Activity implements OnClickListener {
private static final String OAUTH_TOKEN_ACTION = "org.opensourcecurrency.hack.OAUTH_TOKEN";
private static final String TAG = "OpenTransact";
+ private ProviderData providers;
+//private ProviderData providers2;
+
private ProgressDialog progress;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
- SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
+ //SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
- String access_token = prefs.getString("access_token","none");
- String expires_in = prefs.getString("expires_in","none");
+ //String access_token = prefs.getString("access_token","none");
+ //String expires_in = prefs.getString("expires_in","none");
+//providers2=new ProviderData(this);
+ //Log.d(TAG,"onCreate: " + expires_in + " : " + access_token);
+//providers2.addProvider("demo", "http://demo.opensourcecurrency.org", "http://demo.opensourcecurrency.org/transacts", "xBoHeeNNFt3LQ7U1tvAb8BVKr32duE6rdWtpCSFD", "HraYcLT5F5nRll5KF5tw8umdER3EOrsFXIEro67T");
- Log.d(TAG,"onCreate: " + expires_in + " : " + access_token);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
@@ -59,9 +69,9 @@ public void onCreate(Bundle savedInstanceState) {
View profileButton = findViewById(R.id.profile_button);
profileButton.setOnClickListener(this);
- Toast toast = Toast.makeText(this, "expires_in: " + expires_in + " : " + access_token, Toast.LENGTH_LONG);
- toast.setGravity(Gravity.CENTER, 0, 0);
- toast.show();
+ //Toast toast = Toast.makeText(this, "expires_in: " + expires_in + " : " + access_token, Toast.LENGTH_LONG);
+ //toast.setGravity(Gravity.CENTER, 0, 0);
+ //toast.show();
}
@Override
@@ -92,20 +102,24 @@ public boolean onCreateOptionsMenu(Menu menu) {
@Override
public boolean onOptionsItemSelected(MenuItem item) {
- String providerValues[];
+ //String providerValues[];
Log.d(TAG,"onOptionsItemSelected");
switch (item.getItemId()) {
case R.id.exit:
return true;
case R.id.login:
Intent intent = new Intent(this,WebViewActivity.class);
- SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
- String provider = prefs.getString("assetProviderPref","");
- providerValues = provider.split(" ");
- Log.d(TAG,"onOptionsItemSelected[login] host: " + providerValues[0]);
- Log.d(TAG,"onOptionsItemSelected[login] clientid: " + providerValues[1]);
- Log.d(TAG,"onOptionsItemSelected[login] redirect_uri: " + providerValues[3]);
- intent.setData(Uri.parse(providerValues[0]+"/oauth/authorize?client_id="+providerValues[1]+"&response_type=code&redirect_uri="+providerValues[3]));
+
+ SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
+
+ providers = new ProviderData(this);
+ Provider provider = providers.getProvider(prefs.getString("assetProviderPref",""));
+
+ Log.d(TAG,"onOptionsItemSelected[login] provider_url: " + provider.providerUrl);
+ Log.d(TAG,"onOptionsItemSelected[login] clientid: " + provider.clientId);
+ Log.d(TAG,"onOptionsItemSelected[login] redirect_uri: " + provider.redirectUrl);
+ intent.setData(Uri.parse(provider.providerUrl+"/oauth/authorize?client_id="+provider.clientId+"&response_type=code&redirect_uri="+provider.redirectUrl));
+
startActivityForResult(intent,0);
return true;
case R.id.settings:
@@ -117,12 +131,12 @@ public boolean onOptionsItemSelected(MenuItem item) {
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- String providerValues[];
Log.d(TAG,"onActivityResult");
switch (requestCode) {
case 0:
if (resultCode != RESULT_OK || data == null) {
+ Log.d(TAG,"XXX onActivityResult: BAD RESULT");
return;
}
String token = data.getStringExtra("token");
@@ -130,25 +144,31 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Toast toast = Toast.makeText(this, token, Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
-
- SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
- String provider = prefs.getString("assetProviderPref","");
- providerValues = provider.split(" ");
- Log.d(TAG,"onActivityResult host: " + providerValues[0]);
- Log.d(TAG,"onActivityResult clientid: " + providerValues[1]);
- Log.d(TAG,"onActivityResult client secret: " + providerValues[2]);
- Log.d(TAG,"onActivityResult redirect uri: " + providerValues[3]);
+ SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
+
+ providers = new ProviderData(this);
+ Provider provider = providers.getProvider(prefs.getString("assetProviderPref",""));
+
+ String provider_url = provider.providerUrl;
+ String redirect_url = provider.redirectUrl;
+ String client_id = provider.clientId;
+ String client_secret = provider.clientSecret;
+
+ Log.d(TAG,"onActivityResult provider_url: " + provider_url);
+ Log.d(TAG,"onActivityResult clientid: " + client_id);
+ Log.d(TAG,"onActivityResult client secret: " + client_secret);
+ Log.d(TAG,"onActivityResult redirect_uri: " + redirect_url);
try {
- String url = providerValues[0] + "/oauth/token";
+ String url = provider_url + "/oauth/token";
HttpPost tokenRequest = new HttpPost(new URI(url));
List<NameValuePair> parameters = new ArrayList<NameValuePair>();
- parameters.add(new BasicNameValuePair("client_id", providerValues[1]));
- parameters.add(new BasicNameValuePair("client_secret", providerValues[2]));
+ parameters.add(new BasicNameValuePair("client_id", client_id));
+ parameters.add(new BasicNameValuePair("client_secret", client_secret));
parameters.add(new BasicNameValuePair("code", token));
parameters.add(new BasicNameValuePair("grant_type","authorization_code"));
- parameters.add(new BasicNameValuePair("redirect_uri",providerValues[3]));
+ parameters.add(new BasicNameValuePair("redirect_uri",redirect_url));
tokenRequest.setEntity(new UrlEncodedFormEntity(parameters));
RestTask task = new RestTask(this, OAUTH_TOKEN_ACTION);
@@ -213,4 +233,4 @@ public void onReceive(Context context, Intent intent) {
}
};
-}
+}
View
70 src/org/opensourcecurrency/hack/Prefs.java
@@ -1,8 +1,7 @@
package org.opensourcecurrency.hack;
-import static org.opensourcecurrency.hack.ConstantsProviders.PROVIDERS_TABLE_NAME;
-
import java.text.SimpleDateFormat;
+import java.util.ArrayList;
import java.util.Date;
import android.content.ContentValues;
@@ -28,7 +27,6 @@
public class Prefs extends PreferenceActivity {
private static final String TAG = "OpenTransact";
private ProviderData providers;
- private static String[] FROM = { _ID, NAME,PROVIDER_URL,REDIRECT_URL,CLIENT_ID,CLIENT_SECRET,PROVIDER_CREATED_AT };
@Override
protected void onCreate(Bundle savedInstanceState) {
@@ -43,40 +41,32 @@ private PreferenceScreen createPreferenceHierarchy() {
Cursor cursor;
String [] provider_names;
String [] provider_urls;
-
- // XXX
- providers = new ProviderData(this);
-
+
+ providers = new ProviderData(this);
+ ArrayList<Provider> assetProviders;
+
try {
- //addProvider("picomoney", "https://picomoney.com", "https://picomoney.com/picos", "cc6f64d61ae2c903d1bb", "77338e2faebdc5e4a55a94f76c01940ad26d4ecf");
- //addProvider("ubuntu.local", "http://192.168.1.102:3000", "http://192.168.1.102:3000/transacts", "xBoHeeNNFt3LQ7U1tvAb8BVKr32duE6rdWtpCSFD", "HraYcLT5F5nRll5KF5tw8umdER3EOrsFXIEro67T");
- cursor = getProviders();
+ assetProviders = providers.getProviders("",new String [] {});
} finally {
providers.close();
}
+
Log.d(TAG, "XXX -----------------------------------------------------------");
- int count = cursor.getCount();
- Log.d(TAG, "XXX count: " + count);
+ int count = assetProviders.size();
+ Log.d(TAG, "XXX createPreferenceHierarchy size: " + count);
provider_names = new String[count];
provider_urls = new String[count];
int i = 0;
- while (cursor.moveToNext()) {
- long id = cursor.getLong(0);
- provider_names[i] = cursor.getString(1);
- provider_urls[i] = cursor.getString(2);
- String redirect_url = cursor.getString(3);
- String client_id = cursor.getString(4);
- String client_secret = cursor.getString(5);
- long created_at = cursor.getLong(6);
- Log.d(TAG,"Prefs id: " + id);
- Log.d(TAG,"Prefs provider name: " + provider_names[i]);
- Log.d(TAG,"Prefs provider url: " + provider_urls[i]);
- Log.d(TAG,"Prefs redirect url: " + redirect_url);
- Log.d(TAG,"Prefs client id: " + client_id);
- Log.d(TAG,"Prefs client secret: " + client_secret);
- Log.d(TAG,"Prefs created at: " + created_at);
+ while (i < assetProviders.size()) {
+ Provider p = assetProviders.get(i);
+ provider_names[i] = p.providerName;
+ provider_urls[i] = p.providerUrl;
+
+ Log.d(TAG, "[" + i + "]" + "Prefs provider name: " + provider_names[i]);
+ Log.d(TAG, "[" + i + "]" + "Prefs provider url: " + provider_urls[i]);
+
i++;
}
@@ -97,30 +87,4 @@ private PreferenceScreen createPreferenceHierarchy() {
return root;
}
-
- private Cursor getProviders() {
- Log.d(TAG,"XXX Prefs#getProviders");
- SQLiteDatabase db = providers.getReadableDatabase();
- Cursor cursor = db.query(PROVIDERS_TABLE_NAME, FROM, null, null, null, null, null);
- Log.d(TAG,"XXX Prefs#getProviders count: " + cursor.getCount());
- startManagingCursor(cursor);
- return cursor;
- }
-
- private void addProvider(String name, String provider_url, String redirect_url, String client_id, String client_secret) {
- Log.d(TAG,"Cyberwire#addProvider: " + name);
-
- SQLiteDatabase db = providers.getWritableDatabase();
- ContentValues values = new ContentValues();
- values.put(NAME, name);
- values.put(PROVIDER_URL, provider_url);
- values.put(REDIRECT_URL, redirect_url);
- values.put(CLIENT_ID, client_id);
- values.put(CLIENT_SECRET, client_secret);
- SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- values.put(PROVIDER_CREATED_AT, dateFormat.format(new Date()));
-
- values.put(PROVIDER_CREATED_AT, System.currentTimeMillis());
- db.insertOrThrow(PROVIDERS_TABLE_NAME, null, values);
- }
}
View
30 src/org/opensourcecurrency/hack/Provider.java
@@ -0,0 +1,30 @@
+package org.opensourcecurrency.hack;
+
+public class Provider {
+ public String providerName;
+ public String providerUrl;
+ public String redirectUrl;
+ public String clientId;
+ public String clientSecret;
+
+ public void setName(String name) {
+ providerName = name;
+ }
+
+ public void setProviderUrl(String provider_url) {
+ providerUrl = provider_url;
+ }
+
+ public void setRedirectUrl(String redirect_url) {
+ redirectUrl = redirect_url;
+ }
+
+ public void setClientId(String client_id) {
+ clientId = client_id;
+ }
+
+ public void setClientSecret(String client_secret) {
+ clientSecret = client_secret;
+ }
+
+}
View
72 src/org/opensourcecurrency/hack/ProviderData.java
@@ -9,6 +9,10 @@
import static org.opensourcecurrency.hack.ConstantsProviders.CLIENT_SECRET;
import static org.opensourcecurrency.hack.ConstantsProviders.PROVIDER_CREATED_AT;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.ArrayList;
+
import static org.opensourcecurrency.hack.ConstantsAssets.ASSETS_TABLE_NAME;
import static org.opensourcecurrency.hack.ConstantsAssets.ASSET_PROVIDER_ID;
import static org.opensourcecurrency.hack.ConstantsAssets.ASSET_URL;
@@ -26,10 +30,16 @@
import static org.opensourcecurrency.hack.ConstantsRefreshTokens.REFRESH_TOKEN_EXPIRES_AT;
import static org.opensourcecurrency.hack.ConstantsRefreshTokens.REFRESH_TOKEN_CREATED_AT;
+import org.opensourcecurrency.hack.Provider;
+
+import android.content.ContentValues;
import android.content.Context;
+import android.content.SharedPreferences;
+import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
+import android.preference.PreferenceManager;
import android.util.Log;
@@ -38,6 +48,8 @@
private static final int DATABASE_VERSION = 1;
private static final String TAG = "OpenTransact";
+ private static String[] FROM = { _ID, NAME,PROVIDER_URL,REDIRECT_URL,CLIENT_ID,CLIENT_SECRET,PROVIDER_CREATED_AT };
+
public ProviderData(Context ctx) {
super(ctx, DATABASE_NAME, null, DATABASE_VERSION);
}
@@ -77,5 +89,65 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
+
+ public ArrayList<Provider> getProviders(String selection, String [] selectionArgs) {
+ Log.d(TAG,"XXX ProviderData#getProviders");
+ SQLiteDatabase db = getReadableDatabase();
+ ArrayList<Provider> resultList = new ArrayList<Provider>();
+
+ Cursor cursor = db.query(PROVIDERS_TABLE_NAME, FROM, selection, selectionArgs, null, null, null);
+ Log.d(TAG,"XXX ProviderData#getProviders count: " + cursor.getCount());
+ while (cursor.moveToNext())
+ {
+ try {
+ Provider ob = new Provider();
+ ob.setName(cursor.getString(cursor.getColumnIndex(NAME)));
+ ob.setProviderUrl(cursor.getString(cursor.getColumnIndex(PROVIDER_URL)));
+ ob.setRedirectUrl(cursor.getString(cursor.getColumnIndex(REDIRECT_URL)));
+ ob.setClientId(cursor.getString(cursor.getColumnIndex(CLIENT_ID)));
+ ob.setClientSecret(cursor.getString(cursor.getColumnIndex(CLIENT_SECRET)));
+ resultList.add(ob);
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+ cursor.close();
+ db.close();
+
+ return resultList;
+ }
+
+ public Provider getProvider(String providername) {
+ Log.d(TAG,"XXX getProvider: " + providername);
+
+ Provider provider;
+ ArrayList<Provider> assetProviders;
+
+ try {
+ assetProviders = getProviders(PROVIDER_URL + " = ?", new String[] {providername});
+ } finally {
+ close();
+ }
+
+ provider = assetProviders.get(0);
+ return provider;
+ }
+
+ public void addProvider(String name, String provider_url, String redirect_url, String client_id, String client_secret) {
+ Log.d(TAG,"ProviderData#addProvider: " + name);
+
+ SQLiteDatabase db = getWritableDatabase();
+ ContentValues values = new ContentValues();
+ values.put(NAME, name);
+ values.put(PROVIDER_URL, provider_url);
+ values.put(REDIRECT_URL, redirect_url);
+ values.put(CLIENT_ID, client_id);
+ values.put(CLIENT_SECRET, client_secret);
+ SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+ values.put(PROVIDER_CREATED_AT, dateFormat.format(new Date()));
+
+ //values.put(PROVIDER_CREATED_AT, System.currentTimeMillis());
+ db.insertOrThrow(PROVIDERS_TABLE_NAME, null, values);
+ }
}
View
17 src/org/opensourcecurrency/hack/Send.java
@@ -34,6 +34,7 @@
private static final String OAUTH_PAYMENT_ACTION = "org.opensourcecurrency.hack.OAUTH_PAYMENT";
private static final String TAG = "OpenTransact";
+ private ProviderData providers;
private ProgressDialog progress;
private EditText toText;
@@ -58,23 +59,25 @@ public void onCreate(Bundle savedInstanceState) {
@Override
public void onClick(View view) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
- String provider = prefs.getString("assetProviderPref","");
+ //String provider = prefs.getString("assetProviderPref","");
String access_token = prefs.getString("access_token","none");
Log.d(TAG,"access_token : " + access_token);
- String providerValues[];
- providerValues = provider.split(" ");
- final String asset_url = providerValues[4];
+ //String providerValues[];
+ //providerValues = provider.split(" ");
+ //final String asset_url = providerValues[4];
+
+ providers = new ProviderData(this);
+ Provider provider = providers.getProvider(prefs.getString("assetProviderPref",""));
+ String asset_path="/transacts/hours";
switch (view.getId()) {
case R.id.pay_button:
- Log.d(TAG,"Send#onClick host: " + providerValues[0]);
- Log.d(TAG,"Send#onClick asset: " + asset_url);
//Log.d(TAG,"onClick: to=" + toText.getText().toString());
//Log.d(TAG,"onClick: amount=" + amountText.getText().toString());
//Log.d(TAG,"onClick: note=" + noteText.getText().toString());
try {
- HttpPost paymentRequest = new HttpPost(new URI(asset_url));
+ HttpPost paymentRequest = new HttpPost(new URI(provider.providerUrl+asset_path));
paymentRequest.setHeader("Accept","application/json");
paymentRequest.setHeader("Authorization","Bearer " + access_token);
List<NameValuePair> parameters = new ArrayList<NameValuePair>();
View
17 src/org/opensourcecurrency/hack/Transactions.java
@@ -1,5 +1,6 @@
package org.opensourcecurrency.hack;
+import static org.opensourcecurrency.hack.ConstantsProviders.PROVIDER_URL;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.preference.PreferenceManager;
@@ -15,6 +16,8 @@
import android.widget.ListView;
import android.widget.TextView;
import java.net.URI;
+import java.util.ArrayList;
+
import org.apache.http.client.methods.HttpGet;
import org.json.JSONException;
@@ -26,6 +29,7 @@
TextView selection;
private static final String OAUTH_LISTPAYMENTS_ACTION = "org.opensourcecurrency.hack.OAUTH_LISTPAYMENTS";
private static final String TAG = "OpenTransact";
+ private ProviderData providers;
private ProgressDialog progress;
@Override
@@ -34,15 +38,16 @@ public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.transactions);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
- String provider = prefs.getString("assetProviderPref","");
String access_token = prefs.getString("access_token","none");
Log.d(TAG,"access_token : " + access_token);
- String providerValues[];
- providerValues = provider.split(" ");
- final String asset_url = providerValues[4];
+
+ providers = new ProviderData(this);
+ Provider provider = providers.getProvider(prefs.getString("assetProviderPref",""));
+
+ String asset_path="/transacts/hours";
try {
- HttpGet transactionsRequest = new HttpGet(new URI(asset_url));
+ HttpGet transactionsRequest = new HttpGet(new URI(provider.providerUrl + asset_path));
transactionsRequest.setHeader("Accept","application/json");
transactionsRequest.setHeader("Authorization","Bearer " + access_token);
RestTask task = new RestTask(this, OAUTH_LISTPAYMENTS_ACTION);
@@ -52,7 +57,7 @@ public void onCreate(Bundle savedInstanceState) {
e.printStackTrace();
}
}
-
+
public void onListItemClick(ListView parent, View v, int position, long id) {
//selection.setText(items[position]);
}
Please sign in to comment.
Something went wrong with that request. Please try again.