Java tutorial
/* * Copyright 2011-2014 the original author or authors. * * 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 com.kncwallet.wallet.ui; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.content.SharedPreferences.OnSharedPreferenceChangeListener; import android.graphics.Bitmap; import android.nfc.NfcManager; import android.os.Bundle; import android.preference.PreferenceManager; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.View.OnLongClickListener; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; import com.google.bitcoin.core.Address; import com.google.bitcoin.uri.BitcoinURI; import com.kncwallet.wallet.Constants; import com.kncwallet.wallet.WalletApplication; import com.kncwallet.wallet.util.BitmapFragment; import com.kncwallet.wallet.util.Nfc; import com.kncwallet.wallet.util.Qr; import com.kncwallet.wallet.util.WalletUtils; import com.kncwallet.wallet.R; /** * @author Andreas Schildbach */ public final class WalletAddressFragment extends Fragment { private FragmentActivity activity; private WalletApplication application; private SharedPreferences prefs; private NfcManager nfcManager; private View bitcoinAddressButton; private TextView bitcoinAddressLabel; private ImageView bitcoinAddressQrView; private Address lastSelectedAddress; private Bitmap qrCodeBitmap; @Override public void onAttach(final Activity activity) { super.onAttach(activity); this.activity = (FragmentActivity) activity; this.application = (WalletApplication) activity.getApplication(); this.prefs = PreferenceManager.getDefaultSharedPreferences(activity); this.nfcManager = (NfcManager) activity.getSystemService(Context.NFC_SERVICE); } @Override public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) { final View view = inflater.inflate(R.layout.wallet_address_fragment, container, false); bitcoinAddressLabel = (TextView) view.findViewById(R.id.bitcoin_address_label); bitcoinAddressQrView = (ImageView) view.findViewById(R.id.bitcoin_address_qr); bitcoinAddressQrView.setOnClickListener(new OnClickListener() { @Override public void onClick(final View v) { handleShowQRCode(); } }); return view; } @Override public void onResume() { super.onResume(); prefs.registerOnSharedPreferenceChangeListener(prefsListener); updateView(); } @Override public void onPause() { prefs.unregisterOnSharedPreferenceChangeListener(prefsListener); Nfc.unpublish(nfcManager, getActivity()); super.onPause(); } private void updateView() { final Address selectedAddress = application.determineSelectedAddress(); if (!selectedAddress.equals(lastSelectedAddress)) { lastSelectedAddress = selectedAddress; bitcoinAddressLabel.setText(WalletUtils.formatAddress(selectedAddress, Constants.ADDRESS_FORMAT_GROUP_SIZE, Constants.ADDRESS_FORMAT_LINE_SIZE)); final String addressStr = BitcoinURI.convertToBitcoinURI(selectedAddress, null, null, null); final int size = (int) (256 * getResources().getDisplayMetrics().density); qrCodeBitmap = Qr.bitmap(addressStr, size); bitcoinAddressQrView.setImageBitmap(qrCodeBitmap); Nfc.publishUri(nfcManager, getActivity(), addressStr); } } private void handleShowQRCode() { BitmapFragment.show(getFragmentManager(), qrCodeBitmap); } private final OnSharedPreferenceChangeListener prefsListener = new OnSharedPreferenceChangeListener() { @Override public void onSharedPreferenceChanged(final SharedPreferences sharedPreferences, final String key) { if (Constants.PREFS_KEY_SELECTED_ADDRESS.equals(key)) updateView(); } }; }