diff --git a/app/build.gradle b/app/build.gradle index 9679093..09a4aab 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -33,5 +33,6 @@ dependencies { compile 'com.google.code.gson:gson:2.8.1' compile 'com.github.kenglxn.QRGen:android:2.4.0' compile 'me.dm7.barcodescanner:zxing:1.9.8' + compile 'pl.droidsonroids.gif:android-gif-drawable:1.2.10' testCompile 'junit:junit:4.12' } diff --git a/app/src/main/java/com/flashwifi/wifip2p/AddressBalanceTransfer.java b/app/src/main/java/com/flashwifi/wifip2p/AddressBalanceTransfer.java new file mode 100644 index 0000000..052e464 --- /dev/null +++ b/app/src/main/java/com/flashwifi/wifip2p/AddressBalanceTransfer.java @@ -0,0 +1,38 @@ +package com.flashwifi.wifip2p; + +public class AddressBalanceTransfer { + + private String depositAddress; + private String balance; + private String message; + + public AddressBalanceTransfer(String inDepositAddress, String inBalance, String inMessage){ + depositAddress = inDepositAddress; + balance = inBalance; + message = inMessage; + } + + public String getDepositAddress() { + return depositAddress; + } + + public void setDepositAddress(String depositAddress) { + this.depositAddress = depositAddress; + } + + public String getBalance() { + return balance; + } + + public void setBalance(String balance) { + this.balance = balance; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } +} \ No newline at end of file diff --git a/app/src/main/java/com/flashwifi/wifip2p/FundWalletFragment.java b/app/src/main/java/com/flashwifi/wifip2p/FundWalletFragment.java index 62f0105..b7d76f0 100644 --- a/app/src/main/java/com/flashwifi/wifip2p/FundWalletFragment.java +++ b/app/src/main/java/com/flashwifi/wifip2p/FundWalletFragment.java @@ -3,11 +3,11 @@ package com.flashwifi.wifip2p; import android.app.Fragment; import android.content.Context; import android.graphics.Bitmap; -import android.os.AsyncTask; import android.os.Bundle; import android.os.Handler; import android.os.Looper; import android.os.Message; +import android.support.v4.widget.SwipeRefreshLayout; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -19,8 +19,7 @@ import com.flashwifi.wifip2p.iotaAPI.Requests.WalletAddressAndBalanceChecker; import net.glxn.qrgen.android.QRCode; -import java.util.Iterator; -import java.util.List; +import pl.droidsonroids.gif.GifImageView; /** * A simple {@link Fragment} subclass. @@ -29,7 +28,8 @@ import java.util.List; */ public class FundWalletFragment extends Fragment { - private static final int TASK_COMPLETE = 1; + private static final int FUND_WALLET = 0; + private static final int BALANCE_RETRIEVE_TASK_COMPLETE = 1; private String seed; private String depositAddress; private String balance; @@ -38,8 +38,15 @@ public class FundWalletFragment extends Fragment { private TextView addressTextView; private ImageView qrImageView; + private GifImageView loadingGifImageView; + + private SwipeRefreshLayout mSwipeRefreshLayout; + private Handler mHandler; + private static Boolean transactionInProgress = false; + private WalletAddressAndBalanceChecker addressAndBalanceChecker; + public FundWalletFragment() { // Required empty public constructor } @@ -68,8 +75,14 @@ public class FundWalletFragment extends Fragment { @Override public void handleMessage(Message inputMessage) { switch (inputMessage.what) { - case TASK_COMPLETE: - String returnStatus = (String) inputMessage.obj; + case BALANCE_RETRIEVE_TASK_COMPLETE: + AddressBalanceTransfer addressBalanceTransfer = (AddressBalanceTransfer) inputMessage.obj; + balance = addressBalanceTransfer.getBalance(); + depositAddress = addressBalanceTransfer.getDepositAddress(); + String returnStatus = addressBalanceTransfer.getMessage(); + + hideLoadingGIF(); + transactionInProgress = false; if(returnStatus == "noError"){ balanceTextView.setText(balance + " i"); @@ -95,6 +108,23 @@ public class FundWalletFragment extends Fragment { }; } + @Override + public void onDestroy() { + addressAndBalanceChecker.cancel(true); + transactionInProgress = false; + super.onDestroy(); + } + + private void hideLoadingGIF() { + loadingGifImageView.setVisibility(View.GONE); + qrImageView.setVisibility(View.VISIBLE); + } + + private void showLoadingGIF() { + loadingGifImageView.setVisibility(View.VISIBLE); + qrImageView.setVisibility(View.GONE); + } + private void makeToastFundWalletFragment(String s) { if(getActivity() != null){ Toast.makeText(getActivity(), s, Toast.LENGTH_SHORT).show(); @@ -115,6 +145,8 @@ public class FundWalletFragment extends Fragment { balanceTextView = (TextView) fundWalletFragmentView.findViewById(R.id.FundWalletBalanceValue); addressTextView = (TextView) fundWalletFragmentView.findViewById(R.id.AddressValue); qrImageView = (ImageView) fundWalletFragmentView.findViewById(R.id.QRCode); + loadingGifImageView = (GifImageView) fundWalletFragmentView.findViewById(R.id.FundWalletLoadingGIF); + mSwipeRefreshLayout = (SwipeRefreshLayout) fundWalletFragmentView.findViewById(R.id.FundWalletSwipeRefresh); // Set Listeners balanceTextView.setOnClickListener(new View.OnClickListener() { @@ -131,48 +163,38 @@ public class FundWalletFragment extends Fragment { } }); - Toast.makeText(getActivity(), "Retrieving balance and address...", - Toast.LENGTH_SHORT).show(); - - AsyncTask.execute(new Runnable() { + mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override - public void run() { - WalletAddressAndBalanceChecker addressAndBalanceChecker = new WalletAddressAndBalanceChecker(getActivity(),getActivity().getString(R.string.preference_file_key)); - List addressList = addressAndBalanceChecker.getAddress(seed); - - if(addressList != null && addressList.get(0) == "Unable to resolve host"){ - Message completeMessage = mHandler.obtainMessage(TASK_COMPLETE, "hostError"); - completeMessage.sendToTarget(); - } - else if(addressList != null){ - - System.out.println("|AddressListReturned:|"); - System.out.println(addressList.size()); - System.out.println(addressList.get(addressList.size()-1)); - - depositAddress = addressList.get(addressList.size()-1); - - balance = addressAndBalanceChecker.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(); - } + public void onRefresh() { + initiateRefresh(); } }); + + showLoadingGIF(); + Toast.makeText(getActivity(), "Retrieving balance and address...", Toast.LENGTH_SHORT).show(); + getBalance(); + return fundWalletFragmentView; } + private void initiateRefresh() { + mSwipeRefreshLayout.setRefreshing(false); + + if(transactionInProgress == false){ + balanceTextView.setText(""); + addressTextView.setText(""); + showLoadingGIF(); + Toast.makeText(getActivity(), "Retrieving balance and address...", Toast.LENGTH_SHORT).show(); + getBalance(); + } + } + + private void getBalance(){ + transactionInProgress = true; + addressAndBalanceChecker = new WalletAddressAndBalanceChecker(getActivity(),getActivity().getString(R.string.preference_file_key),seed, mHandler,FUND_WALLET,true); + addressAndBalanceChecker.execute(); + } + public void textCopyBalanceClick() { String balanceValue = balanceTextView.getText().toString(); diff --git a/app/src/main/java/com/flashwifi/wifip2p/HomeActivity.java b/app/src/main/java/com/flashwifi/wifip2p/HomeActivity.java index 09a8f09..46ed1f4 100644 --- a/app/src/main/java/com/flashwifi/wifip2p/HomeActivity.java +++ b/app/src/main/java/com/flashwifi/wifip2p/HomeActivity.java @@ -60,15 +60,9 @@ public class HomeActivity extends AppCompatActivity { } private void decryptSeed(String password) { - View view = findViewById(R.id.home_view); - - if(password.equals("")){ - Snackbar.make(view, getString(R.string.password_empty), Snackbar.LENGTH_LONG).setAction("Action", null).show(); - return; - } - EncryptedPreferences encryptedPreferences = new EncryptedPreferences.Builder(this).withEncryptionPassword(password).build(); String seed = encryptedPreferences.getString(getString(R.string.encrypted_seed), null); + View view = findViewById(R.id.home_view); if (seed != null) { Snackbar.make(view, getString(R.string.seed_decrypted), Snackbar.LENGTH_LONG).setAction("Action", null).show(); @@ -94,10 +88,7 @@ public class HomeActivity extends AppCompatActivity { private void generateNewSeed() { // generate the seed - - //final String seed = SeedRandomGenerator.generateNewSeed(); - //Set Testnet seed here: - final String seed = "EJ9SPL9GIK9EFICFRPQU9LLSCPNESAWRPYVKQRBZQVACRBVKVZRIWOWUBRJKWJMXLPAXDXWI9IDMAOTOZ"; + final String seed = SeedRandomGenerator.generateNewSeed(); TextView seedText = (TextView) findViewById(R.id.seedTextView); seedText.setVisibility(View.VISIBLE); diff --git a/app/src/main/java/com/flashwifi/wifip2p/MainActivity.java b/app/src/main/java/com/flashwifi/wifip2p/MainActivity.java index 2b1f6ee..7ed4b20 100644 --- a/app/src/main/java/com/flashwifi/wifip2p/MainActivity.java +++ b/app/src/main/java/com/flashwifi/wifip2p/MainActivity.java @@ -3,14 +3,10 @@ package com.flashwifi.wifip2p; import android.app.Fragment; import android.app.FragmentManager; import android.content.Intent; -import android.hardware.Camera; -import android.net.Uri; import android.os.Bundle; import android.support.design.widget.FloatingActionButton; -import android.support.design.widget.Snackbar; -import android.view.LayoutInflater; -import android.view.View; import android.support.design.widget.NavigationView; +import android.support.design.widget.Snackbar; import android.support.v4.view.GravityCompat; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBarDrawerToggle; @@ -18,7 +14,7 @@ import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.Menu; import android.view.MenuItem; -import android.view.ViewGroup; +import android.view.View; import android.widget.ListView; public class MainActivity extends AppCompatActivity diff --git a/app/src/main/java/com/flashwifi/wifip2p/QRScannerFragment.java b/app/src/main/java/com/flashwifi/wifip2p/QRScannerFragment.java index 11bb225..b9d1e4b 100644 --- a/app/src/main/java/com/flashwifi/wifip2p/QRScannerFragment.java +++ b/app/src/main/java/com/flashwifi/wifip2p/QRScannerFragment.java @@ -1,6 +1,7 @@ package com.flashwifi.wifip2p; import android.app.Fragment; +import android.content.pm.ActivityInfo; import android.os.Bundle; import android.view.ViewGroup; import android.widget.EditText; @@ -24,10 +25,15 @@ public class QRScannerFragment extends Fragment implements ZXingScannerView.Resu editTextAddress = addressEditTextTransfer.getEditTextAddress(); } - scannerView = new ZXingScannerView(getActivity()); + if(getActivity() != null){ + getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); + scannerView = new ZXingScannerView(getActivity()); - ViewGroup contentFrame = (ViewGroup) getActivity().findViewById(R.id.content_frame); - contentFrame.addView(scannerView); + ViewGroup contentFrame = (ViewGroup) getActivity().findViewById(R.id.content_frame); + if(contentFrame != null && scannerView != null){ + contentFrame.addView(scannerView); + } + } } @Override @@ -44,6 +50,12 @@ public class QRScannerFragment extends Fragment implements ZXingScannerView.Resu scannerView.stopCameraPreview(); } + public void onDestroy() { + scannerView.stopCamera(); + scannerView.stopCameraPreview(); + super.onDestroy(); + } + @Override public void handleResult(Result result) { diff --git a/app/src/main/java/com/flashwifi/wifip2p/WithdrawWalletFragment.java b/app/src/main/java/com/flashwifi/wifip2p/WithdrawWalletFragment.java index 560ab56..bd4a511 100644 --- a/app/src/main/java/com/flashwifi/wifip2p/WithdrawWalletFragment.java +++ b/app/src/main/java/com/flashwifi/wifip2p/WithdrawWalletFragment.java @@ -1,15 +1,16 @@ package com.flashwifi.wifip2p; import android.app.AlertDialog; +import android.app.Fragment; +import android.app.FragmentTransaction; import android.content.Context; import android.content.DialogInterface; import android.content.pm.PackageManager; -import android.os.AsyncTask; import android.os.Bundle; -import android.app.Fragment; import android.os.Handler; import android.os.Looper; import android.os.Message; +import android.support.v4.widget.SwipeRefreshLayout; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -22,11 +23,9 @@ import android.widget.Toast; import com.flashwifi.wifip2p.iotaAPI.Requests.WalletAddressAndBalanceChecker; import com.flashwifi.wifip2p.iotaAPI.Requests.WalletTransferRequest; -import java.util.List; -import jota.IotaAPI; - import jota.error.ArgumentException; import jota.utils.Checksum; +import pl.droidsonroids.gif.GifImageView; import static android.Manifest.permission.CAMERA; @@ -40,6 +39,7 @@ public class WithdrawWalletFragment extends Fragment { private static final int BALANCE_RETRIEVE_TASK_COMPLETE = 1; private static final int TRANSFER_TASK_COMPLETE = 2; + private static final int WITHDRAW_WALLET = 1; private String appWalletSeed; private String appWalletBalance; private ImageButton qrScannerButton; @@ -47,11 +47,17 @@ public class WithdrawWalletFragment extends Fragment { private EditText editTextAmount; private EditText editTextMessage; private EditText editTextTag; + private WalletAddressAndBalanceChecker addressAndBalanceChecker; + + private GifImageView loadingGifImageView; + private SwipeRefreshLayout mSwipeRefreshLayout; private Button sendButton; private TextView balanceTextView; private Handler mHandler; + private static Boolean transactionInProgress = false; + public WithdrawWalletFragment() { // Required empty public constructor } @@ -80,9 +86,13 @@ public class WithdrawWalletFragment extends Fragment { mHandler = new Handler(Looper.getMainLooper()) { @Override public void handleMessage(Message inputMessage) { + AddressBalanceTransfer addressBalanceTransfer = (AddressBalanceTransfer) inputMessage.obj; switch (inputMessage.what) { case BALANCE_RETRIEVE_TASK_COMPLETE: - String returnStatus = (String) inputMessage.obj; + appWalletBalance = addressBalanceTransfer.getBalance(); + String returnStatus = addressBalanceTransfer.getMessage(); + hideLoadingGIF(); + transactionInProgress = false; if(returnStatus == "noError"){ balanceTextView.setText(appWalletBalance + " i"); @@ -90,6 +100,8 @@ public class WithdrawWalletFragment extends Fragment { } else if (returnStatus == "noErrorNoUpdateMessage"){ balanceTextView.setText(appWalletBalance + " i"); + clearAllTransferValues(); + makeFieldsEditable(); } else if (returnStatus == "hostError"){ makeToastFundWalletFragment("Unable to reach host (node)"); @@ -106,13 +118,37 @@ public class WithdrawWalletFragment extends Fragment { break; case TRANSFER_TASK_COMPLETE: - String transferStatus = (String) inputMessage.obj; + String transferStatus = addressBalanceTransfer.getMessage(); makeToastFundWalletFragment(transferStatus); + getBalance(false); + Toast.makeText(getActivity(), "Updating balance...", Toast.LENGTH_SHORT).show(); } } }; } + @Override + public void onDestroy() { + addressAndBalanceChecker.cancel(true); + transactionInProgress = false; + super.onDestroy(); + } + + private void clearAllTransferValues() { + editTextAddress.setText(""); + editTextAmount.setText(""); + editTextMessage.setText(""); + editTextTag.setText(""); + } + + private void hideLoadingGIF() { + loadingGifImageView.setVisibility(View.GONE); + } + + private void showLoadingGIF() { + loadingGifImageView.setVisibility(View.VISIBLE); + } + @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { @@ -126,8 +162,9 @@ public class WithdrawWalletFragment extends Fragment { editTextAmount = (EditText) withdrawWalletFragmentView.findViewById(R.id.WithdrawWalletAmount); editTextMessage = (EditText) withdrawWalletFragmentView.findViewById(R.id.WithdrawWalletMessage); editTextTag = (EditText) withdrawWalletFragmentView.findViewById(R.id.WithdrawWalletTag); - + loadingGifImageView = (GifImageView) withdrawWalletFragmentView.findViewById(R.id.WithdrawWalletLoadingGIF); balanceTextView = (TextView) withdrawWalletFragmentView.findViewById(R.id.WithdrawWalletBalanceValue); + mSwipeRefreshLayout = (SwipeRefreshLayout) withdrawWalletFragmentView.findViewById(R.id.WithdrawWalletSwipeRefresh); // Set Listeners qrScannerButton.setOnClickListener(new View.OnClickListener() { @@ -144,103 +181,55 @@ public class WithdrawWalletFragment extends Fragment { } }); + mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { + @Override + public void onRefresh() { + initiateRefresh(); + } + }); + + showLoadingGIF(); Toast.makeText(getActivity(), "Retrieving balance...", Toast.LENGTH_SHORT).show(); - - getBalance(true,false); + + getBalance(true); return withdrawWalletFragmentView; } - private void getBalance(Boolean async, Boolean inNoUpdateMessage) { + private void getBalance(Boolean updateMessage) { + transactionInProgress = true; + addressAndBalanceChecker = new WalletAddressAndBalanceChecker(getActivity(),getActivity().getString(R.string.preference_file_key),appWalletSeed, mHandler,WITHDRAW_WALLET,updateMessage); + addressAndBalanceChecker.execute(); + } - final Boolean noUpdateMessageFinal = inNoUpdateMessage; + private void initiateRefresh() { + mSwipeRefreshLayout.setRefreshing(false); - if(async){ - //Get balance with new async call - AsyncTask.execute(new Runnable() { - @Override - public void run() { - WalletAddressAndBalanceChecker addressAndBalanceChecker = new WalletAddressAndBalanceChecker(getActivity(),getActivity().getString(R.string.preference_file_key)); - List addressList = addressAndBalanceChecker.getAddress(appWalletSeed); - - if(addressList != null && addressList.get(0) == "Unable to resolve host"){ - Message completeMessage = mHandler.obtainMessage(BALANCE_RETRIEVE_TASK_COMPLETE, "hostError"); - completeMessage.sendToTarget(); - } - else if(addressList != null){ - appWalletBalance = addressAndBalanceChecker.getBalance(addressList); - if(appWalletBalance != null){ - if(noUpdateMessageFinal){ - Message completeMessage = mHandler.obtainMessage(BALANCE_RETRIEVE_TASK_COMPLETE, "noErrorNoUpdateMessage"); - completeMessage.sendToTarget(); - } - else{ - Message completeMessage = mHandler.obtainMessage(BALANCE_RETRIEVE_TASK_COMPLETE, "noError"); - completeMessage.sendToTarget(); - } - } - else{ - //Balance Retrieval Error - Message completeMessage = mHandler.obtainMessage(BALANCE_RETRIEVE_TASK_COMPLETE, "balanceError"); - completeMessage.sendToTarget(); - } - } - else{ - //Address Retrieval Error - Message completeMessage = mHandler.obtainMessage(BALANCE_RETRIEVE_TASK_COMPLETE, "addressError"); - completeMessage.sendToTarget(); - } - } - }); - } - else{ - //Get balance without async call (perform on called thread) - WalletAddressAndBalanceChecker addressAndBalanceChecker = new WalletAddressAndBalanceChecker(getActivity(),getActivity().getString(R.string.preference_file_key)); - List addressList = addressAndBalanceChecker.getAddress(appWalletSeed); - - if(addressList != null && addressList.get(0) == "Unable to resolve host"){ - Message completeMessage = mHandler.obtainMessage(BALANCE_RETRIEVE_TASK_COMPLETE, "hostError"); - completeMessage.sendToTarget(); - } - else if(addressList != null){ - appWalletBalance = addressAndBalanceChecker.getBalance(addressList); - if(appWalletBalance != null){ - if(noUpdateMessageFinal){ - Message completeMessage = mHandler.obtainMessage(BALANCE_RETRIEVE_TASK_COMPLETE, "noErrorNoUpdateMessage"); - completeMessage.sendToTarget(); - } - else{ - Message completeMessage = mHandler.obtainMessage(BALANCE_RETRIEVE_TASK_COMPLETE, "noError"); - completeMessage.sendToTarget(); - } - } - else{ - //Balance Retrieval Error - Message completeMessage = mHandler.obtainMessage(BALANCE_RETRIEVE_TASK_COMPLETE, "balanceError"); - completeMessage.sendToTarget(); - } - } - else{ - //Address Retrieval Error - Message completeMessage = mHandler.obtainMessage(BALANCE_RETRIEVE_TASK_COMPLETE, "addressError"); - completeMessage.sendToTarget(); - } + if(transactionInProgress == false){ + balanceTextView.setText(""); + showLoadingGIF(); + Toast.makeText(getActivity(), "Retrieving balance...", Toast.LENGTH_SHORT).show(); + getBalance(true); } } private void makeToastFundWalletFragment(String s) { if(getActivity() != null){ - System.out.println("makeToastFundWalletFragment: "+s); Toast.makeText(getActivity(), s, Toast.LENGTH_SHORT).show(); } } private void qrScannerButtonClick() { - if (!hasCameraPermission(getActivity())) { - Toast.makeText(getActivity(), "Camera permission not granted", Toast.LENGTH_SHORT).show(); + if(transactionInProgress == false) { + if (!hasCameraPermission(getActivity())) { + Toast.makeText(getActivity(), "Camera permission not granted", Toast.LENGTH_SHORT).show(); /* TODO: Ask for permission. Currently depends on manifest for permission. Should ask at runtime */ - } else { - launchQRScanner(); + } else { + launchQRScanner(); + } + } + else{ + Toast.makeText(getActivity(), "Please wait for balance to be retrieved", Toast.LENGTH_SHORT).show(); } } @@ -251,7 +240,7 @@ public class WithdrawWalletFragment extends Fragment { final String message = editTextMessage.getText().toString(); final String tag = editTextTag.getText().toString(); - if(appWalletBalance == null || appWalletBalance.isEmpty()){ + if(appWalletBalance == null || appWalletBalance.isEmpty() || transactionInProgress){ Toast.makeText(getActivity(), "Please wait for balance to be retrieved", Toast.LENGTH_SHORT).show(); } else if(isValidAddress() == false){ @@ -274,42 +263,54 @@ public class WithdrawWalletFragment extends Fragment { alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { + showLoadingGIF(); + makeFieldsNonEditable(); + Toast.makeText(getActivity(), "Sending... (It is assumed that the balance is up to date)", Toast.LENGTH_SHORT).show(); //Send transfer - AsyncTask.execute(new Runnable() { - @Override - public void run() { - String result; - if(sendAmount.isEmpty()){ - String zeroSendAmount = "0"; - WalletTransferRequest transferRequest = new WalletTransferRequest(sendAddress,appWalletSeed,zeroSendAmount,message,tag,getActivity()); - result = transferRequest.sendRequest(); - System.out.println("sendButtonClick: "+result); - } - else{ - WalletTransferRequest transferRequest = new WalletTransferRequest(sendAddress,appWalletSeed,sendAmount,message,tag,getActivity()); - result = transferRequest.sendRequest(); - System.out.println("sendButtonClick: "+result); - } - getBalance(false,true); - Message completeMessage = mHandler.obtainMessage(TRANSFER_TASK_COMPLETE, result); - completeMessage.sendToTarget(); - } - }); - //TODO: Empty all fields + sendTransfer(sendAddress,sendAmount,message,tag); } }); alertDialog.show(); } } + private void sendTransfer(String sendAddress, String sendAmount, String message, String tag) { + String finalSendAmount; + if(sendAmount.isEmpty()){ + finalSendAmount = "0"; + }else{ + finalSendAmount = sendAmount; + } + WalletTransferRequest transferRequest = new WalletTransferRequest(sendAddress,appWalletSeed,finalSendAmount,message,tag,getActivity(),mHandler); + transactionInProgress = true; + transferRequest.execute(); + } + + private void makeFieldsNonEditable() { + editTextAddress.setEnabled(false); + editTextAmount.setEnabled(false); + editTextMessage.setEnabled(false); + editTextTag.setEnabled(false); + } + + private void makeFieldsEditable() { + editTextAddress.setText(""); + editTextAmount.setText(""); + editTextMessage.setText(""); + editTextTag.setText(""); + + editTextAddress.setEnabled(true); + editTextAmount.setEnabled(true); + editTextMessage.setEnabled(true); + editTextTag.setEnabled(true); + } + public static boolean hasCameraPermission(Context context) { int result = context.checkCallingOrSelfPermission(CAMERA); return result == PackageManager.PERMISSION_GRANTED; } private void launchQRScanner() { - /* - TODO:Uncomment to re-enable scanner Fragment fragment = new QRScannerFragment(); Bundle bundle = new Bundle(); @@ -322,14 +323,8 @@ public class WithdrawWalletFragment extends Fragment { getActivity().getFragmentManager().beginTransaction() .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN) .add(fragment, null) - //.add(R.id.content_frame, fragment, null) .addToBackStack(null) .commit(); - //fragment.setRetainInstance(true); - */ - - editTextAddress.setText("ULRSUDTQLEQLXUMXEOWPEUIHRZUJFPUZRHVBZC9XYIVZJWGFOJNLDHQNQAZPPVOSTVBUT9T9EJRNMGGO9"); - } @Override diff --git a/app/src/main/java/com/flashwifi/wifip2p/iotaAPI/Requests/WalletAddressAndBalanceChecker.java b/app/src/main/java/com/flashwifi/wifip2p/iotaAPI/Requests/WalletAddressAndBalanceChecker.java index 2a768d6..751c004 100644 --- a/app/src/main/java/com/flashwifi/wifip2p/iotaAPI/Requests/WalletAddressAndBalanceChecker.java +++ b/app/src/main/java/com/flashwifi/wifip2p/iotaAPI/Requests/WalletAddressAndBalanceChecker.java @@ -6,8 +6,11 @@ package com.flashwifi.wifip2p.iotaAPI.Requests; import android.content.Context; import android.content.SharedPreferences; +import android.os.AsyncTask; +import android.os.Handler; +import android.os.Message; -import com.flashwifi.wifip2p.R; +import com.flashwifi.wifip2p.AddressBalanceTransfer; import java.util.ArrayList; import java.util.List; @@ -18,16 +21,30 @@ import jota.dto.response.GetNewAddressResponse; import jota.error.ArgumentException; import jota.model.Transaction; -public class WalletAddressAndBalanceChecker { +public class WalletAddressAndBalanceChecker extends AsyncTask { + + private static final int BALANCE_RETRIEVE_TASK_COMPLETE = 1; + + private static final int FUND_WALLET = 0; + private static final int WITHDRAW_WALLET = 1; private static IotaAPI api; private static Context context; private String prefFile; + private String seed; + private Handler mHandler; + private int type; + private Boolean updateMessage; + //GetNodeInfoResponse response = api.getNodeInfo(); - public WalletAddressAndBalanceChecker(Context inActivity, String inPrefFile) { + public WalletAddressAndBalanceChecker(Context inActivity, String inPrefFile, String inSeed, Handler inMHandler, int inType, Boolean inUpdateMessage) { context = inActivity; prefFile = inPrefFile; + seed = inSeed; + mHandler = inMHandler; + type = inType; + updateMessage = inUpdateMessage; //Mainnet node: /* @@ -47,6 +64,50 @@ public class WalletAddressAndBalanceChecker { } + @Override + protected Void doInBackground(Void... voids) { + if(context != null){ + List addressList = getAddress(seed); + + if(addressList != null && addressList.get(0) == "Unable to resolve host"){ + AddressBalanceTransfer addressBalanceTransfer = new AddressBalanceTransfer(null,null,"hostError"); + Message completeMessage = mHandler.obtainMessage(BALANCE_RETRIEVE_TASK_COMPLETE, addressBalanceTransfer); + completeMessage.sendToTarget(); + } + else if(addressList != null){ + + String depositAddress = addressList.get(addressList.size()-1); + String balance = getBalance(addressList); + + if(balance != null){ + if(type == WITHDRAW_WALLET && updateMessage == false){ + AddressBalanceTransfer addressBalanceTransfer = new AddressBalanceTransfer(depositAddress,balance,"noErrorNoUpdateMessage"); + Message completeMessage = mHandler.obtainMessage(BALANCE_RETRIEVE_TASK_COMPLETE,addressBalanceTransfer); + completeMessage.sendToTarget(); + } + else{ + AddressBalanceTransfer addressBalanceTransfer = new AddressBalanceTransfer(depositAddress,balance,"noError"); + Message completeMessage = mHandler.obtainMessage(BALANCE_RETRIEVE_TASK_COMPLETE,addressBalanceTransfer); + completeMessage.sendToTarget(); + } + } + else{ + //Balance Retrieval Error + AddressBalanceTransfer addressBalanceTransfer = new AddressBalanceTransfer(null,null,"balanceError"); + Message completeMessage = mHandler.obtainMessage(BALANCE_RETRIEVE_TASK_COMPLETE, addressBalanceTransfer); + completeMessage.sendToTarget(); + } + } + else{ + //Address Retrieval Error + AddressBalanceTransfer addressBalanceTransfer = new AddressBalanceTransfer(null,null,"addressError"); + Message completeMessage = mHandler.obtainMessage(BALANCE_RETRIEVE_TASK_COMPLETE, addressBalanceTransfer); + completeMessage.sendToTarget(); + } + } + return null; + } + public String getBalance(List inAddresses){ String[] balanceArray; try { @@ -56,7 +117,14 @@ public class WalletAddressAndBalanceChecker { e.printStackTrace(); return null; } - return balanceArray[balanceArray.length-2]; + + if(balanceArray.length>1){ + return balanceArray[balanceArray.length-2]; + } + else{ + return balanceArray[balanceArray.length-1]; + } + } public List getAddress(String seed) { @@ -98,12 +166,19 @@ public class WalletAddressAndBalanceChecker { } else{ //Found transactions, increment for new address - keyIndex+=1; + keyIndex = keyIndex + 1; } } } - //Put the second last address to search - putKeyIndex(keyIndex-1); + + if(keyIndex == 0){ + //Put the initial address to search. No transactions for the seed yet. + putKeyIndex(keyIndex); + } + else{ + //Put the second last address to search + putKeyIndex(keyIndex-1); + } return addressList; } diff --git a/app/src/main/java/com/flashwifi/wifip2p/iotaAPI/Requests/WalletTransferRequest.java b/app/src/main/java/com/flashwifi/wifip2p/iotaAPI/Requests/WalletTransferRequest.java index 37eceae..3156f56 100644 --- a/app/src/main/java/com/flashwifi/wifip2p/iotaAPI/Requests/WalletTransferRequest.java +++ b/app/src/main/java/com/flashwifi/wifip2p/iotaAPI/Requests/WalletTransferRequest.java @@ -1,7 +1,11 @@ package com.flashwifi.wifip2p.iotaAPI.Requests; -import android.app.Activity; -import android.widget.Toast; +import android.content.Context; +import android.os.AsyncTask; +import android.os.Handler; +import android.os.Message; + +import com.flashwifi.wifip2p.AddressBalanceTransfer; import java.util.ArrayList; import java.util.List; @@ -9,29 +13,31 @@ import java.util.List; import jota.IotaAPI; import jota.dto.response.SendTransferResponse; import jota.error.ArgumentException; -import jota.model.Transaction; import jota.model.Transfer; -public class WalletTransferRequest { +public class WalletTransferRequest extends AsyncTask { + + private static final int TRANSFER_TASK_COMPLETE = 2; private static IotaAPI api; - private static Activity activity; + private static Context context; private String appWalletSeed; private String sendAddress; private String sendAmount; private String message; private String tag; private String transferResult; + private Handler mHandler; - - public WalletTransferRequest(String inSendAddress, String inAppWalletSeed, String inSendAmount, String inMessage, String inTag, Activity inActivity) { + public WalletTransferRequest(String inSendAddress, String inAppWalletSeed, String inSendAmount, String inMessage, String inTag, Context inContext, Handler inMHandler) { sendAddress = inSendAddress; appWalletSeed = inAppWalletSeed; sendAmount = inSendAmount; message = inMessage; tag = inTag; - activity = inActivity; + context = inContext; + mHandler = inMHandler; //Mainnet node: /* @@ -51,6 +57,24 @@ public class WalletTransferRequest { } + @Override + protected Void doInBackground(Void... voids) { + sendRequest(); + + String result = null; + + if(context != null){ + result = sendRequest(); + } + + AddressBalanceTransfer addressBalanceTransfer = new AddressBalanceTransfer(null,null,null); + addressBalanceTransfer.setMessage(result); + + Message completeMessage = mHandler.obtainMessage(TRANSFER_TASK_COMPLETE, addressBalanceTransfer); + completeMessage.sendToTarget(); + return null; + } + public String sendRequest(){ List transfers = new ArrayList<>(); @@ -106,5 +130,4 @@ public class WalletTransferRequest { } return transferResult; } - } diff --git a/app/src/main/res/drawable/iota_progress.gif b/app/src/main/res/drawable/iota_progress.gif new file mode 100644 index 0000000..f5a3140 Binary files /dev/null and b/app/src/main/res/drawable/iota_progress.gif differ diff --git a/app/src/main/res/layout/fragment_fund_wallet.xml b/app/src/main/res/layout/fragment_fund_wallet.xml index 0f5a9d2..6bc8286 100644 --- a/app/src/main/res/layout/fragment_fund_wallet.xml +++ b/app/src/main/res/layout/fragment_fund_wallet.xml @@ -1,110 +1,137 @@ - + android:layout_height="match_parent"> - - - - - - - - - - - - - - - - + android:fillViewport="true"> - + android:background="#1365E5" + android:orientation="vertical"> - + - + - \ No newline at end of file + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_withdraw_wallet.xml b/app/src/main/res/layout/fragment_withdraw_wallet.xml index 0d4017b..a2cc643 100644 --- a/app/src/main/res/layout/fragment_withdraw_wallet.xml +++ b/app/src/main/res/layout/fragment_withdraw_wallet.xml @@ -1,204 +1,227 @@ - + android:layout_height="match_parent"> - + - - - - - - - - - - - - - - - - - - - - + + - + android:background="#1365E5" + android:orientation="horizontal"> - + + + + + + + android:background="#1570FF" + android:animateLayoutChanges="true" + android:orientation="vertical"> + + + + + + + + + + + + + + + + + + + + + +