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 2013-2014 the original author or authors.
*/*www.java2s.com*/
* 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.util;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;
import org.bitcoinj.core.Coin;
import org.bitcoinj.core.ECKey;
import org.bitcoinj.core.Transaction;
import org.bitcoinj.core.Wallet;
import org.bitcoinj.core.WalletEventListener;
import org.bitcoinj.script.Script;
import android.os.Handler;
/**
* @author Andreas Schildbach
*/publicabstractclass ThrottlingWalletChangeListener implements WalletEventListener
{
privatefinallong throttleMs;
privatefinalboolean coinsRelevant;
privatefinalboolean reorganizeRelevant;
privatefinalboolean confidenceRelevant;
privatefinal AtomicLong lastMessageTime = new AtomicLong(0);
privatefinal Handler handler = new Handler();
privatefinal AtomicBoolean relevant = new AtomicBoolean();
privatestaticfinallong DEFAULT_THROTTLE_MS = 500;
public ThrottlingWalletChangeListener()
{
this(DEFAULT_THROTTLE_MS);
}
public ThrottlingWalletChangeListener(finallong throttleMs)
{
this(throttleMs, true, true, true);
}
public ThrottlingWalletChangeListener(finalboolean coinsRelevant, finalboolean reorganizeRelevant, finalboolean confidenceRelevant)
{
this(DEFAULT_THROTTLE_MS, coinsRelevant, reorganizeRelevant, confidenceRelevant);
}
public ThrottlingWalletChangeListener(finallong throttleMs, finalboolean coinsRelevant, finalboolean reorganizeRelevant,
finalboolean confidenceRelevant)
{
this.throttleMs = throttleMs;
this.coinsRelevant = coinsRelevant;
this.reorganizeRelevant = reorganizeRelevant;
this.confidenceRelevant = confidenceRelevant;
}
@Override
publicfinalvoid onWalletChanged(final Wallet wallet)
{
if (relevant.getAndSet(false))
{
handler.removeCallbacksAndMessages(null);
finallong now = System.currentTimeMillis();
if (now - lastMessageTime.get() > throttleMs)
handler.post(runnable);
else
handler.postDelayed(runnable, throttleMs);
}
}
privatefinal Runnable runnable = new Runnable()
{
@Override
publicvoid run()
{
lastMessageTime.set(System.currentTimeMillis());
onThrottledWalletChanged();
}
};
publicvoid removeCallbacks()
{
handler.removeCallbacksAndMessages(null);
}
/** will be called back on UI thread */publicabstractvoid onThrottledWalletChanged();
@Override
publicvoid onCoinsReceived(final Wallet wallet, final Transaction tx, final Coin prevBalance, final Coin newBalance)
{
if (coinsRelevant)
relevant.set(true);
}
@Override
publicvoid onCoinsSent(final Wallet wallet, final Transaction tx, final Coin prevBalance, final Coin newBalance)
{
if (coinsRelevant)
relevant.set(true);
}
@Override
publicvoid onReorganize(final Wallet wallet)
{
if (reorganizeRelevant)
relevant.set(true);
}
@Override
publicvoid onTransactionConfidenceChanged(final Wallet wallet, final Transaction tx)
{
if (confidenceRelevant)
relevant.set(true);
}
@Override
publicvoid onKeysAdded(final List<ECKey> keys)
{
// swallow
}
@Override
publicvoid onScriptsAdded(final Wallet wallet, final List<Script> scripts)
{
// swallow
}
}