Android Open Source - My-Wallet-Android Exchange Rates Provider






From Project

Back to project page My-Wallet-Android.

License

The source code is released under:

GNU General Public License

If you think the Android project My-Wallet-Android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright 2011-2012 the original author or authors.
 *//from   w w  w  .j av  a2s .c  o m
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package piuk.blockchain.android;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.net.URLConnection;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;

import org.json.JSONException;
import org.json.JSONObject;

import piuk.blockchain.android.util.IOUtils;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.net.Uri;
import android.provider.BaseColumns;

/**
 * @author Andreas Schildbach
 */
public class ExchangeRatesProvider extends ContentProvider
{
  public static final Uri CONTENT_URI = Uri.parse("content://" + Constants.PACKAGE_NAME + '.' + "exchange_rates");

  public static final String KEY_CURRENCY_CODE = "currency_code";
  public static final String KEY_EXCHANGE_RATE = "exchange_rate";

  private Map<String, Double> exchangeRates = null;

  @Override
  public boolean onCreate()
  {
    return true;
  }

  @Override
  public Cursor query(final Uri uri, final String[] projection, final String selection, final String[] selectionArgs, final String sortOrder)
  {
    if (exchangeRates == null)
    {
      exchangeRates = getExchangeRates();
      
      if (exchangeRates == null)
        return null;
    }
  
    final MatrixCursor cursor = new MatrixCursor(new String[] { BaseColumns._ID, KEY_CURRENCY_CODE, KEY_EXCHANGE_RATE });

    if (selection == null)
    {
      for (final Map.Entry<String, Double> entry : exchangeRates.entrySet())
        cursor.newRow().add(entry.getKey().hashCode()).add(entry.getKey()).add(entry.getValue());
    }
    else if (selection.equals(KEY_CURRENCY_CODE))
    {

      if (selectionArgs == null)
        return null;

      final String code = selectionArgs[0];
      final Double rate = exchangeRates.get(code);
      cursor.newRow().add(code.hashCode()).add(code).add(rate);
    }

    return cursor;
  }

  @Override
  public Uri insert(final Uri uri, final ContentValues values)
  {
    throw new UnsupportedOperationException();
  }

  @Override
  public int update(final Uri uri, final ContentValues values, final String selection, final String[] selectionArgs)
  {
    throw new UnsupportedOperationException();
  }

  @Override
  public int delete(final Uri uri, final String selection, final String[] selectionArgs)
  {
    throw new UnsupportedOperationException();
  }

  @Override
  public String getType(final Uri uri)
  {
    throw new UnsupportedOperationException();
  }

  private static Map<String, Double> getExchangeRates()
  {
    try
    {
      final URLConnection connection = new URL("http://bitcoincharts.com/t/weighted_prices.json").openConnection();
      // https://mtgox.com/code/data/ticker.php
      // https://bitmarket.eu/api/ticker
      // http://bitcoincharts.com/t/weighted_prices.json

      connection.connect();
      final Reader is = new InputStreamReader(new BufferedInputStream(connection.getInputStream()));
      final StringBuilder content = new StringBuilder();
      IOUtils.copy(is, content);
      is.close();

      final Map<String, Double> rates = new TreeMap<String, Double>();

      final JSONObject head = new JSONObject(content.toString());
      for (@SuppressWarnings("unchecked")
      final Iterator<String> i = head.keys(); i.hasNext();)
      {
        final String currencyCode = i.next();
        if (!"timestamp".equals(currencyCode))
        {
          final JSONObject o = head.getJSONObject(currencyCode);
          double rate = o.optDouble("24h", 0);
          if (rate == 0)
            rate = o.optDouble("7d", 0);
          if (rate == 0)
            rate = o.optDouble("30d", 0);

          if (rate != 0)
            rates.put(currencyCode, rate);
        }
      }

      return rates;
    }
    catch (final IOException x)
    {
      x.printStackTrace();
    }
    catch (final JSONException x)
    {
      x.printStackTrace();
    }

    return null;
  }
}




Java Source Code List

piuk.BitcoinAddress.java
piuk.BitcoinScript.java
piuk.Hash.java
piuk.MyBlockChain.java
piuk.MyRemoteWallet.java
piuk.MyTransactionConfidence.java
piuk.MyTransactionInput.java
piuk.MyTransactionOutPoint.java
piuk.MyTransactionOutput.java
piuk.MyTransaction.java
piuk.MyWallet.java
piuk.blockchain.android.AddressBookProvider.java
piuk.blockchain.android.BlockchainService.java
piuk.blockchain.android.Constants.java
piuk.blockchain.android.DetermineFirstSeenThread.java
piuk.blockchain.android.ExchangeRatesProvider.java
piuk.blockchain.android.WalletApplication.java
piuk.blockchain.android.WalletBalanceWidgetProvider.java
piuk.blockchain.android.ui.AbstractWalletActivity.java
piuk.blockchain.android.ui.AmountCalculatorFragment.java
piuk.blockchain.android.ui.CurrencyAmountView.java
piuk.blockchain.android.ui.CurrencyCodeDrawable.java
piuk.blockchain.android.ui.EditAddressBookEntryFragment.java
piuk.blockchain.android.ui.ExchangeRatesFragment.java
piuk.blockchain.android.ui.NewAccountFragment.java
piuk.blockchain.android.ui.PairWalletActivity.java
piuk.blockchain.android.ui.PreferencesActivity.java
piuk.blockchain.android.ui.RequestCoinsActivity.java
piuk.blockchain.android.ui.RequestCoinsFragment.java
piuk.blockchain.android.ui.SecondPasswordFragment.java
piuk.blockchain.android.ui.SendCoinsActivity.java
piuk.blockchain.android.ui.SendCoinsFragment.java
piuk.blockchain.android.ui.SendingAddressesFragment.java
piuk.blockchain.android.ui.TransactionActivity.java
piuk.blockchain.android.ui.TransactionFragment.java
piuk.blockchain.android.ui.WalletActivity.java
piuk.blockchain.android.ui.WalletAddressesFragment.java
piuk.blockchain.android.ui.WalletBalanceFragment.java
piuk.blockchain.android.ui.WalletTransactionsFragment.java
piuk.blockchain.android.ui.WelcomeFragment.java
piuk.blockchain.android.util.ActionBarFragment.java
piuk.blockchain.android.util.Base43.java
piuk.blockchain.android.util.CircularProgressView.java
piuk.blockchain.android.util.ErrorReporter.java
piuk.blockchain.android.util.IOUtils.java
piuk.blockchain.android.util.Iso8601Format.java
piuk.blockchain.android.util.NfcTools.java
piuk.blockchain.android.util.QrDialog.java
piuk.blockchain.android.util.ViewPagerTabs.java
piuk.blockchain.android.util.WalletUtils.java