@Override
        public void changed() {
          if (localAmountView.getAmount() != null) setExchangeDirection(false);
          else btcAmountView.setHint(null);

          if (listener != null) listener.changed();
        }
 @CheckForNull
 public BigInteger getAmount() {
   if (exchangeDirection) {
     return btcAmountView.getAmount();
   } else if (exchangeRate != null) {
     final BigInteger localAmount = localAmountView.getAmount();
     return localAmount != null ? WalletUtils.btcValue(localAmount, exchangeRate.rate) : null;
   } else {
     return null;
   }
 }
 @CheckForNull
 public Coin getAmount() {
   if (exchangeDirection) {
     return (Coin) btcAmountView.getAmount();
   } else if (exchangeRate != null) {
     final Fiat localAmount = (Fiat) localAmountView.getAmount();
     return localAmount != null ? exchangeRate.fiatToCoin(localAmount) : null;
   } else {
     return null;
   }
 }
  private void done() {
    final BigInteger amount =
        exchangeDirection
            ? btcAmountView.getAmount()
            : new BigDecimal(localAmountView.getAmount())
                .divide(new BigDecimal(exchangeRate), RoundingMode.HALF_UP)
                .toBigInteger();

    ((Listener) getTargetFragment()).useCalculatedAmount(amount);

    dismiss();
  }
  @Override
  public View onCreateView(
      final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
    final View view = inflater.inflate(R.layout.request_coins_fragment, container, false);

    qrView = (ImageView) view.findViewById(R.id.request_coins_qr);
    qrView.setOnClickListener(
        new OnClickListener() {
          @Override
          public void onClick(final View v) {
            BitmapFragment.show(getFragmentManager(), qrCodeBitmap);
          }
        });

    final CurrencyAmountView btcAmountView =
        (CurrencyAmountView) view.findViewById(R.id.request_coins_amount_btc);
    btcAmountView.setCurrencySymbol(config.getBtcPrefix());
    btcAmountView.setInputPrecision(config.getBtcMaxPrecision());
    btcAmountView.setHintPrecision(config.getBtcPrecision());
    btcAmountView.setShift(config.getBtcShift());

    final CurrencyAmountView localAmountView =
        (CurrencyAmountView) view.findViewById(R.id.request_coins_amount_local);
    localAmountView.setInputPrecision(Constants.LOCAL_PRECISION);
    localAmountView.setHintPrecision(Constants.LOCAL_PRECISION);
    amountCalculatorLink = new CurrencyCalculatorLink(btcAmountView, localAmountView);

    acceptBluetoothPaymentView =
        (CheckBox) view.findViewById(R.id.request_coins_accept_bluetooth_payment);
    acceptBluetoothPaymentView.setVisibility(
        ENABLE_BLUETOOTH_LISTENING && bluetoothAdapter != null ? View.VISIBLE : View.GONE);
    acceptBluetoothPaymentView.setChecked(
        ENABLE_BLUETOOTH_LISTENING && bluetoothAdapter != null && bluetoothAdapter.isEnabled());
    acceptBluetoothPaymentView.setOnCheckedChangeListener(
        new OnCheckedChangeListener() {
          @Override
          public void onCheckedChanged(final CompoundButton buttonView, final boolean isChecked) {
            if (ENABLE_BLUETOOTH_LISTENING && bluetoothAdapter != null && isChecked) {
              if (bluetoothAdapter.isEnabled()) {
                startBluetoothListening();
              } else {
                // ask for permission to enable bluetooth
                startActivityForResult(
                    new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE),
                    REQUEST_CODE_ENABLE_BLUETOOTH);
              }
            } else {
              stopBluetoothListening();
            }

            updateView();
          }
        });

    initiateRequestView =
        (TextView) view.findViewById(R.id.request_coins_fragment_initiate_request);

    return view;
  }
  public void setBtcAmount(@Nonnull final Coin amount) {
    final Listener listener = this.listener;
    this.listener = null;

    btcAmountView.setAmount(amount, true);

    this.listener = listener;
  }
  private void updateAppearance() {
    if (exchangeRate != null) {
      localAmountView.setEnabled(true);

      final BigDecimal bdExchangeRate = new BigDecimal(exchangeRate);

      if (exchangeDirection) {
        final BigInteger btcAmount = btcAmountView.getAmount();
        if (btcAmount != null) {
          localAmountView.setAmount(null);
          localAmountView.setHint(
              new BigDecimal(btcAmount).multiply(bdExchangeRate).toBigInteger());
          btcAmountView.setHint(null);
        }
      } else {
        final BigInteger localAmount = localAmountView.getAmount();
        if (localAmount != null) {
          btcAmountView.setAmount(null);
          btcAmountView.setHint(
              new BigDecimal(localAmount)
                  .divide(bdExchangeRate, RoundingMode.HALF_UP)
                  .toBigInteger());
          localAmountView.setHint(null);
        }
      }

      exchangeRateView.setText(
          getString(
              R.string.amount_calculator_dialog_exchange_rate,
              exchangeCurrency,
              WalletUtils.formatValue(WalletUtils.localValue(Utils.COIN, bdExchangeRate))));
    } else {
      localAmountView.setEnabled(false);

      exchangeRateView.setText(R.string.amount_calculator_dialog_exchange_rate_not_available);
    }
  }
 public void setNextFocusId(final int nextFocusId) {
   btcAmountView.setNextFocusId(nextFocusId);
   localAmountView.setNextFocusId(nextFocusId);
 }
 public View activeTextView() {
   if (exchangeDirection) return btcAmountView.getTextView();
   else return localAmountView.getTextView();
 }
  private void update() {
    btcAmountView.setEnabled(enabled);

    if (exchangeRate != null) {
      localAmountView.setEnabled(enabled);
      localAmountView.setCurrencySymbol(exchangeRate.fiat.currencyCode);

      if (exchangeDirection) {
        final Coin btcAmount = (Coin) btcAmountView.getAmount();
        if (btcAmount != null) {
          localAmountView.setAmount(null, false);
          localAmountView.setHint(exchangeRate.coinToFiat(btcAmount));
          btcAmountView.setHint(null);
        }
      } else {
        final Fiat localAmount = (Fiat) localAmountView.getAmount();
        if (localAmount != null) {
          btcAmountView.setAmount(null, false);
          btcAmountView.setHint(exchangeRate.fiatToCoin(localAmount));
          localAmountView.setHint(null);
        }
      }
    } else {
      localAmountView.setEnabled(false);
      localAmountView.setHint(null);
      btcAmountView.setHint(null);
    }
  }
  @Override
  public View onCreateView(
      final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
    final View view = inflater.inflate(R.layout.request_coins_fragment, container, false);

    qrView = (ImageView) view.findViewById(R.id.request_coins_qr);
    qrView.setOnClickListener(
        new OnClickListener() {
          @Override
          public void onClick(final View v) {
            BitmapFragment.show(getFragmentManager(), qrCodeBitmap);
          }
        });

    final CurrencyAmountView btcAmountView =
        (CurrencyAmountView) view.findViewById(R.id.request_coins_amount_btc);
    btcAmountView.setCurrencySymbol(config.getBtcPrefix());
    btcAmountView.setInputPrecision(config.getBtcMaxPrecision());
    btcAmountView.setHintPrecision(config.getBtcPrecision());
    btcAmountView.setShift(config.getBtcShift());

    final CurrencyAmountView localAmountView =
        (CurrencyAmountView) view.findViewById(R.id.request_coins_amount_local);
    localAmountView.setInputPrecision(Constants.LOCAL_PRECISION);
    localAmountView.setHintPrecision(Constants.LOCAL_PRECISION);
    amountCalculatorLink = new CurrencyCalculatorLink(btcAmountView, localAmountView);

    addressView = (Spinner) view.findViewById(R.id.request_coins_fragment_address);
    final List<ECKey> keys = new LinkedList<ECKey>();
    for (final ECKey key : application.getWallet().getKeys())
      if (!wallet.isKeyRotating(key)) keys.add(key);
    final WalletAddressesAdapter adapter = new WalletAddressesAdapter(activity, wallet, false);
    adapter.replace(keys);
    addressView.setAdapter(adapter);
    final Address selectedAddress = application.determineSelectedAddress();
    for (int i = 0; i < keys.size(); i++) {
      final Address address = keys.get(i).toAddress(Constants.NETWORK_PARAMETERS);
      if (address.equals(selectedAddress)) {
        addressView.setSelection(i);
        break;
      }
    }

    acceptBluetoothPaymentView =
        (CheckBox) view.findViewById(R.id.request_coins_accept_bluetooth_payment);
    acceptBluetoothPaymentView.setVisibility(
        ENABLE_BLUETOOTH_LISTENING && bluetoothAdapter != null ? View.VISIBLE : View.GONE);
    acceptBluetoothPaymentView.setChecked(
        ENABLE_BLUETOOTH_LISTENING && bluetoothAdapter != null && bluetoothAdapter.isEnabled());
    acceptBluetoothPaymentView.setOnCheckedChangeListener(
        new OnCheckedChangeListener() {
          @Override
          public void onCheckedChanged(final CompoundButton buttonView, final boolean isChecked) {
            if (ENABLE_BLUETOOTH_LISTENING && bluetoothAdapter != null && isChecked) {
              if (bluetoothAdapter.isEnabled()) {
                startBluetoothListening();
              } else {
                // ask for permission to enable bluetooth
                startActivityForResult(
                    new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE),
                    REQUEST_CODE_ENABLE_BLUETOOTH);
              }
            } else {
              stopBluetoothListening();
            }

            updateView();
          }
        });

    initiateRequestView =
        (TextView) view.findViewById(R.id.request_coins_fragment_initiate_request);

    return view;
  }
  private void update() {
    btcAmountView.setEnabled(enabled);

    if (exchangeRate != null) {
      localAmountView.setEnabled(enabled);
      localAmountView.setCurrencySymbol(exchangeRate.currencyCode);

      if (exchangeDirection) {
        final BigInteger btcAmount = btcAmountView.getAmount();
        if (btcAmount != null) {
          localAmountView.setAmount(null, false);
          localAmountView.setHint(WalletUtils.localValue5_BTC(btcAmount, exchangeRate.rate));
          btcAmountView.setHint(null);
        }
      } else {
        final BigInteger localAmount = localAmountView.getAmount();
        if (localAmount != null) {
          btcAmountView.setAmount(null, false);
          btcAmountView.setHint(WalletUtils.btcValue_BTCtoBTQ(localAmount, exchangeRate.rate));
          localAmountView.setHint(null);
        }
      }
    } else {
      localAmountView.setEnabled(false);
      localAmountView.setHint(null);
      btcAmountView.setHint(null);
    }
  }
  public void updateView() {
    viewBalance.setAmount(application.getWallet().getBalance(BalanceType.ESTIMATED));

    getLoaderManager().restartLoader(0, null, this);
  }
  @Override
  public Dialog onCreateDialog(final Bundle savedInstanceState) {
    final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
    exchangeCurrency =
        prefs.getString(Constants.PREFS_KEY_EXCHANGE_CURRENCY, Constants.DEFAULT_EXCHANGE_CURRENCY);

    final AlertDialog.Builder dialog = new AlertDialog.Builder(activity);
    dialog.setInverseBackgroundForced(true);
    dialog.setTitle(R.string.amount_calculator_dialog_title);

    final View view = inflater.inflate(R.layout.amount_calculator_dialog, null);

    btcAmountView = (CurrencyAmountView) view.findViewById(R.id.amount_calculator_row_btc);
    btcAmountView.setListener(
        new CurrencyAmountView.Listener() {
          public void changed() {
            if (btcAmountView.getAmount() != null) {
              exchangeDirection = true;

              updateAppearance();
            } else {
              localAmountView.setHint(null);
            }
          }

          public void done() {
            AmountCalculatorFragment.this.done();
          }

          public void focusChanged(final boolean hasFocus) {}
        });

    localAmountView = (CurrencyAmountView) view.findViewById(R.id.amount_calculator_row_local);
    localAmountView.setCurrencyCode(exchangeCurrency);
    localAmountView.setListener(
        new CurrencyAmountView.Listener() {
          public void changed() {
            if (localAmountView.getAmount() != null) {
              exchangeDirection = false;

              updateAppearance();
            } else {
              btcAmountView.setHint(null);
            }
          }

          public void done() {
            AmountCalculatorFragment.this.done();
          }

          public void focusChanged(final boolean hasFocus) {}
        });

    exchangeRateView = (TextView) view.findViewById(R.id.amount_calculator_rate);

    dialog.setView(view);

    dialog.setPositiveButton(
        R.string.amount_calculator_dialog_button_use,
        new DialogInterface.OnClickListener() {
          public void onClick(final DialogInterface dialog, final int whichButton) {
            done();
          }
        });
    dialog.setNegativeButton(
        R.string.button_cancel,
        new DialogInterface.OnClickListener() {
          public void onClick(final DialogInterface dialog, final int whichButton) {
            dismiss();
          }
        });

    updateAppearance();

    getLoaderManager().initLoader(0, null, this);

    return dialog.create();
  }