use of com.odysee.app.model.WalletSync in project odysee-android by OdyseeTeam.
the class MainActivity method syncSet.
public void syncSet(String hash, String data) {
if (syncSetTask == null || syncSetTask.getStatus() == AsyncTask.Status.FINISHED) {
syncSetTask = new SyncSetTask(Lbryio.lastRemoteHash, hash, data, new DefaultSyncTaskHandler() {
@Override
public void onSyncSetSuccess(String hash) {
Lbryio.lastRemoteHash = hash;
Lbryio.lastWalletSync = new WalletSync(hash, data);
if (!pendingSyncSetQueue.isEmpty()) {
fullSyncInProgress = true;
WalletSync nextSync = pendingSyncSetQueue.remove(0);
syncSet(nextSync.getHash(), nextSync.getData());
} else if (queuedSyncCount > 0) {
queuedSyncCount--;
syncApplyAndSet();
}
fullSyncInProgress = false;
}
@Override
public void onSyncSetError(Exception error) {
// log app exceptions
if (!pendingSyncSetQueue.isEmpty()) {
WalletSync nextSync = pendingSyncSetQueue.remove(0);
syncSet(nextSync.getHash(), nextSync.getData());
} else if (queuedSyncCount > 0) {
queuedSyncCount--;
syncApplyAndSet();
}
fullSyncInProgress = false;
}
});
syncSetTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} else {
WalletSync pending = new WalletSync(hash, data);
pendingSyncSetQueue.add(pending);
}
}
use of com.odysee.app.model.WalletSync in project odysee-android by OdyseeTeam.
the class SyncGetTask method doInBackground.
protected WalletSync doInBackground(Void... params) {
try {
password = Helper.isNullOrEmpty(password) ? "" : password;
JSONObject result = (JSONObject) Lbry.genericApiCall(Lbry.METHOD_WALLET_STATUS, Lbryio.AUTH_TOKEN);
boolean isLocked = Helper.getJSONBoolean("is_locked", false, result);
boolean unlockSuccessful = !isLocked || (boolean) Lbry.authenticatedGenericApiCall(Lbry.METHOD_WALLET_UNLOCK, Lbry.buildSingleParam("password", password), Lbryio.AUTH_TOKEN);
if (!unlockSuccessful) {
throw new WalletException("The wallet could not be unlocked with the provided password.");
}
String hash = (String) Lbry.authenticatedGenericApiCall(Lbry.METHOD_SYNC_HASH, null, Lbryio.AUTH_TOKEN);
try {
JSONObject response = (JSONObject) Lbryio.parseResponse(Lbryio.call("sync", "get", Lbryio.buildSingleParam("hash", hash), Helper.METHOD_POST, null));
WalletSync walletSync = new WalletSync(Helper.getJSONString("hash", null, response), Helper.getJSONString("data", null, response), Helper.getJSONBoolean("changed", false, response));
if (applySyncChanges && (!hash.equalsIgnoreCase(walletSync.getHash()) || walletSync.isChanged())) {
// Lbry.sync_apply({ password, data: response.data, blocking: true });
try {
Map<String, Object> options = new HashMap<>();
options.put("password", Helper.isNullOrEmpty(password) ? "" : password);
options.put("data", walletSync.getData());
options.put("blocking", true);
JSONObject syncApplyResponse = (JSONObject) Lbry.authenticatedGenericApiCall(Lbry.METHOD_SYNC_APPLY, options, Lbryio.AUTH_TOKEN);
syncHash = Helper.getJSONString("hash", null, syncApplyResponse);
syncData = Helper.getJSONString("data", null, syncApplyResponse);
applySyncSuccessful = true;
} catch (ApiCallException | ClassCastException ex) {
// sync_apply failed
syncApplyError = ex;
}
}
if (Lbryio.isSignedIn() && !Lbryio.userHasSyncedWallet) {
// indicate that the user owns a synced wallet (only if the user is signed in)
Lbryio.userHasSyncedWallet = true;
}
return walletSync;
} catch (LbryioResponseException ex) {
// wallet sync data doesn't exist
return null;
}
} catch (ApiCallException | WalletException | ClassCastException | LbryioRequestException ex) {
error = ex;
return null;
}
}
use of com.odysee.app.model.WalletSync in project odysee-android by OdyseeTeam.
the class MainActivity method syncWalletAndLoadPreferences.
public void syncWalletAndLoadPreferences() {
if (!userSyncEnabled()) {
return;
}
if (fullSyncInProgress) {
queuedSyncCount++;
}
fullSyncInProgress = true;
String password = getSecureValue(SECURE_VALUE_KEY_SAVED_PASSWORD, this, Lbry.KEYSTORE);
SyncGetTask task = new SyncGetTask(password, true, null, new DefaultSyncTaskHandler() {
@Override
public void onSyncGetSuccess(WalletSync walletSync) {
Lbryio.lastWalletSync = walletSync;
Lbryio.lastRemoteHash = walletSync.getHash();
loadSharedUserState();
}
@Override
public void onSyncGetWalletNotFound() {
// But if it does, send what we have
if (Lbryio.isSignedIn()) {
syncApplyAndSet();
}
}
@Override
public void onSyncGetError(Exception error) {
// pass
Log.e(TAG, String.format("sync get failed: %s", error != null ? error.getMessage() : "no error message"), error);
fullSyncInProgress = false;
if (queuedSyncCount > 0) {
queuedSyncCount--;
syncApplyAndSet();
}
}
@Override
public void onSyncApplySuccess(String hash, String data) {
if (!hash.equalsIgnoreCase(Lbryio.lastRemoteHash)) {
syncSet(hash, data);
} else {
fullSyncInProgress = false;
queuedSyncCount = 0;
}
loadSharedUserState();
}
@Override
public void onSyncApplyError(Exception error) {
// pass
Log.e(TAG, String.format("sync apply failed: %s", error != null ? error.getMessage() : "no error message"), error);
fullSyncInProgress = false;
if (queuedSyncCount > 0) {
queuedSyncCount--;
syncApplyAndSet();
}
}
});
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
use of com.odysee.app.model.WalletSync in project odysee-android by OdyseeTeam.
the class SignInActivity method runWalletSync.
private void runWalletSync(String password) {
if (walletSyncStarted) {
return;
}
walletSyncStarted = true;
Helper.setViewVisibility(layoutCollect, View.GONE);
Helper.setViewVisibility(layoutVerify, View.GONE);
Helper.setViewVisibility(closeSignupSignIn, View.GONE);
Helper.setViewVisibility(layoutWalletSyncContainer, View.VISIBLE);
Helper.setViewVisibility(walletSyncProgress, View.VISIBLE);
Helper.setViewVisibility(textWalletSyncLoading, View.VISIBLE);
password = Utils.getSecureValue(MainActivity.SECURE_VALUE_KEY_SAVED_PASSWORD, this, Lbry.KEYSTORE);
if (Helper.isNullOrEmpty(password)) {
password = Helper.getValue(inputWalletSyncPassword.getText());
}
final String actual = password;
SyncGetTask task = new SyncGetTask(password, false, null, new DefaultSyncTaskHandler() {
@Override
public void onSyncGetSuccess(WalletSync walletSync) {
currentWalletSync = walletSync;
Lbryio.lastRemoteHash = walletSync.getHash();
if (Helper.isNullOrEmpty(actual)) {
processExistingWallet(walletSync);
} else {
processExistingWalletWithPassword(actual);
}
}
@Override
public void onSyncGetWalletNotFound() {
// no wallet found, get sync apply data and run the process
processNewWallet();
}
@Override
public void onSyncGetError(Exception error) {
// try again
Helper.setViewVisibility(walletSyncProgress, View.GONE);
// Helper.setViewText(textWalletSyncLoading, error.getMessage());
Helper.setViewVisibility(textWalletSyncLoading, View.GONE);
Helper.setViewVisibility(layoutWalletSyncInputArea, View.VISIBLE);
Helper.setViewVisibility(closeSignupSignIn, View.VISIBLE);
walletSyncStarted = false;
}
});
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
use of com.odysee.app.model.WalletSync in project odysee-android by OdyseeTeam.
the class SignInFragment method runWalletSync.
private void runWalletSync(String password) {
if (walletSyncStarted) {
return;
}
walletSyncStarted = true;
Helper.setViewVisibility(layoutCollect, View.GONE);
Helper.setViewVisibility(layoutVerify, View.GONE);
Helper.setViewVisibility(layoutWalletSyncContainer, View.VISIBLE);
Helper.setViewVisibility(walletSyncProgress, View.VISIBLE);
Helper.setViewVisibility(textWalletSyncLoading, View.VISIBLE);
password = Utils.getSecureValue(MainActivity.SECURE_VALUE_KEY_SAVED_PASSWORD, getContext(), Lbry.KEYSTORE);
if (Helper.isNullOrEmpty(password)) {
password = Helper.getValue(inputWalletSyncPassword.getText());
}
final String actual = password;
SyncGetTask task = new SyncGetTask(password, false, null, new DefaultSyncTaskHandler() {
@Override
public void onSyncGetSuccess(WalletSync walletSync) {
currentWalletSync = walletSync;
Lbryio.lastRemoteHash = walletSync.getHash();
if (Helper.isNullOrEmpty(actual)) {
processExistingWallet(walletSync);
} else {
processExistingWalletWithPassword(actual);
}
}
@Override
public void onSyncGetWalletNotFound() {
// no wallet found, get sync apply data and run the process
processNewWallet();
}
@Override
public void onSyncGetError(Exception error) {
// try again
Helper.setViewVisibility(walletSyncProgress, View.GONE);
// Helper.setViewText(textWalletSyncLoading, error.getMessage());
Helper.setViewVisibility(textWalletSyncLoading, View.GONE);
Helper.setViewVisibility(layoutWalletSyncInputArea, View.VISIBLE);
walletSyncStarted = false;
}
});
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
Aggregations