use of run.wallet.iota.api.requests.GetFirstLoadRequest in project run-wallet-android by runplay.
the class GetFirstLoadRequestHandler method handle.
@Override
public ApiResponse handle(ApiRequest request) {
FirstTimeHolder holder = new FirstTimeHolder();
// StopWatch stopWatch = new StopWatch();
GetFirstLoadRequest firstLoadRequest = (GetFirstLoadRequest) request;
Wallet wallet = null;
List<Address> allAddresses = new ArrayList<>();
List<Transfer> transfers = new ArrayList<>();
long started = System.currentTimeMillis();
if (!firstLoadRequest.getSeed().isappgenerated) {
holders.put(firstLoadRequest.getSeed().id, holder);
while (holder.userConfirmedBalance == null || started < System.currentTimeMillis() - 60000) {
try {
this.wait(1000);
} catch (Exception e) {
}
}
} else {
holder.userConfirmedBalance = false;
holders.put(firstLoadRequest.getSeed().id, holder);
}
boolean userDeclaredBalance = holder.userConfirmedBalance == null ? false : holder.userConfirmedBalance.booleanValue();
holder.userConfirmedBalance = userDeclaredBalance ? Boolean.TRUE : Boolean.FALSE;
// Log.e("FL","uc="+holder.userConfirmedBalance+" - ud="+userDeclaredBalance);
if (!userDeclaredBalance || firstLoadRequest.getSeed().isappgenerated) {
try {
final GetNewAddressResponse gna = apiProxy.getNewAddress(String.valueOf(Store.getSeedRaw(context, firstLoadRequest.getSeed())), firstLoadRequest.getSecurity(), 0, false, 1, false);
for (String add : gna.getAddresses()) {
Address newaddress = new Address(add, false, false);
newaddress.setIndexName(1);
allAddresses.add(newaddress);
}
} catch (Exception e) {
}
wallet = new Wallet(firstLoadRequest.getSeed().id, 0, System.currentTimeMillis());
} else {
long timestamp = System.currentTimeMillis();
holder.showWaitMessage = true;
holder.predictaddress = 0;
Map<Integer, Address> already = new HashMap<>();
int start = 0;
boolean stop = false;
List<Bundle> allbundles = new ArrayList<>();
Map<String, Boolean> hasalready = new HashMap<>();
wallet = new Wallet(((GetFirstLoadRequest) request).getSeed().id, 0, System.currentTimeMillis());
List<Transfer> addInTransfers = new ArrayList<>();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
final int maxAddresses = Sf.toInt(prefs.getString(Constants.PREF_FIRST_LOAD_ATTEMPTS, "" + Constants.PREF_FIRST_LOAD_ATTEMPTS_DEFAULT));
final int stopWhenCountEmpty = Sf.toInt(prefs.getString(Constants.PREF_FIRST_LOAD_RANGE, "" + Constants.PREF_FIRST_LOAD_RANGE_DEFAULT));
while (!stop) {
Address alreadyAddress = already.get(start);
Map<String, Boolean> snapshotBalanceAlreadyAddresses = new HashMap();
boolean foundTransfers = false;
if (alreadyAddress != null) {
holder.countaddress++;
alreadyAddress.setIndex(start);
alreadyAddress.setIndexName(start + 1);
allAddresses.add(alreadyAddress);
} else {
Address newaddress = null;
long useBalance = 0L;
try {
GetNewAddressResponse gnr = apiProxy.getNewAddress(String.valueOf(Store.getSeedRaw(context, firstLoadRequest.getSeed())), firstLoadRequest.getSecurity(), start, false, 1, false);
String add = gnr.getAddresses().get(0);
// Log.e("FIRST-TIME", "CALC ADDRESS: " + add + " -- " + start+"-");
newaddress = new Address(add, false, false);
holder.countaddress++;
newaddress.setIndex(start);
newaddress.setIndexName(start + 1);
allAddresses.add(newaddress);
List<String> tmpadd = new ArrayList<>();
tmpadd.add(newaddress.getAddress());
GetBalancesResponse gbal = apiProxy.getBalances(100, tmpadd);
useBalance = Sf.toLong(gbal.getBalances()[0]);
newaddress.setValue(useBalance);
newaddress.setLastMilestone(gbal.getMilestoneIndex());
newaddress.setAttached(true);
Bundle[] bundles = apiProxy.bundlesFromAddresses(new String[] { newaddress.getAddress() }, true);
if (bundles != null && bundles.length > 0) {
newaddress.setAttached(true);
foundTransfers = true;
for (int i = 0; i < bundles.length; i++) {
String hash = bundles[i].getTransactions().get(0).getHash();
if (hasalready.get(hash) == null) {
hasalready.put(hash, true);
allbundles.add(bundles[i]);
}
}
}
long oldWallet = wallet.getBalance();
long expecting = oldWallet + useBalance;
holder.predictaddress = expecting;
transfers.clear();
Audit.bundlePopulateTransfers(allbundles.toArray(new Bundle[allbundles.size()]), transfers, allAddresses);
Audit.setTransfersToAddresses(((GetFirstLoadRequest) request).getSeed(), transfers, allAddresses, wallet, addInTransfers);
if (wallet.getBalance() != expecting && useBalance > 0) {
if (snapshotBalanceAlreadyAddresses.get(newaddress.getAddress()) == null) {
long theBalance = expecting - wallet.getBalance();
// Log.e("MK-SNAP","snap::: set-bal: "+theBalance+" - wallet:"+wallet.getBalance()+" - expect:"+expecting+" - add-bal"+useBalance+" - snap-add-tran-value:"+newaddress.getValue()+" - on-address:"+newaddress.getAddress());
Transfer addInTran = new Transfer(timestamp, newaddress.getAddress(), "SNAP" + SeedRandomGenerator.generateNewSeed().substring(0, 23), true, theBalance, "Snapshot balance confirmed", "SNAP999999999999999999");
TransferTransaction tt = new TransferTransaction(newaddress.getAddress(), theBalance);
List<TransferTransaction> tmptt = new ArrayList<>();
tmptt.add(tt);
addInTran.setTransactions(tmptt);
addInTransfers.add(addInTran);
Audit.setTransfersToAddresses(((GetFirstLoadRequest) request).getSeed(), transfers, allAddresses, wallet, addInTransfers);
}
}
snapshotBalanceAlreadyAddresses.put(newaddress.getAddress(), true);
holder.counttransfers = transfers.size();
} catch (Exception e) {
Log.e("ERR-FLR020", "address index: " + start + ", ex: " + e.getMessage());
}
}
int lastComplete = 0;
// boolean hasLastComplete=false;
for (int i = 0; i < allAddresses.size(); i++) {
Address tmpadd = allAddresses.get(i);
if (tmpadd.getValue() != 0) {
lastComplete = i;
}
}
// Log.e("WALLET","current value: "+wallet.getBalance()+" - "+wallet.getBalancePendingIn()+" -- "+wallet.getBalancePendingOut());
int countempty = 0;
if (!foundTransfers && lastComplete > 0 && allAddresses.size() >= stopWhenCountEmpty) {
for (int i = allAddresses.size() - 1; i >= 0 && i > lastComplete; i--) {
countempty++;
}
}
start++;
// Log.e("COUNT_EMPTY",allAddresses.size()+" addresses, empty: "+countempty);
if (countempty >= stopWhenCountEmpty || start > maxAddresses) {
stop = true;
}
}
for (Transfer tran : transfers) {
if (!tran.getTransactions().isEmpty()) {
if (tran.getTimestamp() < timestamp)
timestamp = tran.getTimestamp();
}
}
timestamp = timestamp - 600000;
for (Transfer addIn : addInTransfers) {
addIn.setTimestamp(--timestamp);
}
allAddresses = allAddresses.subList(0, allAddresses.size() - (stopWhenCountEmpty));
Audit.setTransfersToAddresses(((GetFirstLoadRequest) request).getSeed(), transfers, allAddresses, wallet, addInTransfers);
}
boolean hasTransfer = false;
for (int i = allAddresses.size() - 1; i >= 0; i--) {
Address address = allAddresses.get(i);
if (address.getValue() != 0 || address.getPendingValue() != 0) {
hasTransfer = true;
} else if (hasTransfer) {
address.setUsed(true);
}
}
Store.setAccountData(context, ((GetFirstLoadRequest) request).getSeed(), wallet, transfers, allAddresses);
holder.isFinished = true;
AppService.auditAddressesWithDelay(context, ((GetFirstLoadRequest) request).getSeed());
return new GetFirstLoadResponse();
}
use of run.wallet.iota.api.requests.GetFirstLoadRequest in project run-wallet-android by runplay.
the class AppService method getFirstTimeLoad.
public static void getFirstTimeLoad(Context context) {
if (Validator.isValidCaller() && Store.getCurrentSeed() != null) {
TaskManager rt = new TaskManager(SERVICE);
GetFirstLoadRequest gna = new GetFirstLoadRequest(Store.getCurrentSeed());
runTask(rt, gna);
}
}
use of run.wallet.iota.api.requests.GetFirstLoadRequest in project run-wallet-android by runplay.
the class UiManager method displayInfoBar.
public static void displayInfoBar(Activity context, LinearLayout forView) {
makeElements(context);
forView.setBackgroundColor(B.getColor(context, AppTheme.getPrimaryDark()));
try {
List<ApiRequest> attaching = AppService.getRunningTasks();
List<LinearLayout> processing = new ArrayList<>();
if (!attaching.isEmpty()) {
Seeds.Seed currentSeed = Store.getCurrentSeed();
for (ApiRequest req : attaching) {
if (req instanceof SendTransferRequest) {
SendTransferRequest str = (SendTransferRequest) req;
if (str.getSeed().id.equals(currentSeed.id)) {
if (str.getValue() == 0) {
processing.add(createProcessRunningPod(context, R.drawable.tran_white, context.getString(R.string.info_new_attach), 0));
} else {
processing.add(createProcessRunningPod(context, R.drawable.send_white, context.getString(R.string.info_transfer), str.getValue()));
}
}
} else if (req instanceof GetAccountDataRequest) {
GetAccountDataRequest request = (GetAccountDataRequest) req;
if (request.getSeed().id.equals(currentSeed.id)) {
processing.add(createProcessRunningPod(context, R.drawable.refresh_white, context.getString(R.string.info_refresh), 0));
}
} else if (req instanceof GetNewAddressRequest) {
GetNewAddressRequest request = (GetNewAddressRequest) req;
if (request.getSeed().id.equals(currentSeed.id)) {
processing.add(createProcessRunningPod(context, R.drawable.tran_white, context.getString(R.string.info_new_address), 0));
}
} else if (req instanceof GetFirstLoadRequest) {
GetFirstLoadRequest request = (GetFirstLoadRequest) req;
if (request.getSeed().id.equals(currentSeed.id)) {
processing.add(createProcessRunningPod(context, R.drawable.refresh_white, context.getString(R.string.info_first_load), 0));
}
} else if (req instanceof AuditAddressesRequest) {
AuditAddressesRequest request = (AuditAddressesRequest) req;
if (request.getSeed().id.equals(currentSeed.id)) {
processing.add(createProcessRunningPod(context, R.drawable.refresh_white, context.getString(R.string.info_audit), 0));
}
} else if (req instanceof ReplayBundleRequest) {
ReplayBundleRequest request = (ReplayBundleRequest) req;
if (request.getSeed().id.equals(currentSeed.id)) {
processing.add(createProcessRunningPod(context, R.drawable.send_white, context.getString(R.string.info_resend), 0));
}
} else if (req instanceof NudgeRequest) {
NudgeRequest request = (NudgeRequest) req;
if (request.getSeed().id.equals(currentSeed.id)) {
processing.add(createProcessRunningPod(context, R.drawable.send_white, context.getString(R.string.info_nudge), 0));
}
} else if (req instanceof AutoNudgeRequest) {
// if(request.getSeed().id.equals(currentSeed.id)) {
processing.add(createProcessRunningPod(context, R.drawable.send_white, context.getString(R.string.auto), 0));
// }
} else if (req instanceof RefreshUsedAddressesRequest) {
// if(request.getSeed().id.equals(currentSeed.id)) {
processing.add(createProcessRunningPod(context, R.drawable.send_white, context.getString(R.string.info_audit), 0));
// }
}
}
}
forView.removeAllViews();
if (!processing.isEmpty()) {
for (LinearLayout addview : processing) {
forView.addView(addview);
}
forView.canScrollHorizontally(View.LAYOUT_DIRECTION_LTR);
if (forView.getVisibility() != View.VISIBLE) {
forView.setVisibility(View.VISIBLE);
AnimationUtils.loadAnimation(context, R.anim.slide_in_from_bottom);
}
} else if (Store.getUsedAddressCheckResult() != null) {
Snackbar.make(forView, Store.getUsedAddressCheckResult(), Snackbar.LENGTH_LONG).show();
Store.setUsedAddressCheckResult(null);
} else if (Store.getCurrentSeed().warnUsed) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean showused = prefs.getBoolean(Constants.PREFERENCES_SHOW_USED, true);
TextView messy = new TextView(context);
if (showused) {
messy.setText(context.getString(R.string.usedAddressWarn));
} else {
messy.setText(context.getString(R.string.usedAddressSettings));
}
messy.setTextColor(Color.WHITE);
messy.setPadding(30, 30, 30, 30);
messy.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent settings = new Intent(context, SettingsActivity.class);
context.startActivityForResult(settings, 0);
}
});
forView.addView(messy);
forView.canScrollHorizontally(View.LAYOUT_DIRECTION_LTR);
if (forView.getVisibility() != View.VISIBLE) {
AnimationUtils.loadAnimation(context, R.anim.slide_in_from_bottom);
}
} else {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
if (prefs.getBoolean(Constants.PREFERENCES_SHOW_CANCELLED, true) && !prefs.getBoolean(Constants.PREFERENCES_SHOW_ATTACH, true) && Store.getTransfers().size() > 15) {
int count = prefs.getInt(Constants.PREF_MSG_MESSY, 0);
if (count < 2) {
TextView messy = new TextView(context);
messy.setText(context.getString(R.string.message_messy_view));
messy.setTextColor(Color.WHITE);
messy.setPadding(30, 30, 30, 30);
// messy.setText
messy.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent settings = new Intent(context, SettingsActivity.class);
context.startActivityForResult(settings, 0);
}
});
forView.addView(messy);
forView.canScrollHorizontally(View.LAYOUT_DIRECTION_LTR);
if (forView.getVisibility() != View.VISIBLE) {
AnimationUtils.loadAnimation(context, R.anim.slide_in_from_bottom);
}
prefs.edit().putInt(Constants.PREF_MSG_MESSY, ++count).commit();
}
} else {
AnimationUtils.loadAnimation(context, R.anim.slide_out_to_bottom);
}
}
} catch (Exception e) {
}
}
Aggregations