Change Sync funciton to Async functions
This commit is contained in:
parent
b63feeed17
commit
9c62f7a422
1 changed files with 91 additions and 75 deletions
|
@ -1,5 +1,6 @@
|
||||||
package cy.agorise.crystalwallet.manager;
|
package cy.agorise.crystalwallet.manager;
|
||||||
|
|
||||||
|
import android.annotation.SuppressLint;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
|
||||||
import com.google.common.primitives.UnsignedLong;
|
import com.google.common.primitives.UnsignedLong;
|
||||||
|
@ -31,6 +32,7 @@ import cy.agorise.crystalwallet.models.AccountSeed;
|
||||||
import cy.agorise.crystalwallet.models.BitsharesAsset;
|
import cy.agorise.crystalwallet.models.BitsharesAsset;
|
||||||
import cy.agorise.crystalwallet.models.BitsharesAssetInfo;
|
import cy.agorise.crystalwallet.models.BitsharesAssetInfo;
|
||||||
import cy.agorise.crystalwallet.models.CryptoCoinTransaction;
|
import cy.agorise.crystalwallet.models.CryptoCoinTransaction;
|
||||||
|
import cy.agorise.crystalwallet.models.CryptoCurrency;
|
||||||
import cy.agorise.crystalwallet.models.CryptoNetAccount;
|
import cy.agorise.crystalwallet.models.CryptoNetAccount;
|
||||||
import cy.agorise.crystalwallet.models.GrapheneAccount;
|
import cy.agorise.crystalwallet.models.GrapheneAccount;
|
||||||
import cy.agorise.crystalwallet.models.GrapheneAccountInfo;
|
import cy.agorise.crystalwallet.models.GrapheneAccountInfo;
|
||||||
|
@ -56,6 +58,9 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI
|
||||||
|
|
||||||
private final static String BITSHARES_TESTNET_CHAIN_ID= "9cf6f255a208100d2bb275a3c52f4b1589b7ec9c9bfc2cb2a5fe6411295106d8";
|
private final static String BITSHARES_TESTNET_CHAIN_ID= "9cf6f255a208100d2bb275a3c52f4b1589b7ec9c9bfc2cb2a5fe6411295106d8";
|
||||||
|
|
||||||
|
private final static String SIMPLE_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss";
|
||||||
|
private final static String DEFAULT_TIME_ZONE = "GMT";
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void createAccountFromSeed(CryptoNetAccount account, final ManagerRequest request, final Context context) {
|
public void createAccountFromSeed(CryptoNetAccount account, final ManagerRequest request, final Context context) {
|
||||||
if(account instanceof GrapheneAccount) {
|
if(account instanceof GrapheneAccount) {
|
||||||
|
@ -220,6 +225,7 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI
|
||||||
this.validateCreateAccount((ValidateCreateBitsharesAccountRequest) request);
|
this.validateCreateAccount((ValidateCreateBitsharesAccountRequest) request);
|
||||||
}else{
|
}else{
|
||||||
//TODO not implemented
|
//TODO not implemented
|
||||||
|
System.out.println("Error request not implemented " + request.getClass().getName());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -273,26 +279,28 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI
|
||||||
AccountSeed seed = AccountSeed.getAccountSeed(SeedType.BIP39, context);
|
AccountSeed seed = AccountSeed.getAccountSeed(SeedType.BIP39, context);
|
||||||
CrystalDatabase db = CrystalDatabase.getAppDatabase(context);
|
CrystalDatabase db = CrystalDatabase.getAppDatabase(context);
|
||||||
long idSeed = db.accountSeedDao().insertAccountSeed(seed);
|
long idSeed = db.accountSeedDao().insertAccountSeed(seed);
|
||||||
|
assert seed != null;
|
||||||
seed.setId(idSeed);
|
seed.setId(idSeed);
|
||||||
seed.setName(createRequest.getAccountName());
|
seed.setName(createRequest.getAccountName());
|
||||||
GrapheneAccount account = new GrapheneAccount();
|
GrapheneAccount account = new GrapheneAccount();
|
||||||
account.setName(createRequest.getAccountName());
|
account.setName(createRequest.getAccountName());
|
||||||
account.setSeedId(idSeed);
|
account.setSeedId(idSeed);
|
||||||
account.setAccountIndex(0);
|
account.setAccountIndex(0);
|
||||||
account.setCryptoNet(CryptoNet.BITSHARES);
|
account.setCryptoNet(CryptoNet.BITSHARES);
|
||||||
this.createAccountFromSeed(account,new ManagerRequest(){
|
this.createAccountFromSeed(account, new ManagerRequest() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void success(Object answer) {
|
public void success(Object answer) {
|
||||||
createRequest.setAccountExists(false);
|
createRequest.setAccountExists(false);
|
||||||
createRequest.setAccount((GrapheneAccount)answer);
|
createRequest.setAccount((GrapheneAccount) answer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void fail() {
|
||||||
|
createRequest.setAccountExists(true);
|
||||||
|
}
|
||||||
|
}, context);
|
||||||
|
|
||||||
@Override
|
|
||||||
public void fail() {
|
|
||||||
createRequest.setAccountExists(true);
|
|
||||||
}
|
|
||||||
},context );
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -324,14 +332,49 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI
|
||||||
/**
|
/**
|
||||||
* Broadcast a transaction request
|
* Broadcast a transaction request
|
||||||
*/
|
*/
|
||||||
private void validateSendRequest(final ValidateBitsharesSendRequest sendRequest){
|
private void validateSendRequest(final ValidateBitsharesSendRequest sendRequest) {
|
||||||
//TODO feeAsset
|
//TODO feeAsset
|
||||||
final String idAsset = getAssetInfoByName(sendRequest.getAsset());
|
CrystalDatabase db = CrystalDatabase.getAppDatabase(sendRequest.getContext());
|
||||||
|
CryptoCurrency currency = db.cryptoCurrencyDao().getByNameAndCryptoNet(sendRequest.getAsset(), CryptoNet.BITSHARES.name());
|
||||||
|
if (currency == null){
|
||||||
|
getAssetInfoByName(sendRequest.getAsset(), new ManagerRequest() {
|
||||||
|
@Override
|
||||||
|
public void success(Object answer) {
|
||||||
|
validateSendRequest(sendRequest, ((BitsharesAsset) answer).getBitsharesId());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void fail() {
|
||||||
|
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}else{
|
||||||
|
BitsharesAssetInfo info = db.bitsharesAssetDao().getBitsharesAssetInfo(currency.getId());
|
||||||
|
if (info == null || info.getBitsharesId() == null || info.getBitsharesId().isEmpty()){
|
||||||
|
getAssetInfoByName(sendRequest.getAsset(), new ManagerRequest() {
|
||||||
|
@Override
|
||||||
|
public void success(Object answer) {
|
||||||
|
validateSendRequest(sendRequest, ((BitsharesAsset) answer).getBitsharesId());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void fail() {
|
||||||
|
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}else {
|
||||||
|
this.validateSendRequest(sendRequest, info.getBitsharesId());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void validateSendRequest(final ValidateBitsharesSendRequest sendRequest , final String idAsset){
|
||||||
final Asset feeAsset = new Asset(idAsset);
|
final Asset feeAsset = new Asset(idAsset);
|
||||||
final UserAccount fromUserAccount =new UserAccount(sendRequest.getSourceAccount().getAccountId());
|
final UserAccount fromUserAccount =new UserAccount(sendRequest.getSourceAccount().getAccountId());
|
||||||
|
|
||||||
//TODO cached to accounts
|
//TODO cached to accounts
|
||||||
this.getAccountInfoByName(sendRequest.getToAccount(), new ManagerRequest() {
|
this.getAccountInfoByName(sendRequest.getToAccount(), new ManagerRequest() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void success(Object answer) {
|
public void success(Object answer) {
|
||||||
GrapheneAccount toUserGrapheneAccount = (GrapheneAccount) answer;
|
GrapheneAccount toUserGrapheneAccount = (GrapheneAccount) answer;
|
||||||
|
@ -344,6 +387,7 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI
|
||||||
if(sendRequest.getMemo() != null) {
|
if(sendRequest.getMemo() != null) {
|
||||||
//builder.setMemo(new Memo(fromUserAccount,toUserAccount,0,sendRequest.getMemo().getBytes()));
|
//builder.setMemo(new Memo(fromUserAccount,toUserAccount,0,sendRequest.getMemo().getBytes()));
|
||||||
//TODO memo
|
//TODO memo
|
||||||
|
System.out.println("transaction has memo");
|
||||||
}
|
}
|
||||||
ArrayList<BaseOperation> operationList = new ArrayList<>();
|
ArrayList<BaseOperation> operationList = new ArrayList<>();
|
||||||
operationList.add(builder.build());
|
operationList.add(builder.build());
|
||||||
|
@ -402,27 +446,13 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO expand function to be more generic
|
//TODO expand function to be more generic
|
||||||
private String getAssetInfoByName(String assetName){
|
private void getAssetInfoByName(String assetName, ManagerRequest request){
|
||||||
final Object SYNC = new Object();
|
|
||||||
long timeout = 60000;
|
|
||||||
|
|
||||||
AssetIdOrNameListener nameListener = new AssetIdOrNameListener(SYNC);
|
AssetIdOrNameListener nameListener = new AssetIdOrNameListener(request);
|
||||||
ApiRequest request = new ApiRequest(0, nameListener);
|
ApiRequest assetRequest = new ApiRequest(0, nameListener);
|
||||||
ArrayList<String> assetNames = new ArrayList<>();
|
ArrayList<String> assetNames = new ArrayList<>();
|
||||||
assetNames.add(assetName);
|
assetNames.add(assetName);
|
||||||
GrapheneApiGenerator.getAssetByName(assetNames, request);
|
GrapheneApiGenerator.getAssetByName(assetNames, assetRequest);
|
||||||
|
|
||||||
long cTime = System.currentTimeMillis();
|
|
||||||
|
|
||||||
while(!nameListener.ready && (System.currentTimeMillis()-cTime) < timeout){
|
|
||||||
synchronized (SYNC){
|
|
||||||
try {
|
|
||||||
SYNC.wait(100);
|
|
||||||
} catch (InterruptedException ignore) {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nameListener.asset.getBitsharesId();
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -431,7 +461,7 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI
|
||||||
* @param idAccount database id of the account
|
* @param idAccount database id of the account
|
||||||
* @param context The android context of this application
|
* @param context The android context of this application
|
||||||
*/
|
*/
|
||||||
public static void refreshAccountTransactions(long idAccount, Context context){
|
private static void refreshAccountTransactions(long idAccount, Context context){
|
||||||
CrystalDatabase db = CrystalDatabase.getAppDatabase(context);
|
CrystalDatabase db = CrystalDatabase.getAppDatabase(context);
|
||||||
List<CryptoCoinTransaction> transactions = db.transactionDao().getByIdAccount(idAccount);
|
List<CryptoCoinTransaction> transactions = db.transactionDao().getByIdAccount(idAccount);
|
||||||
CryptoNetAccount account = db.cryptoNetAccountDao().getById(idAccount);
|
CryptoNetAccount account = db.cryptoNetAccountDao().getById(idAccount);
|
||||||
|
@ -487,23 +517,22 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handles the succes request of the transaction, if the amount of transaction is equal to the limit, ask for more transaction
|
* Handles the success request of the transaction, if the amount of transaction is equal to the limit, ask for more transaction
|
||||||
* @param answer The answer, this object depends on the kind of request is made to the api
|
* @param answer The answer, this object depends on the kind of request is made to the api
|
||||||
* @param idPetition the id of the ApiRequest petition
|
* @param idPetition the id of the ApiRequest petition
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void success(Object answer, int idPetition) {
|
public void success(Object answer, int idPetition) {
|
||||||
List<HistoricalTransfer> transfers = (List<HistoricalTransfer>) answer ;
|
List<HistoricalTransfer> transfers = (List<HistoricalTransfer>) answer ;
|
||||||
for(HistoricalTransfer transfer : transfers) {
|
for(final HistoricalTransfer transfer : transfers) {
|
||||||
if (transfer.getOperation() != null){
|
if (transfer.getOperation() != null){
|
||||||
CryptoCoinTransaction transaction = new CryptoCoinTransaction();
|
final CryptoCoinTransaction transaction = new CryptoCoinTransaction();
|
||||||
transaction.setAccountId(account.getId());
|
transaction.setAccountId(account.getId());
|
||||||
transaction.setAmount(transfer.getOperation().getAssetAmount().getAmount().longValue());
|
transaction.setAmount(transfer.getOperation().getAssetAmount().getAmount().longValue());
|
||||||
BitsharesAssetInfo info = db.bitsharesAssetDao().getBitsharesAssetInfoById(transfer.getOperation().getAssetAmount().getAsset().getObjectId());
|
BitsharesAssetInfo info = db.bitsharesAssetDao().getBitsharesAssetInfoById(transfer.getOperation().getAssetAmount().getAsset().getObjectId());
|
||||||
|
|
||||||
if (info == null) {
|
if (info == null) {
|
||||||
//The cryptoCurrency is not in the database, queringfor its data
|
//The cryptoCurrency is not in the database, queringfor its data
|
||||||
final Object SYNC = new Object(); //Object to syn the answer
|
|
||||||
ApiRequest assetRequest = new ApiRequest(0, new ApiRequestListener() {
|
ApiRequest assetRequest = new ApiRequest(0, new ApiRequestListener() {
|
||||||
@Override
|
@Override
|
||||||
public void success(Object answer, int idPetition) {
|
public void success(Object answer, int idPetition) {
|
||||||
|
@ -514,39 +543,24 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI
|
||||||
info.setCryptoCurrencyId(idCryptoCurrency);
|
info.setCryptoCurrencyId(idCryptoCurrency);
|
||||||
asset.setId((int)idCryptoCurrency);
|
asset.setId((int)idCryptoCurrency);
|
||||||
db.bitsharesAssetDao().insertBitsharesAssetInfo(info);
|
db.bitsharesAssetDao().insertBitsharesAssetInfo(info);
|
||||||
|
saveTransaction(transaction,info,transfer);
|
||||||
}
|
}
|
||||||
synchronized (SYNC){
|
|
||||||
SYNC.notifyAll();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void fail(int idPetition) {
|
public void fail(int idPetition) {
|
||||||
synchronized (SYNC){
|
//TODO Error
|
||||||
SYNC.notifyAll();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
ArrayList<String> assets = new ArrayList<>();
|
ArrayList<String> assets = new ArrayList<>();
|
||||||
assets.add(transfer.getOperation().getAssetAmount().getAsset().getObjectId());
|
assets.add(transfer.getOperation().getAssetAmount().getAsset().getObjectId());
|
||||||
GrapheneApiGenerator.getAssetById(assets,assetRequest);
|
GrapheneApiGenerator.getAssetById(assets,assetRequest);
|
||||||
|
|
||||||
synchronized (SYNC){
|
}else{
|
||||||
try {SYNC.wait(60000);} catch (InterruptedException ignore) {}
|
saveTransaction(transaction,info,transfer);
|
||||||
}
|
|
||||||
info = db.bitsharesAssetDao().getBitsharesAssetInfoById(transfer.getOperation().getAssetAmount().getAsset().getObjectId());
|
|
||||||
}
|
}
|
||||||
if( info == null){
|
|
||||||
//We couldn't retrieve the cryptocurrency
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
transaction.setIdCurrency((int)info.getCryptoCurrencyId());
|
|
||||||
transaction.setConfirmed(true); //graphene transaction are always confirmed
|
|
||||||
transaction.setFrom(transfer.getOperation().getFrom().getObjectId());
|
|
||||||
transaction.setInput(!transfer.getOperation().getFrom().getObjectId().equals(account.getAccountId()));
|
|
||||||
transaction.setTo(transfer.getOperation().getTo().getObjectId());
|
|
||||||
|
|
||||||
GrapheneApiGenerator.getBlockHeaderTime(transfer.getBlockNum(), new ApiRequest(0, new GetTransactionDate(transaction, db.transactionDao())));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(transfers.size()>= limit){
|
if(transfers.size()>= limit){
|
||||||
|
@ -562,6 +576,16 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI
|
||||||
public void fail(int idPetition) {
|
public void fail(int idPetition) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void saveTransaction(CryptoCoinTransaction transaction, BitsharesAssetInfo info, HistoricalTransfer transfer){
|
||||||
|
transaction.setIdCurrency((int)info.getCryptoCurrencyId());
|
||||||
|
transaction.setConfirmed(true); //graphene transaction are always confirmed
|
||||||
|
transaction.setFrom(transfer.getOperation().getFrom().getObjectId());
|
||||||
|
transaction.setInput(!transfer.getOperation().getFrom().getObjectId().equals(account.getAccountId()));
|
||||||
|
transaction.setTo(transfer.getOperation().getTo().getObjectId());
|
||||||
|
|
||||||
|
GrapheneApiGenerator.getBlockHeaderTime(transfer.getBlockNum(), new ApiRequest(0, new GetTransactionDate(transaction, db.transactionDao())));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -585,6 +609,7 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI
|
||||||
GrapheneApiGenerator.getEquivalentValue(fromAsset.getBitsharesId(),toAsset.getBitsharesId(), getEquivalentRequest);
|
GrapheneApiGenerator.getEquivalentValue(fromAsset.getBitsharesId(),toAsset.getBitsharesId(), getEquivalentRequest);
|
||||||
}else{
|
}else{
|
||||||
//TODO error
|
//TODO error
|
||||||
|
System.out.println("Equivalen Value error ");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -622,36 +647,27 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI
|
||||||
* Class to retrieve the asset id or the asset name, if one of those is missing
|
* Class to retrieve the asset id or the asset name, if one of those is missing
|
||||||
*/
|
*/
|
||||||
private class AssetIdOrNameListener implements ApiRequestListener{
|
private class AssetIdOrNameListener implements ApiRequestListener{
|
||||||
final Object SYNC;
|
final ManagerRequest request;
|
||||||
boolean ready = false;
|
|
||||||
|
|
||||||
BitsharesAsset asset;
|
BitsharesAsset asset;
|
||||||
|
|
||||||
AssetIdOrNameListener(Object SYNC) {
|
AssetIdOrNameListener(ManagerRequest request) {
|
||||||
this.SYNC = SYNC;
|
this.request = request;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void success(Object answer, int idPetition) {
|
public void success(Object answer, int idPetition) {
|
||||||
if(answer instanceof ArrayList) {
|
if(answer instanceof ArrayList) {
|
||||||
|
|
||||||
if (((ArrayList) answer).get(0) instanceof BitsharesAsset) {
|
if (((ArrayList) answer).get(0) instanceof BitsharesAsset) {
|
||||||
asset = (BitsharesAsset) ((ArrayList) answer).get(0);
|
asset = (BitsharesAsset) ((ArrayList) answer).get(0);
|
||||||
|
request.success(asset);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
synchronized (SYNC){
|
|
||||||
ready = true;
|
|
||||||
SYNC.notifyAll();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void fail(int idPetition) {
|
public void fail(int idPetition) {
|
||||||
synchronized (SYNC){
|
//TODO fail asset retrieve
|
||||||
ready = true;
|
|
||||||
SYNC.notifyAll();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -668,7 +684,7 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI
|
||||||
*/
|
*/
|
||||||
TransactionDao transactionDao;
|
TransactionDao transactionDao;
|
||||||
|
|
||||||
public GetTransactionDate(CryptoCoinTransaction transaction, TransactionDao transactionDao) {
|
GetTransactionDate(CryptoCoinTransaction transaction, TransactionDao transactionDao) {
|
||||||
this.transaction = transaction;
|
this.transaction = transaction;
|
||||||
this.transactionDao = transactionDao;
|
this.transactionDao = transactionDao;
|
||||||
}
|
}
|
||||||
|
@ -676,8 +692,8 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI
|
||||||
@Override
|
@Override
|
||||||
public void success(Object answer, int idPetition) {
|
public void success(Object answer, int idPetition) {
|
||||||
if(answer instanceof BlockHeader){
|
if(answer instanceof BlockHeader){
|
||||||
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
|
@SuppressLint("SimpleDateFormat") SimpleDateFormat dateFormat = new SimpleDateFormat(SIMPLE_DATE_FORMAT);
|
||||||
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
|
dateFormat.setTimeZone(TimeZone.getTimeZone(DEFAULT_TIME_ZONE));
|
||||||
try {
|
try {
|
||||||
transaction.setDate(dateFormat.parse(((BlockHeader) answer).timestamp));
|
transaction.setDate(dateFormat.parse(((BlockHeader) answer).timestamp));
|
||||||
if (transactionDao.getByTransaction(transaction.getDate(),transaction.getFrom(),transaction.getTo(),transaction.getAmount()) == null) {
|
if (transactionDao.getByTransaction(transaction.getDate(),transaction.getFrom(),transaction.getTo(),transaction.getAmount()) == null) {
|
||||||
|
|
Loading…
Reference in a new issue