/
Transactions.java
114 lines (102 loc) · 4.05 KB
/
Transactions.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package org.opensourcecurrency.hack;
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;
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 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();
}
}
};
}