forked from Litecoin-Java/bitcoin-wallet
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
266 additions
and
211 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
wallet/src/de/schildbach/wallet/exchange/GoogleRateLookup.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package de.schildbach.wallet.exchange; | ||
|
||
import android.util.Log; | ||
import de.schildbach.wallet.ExchangeRatesProvider; | ||
import de.schildbach.wallet.util.GenericUtils; | ||
import org.json.JSONArray; | ||
import org.json.JSONException; | ||
import org.json.JSONObject; | ||
|
||
import java.math.BigDecimal; | ||
import java.util.Map; | ||
import java.util.TreeMap; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
public class GoogleRateLookup extends RateLookup { | ||
private static final String TAG = GoogleRateLookup.class.getName(); | ||
|
||
public GoogleRateLookup() | ||
{ | ||
super("http://spreadsheets.google.com/feeds/list/0Av2v4lMxiJ1AdE9laEZJdzhmMzdmcW90VWNfUTYtM2c/2/public/basic?alt=json"); | ||
} | ||
|
||
public Map<String, ExchangeRatesProvider.ExchangeRate> getRates(ExchangeRatesProvider.ExchangeRate usdRate) { | ||
final BigDecimal decUsdRate = GenericUtils.fromNanoCoins(usdRate.rate, 0); | ||
if(getData()) | ||
{ | ||
// We got data from the HTTP connection | ||
final Map<String, ExchangeRatesProvider.ExchangeRate> rates = | ||
new TreeMap<String, ExchangeRatesProvider.ExchangeRate>(); | ||
try | ||
{ | ||
JSONObject head = new JSONObject(this.data); | ||
JSONArray resultArray; | ||
|
||
head = head.getJSONObject("feed"); | ||
resultArray = head.getJSONArray("entry"); | ||
// Format: eg. _cpzh4: 3.673 | ||
Pattern p = Pattern.compile("_cpzh4: ([\\d\\.]+)"); | ||
for(int i = 0; i < resultArray.length(); ++i) { | ||
String currencyCd = resultArray.getJSONObject(i).getJSONObject("title").getString("$t"); | ||
String rateStr = resultArray.getJSONObject(i).getJSONObject("content").getString("$t"); | ||
Matcher m = p.matcher(rateStr); | ||
if(m.matches()) | ||
{ | ||
// Just get the good part | ||
rateStr = m.group(1); | ||
Log.d(TAG, "Currency: " + currencyCd); | ||
Log.d(TAG, "Rate: " + rateStr); | ||
Log.d(TAG, "USD Rate: " + decUsdRate.toString()); | ||
BigDecimal rate = new BigDecimal(rateStr); | ||
Log.d(TAG, "Converted Rate: " + rate.toString()); | ||
rate = decUsdRate.multiply(rate); | ||
Log.d(TAG, "Final Rate: " + rate.toString()); | ||
if (rate.signum() > 0) | ||
{ | ||
rates.put(currencyCd, new ExchangeRatesProvider.ExchangeRate(currencyCd, | ||
GenericUtils.toNanoCoinsRounded(rate.toString(), 0), this.url.getHost())); | ||
} | ||
} | ||
} | ||
} catch(JSONException e) { | ||
Log.i(TAG, "Bad JSON response from Google Spreadsheets!: " + data); | ||
return null; | ||
} | ||
return rates; | ||
} | ||
return null; | ||
} | ||
} |
Oops, something went wrong.