Search in sources :

Example 16 with LbryioResponseException

use of com.odysee.app.exceptions.LbryioResponseException 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 17 with LbryioResponseException

use of com.odysee.app.exceptions.LbryioResponseException in project odysee-android by OdyseeTeam.

the class ClaimRewardTask method doInBackground.

public String doInBackground(Void... params) {
    String message = null;
    try {
        String txid = null;
        if (Reward.TYPE_FIRST_CHANNEL.equalsIgnoreCase(type)) {
            // fetch a channel
            txid = fetchSingleClaimTxid(Claim.TYPE_CHANNEL);
        } else if (Reward.TYPE_FIRST_PUBLISH.equalsIgnoreCase(type)) {
            // fetch a publish
            txid = fetchSingleClaimTxid(Claim.TYPE_STREAM);
        }
        // Get a new wallet address for the reward
        String address = (String) Lbry.genericApiCall(Lbry.METHOD_ADDRESS_UNUSED, authToken);
        Map<String, String> options = new HashMap<>();
        options.put("reward_type", type);
        options.put("wallet_address", address);
        if (!Helper.isNullOrEmpty(rewardCode)) {
            options.put("code", rewardCode);
        }
        if (!Helper.isNullOrEmpty(txid)) {
            options.put("transaction_id", txid);
        }
        if (authToken != null) {
            options.put("auth_token", authToken);
        }
        JSONObject reward = (JSONObject) Lbryio.parseResponse(Lbryio.call("reward", "claim", options, Helper.METHOD_POST, null));
        if (reward != null) {
            amountClaimed = Helper.getJSONDouble("reward_amount", 0, reward);
            message = Helper.getJSONString("reward_notification", "", reward);
        }
    } catch (ApiCallException | JSONException | LbryioRequestException | LbryioResponseException ex) {
        error = ex;
    }
    return message;
}
Also used : JSONObject(org.json.JSONObject) HashMap(java.util.HashMap) ApiCallException(com.odysee.app.exceptions.ApiCallException) LbryioRequestException(com.odysee.app.exceptions.LbryioRequestException) JSONException(org.json.JSONException) LbryioResponseException(com.odysee.app.exceptions.LbryioResponseException)

Example 18 with LbryioResponseException

use of com.odysee.app.exceptions.LbryioResponseException in project odysee-android by OdyseeTeam.

the class SignInActivity method performSignIn.

private void performSignIn(final String email, final String password) {
    if (requestInProgress) {
        return;
    }
    if (!emailSignInChecked) {
        requestInProgress = true;
        beforeSignInTransition();
        executor.execute(new Runnable() {

            @Override
            public void run() {
                Map<String, String> options = new HashMap<>();
                options.put("email", email);
                try {
                    Object response = Lbryio.parseResponse(Lbryio.call("user", "exists", options, Helper.METHOD_POST, SignInActivity.this));
                    requestInProgress = false;
                    if (response instanceof JSONObject) {
                        JSONObject json = (JSONObject) response;
                        boolean hasPassword = Helper.getJSONBoolean("has_password", false, json);
                        if (!hasPassword) {
                            setCurrentEmail(email);
                            handleUserSignInWithoutPassword(email);
                            return;
                        }
                        // email exists, we can use sign in flow. Show password field
                        setCurrentEmail(email);
                        emailSignInChecked = true;
                        displaySignInControls();
                        displayMagicLink();
                    }
                } catch (LbryioRequestException | LbryioResponseException ex) {
                    if (ex instanceof LbryioResponseException) {
                        LbryioResponseException rex = (LbryioResponseException) ex;
                        if (rex.getStatusCode() == 412) {
                            // old email verification flow
                            setCurrentEmail(email);
                            handleUserSignInWithoutPassword(email);
                            Bundle bundle = new Bundle();
                            bundle.putString("email", currentEmail);
                            LbryAnalytics.logEvent(LbryAnalytics.EVENT_EMAIL_ADDED, bundle);
                            LbryAnalytics.logEvent("");
                            requestInProgress = false;
                            return;
                        }
                    }
                    requestInProgress = false;
                    restoreControls(true);
                    showError(ex.getMessage());
                }
            }
        });
        return;
    }
    if (Helper.isNullOrEmpty(password)) {
        showError(getString(R.string.please_enter_signin_password));
        return;
    }
    beforeSignInTransition();
    requestInProgress = true;
    executor.execute(new Runnable() {

        @Override
        public void run() {
            Map<String, String> options = new HashMap<>();
            options.put("email", email);
            options.put("password", password);
            try {
                Object response = Lbryio.parseResponse(Lbryio.call("user", "signin", options, Helper.METHOD_POST, SignInActivity.this));
                requestInProgress = false;
                if (response instanceof JSONObject) {
                    Type type = new TypeToken<User>() {
                    }.getType();
                    Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create();
                    User user = gson.fromJson(response.toString(), type);
                    Lbryio.currentUser = user;
                    addOdyseeAccountExplicitly(user.getPrimaryEmail());
                    finishWithWalletSync();
                    return;
                }
            } catch (LbryioRequestException | LbryioResponseException ex) {
                if (ex instanceof LbryioResponseException) {
                    if (((LbryioResponseException) ex).getStatusCode() == 409) {
                        handleEmailVerificationFlow(email);
                        return;
                    }
                }
                showError(ex.getMessage());
                requestInProgress = false;
                restoreControls(true);
                return;
            }
            requestInProgress = false;
            restoreControls(true);
            showError(getString(R.string.unknown_error_occurred));
        }
    });
}
Also used : User(com.odysee.app.model.lbryinc.User) GsonBuilder(com.google.gson.GsonBuilder) Bundle(android.os.Bundle) Gson(com.google.gson.Gson) Type(java.lang.reflect.Type) JSONObject(org.json.JSONObject) TypeToken(com.google.gson.reflect.TypeToken) JSONObject(org.json.JSONObject) Map(java.util.Map) HashMap(java.util.HashMap) LbryioResponseException(com.odysee.app.exceptions.LbryioResponseException)

Example 19 with LbryioResponseException

use of com.odysee.app.exceptions.LbryioResponseException in project odysee-android by OdyseeTeam.

the class SignInFragment method performSignIn.

private void performSignIn(final String email, final String password) {
    if (requestInProgress) {
        return;
    }
    if (!emailSignInChecked) {
        requestInProgress = true;
        if (firstRunStepHandler != null) {
            firstRunStepHandler.onRequestInProgress(false);
        }
        beforeSignInTransition();
        executor.execute(new Runnable() {

            @Override
            public void run() {
                Map<String, String> options = new HashMap<>();
                options.put("email", email);
                try {
                    Object response = Lbryio.parseResponse(Lbryio.call("user", "exists", options, Helper.METHOD_POST, getContext()));
                    requestInProgress = false;
                    if (response instanceof JSONObject) {
                        JSONObject json = (JSONObject) response;
                        boolean hasPassword = Helper.getJSONBoolean("has_password", false, json);
                        if (!hasPassword) {
                            setCurrentEmail(email);
                            handleUserSignInWithoutPassword(email);
                            return;
                        }
                        // email exists, we can use sign in flow. Show password field
                        setCurrentEmail(email);
                        emailSignInChecked = true;
                        displaySignInControls();
                        displayMagicLink();
                    }
                } catch (LbryioRequestException | LbryioResponseException ex) {
                    if (ex instanceof LbryioResponseException) {
                        LbryioResponseException rex = (LbryioResponseException) ex;
                        if (rex.getStatusCode() == 412) {
                            // old email verification flow
                            setCurrentEmail(email);
                            handleUserSignInWithoutPassword(email);
                            Bundle bundle = new Bundle();
                            bundle.putString("email", currentEmail);
                            LbryAnalytics.logEvent(LbryAnalytics.EVENT_EMAIL_ADDED, bundle);
                            LbryAnalytics.logEvent("");
                            requestInProgress = false;
                            return;
                        }
                    }
                    requestInProgress = false;
                    restoreControls(true);
                    if (firstRunStepHandler != null) {
                        firstRunStepHandler.onRequestCompleted(FirstRunActivity.FIRST_RUN_STEP_ACCOUNT);
                    }
                    showError(ex.getMessage());
                }
            }
        });
        return;
    }
    if (Helper.isNullOrEmpty(password)) {
        showError(getString(R.string.please_enter_signin_password));
        return;
    }
    beforeSignInTransition();
    requestInProgress = true;
    executor.execute(new Runnable() {

        @Override
        public void run() {
            Map<String, String> options = new HashMap<>();
            options.put("email", email);
            options.put("password", password);
            try {
                Object response = Lbryio.parseResponse(Lbryio.call("user", "signin", options, Helper.METHOD_POST, getContext()));
                requestInProgress = false;
                if (response instanceof JSONObject) {
                    Type type = new TypeToken<User>() {
                    }.getType();
                    Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create();
                    User user = gson.fromJson(response.toString(), type);
                    Lbryio.currentUser = user;
                    addOdyseeAccountExplicitly(user.getPrimaryEmail());
                    finishWithWalletSync();
                    return;
                }
            } catch (LbryioRequestException | LbryioResponseException ex) {
                if (ex instanceof LbryioResponseException) {
                    if (((LbryioResponseException) ex).getStatusCode() == 409) {
                        handleEmailVerificationFlow(email);
                        return;
                    }
                }
                showError(ex.getMessage());
                requestInProgress = false;
                restoreControls(true);
                if (firstRunStepHandler != null) {
                    firstRunStepHandler.onRequestCompleted(FirstRunActivity.FIRST_RUN_STEP_ACCOUNT);
                }
                return;
            }
            requestInProgress = false;
            restoreControls(true);
            if (firstRunStepHandler != null) {
                firstRunStepHandler.onRequestCompleted(FirstRunActivity.FIRST_RUN_STEP_ACCOUNT);
            }
            showError(getString(R.string.unknown_error_occurred));
        }
    });
}
Also used : User(com.odysee.app.model.lbryinc.User) GsonBuilder(com.google.gson.GsonBuilder) Bundle(android.os.Bundle) Gson(com.google.gson.Gson) Type(java.lang.reflect.Type) JSONObject(org.json.JSONObject) TypeToken(com.google.gson.reflect.TypeToken) JSONObject(org.json.JSONObject) Map(java.util.Map) HashMap(java.util.HashMap) LbryioResponseException(com.odysee.app.exceptions.LbryioResponseException)

Aggregations

LbryioResponseException (com.odysee.app.exceptions.LbryioResponseException)19 LbryioRequestException (com.odysee.app.exceptions.LbryioRequestException)16 JSONObject (org.json.JSONObject)16 HashMap (java.util.HashMap)12 JSONException (org.json.JSONException)8 Map (java.util.Map)5 JSONArray (org.json.JSONArray)5 Bundle (android.os.Bundle)4 ArrayList (java.util.ArrayList)4 Supplier (java.util.function.Supplier)4 Response (okhttp3.Response)4 AccountManager (android.accounts.AccountManager)3 Activity (android.app.Activity)3 RecyclerView (androidx.recyclerview.widget.RecyclerView)3 Gson (com.google.gson.Gson)3 GsonBuilder (com.google.gson.GsonBuilder)3 MainActivity (com.odysee.app.MainActivity)3 LbryNotification (com.odysee.app.model.lbryinc.LbryNotification)3 Reward (com.odysee.app.model.lbryinc.Reward)3 User (com.odysee.app.model.lbryinc.User)3