Search in sources :

Example 1 with WalletException

use of com.odysee.app.exceptions.WalletException 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)

Aggregations

ApiCallException (com.odysee.app.exceptions.ApiCallException)1 LbryioRequestException (com.odysee.app.exceptions.LbryioRequestException)1 LbryioResponseException (com.odysee.app.exceptions.LbryioResponseException)1 WalletException (com.odysee.app.exceptions.WalletException)1 WalletSync (com.odysee.app.model.WalletSync)1 HashMap (java.util.HashMap)1 JSONObject (org.json.JSONObject)1