2017-08-15 01:38:07 -06:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2017 m2049r
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package com.m2049r.xmrwallet;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.os.Bundle;
|
2017-09-02 02:05:50 -06:00
|
|
|
import android.support.annotation.Nullable;
|
2017-08-23 14:55:47 -06:00
|
|
|
import android.support.constraint.ConstraintLayout;
|
2017-08-30 17:00:27 -06:00
|
|
|
import android.support.v4.app.Fragment;
|
2017-08-15 01:38:07 -06:00
|
|
|
import android.support.v7.widget.DividerItemDecoration;
|
|
|
|
import android.support.v7.widget.RecyclerView;
|
|
|
|
import android.util.Log;
|
|
|
|
import android.view.LayoutInflater;
|
2017-09-02 02:05:50 -06:00
|
|
|
import android.view.Menu;
|
|
|
|
import android.view.MenuInflater;
|
|
|
|
import android.view.MenuItem;
|
2017-08-15 01:38:07 -06:00
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
2017-08-18 05:51:24 -06:00
|
|
|
import android.widget.Button;
|
2017-08-15 01:38:07 -06:00
|
|
|
import android.widget.LinearLayout;
|
|
|
|
import android.widget.ProgressBar;
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
|
|
import com.m2049r.xmrwallet.layout.TransactionInfoAdapter;
|
|
|
|
import com.m2049r.xmrwallet.model.TransactionInfo;
|
|
|
|
import com.m2049r.xmrwallet.model.Wallet;
|
2017-08-31 01:04:32 -06:00
|
|
|
import com.m2049r.xmrwallet.util.Helper;
|
2017-08-15 01:38:07 -06:00
|
|
|
|
2017-08-16 11:39:53 -06:00
|
|
|
import java.text.NumberFormat;
|
2017-08-15 01:38:07 -06:00
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
public class WalletFragment extends Fragment implements TransactionInfoAdapter.OnInteractionListener {
|
|
|
|
private static final String TAG = "WalletFragment";
|
|
|
|
private TransactionInfoAdapter adapter;
|
2017-08-16 11:39:53 -06:00
|
|
|
private NumberFormat formatter = NumberFormat.getInstance();
|
2017-08-15 01:38:07 -06:00
|
|
|
|
2017-08-18 05:51:24 -06:00
|
|
|
TextView tvBalance;
|
2017-08-23 14:55:47 -06:00
|
|
|
TextView tvUnconfirmedAmount;
|
2017-08-18 05:51:24 -06:00
|
|
|
TextView tvBlockHeightProgress;
|
2017-08-23 14:55:47 -06:00
|
|
|
ConstraintLayout clProgress;
|
2017-08-18 05:51:24 -06:00
|
|
|
TextView tvProgress;
|
|
|
|
ProgressBar pbProgress;
|
2017-08-19 05:27:26 -06:00
|
|
|
Button bSend;
|
2017-08-18 05:51:24 -06:00
|
|
|
|
2017-09-02 02:05:50 -06:00
|
|
|
@Override
|
|
|
|
public void onCreate(@Nullable Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
setHasOptionsMenu(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
|
|
|
|
if (activityCallback.hasWallet())
|
2017-09-08 15:59:50 -06:00
|
|
|
inflater.inflate(R.menu.wallet_menu, menu);
|
2017-09-02 02:05:50 -06:00
|
|
|
super.onCreateOptionsMenu(menu, inflater);
|
|
|
|
}
|
|
|
|
|
2017-08-15 01:38:07 -06:00
|
|
|
@Override
|
|
|
|
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
|
|
|
Bundle savedInstanceState) {
|
|
|
|
View view = inflater.inflate(R.layout.wallet_fragment, container, false);
|
|
|
|
|
2017-08-18 05:51:24 -06:00
|
|
|
tvProgress = (TextView) view.findViewById(R.id.tvProgress);
|
|
|
|
pbProgress = (ProgressBar) view.findViewById(R.id.pbProgress);
|
2017-08-23 14:55:47 -06:00
|
|
|
clProgress = (ConstraintLayout) view.findViewById(R.id.clProgress);
|
2017-08-18 05:51:24 -06:00
|
|
|
tvBalance = (TextView) view.findViewById(R.id.tvBalance);
|
2017-09-08 15:59:50 -06:00
|
|
|
tvBalance.setText(getResources().getString(R.string.xmr_balance, Helper.getDisplayAmount(0)));
|
2017-08-23 14:55:47 -06:00
|
|
|
tvUnconfirmedAmount = (TextView) view.findViewById(R.id.tvUnconfirmedAmount);
|
2017-09-08 15:59:50 -06:00
|
|
|
tvUnconfirmedAmount.setText(getResources().getString(R.string.xmr_unconfirmed_amount, Helper.getDisplayAmount(0)));
|
2017-08-18 05:51:24 -06:00
|
|
|
tvBlockHeightProgress = (TextView) view.findViewById(R.id.tvBlockHeightProgress);
|
2017-08-19 05:27:26 -06:00
|
|
|
|
|
|
|
bSend = (Button) view.findViewById(R.id.bSend);
|
2017-08-18 05:51:24 -06:00
|
|
|
|
2017-08-15 01:38:07 -06:00
|
|
|
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.list);
|
|
|
|
|
2017-09-08 15:59:50 -06:00
|
|
|
this.adapter = new TransactionInfoAdapter(getActivity(), this);
|
2017-08-15 01:38:07 -06:00
|
|
|
recyclerView.setAdapter(adapter);
|
|
|
|
|
2017-08-18 05:51:24 -06:00
|
|
|
bSend.setOnClickListener(new View.OnClickListener()
|
|
|
|
|
|
|
|
{
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
activityCallback.onSendRequest();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-08-19 05:27:26 -06:00
|
|
|
if (activityCallback.isSynced()) {
|
|
|
|
onSynced();
|
|
|
|
}
|
|
|
|
|
|
|
|
// activityCallback.setTitle(getString(R.string.status_wallet_loading));
|
2017-08-15 01:38:07 -06:00
|
|
|
|
2017-08-18 05:51:24 -06:00
|
|
|
activityCallback.forceUpdate();
|
|
|
|
|
2017-08-15 01:38:07 -06:00
|
|
|
return view;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Callbacks from TransactionInfoAdapter
|
|
|
|
@Override
|
|
|
|
public void onInteraction(final View view, final TransactionInfo infoItem) {
|
2017-08-19 11:40:48 -06:00
|
|
|
activityCallback.onTxDetailsRequest(infoItem);
|
2017-08-15 01:38:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// called from activity
|
|
|
|
public void onRefreshed(final Wallet wallet, final boolean full) {
|
|
|
|
Log.d(TAG, "onRefreshed()");
|
|
|
|
if (full) {
|
|
|
|
List<TransactionInfo> list = wallet.getHistory().getAll();
|
|
|
|
adapter.setInfos(list);
|
|
|
|
adapter.notifyDataSetChanged();
|
|
|
|
}
|
|
|
|
updateStatus(wallet);
|
|
|
|
}
|
|
|
|
|
2017-08-20 08:18:59 -06:00
|
|
|
public void onSynced() {
|
2017-08-29 14:22:07 -06:00
|
|
|
if (!activityCallback.isWatchOnly()) {
|
2017-08-19 08:20:10 -06:00
|
|
|
bSend.setVisibility(View.VISIBLE);
|
|
|
|
bSend.setEnabled(true);
|
|
|
|
}
|
2017-08-19 05:27:26 -06:00
|
|
|
}
|
|
|
|
|
2017-08-15 01:38:07 -06:00
|
|
|
public void onProgress(final String text) {
|
|
|
|
if (text != null) {
|
2017-08-18 05:51:24 -06:00
|
|
|
tvProgress.setText(text);
|
2017-08-19 08:20:10 -06:00
|
|
|
showProgress();
|
2017-08-15 01:38:07 -06:00
|
|
|
} else {
|
|
|
|
hideProgress();
|
2017-08-18 05:51:24 -06:00
|
|
|
tvProgress.setText(getString(R.string.status_working));
|
2017-08-15 01:38:07 -06:00
|
|
|
onProgress(-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onProgress(final int n) {
|
|
|
|
if (n >= 0) {
|
2017-08-18 05:51:24 -06:00
|
|
|
pbProgress.setIndeterminate(false);
|
|
|
|
pbProgress.setProgress(n);
|
2017-08-15 01:38:07 -06:00
|
|
|
} else {
|
2017-08-18 05:51:24 -06:00
|
|
|
pbProgress.setIndeterminate(true);
|
2017-08-15 01:38:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void showProgress() {
|
2017-08-23 14:55:47 -06:00
|
|
|
clProgress.setVisibility(View.VISIBLE);
|
2017-09-08 15:59:50 -06:00
|
|
|
tvBlockHeightProgress.setVisibility(View.GONE);
|
2017-08-15 01:38:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
public void hideProgress() {
|
2017-08-23 14:55:47 -06:00
|
|
|
clProgress.setVisibility(View.GONE);
|
2017-09-08 15:59:50 -06:00
|
|
|
tvBlockHeightProgress.setVisibility(View.VISIBLE);
|
2017-08-15 01:38:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
String setActivityTitle(Wallet wallet) {
|
|
|
|
if (wallet == null) return null;
|
|
|
|
String shortName = wallet.getName();
|
|
|
|
if (shortName.length() > 16) {
|
|
|
|
shortName = shortName.substring(0, 14) + "...";
|
|
|
|
}
|
2017-09-08 15:59:50 -06:00
|
|
|
// TODO very very rarely this craches because getAddress returns "" or so ...
|
|
|
|
// maybe because this runs in the ui thread and not in a 5MB thread
|
2017-08-23 14:55:47 -06:00
|
|
|
String title = "[" + wallet.getAddress().substring(0, 6) + "] " + shortName;
|
2017-08-15 01:38:07 -06:00
|
|
|
activityCallback.setTitle(title);
|
2017-08-23 14:55:47 -06:00
|
|
|
|
|
|
|
String watchOnly = (wallet.isWatchOnly() ? " " + getString(R.string.watchonly_label) : "");
|
|
|
|
String net = (wallet.isTestNet() ? getString(R.string.connect_testnet) : getString(R.string.connect_mainnet));
|
|
|
|
activityCallback.setSubtitle(net + " " + watchOnly);
|
2017-08-15 01:38:07 -06:00
|
|
|
Log.d(TAG, "wallet title is " + title);
|
|
|
|
return title;
|
|
|
|
}
|
|
|
|
|
|
|
|
private long firstBlock = 0;
|
|
|
|
private String walletTitle = null;
|
|
|
|
|
|
|
|
private void updateStatus(Wallet wallet) {
|
2017-10-03 11:04:45 -06:00
|
|
|
if (!isAdded()) return;
|
2017-08-15 01:38:07 -06:00
|
|
|
Log.d(TAG, "updateStatus()");
|
|
|
|
if (walletTitle == null) {
|
|
|
|
walletTitle = setActivityTitle(wallet);
|
|
|
|
onProgress(100); // of loading
|
|
|
|
}
|
2017-08-23 14:55:47 -06:00
|
|
|
long balance = wallet.getBalance();
|
|
|
|
long unlockedBalance = wallet.getUnlockedBalance();
|
2017-09-08 15:59:50 -06:00
|
|
|
tvBalance.setText(getResources().getString(R.string.xmr_balance, Helper.getDisplayAmount(unlockedBalance)));
|
2017-09-09 04:22:24 -06:00
|
|
|
tvUnconfirmedAmount.setText(getResources().getString(R.string.xmr_unconfirmed_amount, Helper.getDisplayAmount(balance - unlockedBalance)));
|
2017-08-15 01:38:07 -06:00
|
|
|
String sync = "";
|
|
|
|
if (!activityCallback.hasBoundService())
|
|
|
|
throw new IllegalStateException("WalletService not bound.");
|
|
|
|
Wallet.ConnectionStatus daemonConnected = activityCallback.getConnectionStatus();
|
|
|
|
if (daemonConnected == Wallet.ConnectionStatus.ConnectionStatus_Connected) {
|
|
|
|
long daemonHeight = activityCallback.getDaemonHeight();
|
|
|
|
if (!wallet.isSynchronized()) {
|
|
|
|
long n = daemonHeight - wallet.getBlockChainHeight();
|
2017-08-16 11:39:53 -06:00
|
|
|
sync = formatter.format(n) + " " + getString(R.string.status_remaining);
|
2017-08-15 01:38:07 -06:00
|
|
|
if (firstBlock == 0) {
|
|
|
|
firstBlock = wallet.getBlockChainHeight();
|
|
|
|
}
|
|
|
|
int x = 100 - Math.round(100f * n / (1f * daemonHeight - firstBlock));
|
|
|
|
onProgress(getString(R.string.status_syncing) + " " + sync);
|
|
|
|
if (x == 0) x = -1;
|
|
|
|
onProgress(x);
|
|
|
|
} else {
|
2017-08-16 11:39:53 -06:00
|
|
|
sync = getString(R.string.status_synced) + ": " + formatter.format(wallet.getBlockChainHeight());
|
2017-08-15 01:38:07 -06:00
|
|
|
}
|
|
|
|
}
|
2017-08-18 05:51:24 -06:00
|
|
|
tvBlockHeightProgress.setText(sync);
|
2017-08-23 14:55:47 -06:00
|
|
|
//String net = (wallet.isTestNet() ? getString(R.string.connect_testnet) : getString(R.string.connect_mainnet));
|
|
|
|
//activityCallback.setSubtitle(net + " " + daemonConnected.toString().substring(17));
|
|
|
|
// TODO show connected status somewhere
|
2017-08-15 01:38:07 -06:00
|
|
|
}
|
|
|
|
|
2017-08-15 15:59:41 -06:00
|
|
|
Listener activityCallback;
|
2017-08-15 01:38:07 -06:00
|
|
|
|
|
|
|
// Container Activity must implement this interface
|
2017-08-15 15:59:41 -06:00
|
|
|
public interface Listener {
|
2017-08-15 01:38:07 -06:00
|
|
|
boolean hasBoundService();
|
|
|
|
|
2017-08-18 05:51:24 -06:00
|
|
|
void forceUpdate();
|
|
|
|
|
2017-08-15 01:38:07 -06:00
|
|
|
Wallet.ConnectionStatus getConnectionStatus();
|
|
|
|
|
|
|
|
long getDaemonHeight(); //mBoundService.getDaemonHeight();
|
|
|
|
|
|
|
|
void setTitle(String title);
|
|
|
|
|
2017-08-23 14:55:47 -06:00
|
|
|
void setSubtitle(String subtitle);
|
|
|
|
|
2017-08-18 05:51:24 -06:00
|
|
|
void onSendRequest();
|
|
|
|
|
2017-08-19 11:40:48 -06:00
|
|
|
void onTxDetailsRequest(TransactionInfo info);
|
|
|
|
|
2017-08-19 05:27:26 -06:00
|
|
|
boolean isSynced();
|
2017-08-19 08:20:10 -06:00
|
|
|
|
|
|
|
boolean isWatchOnly();
|
|
|
|
|
|
|
|
String getTxKey(String txId);
|
2017-09-02 02:05:50 -06:00
|
|
|
|
|
|
|
void onWalletReceive();
|
|
|
|
|
|
|
|
boolean hasWallet();
|
2017-08-15 01:38:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onAttach(Context context) {
|
|
|
|
super.onAttach(context);
|
2017-08-15 15:59:41 -06:00
|
|
|
if (context instanceof Listener) {
|
|
|
|
this.activityCallback = (Listener) context;
|
2017-08-15 01:38:07 -06:00
|
|
|
} else {
|
|
|
|
throw new ClassCastException(context.toString()
|
2017-08-15 15:59:41 -06:00
|
|
|
+ " must implement Listener");
|
2017-08-15 01:38:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|