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 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.ui;
import java.util.List;
import org.bitcoinj.core.Transaction;
import org.bitcoinj.core.Wallet;
import org.bitcoinj.wallet.DeterministicUpgradeRequiresPassword;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import com.google.common.util.concurrent.ListenableFuture;
import de.schildbach.wallet.WalletApplication;
import de.schildbach.wallet.service.BlockchainService;
import de.schildbach.wallet.service.BlockchainState;
import de.schildbach.wallet.ui.send.MaintenanceDialogFragment;
/**
* @author Andreas Schildbach
*/publicclass MaybeMaintenanceFragment extends Fragment
{
privatestaticfinal String FRAGMENT_TAG = MaybeMaintenanceFragment.class.getName();
publicstaticvoid add(final FragmentManager fm)
{
Fragment fragment = fm.findFragmentByTag(FRAGMENT_TAG);
if (fragment == null)
{
fragment = new MaybeMaintenanceFragment();
fm.beginTransaction().add(fragment, FRAGMENT_TAG).commit();
}
}
private Wallet wallet;
private LocalBroadcastManager broadcastManager;
privateboolean dialogWasShown = false;
@Override
publicvoid onAttach(final Activity activity)
{
super.onAttach(activity);
final WalletApplication application = ((AbstractWalletActivity) activity).getWalletApplication();
this.wallet = application.getWallet();
this.broadcastManager = LocalBroadcastManager.getInstance(activity);
}
@Override
publicvoid onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
@Override
publicvoid onResume()
{
super.onResume();
broadcastManager.registerReceiver(broadcastReceiver, new IntentFilter(BlockchainService.ACTION_BLOCKCHAIN_STATE));
}
@Override
publicvoid onPause()
{
broadcastManager.unregisterReceiver(broadcastReceiver);
super.onPause();
}
privatefinal BroadcastReceiver broadcastReceiver = new BroadcastReceiver()
{
@Override
publicvoid onReceive(final Context context, final Intent broadcast)
{
final BlockchainState blockchainState = BlockchainState.fromIntent(broadcast);
if (!dialogWasShown && !blockchainState.replaying && maintenanceRecommended())
{
MaintenanceDialogFragment.show(getFragmentManager());
dialogWasShown = true;
}
}
};
privateboolean maintenanceRecommended()
{
try
{
final ListenableFuture<List<Transaction>> result = wallet.doMaintenance(null, false);
return !result.get().isEmpty();
}
catch (final DeterministicUpgradeRequiresPassword x)
{
return true;
}
catch (final Exception x)
{
thrownew RuntimeException(x);
}
}
}