Search in sources :

Example 1 with Side

use of com.ichi2.anki.cardviewer.Side in project AnkiChinaAndroid by ankichinateam.

the class AbstractFlashcardViewer method updateCard.

private void updateCard(final String newContent) {
    Timber.d("updateCard()");
    mCacheContent = newContent;
    mUseTimerDynamicMS = 0;
    // Add CSS for font color and font size
    if (mCurrentCard == null) {
        processCardAction(cardWebView -> cardWebView.getSettings().setDefaultFontSize(calculateDynamicFontSize(newContent)));
    }
    if (sDisplayAnswer) {
        addAnswerSounds(newContent);
    } else {
        // reset sounds each time first side of card is displayed, which may happen repeatedly without ever
        // leaving the card (such as when edited)
        mSoundPlayer.resetSounds();
        mAnswerSoundsAdded = false;
        mSoundPlayer.addSounds(mBaseUrl, newContent, SoundSide.QUESTION);
        if (mUseTimer && !mAnswerSoundsAdded && getConfigForCurrentCard().optBoolean("autoplay", false)) {
            addAnswerSounds(mCurrentCard.a());
        }
    }
    String content = Sound.expandSounds(mBaseUrl, newContent);
    content = CardAppearance.fixBoldStyle(content);
    Timber.v("content card = \n %s", content);
    String style = mCardAppearance.getStyle();
    Timber.v("::style:: / %s", style);
    // CSS class for card-specific styling
    String cardClass = mCardAppearance.getCardClass(mCurrentCard.getOrd() + 1, Themes.getCurrentTheme(this));
    if (Template.textContainsMathjax(content)) {
        cardClass += " mathjax-needs-to-render";
    }
    if (isInNightMode()) {
        if (!mCardAppearance.hasUserDefinedNightMode(mCurrentCard)) {
            content = HtmlColors.invertColors(content);
        }
    }
    content = CardAppearance.convertSmpToHtmlEntity(content);
    SharedPreferences prefs = AnkiDroidApp.getSharedPrefs(this);
    String localViewSettingStr = prefs.getString(Consts.KEY_LOCAL_LAYOUT_CONFIG, "");
    boolean dark = false;
    if (AnkiDroidApp.getSharedPrefs(this).getBoolean("invertedColors", false)) {
        int theme = Integer.parseInt(prefs.getString("nightTheme", "0"));
        dark = theme == THEME_NIGHT_DARK || theme == THEME_NIGHT_BLACK;
    }
    if (mCurrentCSS.isEmpty() || mCurrentCSSModelID != mCurrentCard.model().getLong("id")) {
        mCurrentCSSModelID = mCurrentCard.model().getLong("id");
        Timber.i("find new model id css %s", mCurrentCSS);
        if (localViewSettingStr != null && !localViewSettingStr.isEmpty()) {
            try {
                JSONObject viewSetting = new JSONObject(localViewSettingStr);
                JSONObject currentModelSetting = viewSetting.getJSONObject(String.valueOf(mCurrentCSSModelID));
                if (currentModelSetting != null) {
                    mCurrentCSS = convertJson2Css(currentModelSetting, false);
                } else {
                    mCurrentCSS = "";
                }
            } catch (Exception e) {
                e.printStackTrace();
                mCurrentCSS = "";
            }
        }
    }
    Timber.i("now theme is dark:%s", dark);
    if (!dark) {
        if (!mCurrentCSS.isEmpty()) {
            Matcher bgMatcher = Pattern.compile("background-color:(.+?);").matcher(mCurrentCSS);
            Window window = getWindow();
            if (bgMatcher.find()) {
                String fld1 = bgMatcher.group(1).trim();
                if (shouldChangeToolbarBgLikeCss2()) {
                    findViewById(R.id.toolbar).setBackgroundColor(Color.parseColor(fld1));
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        window.setStatusBarColor(Color.parseColor(fld1));
                    }
                }
                mCardWebView.setBackgroundColor(Color.parseColor(fld1));
                findViewById(R.id.bottom_area_layout).setBackgroundColor(Color.parseColor(fld1));
            } else {
                if (shouldChangeToolbarBgLikeCss2()) {
                    int[] attrs = new int[] { R.attr.reviewStatusBarColor };
                    TypedArray ta = obtainStyledAttributes(attrs);
                    findViewById(R.id.toolbar).setBackground(ta.getDrawable(0));
                    ta.recycle();
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        window.setStatusBarColor(Themes.getColorFromAttr(this, getStatusBarColorAttr()));
                    }
                }
                mCardWebView.setBackgroundColor(Color.WHITE);
                findViewById(R.id.bottom_area_layout).setBackgroundColor(Color.TRANSPARENT);
            }
        } else {
            if (shouldChangeToolbarBgLikeCss2()) {
                int[] attrs = new int[] { R.attr.reviewStatusBarColor };
                TypedArray ta = obtainStyledAttributes(attrs);
                findViewById(R.id.toolbar).setBackground(ta.getDrawable(0));
                ta.recycle();
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    Window window = getWindow();
                    window.setStatusBarColor(Themes.getColorFromAttr(this, getStatusBarColorAttr()));
                }
            }
            mCardWebView.setBackgroundColor(Color.WHITE);
            findViewById(R.id.bottom_area_layout).setBackgroundColor(Color.TRANSPARENT);
        }
    }
    // if (mCurrentCSS.isEmpty()) {//保存的配置文件没有,则直接找默认的css
    // mCurrentCSS = mCurrentCard.css().replace("<style>", "").replace("</style>", "");
    // }
    mCardContent = mCardTemplate.replace("::content::", content).replace("::style::", style).replace("::class::", cardClass).replace("::style2::", mCurrentCSS).replace("::class2::", sDisplayAnswer ? "ck-back" : "ck-front");
    Timber.d("base url = %s", mBaseUrl);
    Timber.v("::content:: / %s", content);
    Timber.v("::style2:: / %s", mCurrentCSS);
    if (AnkiDroidApp.getSharedPrefs(this).getBoolean("html_javascript_debugging", false)) {
        try {
            try (FileOutputStream f = new FileOutputStream(new File(CollectionHelper.getCurrentAnkiDroidDirectory(this), "card.html"))) {
                f.write(mCardContent.getBytes());
            }
        } catch (IOException e) {
            Timber.d(e, "failed to save card");
        }
    }
    fillFlashcard();
    if (!mConfigurationChanged) {
        playSoundsVIP(false);
    }
}
Also used : Window(android.view.Window) SharedPreferences(android.content.SharedPreferences) Matcher(java.util.regex.Matcher) IOException(java.io.IOException) SuppressLint(android.annotation.SuppressLint) JSONException(com.ichi2.utils.JSONException) IOException(java.io.IOException) ActivityNotFoundException(android.content.ActivityNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) JSONObject(com.ichi2.utils.JSONObject) TypedArray(android.content.res.TypedArray) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Example 2 with Side

use of com.ichi2.anki.cardviewer.Side in project AnkiChinaAndroid by ankichinateam.

the class Syncer method applyChanges.

public JSONObject applyChanges(JSONObject changes) throws UnexpectedSchemaChange {
    mRChg = changes;
    JSONObject lchg = changes();
    // merge our side before returning
    mergeChanges(lchg, mRChg);
    return lchg;
}
Also used : JSONObject(com.ichi2.utils.JSONObject)

Example 3 with Side

use of com.ichi2.anki.cardviewer.Side in project Anki-Android by ankidroid.

the class Syncer method applyChanges.

public JSONObject applyChanges(JSONObject changes) throws UnexpectedSchemaChange {
    JSONObject lchg = changes();
    // merge our side before returning
    mergeChanges(lchg, changes);
    return lchg;
}
Also used : JSONObject(com.ichi2.utils.JSONObject)

Example 4 with Side

use of com.ichi2.anki.cardviewer.Side in project Anki-Android by Ramblurr.

the class StudyOptionsFragment method initAllContentViews.

private void initAllContentViews(LayoutInflater inflater) {
    mStudyOptionsView = inflater.inflate(R.layout.studyoptions, null);
    Themes.setContentStyle(mStudyOptionsView, Themes.CALLER_STUDYOPTIONS);
    mTextDeckName = (TextView) mStudyOptionsView.findViewById(R.id.studyoptions_deck_name);
    mTextDeckDescription = (TextView) mStudyOptionsView.findViewById(R.id.studyoptions_deck_description);
    mButtonStart = (Button) mStudyOptionsView.findViewById(R.id.studyoptions_start);
    if (AnkiDroidApp.colIsOpen() && AnkiDroidApp.getCol().getDecks().isDyn(AnkiDroidApp.getCol().getDecks().selected())) {
        Button rebBut = (Button) mStudyOptionsView.findViewById(R.id.studyoptions_rebuild_cram);
        rebBut.setOnClickListener(mButtonClickListener);
        Button emptyBut = (Button) mStudyOptionsView.findViewById(R.id.studyoptions_empty_cram);
        emptyBut.setOnClickListener(mButtonClickListener);
        ((LinearLayout) mStudyOptionsView.findViewById(R.id.studyoptions_cram_buttons)).setVisibility(View.VISIBLE);
    }
    if (mFragmented) {
        Button but = (Button) mStudyOptionsView.findViewById(R.id.studyoptions_options);
        but.setOnClickListener(mButtonClickListener);
        mFragmentedCram = (Button) mStudyOptionsView.findViewById(R.id.studyoptions_new_filtercram);
        if (AnkiDroidApp.colIsOpen() && AnkiDroidApp.getCol().getDecks().isDyn(AnkiDroidApp.getCol().getDecks().selected())) {
            mFragmentedCram.setEnabled(false);
            mFragmentedCram.setVisibility(View.GONE);
        } else {
            // If we are in fragmented mode, then the cram option in menu applies to all decks, so we need an extra
            // button in StudyOptions side of screen to create dynamic deck filtered on this deck
            mFragmentedCram.setEnabled(true);
            mFragmentedCram.setVisibility(View.VISIBLE);
        }
        mFragmentedCram.setOnClickListener(mButtonClickListener);
    } else {
        mAddNote = (ImageButton) mStudyOptionsView.findViewById(R.id.studyoptions_add);
        if (AnkiDroidApp.colIsOpen()) {
            Collection col = AnkiDroidApp.getCol();
            if (col.getDecks().isDyn(col.getDecks().selected())) {
                mAddNote.setEnabled(false);
            }
        }
        mCardBrowser = (ImageButton) mStudyOptionsView.findViewById(R.id.studyoptions_card_browser);
        mStatisticsButton = (ImageButton) mStudyOptionsView.findViewById(R.id.studyoptions_statistics);
        mDeckOptions = (ImageButton) mStudyOptionsView.findViewById(R.id.studyoptions_options);
        mAddNote.setOnClickListener(mButtonClickListener);
        mCardBrowser.setOnClickListener(mButtonClickListener);
        mStatisticsButton.setOnClickListener(mButtonClickListener);
        mDeckOptions.setOnClickListener(mButtonClickListener);
    }
    mGlobalBar = (View) mStudyOptionsView.findViewById(R.id.studyoptions_global_bar);
    mGlobalMatBar = (View) mStudyOptionsView.findViewById(R.id.studyoptions_global_mat_bar);
    mBarsMax = (View) mStudyOptionsView.findViewById(R.id.studyoptions_progressbar_content);
    mTextTodayNew = (TextView) mStudyOptionsView.findViewById(R.id.studyoptions_new);
    mTextTodayLrn = (TextView) mStudyOptionsView.findViewById(R.id.studyoptions_lrn);
    mTextTodayRev = (TextView) mStudyOptionsView.findViewById(R.id.studyoptions_rev);
    mTextNewTotal = (TextView) mStudyOptionsView.findViewById(R.id.studyoptions_total_new);
    mTextTotal = (TextView) mStudyOptionsView.findViewById(R.id.studyoptions_total);
    mTextETA = (TextView) mStudyOptionsView.findViewById(R.id.studyoptions_eta);
    mSmallChart = (LinearLayout) mStudyOptionsView.findViewById(R.id.studyoptions_mall_chart);
    mGlobalMatBar.setVisibility(View.INVISIBLE);
    mGlobalBar.setVisibility(View.INVISIBLE);
    mDeckCounts = (LinearLayout) mStudyOptionsView.findViewById(R.id.studyoptions_deckcounts);
    mDeckChart = (LinearLayout) mStudyOptionsView.findViewById(R.id.studyoptions_chart);
    mButtonStart.setOnClickListener(mButtonClickListener);
    // mButtonUp.setOnClickListener(mButtonClickListener);
    // mButtonDown.setOnClickListener(mButtonClickListener);
    // mToggleLimitToggle.setOnClickListener(mButtonClickListener);
    // mToggleCram.setOnClickListener(mButtonClickListener);
    // mToggleNight.setOnClickListener(mButtonClickListener);
    // The view that shows the congratulations view.
    mCongratsView = inflater.inflate(R.layout.studyoptions_congrats, null);
    // The view that shows the learn more options
    mCustomStudyDetailsView = inflater.inflate(R.layout.styled_custom_study_details_dialog, null);
    mCustomStudyTextView1 = (TextView) mCustomStudyDetailsView.findViewById(R.id.custom_study_details_text1);
    mCustomStudyTextView2 = (TextView) mCustomStudyDetailsView.findViewById(R.id.custom_study_details_text2);
    mCustomStudyEditText = (EditText) mCustomStudyDetailsView.findViewById(R.id.custom_study_details_edittext2);
    Themes.setWallpaper(mCongratsView);
    mTextCongratsMessage = (TextView) mCongratsView.findViewById(R.id.studyoptions_congrats_message);
    Themes.setTextViewStyle(mTextCongratsMessage);
    mTextCongratsMessage.setOnClickListener(mButtonClickListener);
    mButtonCongratsUndo = (Button) mCongratsView.findViewById(R.id.studyoptions_congrats_undo);
    mButtonCongratsUnbury = (Button) mCongratsView.findViewById(R.id.studyoptions_congrats_unbury);
    mButtonCongratsCustomStudy = (Button) mCongratsView.findViewById(R.id.studyoptions_congrats_customstudy);
    mButtonCongratsOpenOtherDeck = (Button) mCongratsView.findViewById(R.id.studyoptions_congrats_open_other_deck);
    if (mFragmented) {
        mButtonCongratsOpenOtherDeck.setVisibility(View.GONE);
    }
    mButtonCongratsUndo.setOnClickListener(mButtonClickListener);
    mButtonCongratsUnbury.setOnClickListener(mButtonClickListener);
    mButtonCongratsCustomStudy.setOnClickListener(mButtonClickListener);
    mButtonCongratsOpenOtherDeck.setOnClickListener(mButtonClickListener);
}
Also used : ImageButton(android.widget.ImageButton) Button(android.widget.Button) Collection(com.ichi2.libanki.Collection) LinearLayout(android.widget.LinearLayout)

Aggregations

JSONObject (com.ichi2.utils.JSONObject)3 SuppressLint (android.annotation.SuppressLint)1 ActivityNotFoundException (android.content.ActivityNotFoundException)1 SharedPreferences (android.content.SharedPreferences)1 TypedArray (android.content.res.TypedArray)1 Window (android.view.Window)1 Button (android.widget.Button)1 ImageButton (android.widget.ImageButton)1 LinearLayout (android.widget.LinearLayout)1 Collection (com.ichi2.libanki.Collection)1 JSONException (com.ichi2.utils.JSONException)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Matcher (java.util.regex.Matcher)1