Example usage for android.text.format DateUtils isToday

List of usage examples for android.text.format DateUtils isToday

Introduction

In this page you can find the example usage for android.text.format DateUtils isToday.

Prototype

public static boolean isToday(long when) 

Source Link

Usage

From source file:com.gumgoose.app.quakebuddy.QuakeAdapter.java

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // Check if the existing view is being reused, otherwise inflate the view
    View listItemView = convertView;
    if (listItemView == null) {
        listItemView = LayoutInflater.from(getContext()).inflate(R.layout.earthquake_list_item, parent, false);
    }/*from w  ww.j  a v  a 2  s. co m*/

    // Initiate user's shared preferences for customising the ListView item
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());

    // Get the Quake object at this position in the list
    Quake currentEarthquake = getItem(position);

    // Get the original location String from the Quake object
    String originalLocation = currentEarthquake.getQuakeLocation();

    // If the original location String (i.e. "10km SSW of Basilisa, Philippines") contains
    // a primary location (i.e. "Basilisa, Philippines") and an offset (i.e. "10km SSW of "),
    // store the primary location and the offset separately into 2 different Strings
    String primaryLocation;
    String locationOffset;

    // Check whether the originalLocation String contains the " of " text
    if (originalLocation.contains(LOCATION_SEPARATOR)) {
        // Split the String into different parts based on the " of " text;
        // String #1 shows as "10km SSW" and String #2 shows as "Basilisa, Philippines"
        String[] parts = originalLocation.split(LOCATION_SEPARATOR);
        // Location offset should be "10km SSW" + " of " --> "10km SSW of "
        locationOffset = parts[0] + LOCATION_SEPARATOR;
        // Primary location should be "Basilisa, Philippines"
        primaryLocation = parts[1];
    } else {
        // No " of " text detected in the originalLocation String, instead display "Near the"
        locationOffset = getContext().getString(R.string.near_the);
        // The primary location will be the full location String
        primaryLocation = originalLocation;
    }

    // Find a reference to the list_location_city TextView
    TextView primaryTextView = (TextView) listItemView.findViewById(R.id.list_location_city);
    // Populate the primaryTextView with the earthquake's location
    primaryTextView.setText(primaryLocation);

    // Find a reference to the list_location_heading TextView
    TextView secondaryTextView = (TextView) listItemView.findViewById(R.id.list_location_heading);
    // Change the secondaryTextView color to textColorEarthquakeDetails
    secondaryTextView.setTextColor(ContextCompat.getColor(getContext(), R.color.textColorEarthquakeDetails));
    // Populate the secondaryTextView with the earthquake's location offset
    secondaryTextView.setText(locationOffset);

    // Find a reference to the list_magnitude TextView
    TextView magnitudeView = (TextView) listItemView.findViewById(R.id.list_magnitude);
    // Format the magnitude to show 1 decimal place
    String formattedMagnitude = formatMagnitude(currentEarthquake.getQuakeMagnitude());
    // Populate the magnitudeView with the earthquake's magnitude
    magnitudeView.setText(formattedMagnitude);

    // Find a reference to the list_time TextView
    TextView thirdView = (TextView) listItemView.findViewById(R.id.list_time);
    // Find a reference to the list_date TextView
    TextView fourthView = (TextView) listItemView.findViewById(R.id.list_date);
    // Obtain user's preference on how to display the earthquake times
    String timePref = prefs.getString("time_preference", "ago");

    if (timePref.equals("ago")) {
        // Populate the thirdView TextView with the earthquake time in "Ago" format
        thirdView.setText(getTimeAgo(currentEarthquake.getQuakeUnixTime()));
        // Empty the fourthView TextView
        fourthView.setText("");
    } else {
        // Populate the thirdView TextView with the earthquake time
        thirdView.setText(getStandardTime(currentEarthquake.getQuakeUnixTime()));
        // Populate the fourthView TextView with the earthquake date
        fourthView.setText(getDate(currentEarthquake.getQuakeUnixTime()));
    }

    // Obtain user's preference on whether to display 'Tsunami Watch' theme
    String tsunamiPref = prefs.getString("tsunami_watch_theme", "enabled");
    // Find a reference to the drawable circle object
    GradientDrawable magnitudeCircle = (GradientDrawable) magnitudeView.getBackground();

    if (tsunamiPref.equals("enabled") && currentEarthquake.getTsunamiWarning() == 1) {
        // Display current Tsunami watches within the last 24 hours
        if (DateUtils.isToday(currentEarthquake.getQuakeUnixTime())) {
            // Change the circle color to black
            magnitudeCircle.setColor(Color.BLACK);
            // Change the secondaryTextView color to colorAccent
            secondaryTextView.setTextColor(ContextCompat.getColor(getContext(), R.color.colorAccent));
            // Populate the secondaryTextView to "Check for Tsunamis"
            secondaryTextView.setText(R.string.tsunami_header);
        }
    } else {
        // Reference the circle color with respect to the magnitude
        int magnitudeColor = getMagnitudeColor(currentEarthquake.getQuakeMagnitude());
        // Change the circle color
        magnitudeCircle.setColor(magnitudeColor);
    }
    // Return the whole list item layout so it can be displayed
    return listItemView;
}

From source file:com.feathercoin.wallet.feathercoin.ui.TransactionsListFragment.java

@Override
public void onListItemClick(final ListView l, final View v, final int position, final long id) {
    final Transaction tx = (Transaction) adapter.getItem(position);

    activity.startActionMode(new ActionMode.Callback() {
        private Address address;

        public boolean onCreateActionMode(final ActionMode mode, final Menu menu) {
            final MenuInflater inflater = mode.getMenuInflater();
            inflater.inflate(R.menu.wallet_transactions_context, menu);
            menu.findItem(R.id.wallet_transactions_context_show_transaction)
                    .setVisible(prefs.getBoolean(Constants.PREFS_KEY_LABS_TRANSACTION_DETAILS, false));

            return true;
        }//from   w  w  w .j a  v  a  2s.  co  m

        public boolean onPrepareActionMode(final ActionMode mode, final Menu menu) {
            try {
                final Date time = tx.getUpdateTime();
                final DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(activity);
                final DateFormat timeFormat = android.text.format.DateFormat.getTimeFormat(activity);

                mode.setTitle(time != null ? (DateUtils.isToday(time.getTime()) ? getString(R.string.time_today)
                        : dateFormat.format(time)) + ", " + timeFormat.format(time) : null);

                final BigInteger value = tx.getValue(wallet);
                final boolean sent = value.signum() < 0;

                address = sent ? WalletUtils.getToAddress(tx) : WalletUtils.getFromAddress(tx);

                final String label;
                if (tx.isCoinBase())
                    label = getString(R.string.wallet_transactions_fragment_coinbase);
                else if (address != null)
                    label = AddressBookProvider.resolveLabel(activity, address.toString());
                else
                    label = "?";

                final String prefix = getString(sent ? R.string.symbol_to : R.string.symbol_from) + " ";

                mode.setSubtitle(label != null ? prefix + label
                        : WalletUtils.formatAddress(prefix, address, Constants.ADDRESS_FORMAT_GROUP_SIZE,
                                Constants.ADDRESS_FORMAT_LINE_SIZE));

                menu.findItem(R.id.wallet_transactions_context_edit_address).setVisible(address != null);

                return true;
            } catch (final ScriptException x) {
                return false;
            }
        }

        public boolean onActionItemClicked(final ActionMode mode, final MenuItem item) {
            switch (item.getItemId()) {
            case R.id.wallet_transactions_context_edit_address:
                handleEditAddress(tx);

                mode.finish();
                return true;

            case R.id.wallet_transactions_context_show_transaction:
                TransactionActivity.show(activity, tx);

                mode.finish();
                return true;
            }
            return false;
        }

        public void onDestroyActionMode(final ActionMode mode) {
        }

        private void handleEditAddress(final Transaction tx) {
            EditAddressBookEntryFragment.edit(getFragmentManager(), address.toString());
        }
    });
}

From source file:nodomain.freeyourgadget.gadgetbridge.service.devices.miband2.operations.FetchActivityOperation.java

private boolean needsAnotherFetch(GregorianCalendar lastSyncTimestamp) {
    if (fetchCount > 5) {
        LOG.warn("Already jave 5 fetch rounds, not doing another one.");
        return false;
    }/*from   w  ww.  j a v  a  2 s.c  o m*/

    if (DateUtils.isToday(lastSyncTimestamp.getTimeInMillis())) {
        LOG.info("Hopefully no further fetch needed, last synced timestamp is from today.");
        return false;
    }
    if (lastSyncTimestamp.getTimeInMillis() > System.currentTimeMillis()) {
        LOG.warn("Not doing another fetch since last synced timestamp is in the future: "
                + DateTimeUtils.formatDateTime(lastSyncTimestamp.getTime()));
        return false;
    }
    LOG.info("Doing another fetch since last sync timestamp is still too old: "
            + DateTimeUtils.formatDateTime(lastSyncTimestamp.getTime()));
    return true;
}

From source file:com.giovanniterlingen.windesheim.view.ScheduleActivity.java

@Override
public void onResume() {
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.scheduleReload);
    if (!DateUtils.isToday(onPauseMillis)) {
        setViewPager();/*from  ww  w.  java2s .  c om*/
    }
    super.onResume();
}

From source file:de.schildbach.wallet.ui.TransactionsListFragment.java

private void handleTransactionClick(final Transaction tx) {
    activity.startActionMode(new ActionMode.Callback() {
        private Address address;

        public boolean onCreateActionMode(final ActionMode mode, final Menu menu) {
            final MenuInflater inflater = mode.getMenuInflater();
            inflater.inflate(R.menu.wallet_transactions_context, menu);
            menu.findItem(R.id.wallet_transactions_context_show_transaction)
                    .setVisible(prefs.getBoolean(Constants.PREFS_KEY_LABS_TRANSACTION_DETAILS, false));

            return true;
        }//  w  ww  .  ja  v a2s. c om

        public boolean onPrepareActionMode(final ActionMode mode, final Menu menu) {
            try {
                final Date time = tx.getUpdateTime();
                final DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(activity);
                final DateFormat timeFormat = android.text.format.DateFormat.getTimeFormat(activity);

                mode.setTitle(time != null ? (DateUtils.isToday(time.getTime()) ? getString(R.string.time_today)
                        : dateFormat.format(time)) + ", " + timeFormat.format(time) : null);

                final BigInteger value = tx.getValue(wallet);
                final boolean sent = value.signum() < 0;

                address = sent ? WalletUtils.getToAddress(tx) : WalletUtils.getFromAddress(tx);

                final String label;
                if (tx.isCoinBase())
                    label = getString(R.string.wallet_transactions_fragment_coinbase);
                else if (address != null)
                    label = AddressBookProvider.resolveLabel(activity, address.toString());
                else
                    label = "?";

                final String prefix = getString(sent ? R.string.symbol_to : R.string.symbol_from) + " ";

                mode.setSubtitle(label != null ? prefix + label
                        : WalletUtils.formatAddress(prefix, address, Constants.ADDRESS_FORMAT_GROUP_SIZE,
                                Constants.ADDRESS_FORMAT_LINE_SIZE));

                menu.findItem(R.id.wallet_transactions_context_edit_address).setVisible(address != null);

                return true;
            } catch (final ScriptException x) {
                return false;
            }
        }

        public boolean onActionItemClicked(final ActionMode mode, final MenuItem item) {
            switch (item.getItemId()) {
            case R.id.wallet_transactions_context_edit_address:
                handleEditAddress(tx);

                mode.finish();
                return true;

            case R.id.wallet_transactions_context_show_transaction:
                TransactionActivity.show(activity, tx);

                mode.finish();
                return true;
            }
            return false;
        }

        public void onDestroyActionMode(final ActionMode mode) {
        }

        private void handleEditAddress(final Transaction tx) {
            EditAddressBookEntryFragment.edit(getFragmentManager(), address.toString());
        }
    });
}

From source file:biz.wiz.android.wallet.ui.TransactionsListFragment.java

private void handleTransactionClick(@Nonnull final Transaction tx) {
    activity.startActionMode(new ActionMode.Callback() {
        private Address address;
        private byte[] serializedTx;

        private static final int SHOW_QR_THRESHOLD_BYTES = 2500;

        @Override/*w  w  w  .j  a va  2 s .  c o  m*/
        public boolean onCreateActionMode(final ActionMode mode, final Menu menu) {
            final MenuInflater inflater = mode.getMenuInflater();
            inflater.inflate(R.menu.wallet_transactions_context, menu);

            return true;
        }

        @Override
        public boolean onPrepareActionMode(final ActionMode mode, final Menu menu) {
            try {
                final Date time = tx.getUpdateTime();
                final DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(activity);
                final DateFormat timeFormat = android.text.format.DateFormat.getTimeFormat(activity);

                mode.setTitle(time != null ? (DateUtils.isToday(time.getTime()) ? getString(R.string.time_today)
                        : dateFormat.format(time)) + ", " + timeFormat.format(time) : null);

                final Coin value = tx.getValue(wallet);
                final boolean sent = value.signum() < 0;

                address = sent ? WalletUtils.getWalletAddressOfReceived(tx, wallet)
                        : WalletUtils.getFirstFromAddress(tx);

                final String label;
                if (tx.isCoinBase())
                    label = getString(R.string.wallet_transactions_fragment_coinbase);
                else if (address != null)
                    label = AddressBookProvider.resolveLabel(activity, address.toString());
                else
                    label = "?";

                final String prefix = getString(sent ? R.string.symbol_to : R.string.symbol_from) + " ";

                if (tx.getPurpose() != Purpose.KEY_ROTATION)
                    mode.setSubtitle(label != null ? prefix + label
                            : WalletUtils.formatAddress(prefix, address, Constants.ADDRESS_FORMAT_GROUP_SIZE,
                                    Constants.ADDRESS_FORMAT_LINE_SIZE));
                else
                    mode.setSubtitle(null);

                menu.findItem(R.id.wallet_transactions_context_edit_address).setVisible(address != null);

                serializedTx = tx.unsafeBitcoinSerialize();

                menu.findItem(R.id.wallet_transactions_context_show_qr)
                        .setVisible(serializedTx.length < SHOW_QR_THRESHOLD_BYTES);

                return true;
            } catch (final ScriptException x) {
                return false;
            }
        }

        @Override
        public boolean onActionItemClicked(final ActionMode mode, final MenuItem item) {
            switch (item.getItemId()) {
            case R.id.wallet_transactions_context_edit_address:
                handleEditAddress(tx);

                mode.finish();
                return true;

            case R.id.wallet_transactions_context_show_qr:
                handleShowQr();

                mode.finish();
                return true;

            case R.id.wallet_transactions_context_browse:
                startActivity(new Intent(Intent.ACTION_VIEW,
                        Uri.parse(Constants.EXPLORE_BASE_URL + "tx/" + tx.getHashAsString())));

                mode.finish();
                return true;
            }
            return false;
        }

        @Override
        public void onDestroyActionMode(final ActionMode mode) {
        }

        private void handleEditAddress(@Nonnull final Transaction tx) {
            EditAddressBookEntryFragment.edit(getFragmentManager(), address.toString());
        }

        private void handleShowQr() {
            final int size = getResources().getDimensionPixelSize(R.dimen.bitmap_dialog_qr_size);
            final Bitmap qrCodeBitmap = Qr.bitmap(Qr.encodeCompressBinary(serializedTx), size);
            BitmapFragment.show(getFragmentManager(), qrCodeBitmap);
        }
    });
}

From source file:piuk.blockchain.android.ui.WalletTransactionsFragment.java

public void initAdapter() {
    adapter = new ArrayAdapter<Transaction>(activity, 0) {
        final DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(activity);
        final DateFormat timeFormat = android.text.format.DateFormat.getTimeFormat(activity);
        final int colorSignificant = getResources().getColor(R.color.significant);
        final int colorInsignificant = getResources().getColor(R.color.insignificant);
        final int colorSent = getResources().getColor(R.color.color_sent);
        final int colorReceived = getResources().getColor(R.color.color_received);

        @Override/* ww w  .  j  a va  2 s.c om*/
        public View getView(final int position, View row, final ViewGroup parent) {
            if (row == null)
                row = getLayoutInflater(null).inflate(R.layout.transaction_row, null);

            final CurrencyAmountView rowValue = (CurrencyAmountView) row
                    .findViewById(R.id.transaction_row_value);

            final TextView rowLabel = (TextView) row.findViewById(R.id.transaction_row_address);

            final TextView rowTime = (TextView) row.findViewById(R.id.transaction_row_time);

            try {
                synchronized (adapter) {
                    final Transaction tx = getItem(position);

                    if (tx.getHash() == null) {
                        rowLabel.setTextColor(Color.BLACK);
                        rowLabel.setText(R.string.no_transactions);
                        rowLabel.setTypeface(Typeface.DEFAULT);
                        rowLabel.setTextColor(Color.BLACK);

                        rowTime.setVisibility(View.INVISIBLE);
                        rowValue.setVisibility(View.INVISIBLE);
                        rowLabel.setGravity(Gravity.CENTER);

                        return row;
                    } else {

                        rowTime.setVisibility(View.VISIBLE);
                        rowValue.setVisibility(View.VISIBLE);
                        rowLabel.setGravity(Gravity.LEFT);

                        final TransactionConfidence confidence = tx.getConfidence();
                        final ConfidenceType confidenceType = confidence.getConfidenceType();

                        try {
                            BigInteger value = null;

                            if (tx instanceof MyTransaction) {
                                value = ((MyTransaction) tx).getResult();
                            } else {
                                value = tx.getValue(application.bitcoinjWallet);
                            }

                            final boolean sent = value.signum() < 0;

                            final int textColor;
                            if (confidenceType == ConfidenceType.NOT_SEEN_IN_CHAIN) {
                                textColor = colorInsignificant;
                            } else if (confidenceType == ConfidenceType.BUILDING) {
                                textColor = colorSignificant;
                            } else if (confidenceType == ConfidenceType.NOT_IN_BEST_CHAIN) {
                                textColor = colorSignificant;
                            } else if (confidenceType == ConfidenceType.DEAD) {
                                textColor = Color.RED;
                            } else {
                                textColor = colorInsignificant;
                            }

                            final String address;
                            if (sent)
                                if (tx.getOutputs().size() == 0)
                                    address = "Unknown";
                                else
                                    address = tx.getOutputs().get(0).getScriptPubKey().getToAddress()
                                            .toString();
                            else if (tx.getInputs().size() == 0)
                                address = "Generation";
                            else
                                address = tx.getInputs().get(0).getFromAddress().toString();

                            String label = null;
                            if (tx instanceof MyTransaction && ((MyTransaction) tx).getTag() != null)
                                label = ((MyTransaction) tx).getTag();
                            else
                                label = AddressBookProvider.resolveLabel(activity.getContentResolver(),
                                        address);

                            final Date time = tx.getUpdateTime();
                            rowTime.setText(
                                    time != null ? (DateUtils.isToday(time.getTime()) ? timeFormat.format(time)
                                            : dateFormat.format(time)) : null);
                            rowTime.setTextColor(textColor);

                            rowLabel.setTextColor(textColor);
                            rowLabel.setText(label != null ? label : address);
                            rowLabel.setTypeface(label != null ? Typeface.DEFAULT : Typeface.MONOSPACE);

                            rowValue.setCurrencyCode(null);
                            rowValue.setAmountSigned(true);
                            rowValue.setTextColor(textColor);
                            rowValue.setAmount(value);

                            if (sent) {
                                rowValue.setTextColor(colorSent);
                            } else {
                                rowValue.setTextColor(colorReceived);
                            }

                            return row;
                        } catch (final ScriptException x) {
                            throw new RuntimeException(x);
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();

                rowLabel.setTextColor(Color.BLACK);
                rowLabel.setText(R.string.unknown_error);
                rowLabel.setTypeface(Typeface.DEFAULT);
                rowLabel.setTextColor(Color.BLACK);

                rowLabel.setGravity(Gravity.CENTER);
                rowTime.setVisibility(View.INVISIBLE);
                rowValue.setVisibility(View.INVISIBLE);

                return row;
            }
        }
    };

    setAdapterContent();

    setListAdapter(adapter);
}

From source file:de.schildbach.litecoinwallet.ui.TransactionsListFragment.java

private void handleTransactionClick(@Nonnull final Transaction tx) {
    activity.startActionMode(new ActionMode.Callback() {
        private Address address;
        private byte[] serializedTx;

        private static final int SHOW_QR_THRESHOLD_BYTES = 2500;

        @Override/*from  ww w.  ja v  a2  s . c  om*/
        public boolean onCreateActionMode(final ActionMode mode, final Menu menu) {
            final MenuInflater inflater = mode.getMenuInflater();
            inflater.inflate(R.menu.wallet_transactions_context, menu);

            return true;
        }

        @Override
        public boolean onPrepareActionMode(final ActionMode mode, final Menu menu) {
            try {
                final Date time = tx.getUpdateTime();
                final DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(activity);
                final DateFormat timeFormat = android.text.format.DateFormat.getTimeFormat(activity);

                mode.setTitle(time != null ? (DateUtils.isToday(time.getTime()) ? getString(R.string.time_today)
                        : dateFormat.format(time)) + ", " + timeFormat.format(time) : null);

                final BigInteger value = tx.getValue(wallet);
                final boolean sent = value.signum() < 0;

                address = sent ? WalletUtils.getFirstToAddress(tx) : WalletUtils.getFirstFromAddress(tx);

                final String label;
                if (tx.isCoinBase())
                    label = getString(R.string.wallet_transactions_fragment_coinbase);
                else if (address != null)
                    label = AddressBookProvider.resolveLabel(activity, address.toString());
                else
                    label = "?";

                final String prefix = getString(sent ? R.string.symbol_to : R.string.symbol_from) + " ";

                if (tx.getPurpose() != Purpose.KEY_ROTATION)
                    mode.setSubtitle(label != null ? prefix + label
                            : WalletUtils.formatAddress(prefix, address, Constants.ADDRESS_FORMAT_GROUP_SIZE,
                                    Constants.ADDRESS_FORMAT_LINE_SIZE));
                else
                    mode.setSubtitle(null);

                menu.findItem(R.id.wallet_transactions_context_edit_address).setVisible(address != null);

                serializedTx = tx.unsafeBitcoinSerialize();

                menu.findItem(R.id.wallet_transactions_context_show_qr)
                        .setVisible(serializedTx.length < SHOW_QR_THRESHOLD_BYTES);

                Nfc.publishMimeObject(nfcManager, activity, Constants.MIMETYPE_TRANSACTION, serializedTx,
                        false);

                return true;
            } catch (final ScriptException x) {
                return false;
            }
        }

        @Override
        public boolean onActionItemClicked(final ActionMode mode, final MenuItem item) {
            switch (item.getItemId()) {
            case R.id.wallet_transactions_context_edit_address:
                handleEditAddress(tx);

                mode.finish();
                return true;

            case R.id.wallet_transactions_context_show_qr:
                handleShowQr();

                mode.finish();
                return true;

            case R.id.wallet_transactions_context_open_blockexplorer:
                startActivity(new Intent(Intent.ACTION_VIEW,
                        Uri.parse(Constants.BLOCKEXPLORER_BASE_URL + "tx/" + tx.getHashAsString())));

                mode.finish();
                return true;
            }
            return false;
        }

        @Override
        public void onDestroyActionMode(final ActionMode mode) {
            Nfc.unpublish(nfcManager, activity);
        }

        private void handleEditAddress(@Nonnull final Transaction tx) {
            EditAddressBookEntryFragment.edit(getFragmentManager(), address.toString());
        }

        private void handleShowQr() {
            final int size = (int) (384 * getResources().getDisplayMetrics().density);
            final Bitmap qrCodeBitmap = Qr.bitmap(Qr.encodeBinary(serializedTx), size);
            BitmapFragment.show(getFragmentManager(), qrCodeBitmap);
        }
    });
}

From source file:devcoin.wallet.ui.TransactionsListFragment.java

private void handleTransactionClick(@Nonnull final Transaction tx) {
    activity.startActionMode(new ActionMode.Callback() {
        private Address address;
        private byte[] serializedTx;

        private static final int SHOW_QR_THRESHOLD_BYTES = 2500;

        @Override/*ww w.j a  va2s . c  o m*/
        public boolean onCreateActionMode(final ActionMode mode, final Menu menu) {
            final MenuInflater inflater = mode.getMenuInflater();
            inflater.inflate(R.menu.wallet_transactions_context, menu);

            return true;
        }

        @Override
        public boolean onPrepareActionMode(final ActionMode mode, final Menu menu) {
            try {
                final Date time = tx.getUpdateTime();
                final DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(activity);
                final DateFormat timeFormat = android.text.format.DateFormat.getTimeFormat(activity);

                mode.setTitle(time != null ? (DateUtils.isToday(time.getTime()) ? getString(R.string.time_today)
                        : dateFormat.format(time)) + ", " + timeFormat.format(time) : null);

                final BigInteger value = tx.getValue(wallet);
                final boolean sent = value.signum() < 0;

                address = sent ? WalletUtils.getFirstToAddress(tx) : WalletUtils.getFirstFromAddress(tx);

                final String label;
                if (tx.isCoinBase())
                    label = getString(R.string.wallet_transactions_fragment_coinbase);
                else if (address != null)
                    label = AddressBookProvider.resolveLabel(activity, address.toString());
                else
                    label = "?";

                final String prefix = getString(sent ? R.string.symbol_to : R.string.symbol_from) + " ";

                if (tx.getPurpose() != Purpose.KEY_ROTATION)
                    mode.setSubtitle(label != null ? prefix + label
                            : WalletUtils.formatAddress(prefix, address, Constants.ADDRESS_FORMAT_GROUP_SIZE,
                                    Constants.ADDRESS_FORMAT_LINE_SIZE));
                else
                    mode.setSubtitle(null);

                menu.findItem(R.id.wallet_transactions_context_edit_address).setVisible(address != null);

                serializedTx = tx.unsafeBitcoinSerialize();

                menu.findItem(R.id.wallet_transactions_context_show_qr)
                        .setVisible(serializedTx.length < SHOW_QR_THRESHOLD_BYTES);

                Nfc.publishMimeObject(nfcManager, activity, Constants.MIMETYPE_TRANSACTION, serializedTx,
                        false);

                return true;
            } catch (final ScriptException x) {
                return false;
            }
        }

        @Override
        public boolean onActionItemClicked(final ActionMode mode, final MenuItem item) {
            switch (item.getItemId()) {
            case R.id.wallet_transactions_context_edit_address:
                handleEditAddress(tx);

                mode.finish();
                return true;

            case R.id.wallet_transactions_context_show_qr:
                handleShowQr();

                mode.finish();
                return true;

            case R.id.wallet_transactions_context_browse:
                startActivity(new Intent(Intent.ACTION_VIEW,
                        Uri.parse(Constants.EXPLORE_BASE_URL + "tx/" + tx.getHashAsString())));

                mode.finish();
                return true;
            }
            return false;
        }

        @Override
        public void onDestroyActionMode(final ActionMode mode) {
            Nfc.unpublish(nfcManager, activity);
        }

        private void handleEditAddress(@Nonnull final Transaction tx) {
            EditAddressBookEntryFragment.edit(getFragmentManager(), address.toString());
        }

        private void handleShowQr() {
            final int size = (int) (384 * getResources().getDisplayMetrics().density);
            final Bitmap qrCodeBitmap = Qr.bitmap(Qr.encodeBinary(serializedTx), size);
            BitmapFragment.show(getFragmentManager(), qrCodeBitmap);
        }
    });
}

From source file:hashengineering.digitalcoin.wallet.ui.TransactionsListFragment.java

private void handleTransactionClick(@Nonnull final Transaction tx) {
    activity.startActionMode(new ActionMode.Callback() {
        private Address address;
        private byte[] serializedTx;

        private static final int SHOW_QR_THRESHOLD_BYTES = 2500;

        @Override//from   www.  ja  v  a2s.  co  m
        public boolean onCreateActionMode(final ActionMode mode, final Menu menu) {
            final MenuInflater inflater = mode.getMenuInflater();
            inflater.inflate(R.menu.wallet_transactions_context, menu);

            return true;
        }

        @Override
        public boolean onPrepareActionMode(final ActionMode mode, final Menu menu) {
            try {
                final Date time = tx.getUpdateTime();
                final DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(activity);
                final DateFormat timeFormat = android.text.format.DateFormat.getTimeFormat(activity);

                mode.setTitle(time != null ? (DateUtils.isToday(time.getTime()) ? getString(R.string.time_today)
                        : dateFormat.format(time)) + ", " + timeFormat.format(time) : null);

                final BigInteger value = tx.getValue(wallet);
                final boolean sent = value.signum() < 0;

                address = sent ? WalletUtils.getToAddress(tx) : WalletUtils.getFromAddress(tx);

                final String label;
                if (tx.isCoinBase())
                    label = getString(R.string.wallet_transactions_fragment_coinbase);
                else if (address != null)
                    label = AddressBookProvider.resolveLabel(activity, address.toString());
                else
                    label = "?";

                final String prefix = getString(sent ? R.string.symbol_to : R.string.symbol_from) + " ";

                if (tx.getPurpose() != Purpose.KEY_ROTATION)
                    mode.setSubtitle(label != null ? prefix + label
                            : WalletUtils.formatAddress(prefix, address, Constants.ADDRESS_FORMAT_GROUP_SIZE,
                                    Constants.ADDRESS_FORMAT_LINE_SIZE));
                else
                    mode.setSubtitle(null);

                menu.findItem(R.id.wallet_transactions_context_edit_address).setVisible(address != null);

                serializedTx = tx.unsafeBitcoinSerialize();

                menu.findItem(R.id.wallet_transactions_context_show_qr)
                        .setVisible(serializedTx.length < SHOW_QR_THRESHOLD_BYTES);

                Nfc.publishMimeObject(nfcManager, activity, Constants.MIMETYPE_TRANSACTION, serializedTx,
                        false);

                return true;
            } catch (final ScriptException x) {
                return false;
            }
        }

        @Override
        public boolean onActionItemClicked(final ActionMode mode, final MenuItem item) {
            switch (item.getItemId()) {
            case R.id.wallet_transactions_context_edit_address:
                handleEditAddress(tx);

                mode.finish();
                return true;

            case R.id.wallet_transactions_context_show_qr:
                handleShowQr();

                mode.finish();
                return true;

            case R.id.wallet_transactions_context_open_blockexplorer:
                startActivity(new Intent(Intent.ACTION_VIEW,
                        Uri.parse(Constants.BLOCKEXPLORER_BASE_URL + "tx/" + tx.getHashAsString())));

                mode.finish();
                return true;
            }
            return false;
        }

        @Override
        public void onDestroyActionMode(final ActionMode mode) {
            Nfc.unpublish(nfcManager, activity);
        }

        private void handleEditAddress(@Nonnull final Transaction tx) {
            EditAddressBookEntryFragment.edit(getFragmentManager(), address.toString());
        }

        private void handleShowQr() {
            final int size = (int) (384 * getResources().getDisplayMetrics().density);
            final Bitmap qrCodeBitmap = Qr.bitmap(Qr.encodeBinary(serializedTx), size);
            BitmapFragment.show(getFragmentManager(), qrCodeBitmap);
        }
    });
}