Search in sources :

Example 1 with WalletSync

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);
    }
}
Also used : WalletSync(com.odysee.app.model.WalletSync) DefaultSyncTaskHandler(com.odysee.app.tasks.wallet.DefaultSyncTaskHandler) SpannableString(android.text.SpannableString) SyncSetTask(com.odysee.app.tasks.wallet.SyncSetTask) JSONException(org.json.JSONException) LbryUriException(com.odysee.app.exceptions.LbryUriException) ExecutionException(java.util.concurrent.ExecutionException) SQLiteException(android.database.sqlite.SQLiteException) LbryioRequestException(com.odysee.app.exceptions.LbryioRequestException) LbryioResponseException(com.odysee.app.exceptions.LbryioResponseException) ApiCallException(com.odysee.app.exceptions.ApiCallException) AuthTokenInvalidatedException(com.odysee.app.exceptions.AuthTokenInvalidatedException) ParseException(java.text.ParseException)

Example 2 with WalletSync

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;
    }
}
Also used : WalletException(com.odysee.app.exceptions.WalletException) HashMap(java.util.HashMap) ApiCallException(com.odysee.app.exceptions.ApiCallException) LbryioRequestException(com.odysee.app.exceptions.LbryioRequestException) WalletSync(com.odysee.app.model.WalletSync) JSONObject(org.json.JSONObject) JSONObject(org.json.JSONObject) LbryioResponseException(com.odysee.app.exceptions.LbryioResponseException)

Example 3 with WalletSync

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);
}
Also used : WalletSync(com.odysee.app.model.WalletSync) DefaultSyncTaskHandler(com.odysee.app.tasks.wallet.DefaultSyncTaskHandler) SyncGetTask(com.odysee.app.tasks.wallet.SyncGetTask) SpannableString(android.text.SpannableString) JSONException(org.json.JSONException) LbryUriException(com.odysee.app.exceptions.LbryUriException) ExecutionException(java.util.concurrent.ExecutionException) SQLiteException(android.database.sqlite.SQLiteException) LbryioRequestException(com.odysee.app.exceptions.LbryioRequestException) LbryioResponseException(com.odysee.app.exceptions.LbryioResponseException) ApiCallException(com.odysee.app.exceptions.ApiCallException) AuthTokenInvalidatedException(com.odysee.app.exceptions.AuthTokenInvalidatedException) ParseException(java.text.ParseException)

Example 4 with WalletSync

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);
}
Also used : WalletSync(com.odysee.app.model.WalletSync) DefaultSyncTaskHandler(com.odysee.app.tasks.wallet.DefaultSyncTaskHandler) SyncGetTask(com.odysee.app.tasks.wallet.SyncGetTask) LbryioRequestException(com.odysee.app.exceptions.LbryioRequestException) LbryioResponseException(com.odysee.app.exceptions.LbryioResponseException) ExecutionException(java.util.concurrent.ExecutionException)

Example 5 with WalletSync

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);
}
Also used : WalletSync(com.odysee.app.model.WalletSync) DefaultSyncTaskHandler(com.odysee.app.tasks.wallet.DefaultSyncTaskHandler) SyncGetTask(com.odysee.app.tasks.wallet.SyncGetTask) LbryioRequestException(com.odysee.app.exceptions.LbryioRequestException) LbryioResponseException(com.odysee.app.exceptions.LbryioResponseException) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

LbryioRequestException (com.odysee.app.exceptions.LbryioRequestException)5 LbryioResponseException (com.odysee.app.exceptions.LbryioResponseException)5 WalletSync (com.odysee.app.model.WalletSync)5 DefaultSyncTaskHandler (com.odysee.app.tasks.wallet.DefaultSyncTaskHandler)4 ExecutionException (java.util.concurrent.ExecutionException)4 ApiCallException (com.odysee.app.exceptions.ApiCallException)3 SyncGetTask (com.odysee.app.tasks.wallet.SyncGetTask)3 SQLiteException (android.database.sqlite.SQLiteException)2 SpannableString (android.text.SpannableString)2 AuthTokenInvalidatedException (com.odysee.app.exceptions.AuthTokenInvalidatedException)2 LbryUriException (com.odysee.app.exceptions.LbryUriException)2 ParseException (java.text.ParseException)2 JSONException (org.json.JSONException)2 WalletException (com.odysee.app.exceptions.WalletException)1 SyncSetTask (com.odysee.app.tasks.wallet.SyncSetTask)1 HashMap (java.util.HashMap)1 JSONObject (org.json.JSONObject)1