Skip to content

Commit b007843

Browse files
committed
manual json parsing because gradle android plugin does not yet support proguard.cfg
1 parent 9a62610 commit b007843

File tree

3 files changed

+49
-37
lines changed

3 files changed

+49
-37
lines changed

src/main/java/com/github/bitcoinlabs/bitcoinmobileandroid/Outpoint.java

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package com.github.bitcoinlabs.bitcoinmobileandroid;
22

3+
34
public class Outpoint {
4-
private String address;
5-
private String hash;
6-
private int n;
7-
private long satoshis;
5+
final private String address;
6+
final private String hash;
7+
final private int n;
8+
final private long satoshis;
89

910
public Outpoint(String address, String hash, int n, long satoshis) {
1011
super();
@@ -18,34 +19,18 @@ public String getAddress() {
1819
return address;
1920
}
2021

21-
public void setAddress(String address) {
22-
this.address = address;
23-
}
24-
2522
public String getHash() {
2623
return hash;
2724
}
2825

29-
public void setHash(String hash) {
30-
this.hash = hash;
31-
}
32-
3326
public int getIndex() {
3427
return n;
3528
}
3629

37-
public void setIndex(int index) {
38-
this.n = index;
39-
}
40-
4130
public long getSatoshis() {
4231
return satoshis;
4332
}
4433

45-
public void setSatoshis(long satoshis) {
46-
this.satoshis = satoshis;
47-
}
48-
4934
@Override
5035
public String toString() {
5136
return "Outpoint [address=" + address + ", hash=" + hash + ", n=" + n

src/main/java/com/github/bitcoinlabs/bitcoinmobileandroid/OutpointService.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import java.io.InputStream;
44
import java.io.InputStreamReader;
55
import java.io.Reader;
6+
import java.util.ArrayList;
7+
import java.util.List;
68

79
import org.apache.http.HttpEntity;
810
import org.apache.http.HttpResponse;
@@ -12,9 +14,14 @@
1214
import org.apache.http.params.BasicHttpParams;
1315
import org.apache.http.params.HttpConnectionParams;
1416
import org.apache.http.params.HttpParams;
17+
import org.json.JSONObject;
1518

1619
import com.google.bitcoin.core.Address;
1720
import com.google.gson.Gson;
21+
import com.google.gson.JsonArray;
22+
import com.google.gson.JsonElement;
23+
import com.google.gson.JsonObject;
24+
import com.google.gson.JsonParser;
1825

1926
import android.app.IntentService;
2027
import android.app.Notification;
@@ -135,7 +142,24 @@ protected void onHandleIntent(Intent outpointQueryIntent) {
135142
HttpEntity responseEntity = response.getEntity();
136143
InputStream content = responseEntity.getContent();
137144
Reader reader = new InputStreamReader(content);
138-
outpointsResponse = gson.fromJson(reader, OutpointsResponse.class);
145+
// gradle android plugin doesn't yet allow for custom proguard settings needed for gson.fromJson
146+
// outpointsResponse = gson.fromJson(reader, OutpointsResponse.class);
147+
JsonParser parser = new JsonParser();
148+
JsonObject outpointsResponseJSON = parser.parse(reader).getAsJsonObject();
149+
Log.i(getClass().getSimpleName()+"", outpointsResponseJSON+"");
150+
long timestamp = 0;
151+
// timestamp = outpointsResponseJSON.get("timestamp").getAsLong();
152+
JsonArray outpointsJSON = outpointsResponseJSON.get("unspent_outpoints").getAsJsonArray();
153+
List<Outpoint> outpoints = new ArrayList<Outpoint>(outpointsJSON.size());
154+
for (JsonElement outpointJSON : outpointsJSON) {
155+
String hash = outpointJSON.getAsJsonObject().get("hash").getAsString();
156+
int n = outpointJSON.getAsJsonObject().get("n").getAsInt();
157+
long satoshis = outpointJSON.getAsJsonObject().get("satoshis").getAsLong();
158+
String opAddress = outpointJSON.getAsJsonObject().get("address").getAsString();
159+
Outpoint outpoint = new Outpoint(opAddress, hash, n, satoshis);
160+
outpoints.add(outpoint);
161+
}
162+
outpointsResponse = new OutpointsResponse(timestamp, outpoints);
139163
wallet.add(outpointsResponse);
140164
Log.i(getClass().getSimpleName()+"", outpointsResponse+"");
141165
} catch (Exception e) {

src/main/java/com/github/bitcoinlabs/bitcoinmobileandroid/WalletOpenHelper.java

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@ public Address getUnusedAddress() {
117117
if (cursor.getCount() > 0) {
118118
cursor.moveToNext();
119119
String addressString = cursor.getString(0);
120-
cursor.close();
121120
try {
122121
btcAddress = new Address(NetworkParameters.prodNet(), addressString);
123122
} catch (AddressFormatException e) {
@@ -126,29 +125,33 @@ public Address getUnusedAddress() {
126125
} else {
127126
btcAddress = newKey();
128127
}
128+
cursor.close();
129+
db.close();
129130
return btcAddress;
130131
}
131132

132133
public void add(OutpointsResponse outpointsResponse) {
133-
SQLiteDatabase db = getWritableDatabase();
134134
Collection<Outpoint> outpoints = outpointsResponse.getUnspent_outpoints();
135-
for (Outpoint outpoint : outpoints) {
136-
outpoint.getAddress();
137-
try {
138-
ContentValues values = new ContentValues();
139-
values.put(HASH, Utils.hexStringToBytes(outpoint.getHash()));
140-
values.put(ADDRESS, outpoint.getAddress());
141-
values.put(N, outpoint.getIndex());
142-
values.put(SATOSHIS, outpoint.getSatoshis());
143-
db.insertOrThrow("outpoints", null, values );
144-
} catch (SQLiteConstraintException e) {
145-
//do nothing as we will assume we already have a record of this outpoint.
146-
//TODO verify that we have the right ADDRESS and SATOSHIS for this outpoint
135+
if (outpoints != null && outpoints.size() > 0) {
136+
SQLiteDatabase db = getWritableDatabase();
137+
for (Outpoint outpoint : outpoints) {
138+
outpoint.getAddress();
139+
try {
140+
ContentValues values = new ContentValues();
141+
values.put(HASH, Utils.hexStringToBytes(outpoint.getHash()));
142+
values.put(ADDRESS, outpoint.getAddress());
143+
values.put(N, outpoint.getIndex());
144+
values.put(SATOSHIS, outpoint.getSatoshis());
145+
db.insertOrThrow("outpoints", null, values );
146+
} catch (SQLiteConstraintException e) {
147+
//do nothing as we will assume we already have a record of this outpoint.
148+
//TODO verify that we have the right ADDRESS and SATOSHIS for this outpoint
149+
}
147150
}
151+
db.close();
148152
}
149-
db.close();
150153
}
151-
154+
152155
public long getBalance() {
153156
long satoshis = 0;
154157
SQLiteDatabase db = getReadableDatabase();

0 commit comments

Comments
 (0)