mirror of
https://github.com/DanielPollithy/flashwifi.git
synced 2025-10-16 11:45:32 +00:00
Balance + Address Retrieval
-Added async balance and address retrieval based on seed
This commit is contained in:
parent
0c397b9812
commit
fc4e1e8091
@ -1,11 +1,19 @@
|
||||
package com.flashwifi.wifip2p;
|
||||
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.app.Fragment;
|
||||
import android.content.Context;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.os.Message;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* A simple {@link Fragment} subclass.
|
||||
@ -14,7 +22,15 @@ import android.view.ViewGroup;
|
||||
*/
|
||||
public class FundWalletFragment extends Fragment {
|
||||
|
||||
private static final int TASK_COMPLETE = 1;
|
||||
private String seed;
|
||||
private String address;
|
||||
private String balance;
|
||||
|
||||
TextView balanceTextView = null;
|
||||
TextView addressTextView = null;
|
||||
|
||||
Handler mHandler;
|
||||
|
||||
public FundWalletFragment() {
|
||||
// Required empty public constructor
|
||||
@ -38,12 +54,114 @@ public class FundWalletFragment extends Fragment {
|
||||
if (bundle != null) {
|
||||
seed = bundle.getString("seed");
|
||||
}
|
||||
|
||||
//Handle post-asynctask activities of updating UI
|
||||
mHandler = new Handler(Looper.getMainLooper()) {
|
||||
@Override
|
||||
public void handleMessage(Message inputMessage) {
|
||||
switch (inputMessage.what) {
|
||||
case TASK_COMPLETE:
|
||||
String returnStatus = (String) inputMessage.obj;
|
||||
|
||||
if(returnStatus == "noError"){
|
||||
balanceTextView.setText(balance + " i");
|
||||
addressTextView.setText(address);
|
||||
Toast.makeText(getActivity(), "Balance and address updated",
|
||||
Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
else if (returnStatus == "addressError"){
|
||||
Toast.makeText(getActivity(), "Error getting address",
|
||||
Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
else if (returnStatus == "balanceError"){
|
||||
Toast.makeText(getActivity(), "Error getting balance",
|
||||
Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
else{
|
||||
Toast.makeText(getActivity(), "Unknown error",
|
||||
Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||
Bundle savedInstanceState) {
|
||||
// Inflate the layout for this fragment
|
||||
return inflater.inflate(R.layout.fragment_fund_wallet, container, false);
|
||||
View FundWalletFragmentView = inflater.inflate(R.layout.fragment_fund_wallet, container, false);
|
||||
|
||||
balanceTextView = (TextView) FundWalletFragmentView.findViewById(R.id.FundWalletBalanceValue);
|
||||
addressTextView = (TextView) FundWalletFragmentView.findViewById(R.id.AddressValue);
|
||||
|
||||
// Set Listeners
|
||||
balanceTextView.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
textCopyBalanceClick(balanceTextView);
|
||||
}
|
||||
});
|
||||
|
||||
addressTextView.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
textCopyAddressClick(addressTextView);
|
||||
}
|
||||
});
|
||||
|
||||
Toast.makeText(getActivity(), "Retrieving balance and address...",
|
||||
Toast.LENGTH_SHORT).show();
|
||||
|
||||
AsyncTask.execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
WalletAddressAndBalanceChecker addressAndBalanceCheckerbalanceChecker = new WalletAddressAndBalanceChecker();
|
||||
List<String> addressList = addressAndBalanceCheckerbalanceChecker.getAddress(seed);
|
||||
if(addressList != null){
|
||||
address = addressList.get(0);
|
||||
|
||||
balance = addressAndBalanceCheckerbalanceChecker.getBalance(addressList);
|
||||
if(balance != null){
|
||||
Message completeMessage = mHandler.obtainMessage(TASK_COMPLETE, "noError");
|
||||
completeMessage.sendToTarget();
|
||||
}
|
||||
else{
|
||||
//Balance Retrieval Error
|
||||
Message completeMessage = mHandler.obtainMessage(TASK_COMPLETE, "balanceError");
|
||||
completeMessage.sendToTarget();
|
||||
}
|
||||
}
|
||||
else{
|
||||
//Address Retrieval Error
|
||||
Message completeMessage = mHandler.obtainMessage(TASK_COMPLETE, "addressError");
|
||||
completeMessage.sendToTarget();
|
||||
}
|
||||
}
|
||||
});
|
||||
return FundWalletFragmentView;
|
||||
}
|
||||
|
||||
public void textCopyBalanceClick(TextView balanceTextView)
|
||||
{
|
||||
String balanceValue = balanceTextView.getText().toString();
|
||||
setClipboardText(balanceValue);
|
||||
Toast.makeText(getActivity(), "Balance Copied",
|
||||
Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
|
||||
public void textCopyAddressClick(TextView addressTextView)
|
||||
{
|
||||
String addressValue = addressTextView.getText().toString();
|
||||
setClipboardText(addressValue);
|
||||
Toast.makeText(getActivity(), "Address Copied",
|
||||
Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
|
||||
public void setClipboardText(String inText){
|
||||
android.content.ClipboardManager clipboard = (android.content.ClipboardManager) getActivity().getSystemService(Context.CLIPBOARD_SERVICE);
|
||||
android.content.ClipData clip = android.content.ClipData.newPlainText("Copied Text", inText);
|
||||
clipboard.setPrimaryClip(clip);
|
||||
}
|
||||
}
|
||||
@ -158,6 +158,7 @@ public class MainActivity extends AppCompatActivity
|
||||
Bundle bundle = new Bundle();
|
||||
bundle.putString("seed", seed);
|
||||
fragment.setArguments(bundle);
|
||||
fragment.setRetainInstance(true);
|
||||
|
||||
// Insert the fragment by replacing any existing fragment
|
||||
FragmentManager fragmentManager = getFragmentManager();
|
||||
|
||||
@ -0,0 +1,56 @@
|
||||
package com.flashwifi.wifip2p;
|
||||
|
||||
/**
|
||||
* Created by Toby on 1/6/2018.
|
||||
*/
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import jota.IotaAPI;
|
||||
import jota.dto.response.GetBalancesResponse;
|
||||
import jota.dto.response.GetNewAddressResponse;
|
||||
import jota.error.ArgumentException;
|
||||
|
||||
public class WalletAddressAndBalanceChecker {
|
||||
|
||||
private static IotaAPI api;
|
||||
//GetNodeInfoResponse response = api.getNodeInfo();
|
||||
|
||||
public WalletAddressAndBalanceChecker() {
|
||||
//Local node:
|
||||
//api = new IotaAPI.Builder().build();
|
||||
|
||||
//Local node:
|
||||
api = new IotaAPI.Builder()
|
||||
.protocol("http")
|
||||
.host("node.iotawallet.info")
|
||||
.port("14265")
|
||||
.build();
|
||||
}
|
||||
|
||||
public String getBalance(List<String> inAaddresses){
|
||||
try {
|
||||
GetBalancesResponse balanceResultResponse = api.getBalances(100, inAaddresses);
|
||||
String balance = balanceResultResponse.getBalances()[0];
|
||||
return balance;
|
||||
} catch (ArgumentException e) {
|
||||
e.printStackTrace();
|
||||
System.out.println("getBalance Error!");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public List<String> getAddress(String seed) {
|
||||
List<String> addressList = null;
|
||||
GetNewAddressResponse addressResponse = null;
|
||||
try {
|
||||
addressResponse = api.getNewAddress(seed, 2, 0, false, 1, true);
|
||||
} catch (ArgumentException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
if(addressResponse != null){
|
||||
addressList = addressResponse.getAddresses();
|
||||
}
|
||||
return addressList;
|
||||
}
|
||||
}
|
||||
@ -1,81 +1,99 @@
|
||||
<LinearLayout
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
tools:context="com.flashwifi.wifip2p.FundWalletFragment"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:orientation="vertical">
|
||||
android:orientation="vertical"
|
||||
tools:context="com.flashwifi.wifip2p.FundWalletFragment">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/FundWalletTitleLabel"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:background="#0A387F"
|
||||
android:orientation="vertical"
|
||||
android:paddingBottom="20dp"
|
||||
android:paddingEnd="20dp"
|
||||
android:paddingStart="20dp"
|
||||
android:paddingTop="20dp"
|
||||
android:text="@string/FundWalletAddressTitle"
|
||||
android:textColor="#ffffff"
|
||||
android:textStyle="bold" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/AddressValue"
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:background="#0D47A1"
|
||||
android:paddingBottom="20dp"
|
||||
android:paddingEnd="20dp"
|
||||
android:paddingStart="20dp"
|
||||
android:paddingTop="20dp"
|
||||
android:text="@string/FundWalletNoAddress"
|
||||
android:textColor="#ffffff" />
|
||||
android:orientation="horizontal">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/FundWalletBalanceLabel"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:background="#0D47A1"
|
||||
android:orientation="vertical"
|
||||
android:paddingBottom="20dp"
|
||||
android:paddingEnd="20dp"
|
||||
android:paddingStart="20dp"
|
||||
android:paddingTop="20dp"
|
||||
android:text="@string/FundWalletBalance"
|
||||
android:textColor="#ffffff"
|
||||
android:textStyle="bold" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/FundWalletBalanceValue"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:background="#0D47A1"
|
||||
android:orientation="vertical"
|
||||
android:paddingBottom="20dp"
|
||||
android:paddingEnd="20dp"
|
||||
android:paddingStart="40dp"
|
||||
android:paddingTop="20dp"
|
||||
android:text="0 i"
|
||||
android:textColor="#ffffff"
|
||||
android:textStyle="bold" />
|
||||
</LinearLayout>
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:background="#1365E5"
|
||||
android:orientation="horizontal">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/FundWalletTitleLabel"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:background="#1365E5"
|
||||
android:orientation="vertical"
|
||||
android:paddingBottom="20dp"
|
||||
android:paddingEnd="20dp"
|
||||
android:paddingStart="20dp"
|
||||
android:paddingTop="20dp"
|
||||
android:text="@string/FundWalletAddressTitle"
|
||||
android:textColor="#ffffff"
|
||||
android:textStyle="bold" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/AddressValue"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:background="#1365E5"
|
||||
android:paddingBottom="20dp"
|
||||
android:paddingEnd="20dp"
|
||||
android:paddingStart="40dp"
|
||||
android:paddingTop="20dp"
|
||||
android:text=""
|
||||
android:textColor="#ffffff" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:background="#1365E5"
|
||||
android:background="#1570FF"
|
||||
android:orientation="vertical">
|
||||
|
||||
<FrameLayout
|
||||
<TextView
|
||||
android:id="@+id/Temp"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="0dp"
|
||||
android:layout_weight=".8">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/Temp"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:paddingStart="20dp"
|
||||
android:text="QR CODE" />
|
||||
</FrameLayout>
|
||||
|
||||
<FrameLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="0dp"
|
||||
android:layout_weight=".2">
|
||||
|
||||
<Button
|
||||
android:id="@+id/floatingActionButton"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_gravity="bottom|left"
|
||||
android:layout_marginBottom="10dp"
|
||||
android:layout_marginLeft="10dp"
|
||||
android:layout_marginRight="10dp"
|
||||
android:background="#051c40"
|
||||
android:clickable="true"
|
||||
android:elevation="10dp"
|
||||
android:src="@android:drawable/ic_input_add"
|
||||
android:text="+"
|
||||
android:textAlignment="center"
|
||||
android:textColor="#ffffff"
|
||||
android:textSize="20sp"
|
||||
android:textStyle="bold"
|
||||
android:focusable="true"/>
|
||||
</FrameLayout>
|
||||
android:layout_height="wrap_content"
|
||||
android:paddingStart="20dp"
|
||||
android:paddingEnd="20dp"
|
||||
android:paddingTop="20dp"
|
||||
android:paddingBottom="20dp"
|
||||
android:text="QR CODE"
|
||||
android:textColor="#ffffff" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
|
||||
@ -43,9 +43,7 @@
|
||||
<string name="iotaNodeTestSuccess">Connection to Iota Node is possible.</string>
|
||||
|
||||
<!-- Fund Wallet Fragment -->
|
||||
<string name="FundWalletAddressTitle">ADDRESS:</string>
|
||||
<string name="FundWalletNoAddress">No address generated, click +</string>
|
||||
|
||||
|
||||
<string name="FundWalletAddressTitle">DEPOSIT ADDRESS:</string>
|
||||
<string name="FundWalletBalance">ACCOUNT BLANCE: </string>
|
||||
|
||||
</resources>
|
||||
Loading…
Reference in New Issue
Block a user