Permalink
Browse files

list transactions. add email setting

  • Loading branch information...
1 parent cd66e8d commit 33c169396f99275c6d90f31142d026f2160981bd @herestomwiththeweather committed Apr 15, 2012
Showing with 126 additions and 13 deletions.
  1. +13 −8 res/layout/transactions.xml
  2. +6 −0 res/xml/settings.xml
  3. +107 −5 src/org/opensourcecurrency/hack/Transactions.java
@@ -1,12 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@color/background"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <TextView
- android:id="@+id/transactions_content"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/hello" />
+ android:orientation="vertical"
+ android:layout_width="fill_parent"
+ android:layout_height="fill_parent" >
+ <TextView
+ android:id="@+id/selection"
+ android:layout_width="fill_parent"
+ android:layout_height="wrap_content"/>
+ <ListView
+ android:id="@android:id/list"
+ android:layout_width="fill_parent"
+ android:layout_height="fill_parent"
+ android:drawSelectorOnTop="false"
+ />
</LinearLayout>
View
@@ -1,6 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
+ <EditTextPreference
+ android:key="emailPref"
+ android:title="Email Address"
+ android:summary="Enter your email address"
+ android:defaultValue=""
+ />
<ListPreference
android:key="assetProviderPref"
android:title="Asset Provider"
@@ -1,12 +1,114 @@
package org.opensourcecurrency.hack;
-import android.app.Activity;
+import android.app.ProgressDialog;
import android.os.Bundle;
+import android.preference.PreferenceManager;
+import android.app.ListActivity;
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.content.SharedPreferences;
+import android.util.Log;
+import android.view.View;
+import android.widget.ArrayAdapter;
+import android.widget.ListView;
+import android.widget.TextView;
+import java.net.URI;
+import org.apache.http.client.methods.HttpGet;
-public class Transactions extends Activity {
+import org.json.JSONException;
+import org.json.JSONObject;
+import org.json.JSONArray;
+import org.opensourcecurrency.hack.RestTask;
+
+public class Transactions extends ListActivity {
+ TextView selection;
+ private static final String OAUTH_LISTPAYMENTS_ACTION = "org.opensourcecurrency.hack.OAUTH_LISTPAYMENTS";
+ private static final String TAG = "OpenTransact";
+ private ProgressDialog progress;
+
+ @Override
+ public void onCreate(Bundle savedInstanceState) {
+ super.onCreate(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];
+
+ try {
+ HttpGet transactionsRequest = new HttpGet(new URI(asset_url));
+ transactionsRequest.setHeader("Accept","application/json");
+ transactionsRequest.setHeader("Authorization","Bearer " + access_token);
+ RestTask task = new RestTask(this, OAUTH_LISTPAYMENTS_ACTION);
+ task.execute(transactionsRequest);
+ progress = ProgressDialog.show(this, "Fetching Transactions", "Waiting...", true);
+ } catch(Exception e) {
+ e.printStackTrace();
+ }
+ }
+
+ public void onListItemClick(ListView parent, View v, int position, long id) {
+ //selection.setText(items[position]);
+ }
+
+ @Override
+ public void onResume() {
+ super.onResume();
+ registerReceiver(receiver, new IntentFilter(OAUTH_LISTPAYMENTS_ACTION));
+ }
+
@Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.transactions);
+ public void onPause() {
+ super.onPause();
+ unregisterReceiver(receiver);
}
+
+ private BroadcastReceiver receiver = new BroadcastReceiver() {
+ private String counterparty(JSONObject transact,String emailAddress) {
+ try {
+ String to = transact.getString("to");
+ String from = transact.getString("from");
+ if(emailAddress==to) {
+ return from;
+ } else {
+ return to;
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ return "";
+ }
+
+ @Override
+ public void onReceive(Context context, Intent intent) {
+ Log.d(TAG,"onReceive called (Transactions)!");
+ if(progress != null) {
+ progress.dismiss();
+ }
+ SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
+ String emailAddress = prefs.getString("emailPref","");
+ String response = intent.getStringExtra(RestTask.HTTP_RESPONSE);
+ Log.d(TAG,"response(Transactions): "+response);
+
+ try {
+ JSONArray transactions_response = new JSONArray(response);
+ String[] items = new String[transactions_response.length()];
+ for(int i=0;i<transactions_response.length();i++) {
+ JSONObject transact = transactions_response.getJSONObject(i);
+ String amount = transact.getString("amount");
+ items[i] = amount + " " + counterparty(transact,emailAddress);
+ }
+ setListAdapter(new ArrayAdapter<String>(context,android.R.layout.simple_list_item_1,items));
+ selection=(TextView)findViewById(R.id.selection);
+ } catch (JSONException e) {
+ e.printStackTrace();
+ }
+ }
+ };
}

0 comments on commit 33c1693

Please sign in to comment.