@Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Bundle args = getArguments();
    if (args != null) {
      if (args.containsKey(Constants.ARG_ACCOUNT_ID)) {
        String accountId = args.getString(Constants.ARG_ACCOUNT_ID);
        pocket = checkNotNull(application.getAccount(accountId));
      }

      if (args.containsKey(Constants.ARG_URI)) {
        try {
          processUri(args.getString(Constants.ARG_URI));
        } catch (CoinURIParseException e) {
          // TODO handle more elegantly
          Toast.makeText(getActivity(), e.getMessage(), Toast.LENGTH_LONG).show();
          ACRA.getErrorReporter().handleException(e);
        }
      }
      if (pocket == null) {
        List<WalletAccount> accounts = application.getAllAccounts();
        if (accounts.size() > 0) pocket = accounts.get(0);
        if (pocket == null) {
          ACRA.getErrorReporter()
              .putCustomData("wallet-exists", application.getWallet() == null ? "no" : "yes");
          Toast.makeText(getActivity(), R.string.no_such_pocket_error, Toast.LENGTH_LONG).show();
          getActivity().finish();
          return;
        }
      }
      checkNotNull(pocket, "No account selected");
    } else {
      throw new RuntimeException("Must provide account ID or a payment URI");
    }

    sendAmountType = pocket.getCoinType();

    messageFactory = pocket.getCoinType().getMessagesFactory();

    if (savedInstanceState != null) {
      address = (Address) savedInstanceState.getSerializable(STATE_ADDRESS);
      addressTypeCanChange = savedInstanceState.getBoolean(STATE_ADDRESS_CAN_CHANGE_TYPE);
      sendAmount = (Value) savedInstanceState.getSerializable(STATE_AMOUNT);
      sendAmountType = (CoinType) savedInstanceState.getSerializable(STATE_AMOUNT_TYPE);
    }

    updateBalance();
    setHasOptionsMenu(true);
    mNavigationDrawerFragment =
        (NavigationDrawerFragment) getFragmentManager().findFragmentById(R.id.navigation_drawer);

    String localSymbol = config.getExchangeCurrencyCode();
    for (ExchangeRatesProvider.ExchangeRate rate : getRates(getActivity(), localSymbol)) {
      localRates.put(rate.currencyCodeId, rate.rate);
    }

    loaderManager.initLoader(ID_RATE_LOADER, null, rateLoaderCallbacks);
    loaderManager.initLoader(ID_RECEIVING_ADDRESS_LOADER, null, receivingAddressLoaderCallbacks);
  }
  private void parseAddress(String addressStr) throws AddressFormatException {
    List<CoinType> possibleTypes = GenericUtils.getPossibleTypes(addressStr);
    if (possibleTypes.contains(pocket.getCoinType())) {
      setAddress(new Address(pocket.getCoinType(), addressStr), true);
      sendAmountType = pocket.getCoinType();
    } else if (possibleTypes.size() == 1) {
      setAddress(new Address(possibleTypes.get(0), addressStr), true);
      sendAmountType = possibleTypes.get(0);
    } else {
      // This address string could be more that one coin type so first check if this address
      // comes from an account to determine the type.
      List<WalletAccount> possibleAccounts = application.getAccounts(possibleTypes);
      Address addressOfAccount = null;
      for (WalletAccount account : possibleAccounts) {
        Address testAddress = new Address(account.getCoinType(), addressStr);
        if (account.isAddressMine(testAddress)) {
          addressOfAccount = testAddress;
          break;
        }
      }

      if (addressOfAccount != null) {
        // If address is from an account don't show a dialog. The type should not change as
        // we know 100% that is correct one
        setAddress(addressOfAccount, false);
        sendAmountType = (CoinType) addressOfAccount.getParameters();
      } else {
        // As a last resort let the use choose the correct coin type
        showPayToDialog(addressStr);
      }
    }
  }
 /**
  * Start polling for the market information of the current pair, if it is already stated this call
  * does nothing
  */
 private void startPolling(String pair) {
   if (timer == null) {
     ShapeShift shapeShift = application.getShapeShift();
     pollTask = new MyMarketInfoPollTask(handler, shapeShift, pair);
     timer = new Timer();
     timer.schedule(pollTask, 0, Constants.RATE_UPDATE_FREQ_MS);
   }
 }
 @Override
 public void onAttach(Activity activity) {
   super.onAttach(activity);
   try {
     this.listener = (Listener) activity;
     this.application = (WalletApplication) activity.getApplication();
     this.config = application.getConfiguration();
     this.resolver = activity.getContentResolver();
     this.loaderManager = getLoaderManager();
   } catch (ClassCastException e) {
     throw new ClassCastException(activity.toString() + " must implement " + Listener.class);
   }
 }
 private void handleSendConfirm() {
   if (!everythingValid()) { // Sanity check
     log.error("Unexpected validity failure.");
     validateAmount();
     validateAddress();
     validateTxMessage();
     return;
   }
   state = State.PREPARATION;
   updateView();
   if (application.getWallet() != null) {
     onMakeTransaction(address, sendAmount, getTxMessage());
   }
 }
  private void processUri(String uri) throws CoinURIParseException {
    CoinURI coinUri = new CoinURI(uri);
    CoinType scannedType = coinUri.getType();

    if (!Constants.SUPPORTED_COINS.contains(scannedType)) {
      String error = getResources().getString(R.string.unsupported_coin, scannedType.getName());
      throw new CoinURIParseException(error);
    }

    setUri(coinUri);

    if (pocket == null) {
      List<WalletAccount> allAccounts = application.getAllAccounts();
      List<WalletAccount> sendFromAccounts = application.getAccounts(coinUri.getType());
      if (sendFromAccounts.size() == 1) {
        pocket = sendFromAccounts.get(0);
      } else if (allAccounts.size() == 1) {
        pocket = allAccounts.get(0);
      } else {
        throw new CoinURIParseException("No default account found");
      }
    }
  }
 @Override
 public Loader<Cursor> onCreateLoader(final int id, final Bundle args) {
   final String constraint = args != null ? args.getString("constraint") : null;
   // TODO support addresses from other accounts
   Uri uri =
       AddressBookProvider.contentUri(application.getPackageName(), pocket.getCoinType());
   return new CursorLoader(
       application,
       uri,
       null,
       AddressBookProvider.SELECTION_QUERY,
       new String[] {constraint != null ? constraint : ""},
       null);
 }
        @Override
        public void onClick(View v) {
          if (address != null) {
            final boolean showChangeType =
                addressTypeCanChange && GenericUtils.hasMultipleTypes(address);
            ActionMode.Callback callback =
                new UiUtils.AddressActionModeCallback(
                    address, application.getApplicationContext(), getFragmentManager()) {

                  @Override
                  public boolean onCreateActionMode(ActionMode mode, Menu menu) {
                    mode.getMenuInflater().inflate(R.menu.address_options_extra, menu);
                    return super.onCreateActionMode(mode, menu);
                  }

                  @Override
                  public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
                    menu.findItem(R.id.action_change_address_type).setVisible(showChangeType);
                    return super.onPrepareActionMode(mode, menu);
                  }

                  @Override
                  public boolean onActionItemClicked(ActionMode mode, MenuItem menuItem) {
                    switch (menuItem.getItemId()) {
                      case R.id.action_change_address_type:
                        showPayToDialog(getAddress().toString());
                        mode.finish();
                        return true;
                    }
                    return super.onActionItemClicked(mode, menuItem);
                  }
                };
            actionMode = UiUtils.startActionMode(getActivity(), callback);
            // Hack to dismiss this action mode when back is pressed
            if (listener != null && listener instanceof WalletActivity) {
              ((WalletActivity) listener).registerActionMode(actionMode);
            }
          }
        }