Search in sources :

Example 1 with AuthTokenInvalidatedException

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

the class MainActivity method startup.

@SuppressLint("StaticFieldLeak")
private void startup() {
    final Context context = this;
    Lbry.startupInit();
    // perform some tasks before launching
    (new AsyncTask<Void, Void, Boolean>() {

        private final List<StartupStage> startupStages = new ArrayList<>(7);

        private void initStartupStages() {
            startupStages.add(new StartupStage(STARTUP_STAGE_INSTALL_ID_LOADED, false));
            startupStages.add(new StartupStage(STARTUP_STAGE_KNOWN_TAGS_LOADED, false));
            startupStages.add(new StartupStage(STARTUP_STAGE_EXCHANGE_RATE_LOADED, false));
            startupStages.add(new StartupStage(STARTUP_STAGE_USER_AUTHENTICATED, false));
            startupStages.add(new StartupStage(STARTUP_STAGE_NEW_INSTALL_DONE, false));
            startupStages.add(new StartupStage(STARTUP_STAGE_SUBSCRIPTIONS_LOADED, false));
            startupStages.add(new StartupStage(STARTUP_STAGE_SUBSCRIPTIONS_RESOLVED, false));
            startupStages.add(new StartupStage(STARTUP_STAGE_BLOCK_LIST_LOADED, false));
            startupStages.add(new StartupStage(STARTUP_STAGE_FILTER_LIST_LOADED, false));
        }

        protected void onPreExecute() {
            hideActionBar();
            // findViewById(R.id.splash_view).setVisibility(View.VISIBLE);
            LbryAnalytics.setCurrentScreen(MainActivity.this, "Splash", "Splash");
            initStartupStages();
        }

        protected Boolean doInBackground(Void... params) {
            BufferedReader reader = null;
            try {
                // Load the installation id from the file system
                String lbrynetDir = String.format("%s/%s", getAppInternalStorageDir(), "lbrynet");
                String installIdPath = String.format("%s/install_id", lbrynetDir);
                reader = new BufferedReader(new InputStreamReader(new FileInputStream(installIdPath)));
                String installId = reader.readLine();
                if (Helper.isNullOrEmpty(installId)) {
                    // no install_id found (first run didn't start the sdk successfully?)
                    startupStages.set(STARTUP_STAGE_INSTALL_ID_LOADED - 1, new StartupStage(STARTUP_STAGE_INSTALL_ID_LOADED, false));
                    return false;
                }
                Lbry.INSTALLATION_ID = installId;
                startupStages.set(STARTUP_STAGE_INSTALL_ID_LOADED - 1, new StartupStage(STARTUP_STAGE_INSTALL_ID_LOADED, true));
                SQLiteDatabase db = dbHelper.getReadableDatabase();
                List<Tag> fetchedTags = DatabaseHelper.getTags(db);
                Lbry.knownTags = Helper.mergeKnownTags(fetchedTags);
                Collections.sort(Lbry.knownTags, new Tag());
                Lbry.followedTags = Helper.filterFollowedTags(Lbry.knownTags);
                startupStages.set(STARTUP_STAGE_KNOWN_TAGS_LOADED - 1, new StartupStage(STARTUP_STAGE_KNOWN_TAGS_LOADED, true));
                // load the exchange rate
                Lbryio.loadExchangeRate();
                if (Lbryio.LBCUSDRate == 0) {
                    return false;
                }
                startupStages.set(STARTUP_STAGE_EXCHANGE_RATE_LOADED - 1, new StartupStage(STARTUP_STAGE_EXCHANGE_RATE_LOADED, true));
                try {
                    Lbryio.authenticate(context);
                } catch (AuthTokenInvalidatedException ex) {
                    // if this happens, attempt to authenticate again, so that we can obtain a new auth token
                    // this will also result in the user having to sign in again
                    Lbryio.authenticate(context);
                }
                if (Lbryio.currentUser == null) {
                    throw new Exception("Did not retrieve authenticated user.");
                }
                startupStages.set(STARTUP_STAGE_USER_AUTHENTICATED - 1, new StartupStage(STARTUP_STAGE_USER_AUTHENTICATED, true));
                Lbryio.newInstall(context);
                startupStages.set(STARTUP_STAGE_NEW_INSTALL_DONE - 1, new StartupStage(STARTUP_STAGE_NEW_INSTALL_DONE, true));
                startupStages.set(STARTUP_STAGE_SUBSCRIPTIONS_LOADED - 1, new StartupStage(STARTUP_STAGE_SUBSCRIPTIONS_LOADED, true));
                startupStages.set(STARTUP_STAGE_SUBSCRIPTIONS_RESOLVED - 1, new StartupStage(STARTUP_STAGE_SUBSCRIPTIONS_RESOLVED, true));
                JSONObject blockedObject = (JSONObject) Lbryio.parseResponse(Lbryio.call("file", "list_blocked", context));
                JSONArray blockedArray = blockedObject.getJSONArray("outpoints");
                Lbryio.populateOutpointList(Lbryio.blockedOutpoints, blockedArray);
                startupStages.set(STARTUP_STAGE_BLOCK_LIST_LOADED - 1, new StartupStage(STARTUP_STAGE_BLOCK_LIST_LOADED, true));
                JSONObject filteredObject = (JSONObject) Lbryio.parseResponse(Lbryio.call("file", "list_filtered", context));
                JSONArray filteredArray = filteredObject.getJSONArray("outpoints");
                Lbryio.populateOutpointList(Lbryio.filteredOutpoints, filteredArray);
                startupStages.set(STARTUP_STAGE_FILTER_LIST_LOADED - 1, new StartupStage(STARTUP_STAGE_FILTER_LIST_LOADED, true));
            } catch (Exception ex) {
                // nope
                Log.e(TAG, String.format("App startup failed: %s", ex.getMessage()), ex);
                return false;
            } finally {
                Helper.closeCloseable(reader);
            }
            return true;
        }

        protected void onPostExecute(Boolean startupSuccessful) {
            if (!startupSuccessful) {
                // show which startup stage failed
                renderStartupFailed(startupStages);
                appStarted = false;
                return;
            }
            // findViewById(R.id.splash_view).setVisibility(View.GONE);
            showActionBar();
            // loadLastFragment();
            fetchRewards();
            loadRemoteNotifications(false);
            checkUrlIntent(getIntent());
            checkWebSocketClient();
            LbryAnalytics.logEvent(LbryAnalytics.EVENT_APP_LAUNCH);
            appStarted = true;
        }
    }).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
Also used : Context(android.content.Context) InputStreamReader(java.io.InputStreamReader) AsyncTask(android.os.AsyncTask) JSONArray(org.json.JSONArray) SpannableString(android.text.SpannableString) FileInputStream(java.io.FileInputStream) 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) StartupStage(com.odysee.app.model.StartupStage) AuthTokenInvalidatedException(com.odysee.app.exceptions.AuthTokenInvalidatedException) JSONObject(org.json.JSONObject) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) BufferedReader(java.io.BufferedReader) ArrayList(java.util.ArrayList) List(java.util.List) Tag(com.odysee.app.model.Tag) SuppressLint(android.annotation.SuppressLint)

Example 2 with AuthTokenInvalidatedException

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

the class Lbryio method fetchCurrentUser.

public static User fetchCurrentUser(Context context) throws AuthTokenInvalidatedException {
    try {
        Response response = Lbryio.call("user", "me", context);
        JSONObject object = (JSONObject) parseResponse(response);
        Type type = new TypeToken<User>() {
        }.getType();
        Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create();
        User user = gson.fromJson(object.toString(), type);
        return user;
    } catch (LbryioRequestException | LbryioResponseException | ClassCastException | IllegalStateException ex) {
        LbryAnalytics.logError(String.format("/user/me failed: %s", ex.getMessage()), ex.getClass().getName());
        if (ex instanceof LbryioResponseException) {
            LbryioResponseException error = (LbryioResponseException) ex;
            if (error.getStatusCode() == 403) {
                // auth token invalidated
                AUTH_TOKEN = null;
                throw new AuthTokenInvalidatedException();
            }
        }
        Log.e(TAG, "Could not retrieve the current user", ex);
        return null;
    }
}
Also used : User(com.odysee.app.model.lbryinc.User) GsonBuilder(com.google.gson.GsonBuilder) LbryioRequestException(com.odysee.app.exceptions.LbryioRequestException) Gson(com.google.gson.Gson) Response(okhttp3.Response) AuthTokenInvalidatedException(com.odysee.app.exceptions.AuthTokenInvalidatedException) Type(java.lang.reflect.Type) JSONObject(org.json.JSONObject) LbryioResponseException(com.odysee.app.exceptions.LbryioResponseException)

Example 3 with AuthTokenInvalidatedException

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

the class MainActivity method initialiseUserInstall.

private void initialiseUserInstall() {
    ExecutorService service = Executors.newSingleThreadExecutor();
    if (!userInstallInitialised) {
        service.execute(new Runnable() {

            @Override
            public void run() {
                SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
                String installId = sp.getString(PREFERENCE_KEY_INSTALL_ID, null);
                if (Helper.isNullOrEmpty(installId)) {
                    // no install_id found (first run didn't start the sdk successfully?)
                    installId = Lbry.generateId();
                    sp.edit().putString(PREFERENCE_KEY_INSTALL_ID, installId).apply();
                }
                Lbry.INSTALLATION_ID = installId;
                try {
                    try {
                        Lbryio.authenticate(MainActivity.this);
                    } catch (AuthTokenInvalidatedException ex) {
                        // if this happens, attempt to authenticate again, so that we can obtain a new auth token
                        // this will also result in the user having to sign in again
                        Lbryio.authenticate(MainActivity.this);
                    }
                    if (Lbryio.currentUser == null) {
                    // display simplified startup error
                    }
                    Lbryio.newInstall(MainActivity.this);
                } catch (Exception ex) {
                    return;
                }
                checkFirstRun();
                scheduleWalletSyncTask();
                if (!initialBlockedChannelsLoaded) {
                    loadBlockedChannels();
                }
                if (!initialCategoriesLoaded) {
                    loadInitialCategories();
                    return;
                }
                new Handler(Looper.getMainLooper()).post(new Runnable() {

                    @Override
                    public void run() {
                        hideLaunchScreen();
                    }
                });
            }
        });
        userInstallInitialised = true;
        return;
    }
    // if the user install has already been initialised, proceed to load categories
    loadInitialCategories();
}
Also used : AuthTokenInvalidatedException(com.odysee.app.exceptions.AuthTokenInvalidatedException) SharedPreferences(android.content.SharedPreferences) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ExecutorService(java.util.concurrent.ExecutorService) Handler(android.os.Handler) RewardVerifiedHandler(com.odysee.app.tasks.RewardVerifiedHandler) GenericTaskHandler(com.odysee.app.tasks.GenericTaskHandler) ClaimListResultHandler(com.odysee.app.tasks.claim.ClaimListResultHandler) DefaultSyncTaskHandler(com.odysee.app.tasks.wallet.DefaultSyncTaskHandler) 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)

Aggregations

AuthTokenInvalidatedException (com.odysee.app.exceptions.AuthTokenInvalidatedException)3 LbryioRequestException (com.odysee.app.exceptions.LbryioRequestException)3 LbryioResponseException (com.odysee.app.exceptions.LbryioResponseException)3 SQLiteException (android.database.sqlite.SQLiteException)2 SpannableString (android.text.SpannableString)2 ApiCallException (com.odysee.app.exceptions.ApiCallException)2 LbryUriException (com.odysee.app.exceptions.LbryUriException)2 ParseException (java.text.ParseException)2 ExecutionException (java.util.concurrent.ExecutionException)2 JSONException (org.json.JSONException)2 JSONObject (org.json.JSONObject)2 SuppressLint (android.annotation.SuppressLint)1 Context (android.content.Context)1 SharedPreferences (android.content.SharedPreferences)1 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)1 AsyncTask (android.os.AsyncTask)1 Handler (android.os.Handler)1 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 StartupStage (com.odysee.app.model.StartupStage)1