Search in sources :

Example 1 with BE_VIP

use of com.ichi2.anki.DeckPicker.BE_VIP in project AnkiChinaAndroid by ankichinateam.

the class DeckPicker method getVipInfo.

public void getVipInfo() {
    mRefreshVipStateOnResume = false;
    if (!Permissions.hasStorageAccessPermission(this)) {
        return;
    }
    mVip = AnkiDroidApp.getSharedPrefs(DeckPicker.this).getBoolean(Consts.KEY_IS_VIP, false);
    mVipUrl = AnkiDroidApp.getSharedPrefs(DeckPicker.this).getString(Consts.KEY_VIP_URL, "");
    refreshVipState(true);
    getAccount().getToken(this, new MyAccount.TokenCallback() {

        @Override
        public void onSuccess(String token) {
            // 获取vip状态
            OKHttpUtil.get(Consts.ANKI_CHINA_BASE + Consts.API_VERSION + "users/vipInfo", token, "", new OKHttpUtil.MyCallBack() {

                @Override
                public void onFailure(Call call, IOException e) {
                    refreshVipState(false);
                    if (mOpenVipHtmlWhenGetUrl) {
                        mOpenVipHtmlWhenGetUrl = false;
                        runOnUiThread(() -> Toast.makeText(DeckPicker.this, "信息获取失败,请检查网络或稍候再试", Toast.LENGTH_SHORT).show());
                    }
                }

                @Override
                public void onResponse(Call call, String token, Object arg1, Response response) throws IOException {
                    if (response.isSuccessful()) {
                        // Timber.i("init vip info successfully!:%s", response.body());
                        try {
                            final JSONObject object = new JSONObject(response.body().string());
                            final JSONObject item = object.getJSONObject("data");
                            mVipUrl = item.getString("vip_url");
                            Timber.i("get vip url :%s", mVipUrl);
                            if (!mVip && mTurnToVipHtml) {
                                mTurnToVipHtml = false;
                                WebViewActivity.openUrlInApp(DeckPicker.this, String.format(mVipUrl, token, BuildConfig.VERSION_NAME), token, BE_VIP);
                            }
                            mVip = item.getBoolean("is_vip");
                            mVipDay = item.getInt("vip_day");
                            mVipExpireAt = item.getString("vip_end_at");
                            AnkiDroidApp.getSharedPrefs(DeckPicker.this).edit().putBoolean(Consts.KEY_IS_VIP, mVip).putString(Consts.KEY_VIP_URL, mVipUrl).putString(Consts.KEY_VIP_EXPIRED, mVipExpireAt).apply();
                            if (mOpenVipHtmlWhenGetUrl) {
                                mOpenVipHtmlWhenGetUrl = false;
                                openVipUrl(mVipUrl);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    } else {
                        Timber.e("init vip info failed, error code %d", response.code());
                    }
                    refreshVipState(true);
                }
            });
        }

        @Override
        public void onFail(String message) {
            OKHttpUtil.get(Consts.ANKI_CHINA_BASE + Consts.API_VERSION + "users/vipInfo", "", "", new OKHttpUtil.MyCallBack() {

                @Override
                public void onFailure(Call call, IOException e) {
                    refreshVipState(false);
                }

                @Override
                public void onResponse(Call call, String token, Object arg1, Response response) throws IOException {
                    if (response.isSuccessful()) {
                        Timber.i("init vip info successfully!:%s", response.body());
                        try {
                            final JSONObject object = new JSONObject(response.body().string());
                            final JSONObject item = object.getJSONObject("data");
                            mVipUrl = item.getString("vip_url");
                            mVip = item.getBoolean("is_vip");
                            mVipDay = item.getInt("vip_day");
                            mVipExpireAt = item.getString("vip_end_at");
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    } else {
                        Timber.e("init vip info failed, error code %d", response.code());
                    }
                    refreshVipState(false);
                }
            });
        }
    });
}
Also used : Response(okhttp3.Response) Call(okhttp3.Call) JSONObject(org.json.JSONObject) JSONObject(org.json.JSONObject) SpannableString(android.text.SpannableString) IOException(java.io.IOException) JSONException(com.ichi2.utils.JSONException) CustomSyncServerUrlException(com.ichi2.libanki.sync.CustomSyncServerUrlException) IOException(java.io.IOException) ParseException(java.text.ParseException) ConfirmModSchemaException(com.ichi2.anki.exception.ConfirmModSchemaException)

Example 2 with BE_VIP

use of com.ichi2.anki.DeckPicker.BE_VIP in project AnkiChinaAndroid by ankichinateam.

the class DeckPicker method onActivityResult.

@SuppressWarnings("deprecation")
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);
    Timber.i("on activity result:%s", requestCode);
    if (requestCode == REQUEST_PATH_UPDATE) {
        // The collection path was inaccessible on startup so just close the activity and let user restart
        finishWithoutAnimation();
    } else if (resultCode == RESULT_MEDIA_EJECTED) {
        this.onSdCardNotMounted();
        return;
    } else if (resultCode == RESULT_DB_ERROR) {
        handleDbError();
        return;
    } else if (resultCode == RESULT_UPDATE_REST_SPACE) {
        getAccount().getToken(getBaseContext(), new MyAccount.TokenCallback() {

            @Override
            public void onSuccess(String token) {
                OKHttpUtil.get(Consts.ANKI_CHINA_BASE + Consts.API_VERSION + "clouds/current", token, new Object[] { "nothing", false }, checkRestServerSpaceListener);
            }

            @Override
            public void onFail(String message) {
                handleGetTokenFailed(message);
            }
        });
        return;
    }
    if (requestCode == REPORT_ERROR) {
        showStartupScreensAndDialogs(AnkiDroidApp.getSharedPrefs(this), 4);
    } else if (requestCode == SHOW_INFO_WELCOME || requestCode == SHOW_INFO_NEW_VERSION) {
        if (resultCode == RESULT_OK) {
            showStartupScreensAndDialogs(AnkiDroidApp.getSharedPrefs(this), requestCode == SHOW_INFO_WELCOME ? 2 : 3);
        } else {
            finishWithAnimation(ActivityTransitionAnimation.DOWN);
        }
    } else if (requestCode == LOG_IN_FOR_SYNC) {
        mRefreshVipStateOnResume = true;
        mSyncOnResume = resultCode == RESULT_OK;
    } else if ((requestCode == REQUEST_REVIEW || requestCode == SHOW_STUDYOPTIONS) && resultCode == Reviewer.RESULT_NO_MORE_CARDS) {
        // Show a message when reviewing has finished
        if (getCol().getSched().count() == 0) {
            UIUtils.showSimpleSnackbar(this, R.string.studyoptions_congrats_finished, false);
        } else {
            UIUtils.showSimpleSnackbar(this, R.string.studyoptions_no_cards_due, false);
        }
        refreshDeckListUI(false);
    } else if (requestCode == REQUEST_BROWSE_CARDS) {
        // Store the selected deck after opening browser
        if (intent != null && intent.getBooleanExtra("allDecksSelected", false)) {
            AnkiDroidApp.getSharedPrefs(this).edit().putLong("browserDeckIdFromDeckPicker", -1L).apply();
        } else {
            long selectedDeck = getCol().getDecks().selected();
            AnkiDroidApp.getSharedPrefs(this).edit().putLong("browserDeckIdFromDeckPicker", selectedDeck).apply();
        }
    }
    if (requestCode == BE_VIP || requestCode == REFRESH_LOGIN_STATE_AND_TURN_TO_VIP_HTML || requestCode == SHOW_STUDYOPTIONS || requestCode == CHANGE_ACCOUNT) {
        mRefreshVipStateOnResume = true;
        mSyncOnResume = requestCode == CHANGE_ACCOUNT;
        mTurnToVipHtml = requestCode == REFRESH_LOGIN_STATE_AND_TURN_TO_VIP_HTML;
    } else {
        getSupportFragmentManager().getFragments();
        if (getSupportFragmentManager().getFragments().size() > 0) {
            List<Fragment> fragments = getSupportFragmentManager().getFragments();
            for (Fragment mFragment : fragments) {
                mFragment.onActivityResult(requestCode, resultCode, intent);
            }
        }
    }
}
Also used : SpannableString(android.text.SpannableString) Fragment(androidx.fragment.app.Fragment) AsyncDialogFragment(com.ichi2.anki.dialogs.AsyncDialogFragment)

Example 3 with BE_VIP

use of com.ichi2.anki.DeckPicker.BE_VIP in project AnkiChinaAndroid by ankichinateam.

the class AbstractFlashcardViewer method onActivityResult.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Timber.i("onActivityResult:" + requestCode);
    if (requestCode == BE_VIP || requestCode == REFRESH_LOGIN_STATE_AND_TURN_TO_VIP_HTML) {
        mRefreshVipStateOnResume = true;
        mTurnToVipHtml = requestCode == REFRESH_LOGIN_STATE_AND_TURN_TO_VIP_HTML;
    } else if (requestCode == REFRESH_VOICE_INFO) {
        mRefreshVoiceInfoStateOnResume = true;
    } else if (resultCode == DeckPicker.RESULT_DB_ERROR) {
        closeReviewer(DeckPicker.RESULT_DB_ERROR, false);
    } else if (resultCode == DeckPicker.RESULT_MEDIA_EJECTED) {
        finishNoStorageAvailable();
    } else if (requestCode == REFRESH_TOP_BUTTONS) {
        restorePreferences();
        supportInvalidateOptionsMenu();
        invalidateOptionsMenu();
    } else if (requestCode == REQUEST_CODE_SPEAK_SETTING) {
        restorePreferences();
        mReInitBDVoice = true;
    } else if (requestCode == REFRESH_GESTURE) {
        restorePreferences();
    } else if (requestCode == REFRESH_CONTROLLER) {
        restorePreferences();
    }
    /* Reset the schedule and reload the latest card off the top of the stack if required.
           The card could have been rescheduled, the deck could have changed, or a change of
           note type could have lead to the card being deleted */
    if (data != null && data.hasExtra("reloadRequired")) {
        performReload();
    }
    if (requestCode == EDIT_CURRENT_CARD) {
        if (resultCode == RESULT_OK) {
            // content of note was changed so update the note and current card
            Timber.i("AbstractFlashcardViewer:: Saving card...");
            CollectionTask.launchCollectionTask(UPDATE_NOTE, mUpdateCardHandler, new TaskData(sEditorCard, true));
            onEditedNoteChanged();
        } else if (resultCode == RESULT_CANCELED && !(data != null && data.hasExtra("reloadRequired"))) {
            // nothing was changed by the note editor so just redraw the card
            redrawCard();
        }
    } else if (requestCode == DECK_OPTIONS && resultCode == RESULT_OK) {
        performReload();
    }
    if (!mDisableClipboard) {
        clipboardSetText("");
    }
}
Also used : TaskData(com.ichi2.async.TaskData)

Aggregations

SpannableString (android.text.SpannableString)2 Fragment (androidx.fragment.app.Fragment)1 AsyncDialogFragment (com.ichi2.anki.dialogs.AsyncDialogFragment)1 ConfirmModSchemaException (com.ichi2.anki.exception.ConfirmModSchemaException)1 TaskData (com.ichi2.async.TaskData)1 CustomSyncServerUrlException (com.ichi2.libanki.sync.CustomSyncServerUrlException)1 JSONException (com.ichi2.utils.JSONException)1 IOException (java.io.IOException)1 ParseException (java.text.ParseException)1 Call (okhttp3.Call)1 Response (okhttp3.Response)1 JSONObject (org.json.JSONObject)1