Android Open Source - bitfynd-wallet-android Configuration






From Project

Back to project page bitfynd-wallet-android.

License

The source code is released under:

GNU General Public License

If you think the Android project bitfynd-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 2014 the original author or authors.
 *//  w ww.  ja  v a2 s . co 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 de.schildbach.wallet;

import javax.annotation.Nonnull;

import org.bitcoinj.core.Coin;
import org.bitcoinj.utils.Fiat;
import org.bitcoinj.utils.MonetaryFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.text.format.DateUtils;
import de.schildbach.wallet.ExchangeRatesProvider.ExchangeRate;

/**
 * @author Andreas Schildbach
 */
public class Configuration
{
  public final int lastVersionCode;

  private final SharedPreferences prefs;

  public static final String PREFS_KEY_BTC_PRECISION = "btc_precision";
  public static final String PREFS_KEY_CONNECTIVITY_NOTIFICATION = "connectivity_notification";
  public static final String PREFS_KEY_EXCHANGE_CURRENCY = "exchange_currency";
  public static final String PREFS_KEY_TRUSTED_PEER = "trusted_peer";
  public static final String PREFS_KEY_TRUSTED_PEER_ONLY = "trusted_peer_only";
  public static final String PREFS_KEY_DISCLAIMER = "disclaimer";
  private static final String PREFS_KEY_LABS_QR_PAYMENT_REQUEST = "labs_qr_payment_request";

  private static final String PREFS_KEY_LAST_VERSION = "last_version";
  private static final String PREFS_KEY_LAST_USED = "last_used";
  private static final String PREFS_KEY_BEST_CHAIN_HEIGHT_EVER = "best_chain_height_ever";
  private static final String PREFS_KEY_CACHED_EXCHANGE_CURRENCY = "cached_exchange_currency";
  private static final String PREFS_KEY_CACHED_EXCHANGE_RATE_COIN = "cached_exchange_rate_coin";
  private static final String PREFS_KEY_CACHED_EXCHANGE_RATE_FIAT = "cached_exchange_rate_fiat";
  private static final String PREFS_KEY_LAST_EXCHANGE_DIRECTION = "last_exchange_direction";
  private static final String PREFS_KEY_CHANGE_LOG_VERSION = "change_log_version";
  public static final String PREFS_KEY_REMIND_BACKUP = "remind_backup";
  private static final String PREFS_KEY_LAST_BACKUP = "last_backup";

  private static final int PREFS_DEFAULT_BTC_SHIFT = 0;
  private static final int PREFS_DEFAULT_BTC_PRECISION = 4;

  private static final Logger log = LoggerFactory.getLogger(Configuration.class);

  public Configuration(@Nonnull final SharedPreferences prefs)
  {
    this.prefs = prefs;

    this.lastVersionCode = prefs.getInt(PREFS_KEY_LAST_VERSION, 0);
  }

  private int getBtcPrecision()
  {
    final String precision = prefs.getString(PREFS_KEY_BTC_PRECISION, null);
    if (precision != null)
      return precision.charAt(0) - '0';
    else
      return PREFS_DEFAULT_BTC_PRECISION;
  }

  public int getBtcShift()
  {
    final String precision = prefs.getString(PREFS_KEY_BTC_PRECISION, null);
    if (precision != null)
      return precision.length() == 3 ? precision.charAt(2) - '0' : 0;
    else
      return PREFS_DEFAULT_BTC_SHIFT;
  }

  public MonetaryFormat getFormat()
  {
    final int shift = getBtcShift();
    final int minPrecision = shift <= 3 ? 2 : 0;
    final int decimalRepetitions = (getBtcPrecision() - minPrecision) / 2;
    return new MonetaryFormat().shift(shift).minDecimals(minPrecision).repeatOptionalDecimals(2, decimalRepetitions).postfixCode();
  }

  public MonetaryFormat getMaxPrecisionFormat()
  {
    final int shift = getBtcShift();
    if (shift == 0)
      return new MonetaryFormat().shift(0).minDecimals(2).optionalDecimals(2, 2, 2);
    else if (shift == 3)
      return new MonetaryFormat().shift(3).minDecimals(2).optionalDecimals(2, 1);
    else
      return new MonetaryFormat().shift(6).minDecimals(0).optionalDecimals(2);
  }

  public boolean getConnectivityNotificationEnabled()
  {
    return prefs.getBoolean(PREFS_KEY_CONNECTIVITY_NOTIFICATION, true);
  }

  public String getTrustedPeerHost()
  {
    return prefs.getString(PREFS_KEY_TRUSTED_PEER, "").trim();
  }

  public boolean getTrustedPeerOnly()
  {
    return prefs.getBoolean(PREFS_KEY_TRUSTED_PEER_ONLY, false);
  }

  public boolean remindBackup()
  {
    return prefs.getBoolean(PREFS_KEY_REMIND_BACKUP, true);
  }

  public long getLastBackupTime()
  {
    return prefs.getLong(PREFS_KEY_LAST_BACKUP, 0);
  }

  public void armBackupReminder()
  {
    prefs.edit().putBoolean(PREFS_KEY_REMIND_BACKUP, true).commit();
  }

  public void disarmBackupReminder()
  {
    prefs.edit().putBoolean(PREFS_KEY_REMIND_BACKUP, false).putLong(PREFS_KEY_LAST_BACKUP, System.currentTimeMillis()).commit();
  }

  public boolean getDisclaimerEnabled()
  {
    return prefs.getBoolean(PREFS_KEY_DISCLAIMER, true);
  }

    public void setDisclaimerDisabled()
    {
        prefs.edit().putBoolean(PREFS_KEY_DISCLAIMER, false).commit();
    }

  public String getExchangeCurrencyCode()
  {
    return prefs.getString(PREFS_KEY_EXCHANGE_CURRENCY, null);
  }

  public void setExchangeCurrencyCode(final String exchangeCurrencyCode)
  {
    prefs.edit().putString(PREFS_KEY_EXCHANGE_CURRENCY, exchangeCurrencyCode).commit();
  }

  public boolean getQrPaymentRequestEnabled()
  {
    return prefs.getBoolean(PREFS_KEY_LABS_QR_PAYMENT_REQUEST, false);
  }

  public boolean versionCodeCrossed(final int currentVersionCode, final int triggeringVersionCode)
  {
    final boolean wasBelow = lastVersionCode < triggeringVersionCode;
    final boolean wasUsedBefore = lastVersionCode > 0;
    final boolean isNowAbove = currentVersionCode >= triggeringVersionCode;

    return wasUsedBefore && wasBelow && isNowAbove;
  }

  public void updateLastVersionCode(final int currentVersionCode)
  {
    prefs.edit().putInt(PREFS_KEY_LAST_VERSION, currentVersionCode).commit();

    if (currentVersionCode > lastVersionCode)
      log.info("detected app upgrade: " + lastVersionCode + " -> " + currentVersionCode);
    else if (currentVersionCode < lastVersionCode)
      log.warn("detected app downgrade: " + lastVersionCode + " -> " + currentVersionCode);
  }

  public long getLastUsedAgo()
  {
    final long now = System.currentTimeMillis();

    return now - prefs.getLong(PREFS_KEY_LAST_USED, 0);
  }

  public void touchLastUsed()
  {
    final long prefsLastUsed = prefs.getLong(PREFS_KEY_LAST_USED, 0);
    final long now = System.currentTimeMillis();
    prefs.edit().putLong(PREFS_KEY_LAST_USED, now).commit();

    log.info("just being used - last used {} minutes ago", (now - prefsLastUsed) / DateUtils.MINUTE_IN_MILLIS);
  }

  public int getBestChainHeightEver()
  {
    return prefs.getInt(PREFS_KEY_BEST_CHAIN_HEIGHT_EVER, 0);
  }

  public void maybeIncrementBestChainHeightEver(final int bestChainHeightEver)
  {
    if (bestChainHeightEver > getBestChainHeightEver())
      prefs.edit().putInt(PREFS_KEY_BEST_CHAIN_HEIGHT_EVER, bestChainHeightEver).commit();
  }

  public ExchangeRate getCachedExchangeRate()
  {
    if (prefs.contains(PREFS_KEY_CACHED_EXCHANGE_CURRENCY) && prefs.contains(PREFS_KEY_CACHED_EXCHANGE_RATE_COIN)
        && prefs.contains(PREFS_KEY_CACHED_EXCHANGE_RATE_FIAT))
    {
      final String cachedExchangeCurrency = prefs.getString(PREFS_KEY_CACHED_EXCHANGE_CURRENCY, null);
      final Coin cachedExchangeRateCoin = Coin.valueOf(prefs.getLong(PREFS_KEY_CACHED_EXCHANGE_RATE_COIN, 0));
      final Fiat cachedExchangeRateFiat = Fiat.valueOf(cachedExchangeCurrency, prefs.getLong(PREFS_KEY_CACHED_EXCHANGE_RATE_FIAT, 0));
      return new ExchangeRate(new org.bitcoinj.utils.ExchangeRate(cachedExchangeRateCoin, cachedExchangeRateFiat), null);
    }
    else
    {
      return null;
    }
  }

  public void setCachedExchangeRate(final ExchangeRate cachedExchangeRate)
  {
    final Editor edit = prefs.edit();
    edit.putString(PREFS_KEY_CACHED_EXCHANGE_CURRENCY, cachedExchangeRate.getCurrencyCode());
    edit.putLong(PREFS_KEY_CACHED_EXCHANGE_RATE_COIN, cachedExchangeRate.rate.coin.value);
    edit.putLong(PREFS_KEY_CACHED_EXCHANGE_RATE_FIAT, cachedExchangeRate.rate.fiat.value);
    edit.commit();
  }

  public boolean getLastExchangeDirection()
  {
    return prefs.getBoolean(PREFS_KEY_LAST_EXCHANGE_DIRECTION, true);
  }

  public void setLastExchangeDirection(final boolean exchangeDirection)
  {
    prefs.edit().putBoolean(PREFS_KEY_LAST_EXCHANGE_DIRECTION, exchangeDirection).commit();
  }

  public boolean changeLogVersionCodeCrossed(final int currentVersionCode, final int triggeringVersionCode)
  {
    final int changeLogVersion = prefs.getInt(PREFS_KEY_CHANGE_LOG_VERSION, 0);

    final boolean wasBelow = changeLogVersion < triggeringVersionCode;
    final boolean wasUsedBefore = changeLogVersion > 0;
    final boolean isNowAbove = currentVersionCode >= triggeringVersionCode;

    prefs.edit().putInt(PREFS_KEY_CHANGE_LOG_VERSION, currentVersionCode).commit();

    return /* wasUsedBefore && */wasBelow && isNowAbove;
  }

  public void registerOnSharedPreferenceChangeListener(final OnSharedPreferenceChangeListener listener)
  {
    prefs.registerOnSharedPreferenceChangeListener(listener);
  }

  public void unregisterOnSharedPreferenceChangeListener(final OnSharedPreferenceChangeListener listener)
  {
    prefs.unregisterOnSharedPreferenceChangeListener(listener);
  }
}




Java Source Code List

de.schildbach.wallet.AddressBookProvider.java
de.schildbach.wallet.Configuration.java
de.schildbach.wallet.Constants.java
de.schildbach.wallet.ExchangeRatesProvider.java
de.schildbach.wallet.FileAttachmentProvider.java
de.schildbach.wallet.WalletApplication.java
de.schildbach.wallet.WalletBalanceWidgetProvider.java
de.schildbach.wallet.camera.CameraManager.java
de.schildbach.wallet.data.PaymentIntent.java
de.schildbach.wallet.integration.android.BitcoinIntegration.java
de.schildbach.wallet.offline.AcceptBluetoothService.java
de.schildbach.wallet.offline.AcceptBluetoothThread.java
de.schildbach.wallet.offline.DirectPaymentTask.java
de.schildbach.wallet.service.AutosyncReceiver.java
de.schildbach.wallet.service.BlockchainServiceImpl.java
de.schildbach.wallet.service.BlockchainService.java
de.schildbach.wallet.service.BlockchainStateLoader.java
de.schildbach.wallet.service.BlockchainState.java
de.schildbach.wallet.service.UpgradeWalletService.java
de.schildbach.wallet.ui.AbstractBindServiceActivity.java
de.schildbach.wallet.ui.AbstractWalletActivity.java
de.schildbach.wallet.ui.AddressAndLabel.java
de.schildbach.wallet.ui.AddressBookActivity.java
de.schildbach.wallet.ui.BlockListFragment.java
de.schildbach.wallet.ui.CurrencyAmountView.java
de.schildbach.wallet.ui.CurrencyCalculatorLink.java
de.schildbach.wallet.ui.CurrencySymbolDrawable.java
de.schildbach.wallet.ui.CurrencyTextView.java
de.schildbach.wallet.ui.DialogBuilder.java
de.schildbach.wallet.ui.EditAddressBookEntryFragment.java
de.schildbach.wallet.ui.EncryptKeysDialogFragment.java
de.schildbach.wallet.ui.ExchangeRateLoader.java
de.schildbach.wallet.ui.ExchangeRatesActivity.java
de.schildbach.wallet.ui.ExchangeRatesFragment.java
de.schildbach.wallet.ui.FancyListFragment.java
de.schildbach.wallet.ui.FileAdapter.java
de.schildbach.wallet.ui.HelpDialogFragment.java
de.schildbach.wallet.ui.ImportDialogButtonEnablerListener.java
de.schildbach.wallet.ui.InputParser.java
de.schildbach.wallet.ui.MaybeMaintenanceFragment.java
de.schildbach.wallet.ui.NetworkMonitorActivity.java
de.schildbach.wallet.ui.PeerListFragment.java
de.schildbach.wallet.ui.ProgressDialogFragment.java
de.schildbach.wallet.ui.ReportIssueDialogBuilder.java
de.schildbach.wallet.ui.RequestCoinsActivity.java
de.schildbach.wallet.ui.RequestCoinsFragment.java
de.schildbach.wallet.ui.RestoreWalletActivity.java
de.schildbach.wallet.ui.ScanActivity.java
de.schildbach.wallet.ui.ScannerView.java
de.schildbach.wallet.ui.SendCoinsQrActivity.java
de.schildbach.wallet.ui.SendingAddressesFragment.java
de.schildbach.wallet.ui.ShowPasswordCheckListener.java
de.schildbach.wallet.ui.TransactionsListAdapter.java
de.schildbach.wallet.ui.TransactionsListFragment.java
de.schildbach.wallet.ui.WalletActionsFragment.java
de.schildbach.wallet.ui.WalletActivity.java
de.schildbach.wallet.ui.WalletAddressFragment.java
de.schildbach.wallet.ui.WalletAddressesAdapter.java
de.schildbach.wallet.ui.WalletAddressesFragment.java
de.schildbach.wallet.ui.WalletBalanceFragment.java
de.schildbach.wallet.ui.WalletBalanceLoader.java
de.schildbach.wallet.ui.WalletDisclaimerFragment.java
de.schildbach.wallet.ui.WalletTransactionsFragment.java
de.schildbach.wallet.ui.preference.AboutFragment.java
de.schildbach.wallet.ui.preference.DiagnosticsFragment.java
de.schildbach.wallet.ui.preference.PreferenceActivity.java
de.schildbach.wallet.ui.preference.SettingsFragment.java
de.schildbach.wallet.ui.send.DecodePrivateKeyTask.java
de.schildbach.wallet.ui.send.DeriveKeyTask.java
de.schildbach.wallet.ui.send.MaintenanceDialogFragment.java
de.schildbach.wallet.ui.send.RequestPaymentRequestTask.java
de.schildbach.wallet.ui.send.RequestWalletBalanceTask.java
de.schildbach.wallet.ui.send.SendCoinsActivity.java
de.schildbach.wallet.ui.send.SendCoinsFragment.java
de.schildbach.wallet.ui.send.SendCoinsOfflineTask.java
de.schildbach.wallet.ui.send.SweepWalletActivity.java
de.schildbach.wallet.ui.send.SweepWalletFragment.java
de.schildbach.wallet.util.Base43.java
de.schildbach.wallet.util.BitmapFragment.java
de.schildbach.wallet.util.Bluetooth.java
de.schildbach.wallet.util.CircularProgressView.java
de.schildbach.wallet.util.CrashReporter.java
de.schildbach.wallet.util.Crypto.java
de.schildbach.wallet.util.Formats.java
de.schildbach.wallet.util.GenericUtils.java
de.schildbach.wallet.util.HttpGetThread.java
de.schildbach.wallet.util.Io.java
de.schildbach.wallet.util.Iso8601Format.java
de.schildbach.wallet.util.LinuxSecureRandom.java
de.schildbach.wallet.util.MonetarySpannable.java
de.schildbach.wallet.util.Nfc.java
de.schildbach.wallet.util.Qr.java
de.schildbach.wallet.util.ThrottlingWalletChangeListener.java
de.schildbach.wallet.util.ViewPagerTabs.java
de.schildbach.wallet.util.WalletUtils.java
de.schildbach.wallet.util.WholeStringBuilder.java