Search in sources :

Example 11 with JSONObject

use of com.ichi2.utils.JSONObject in project AnkiChinaAndroid by ankichinateam.

the class PosterActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    Themes.setThemeLegacy(this);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_poster);
    String poster = getIntent().getStringExtra("poster");
    root = new JSONObject(poster);
    JSONArray members = root.getJSONArray("members");
    setText(R.id.content_title, root.getString("title"));
    setText(R.id.content_classify, root.getString("level") + "/" + root.getString("subject"));
    setText(R.id.content_price_origin, "原价¥ " + root.getString("origin_price"));
    // 中划线
    ((TextView) findViewById(R.id.content_price_origin)).getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
    setText(R.id.content_price, "¥ " + root.getString("team_price"));
    setText(R.id.content_num, root.getString("study_users") + "人正在学习");
    setText(R.id.content_time_remain, "拼团特惠仅剩" + root.getInt("quota") + "名额\n" + convertSeconds(root.getInt("countDown")) + "后结束");
    setText(R.id.content_num_remain, "还差" + (root.getInt("users") - members.length()) + "人,特享团购价");
    ImageView qrcode = findViewById(R.id.qr_code);
    Bitmap mBitmap = QRCodeUtil.createQRCodeBitmap(root.getString("link"), Utils.dp2px(this, 160), Utils.dp2px(this, 160));
    qrcode.setImageBitmap(mBitmap);
    LinearLayout memberLayout = findViewById(R.id.join_list);
    for (int i = 0; i < members.length(); i++) {
        if (root.getInt("users") > 3) {
            // 需要人数大于3个,那就保留一个空位
            if (i > 1) {
                // 最多添加2个
                break;
            }
        }
        // if(root.getInt("users")==3) {
        // //需要人数刚好是3个,那就三个都占满
        // if (i > 2) {
        // break;
        // }
        // }
        View view = getLayoutInflater().inflate(R.layout.item_poster_avatar, null);
        ImageView avatar = view.findViewById(R.id.avatar);
        if (i == 0) {
            view.findViewById(R.id.host_role).setVisibility(View.VISIBLE);
        }
        Glide.with(this).asBitmap().load(members.getJSONObject(i).getString("avatar")).into(avatar);
        memberLayout.addView(view);
    }
    for (int i = memberLayout.getChildCount(); i < 3; i++) {
        View view = getLayoutInflater().inflate(R.layout.item_poster_avatar_empty, null);
        memberLayout.addView(view);
    }
    JSONArray tags = root.getJSONArray("tags");
    for (int i = 0; i < tags.length(); i++) {
        View view = getLayoutInflater().inflate(R.layout.item_warp_poster, null);
        Button button = view.findViewById(R.id.text);
        button.setText(tags.getString(i));
        ((WarpLinearLayout) findViewById(R.id.tags_layout)).addView(view);
    }
    showShareDialog(findViewById(R.id.rootView));
}
Also used : Bitmap(android.graphics.Bitmap) JSONObject(com.ichi2.utils.JSONObject) Button(android.widget.Button) JSONArray(com.ichi2.utils.JSONArray) WarpLinearLayout(com.ichi2.ui.WarpLinearLayout) ImageView(android.widget.ImageView) ImageView(android.widget.ImageView) View(android.view.View) TextView(android.widget.TextView) LinearLayout(android.widget.LinearLayout) WarpLinearLayout(com.ichi2.ui.WarpLinearLayout) Paint(android.graphics.Paint)

Example 12 with JSONObject

use of com.ichi2.utils.JSONObject in project AnkiChinaAndroid by ankichinateam.

the class StudySettingActivity method updateValues.

private void updateValues() {
    // new
    JSONObject newOptions = mOptions.getJSONObject("new");
    tx_max_learn_card.setText(newOptions.getString("perDay"));
    mCurrentNewOrderValue = Integer.parseInt(newOptions.getString("order"));
    tx_new_card_sequence.setText(mNewOrderMap.get(mCurrentNewOrderValue));
    tx_interval_step.setText(StepsPreference.convertFromJSON(newOptions.getJSONArray("delays")));
    // 毕业间隔
    tx_interval_graduate.setText(newOptions.getJSONArray("ints").getString(0));
    // 简单按钮间隔
    tx_interval_simple.setText(newOptions.getJSONArray("ints").getString(1));
    // 初始难度
    tx_init_level.setText(String.valueOf(newOptions.getInt("initialFactor") / 10));
    // rev
    JSONObject revOptions = mOptions.getJSONObject("rev");
    tx_max_review_card.setText(revOptions.getString("perDay"));
    // 唯一一个全局设置
    mCurrentStudyPreferenceValue = mCol.getConf().getInt("newSpread");
    tx_learn_sequence.setText(mStudyPreferenceMap.get(mCurrentStudyPreferenceValue));
    tx_medal_simple.setText(String.valueOf((int) Math.round((revOptions.getDouble("ease4") * 100))));
    Timber.i("ivl fct:" + revOptions.getDouble("ivlFct"));
    tx_interval_decoration.setText(String.valueOf((int) Math.round(revOptions.getDouble("ivlFct") * 100)));
    tx_max_interval.setText(revOptions.getString("maxIvl"));
    // lapses
    JSONObject lapOptions = mOptions.getJSONObject("lapse");
    tx_error_interval_step.setText(StepsPreference.convertFromJSON(lapOptions.getJSONArray("delays")));
    tx_error_new_interval.setText(String.valueOf((int) Math.round(lapOptions.getDouble("mult") * 100)));
    tx_error_min_interval.setText(lapOptions.getString("minInt"));
    SharedPreferences sharedPreferences = getSharedPreferences(STUDY_SETTING, 0);
    // mCurrentMindModeValue = sharedPreferences.getInt(KEY_MIND_MODE, 0);
    String savedMindModeValue = sharedPreferences.getString(KEY_MIND_MODE, "");
    Map<String, Integer> map = null;
    try {
        Gson gson = new Gson();
        map = gson.fromJson(savedMindModeValue, new TypeToken<Map<String, Integer>>() {
        }.getType());
    } catch (Exception e) {
        e.printStackTrace();
    }
    // Timber.i("我来取数据了 "+ map.get(mDeckIdStr));
    mCurrentMindModeValue = map != null && map.get(mDeckIdStr) != null ? map.get(mDeckIdStr).intValue() : 0;
    tx_algorithm.setText(mMindModeMap.get(mCurrentMindModeValue));
}
Also used : JSONObject(com.ichi2.utils.JSONObject) SharedPreferences(android.content.SharedPreferences) Gson(com.google.gson.Gson) HashMap(java.util.HashMap) Map(java.util.Map)

Example 13 with JSONObject

use of com.ichi2.utils.JSONObject in project AnkiChinaAndroid by ankichinateam.

the class CardContentProvider method addModelToCursor.

private void addModelToCursor(Long modelId, Models models, MatrixCursor rv, String[] columns) {
    Model jsonObject = models.get(modelId);
    MatrixCursor.RowBuilder rb = rv.newRow();
    try {
        for (String column : columns) {
            if (column.equals(FlashCardsContract.Model._ID)) {
                rb.add(modelId);
            } else if (column.equals(FlashCardsContract.Model.NAME)) {
                rb.add(jsonObject.getString("name"));
            } else if (column.equals(FlashCardsContract.Model.FIELD_NAMES)) {
                JSONArray flds = jsonObject.getJSONArray("flds");
                String[] allFlds = new String[flds.length()];
                for (int idx = 0; idx < flds.length(); idx++) {
                    allFlds[idx] = flds.getJSONObject(idx).optString("name", "");
                }
                rb.add(Utils.joinFields(allFlds));
            } else if (column.equals(FlashCardsContract.Model.NUM_CARDS)) {
                rb.add(jsonObject.getJSONArray("tmpls").length());
            } else if (column.equals(FlashCardsContract.Model.CSS)) {
                rb.add(jsonObject.getString("css"));
            } else if (column.equals(FlashCardsContract.Model.DECK_ID)) {
                // #6378 - Anki Desktop changed schema temporarily to allow null
                rb.add(jsonObject.optLong("did", Consts.DEFAULT_DECK_ID));
            } else if (column.equals(FlashCardsContract.Model.SORT_FIELD_INDEX)) {
                rb.add(jsonObject.getLong("sortf"));
            } else if (column.equals(FlashCardsContract.Model.TYPE)) {
                rb.add(jsonObject.getLong("type"));
            } else if (column.equals(FlashCardsContract.Model.LATEX_POST)) {
                rb.add(jsonObject.getString("latexPost"));
            } else if (column.equals(FlashCardsContract.Model.LATEX_PRE)) {
                rb.add(jsonObject.getString("latexPre"));
            } else if (column.equals(FlashCardsContract.Model.NOTE_COUNT)) {
                rb.add(models.useCount(jsonObject));
            } else {
                throw new UnsupportedOperationException("Column \"" + column + "\" is unknown");
            }
        }
    } catch (JSONException e) {
        Timber.e(e, "Error parsing JSONArray");
        throw new IllegalArgumentException("Model " + modelId + " is malformed", e);
    }
}
Also used : Model(com.ichi2.libanki.Model) JSONArray(com.ichi2.utils.JSONArray) JSONException(com.ichi2.utils.JSONException) MatrixCursor(android.database.MatrixCursor)

Example 14 with JSONObject

use of com.ichi2.utils.JSONObject in project AnkiChinaAndroid by ankichinateam.

the class CollectionTask method doInBackGroundDeleteField.

/**
 * Deletes thje given field in the given model
 */
private TaskData doInBackGroundDeleteField(TaskData param) {
    Timber.d("doInBackGroundDeleteField");
    Object[] objects = param.getObjArray();
    Model model = (Model) objects[0];
    JSONObject field = (JSONObject) objects[1];
    Collection col = getCol();
    try {
        col.getModels().remField(model, field);
        col.save();
    } catch (ConfirmModSchemaException e) {
        // Should never be reached
        return new TaskData(false);
    }
    return new TaskData(true);
}
Also used : JSONObject(com.ichi2.utils.JSONObject) Model(com.ichi2.libanki.Model) TemporaryModel(com.ichi2.anki.TemporaryModel) Collection(com.ichi2.libanki.Collection) ConfirmModSchemaException(com.ichi2.anki.exception.ConfirmModSchemaException) JSONObject(com.ichi2.utils.JSONObject)

Example 15 with JSONObject

use of com.ichi2.utils.JSONObject in project AnkiChinaAndroid by ankichinateam.

the class NoteService method createEmptyNote.

/**
 * Creates an empty Note from given Model
 *
 * @param model the model in JSOBObject format
 * @return a new note instance
 */
public static MultimediaEditableNote createEmptyNote(JSONObject model) {
    try {
        JSONArray fieldsArray = model.getJSONArray("flds");
        int numOfFields = fieldsArray.length();
        if (numOfFields > 0) {
            MultimediaEditableNote note = new MultimediaEditableNote();
            note.setNumFields(numOfFields);
            for (int i = 0; i < numOfFields; i++) {
                JSONObject fieldObject = fieldsArray.getJSONObject(i);
                TextField uiTextField = new TextField();
                uiTextField.setName(fieldObject.getString("name"));
                uiTextField.setText(fieldObject.getString("name"));
                note.setField(i, uiTextField);
            }
            note.setModelId(model.getLong("id"));
            return note;
        }
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}
Also used : MultimediaEditableNote(com.ichi2.anki.multimediacard.impl.MultimediaEditableNote) IMultimediaEditableNote(com.ichi2.anki.multimediacard.IMultimediaEditableNote) JSONObject(com.ichi2.utils.JSONObject) JSONArray(com.ichi2.utils.JSONArray) TextField(com.ichi2.anki.multimediacard.fields.TextField) JSONException(com.ichi2.utils.JSONException)

Aggregations

JSONObject (com.ichi2.utils.JSONObject)272 JSONArray (com.ichi2.utils.JSONArray)110 ArrayList (java.util.ArrayList)59 Test (org.junit.Test)55 Collection (com.ichi2.libanki.Collection)46 IOException (java.io.IOException)40 RobolectricTest (com.ichi2.anki.RobolectricTest)34 JSONException (com.ichi2.utils.JSONException)34 Note (com.ichi2.libanki.Note)33 Model (com.ichi2.libanki.Model)31 HashMap (java.util.HashMap)31 SuppressLint (android.annotation.SuppressLint)29 JSONObject (org.json.JSONObject)27 Response (okhttp3.Response)25 JSONException (org.json.JSONException)24 NonNull (androidx.annotation.NonNull)23 Card (com.ichi2.libanki.Card)21 File (java.io.File)21 Map (java.util.Map)20 Intent (android.content.Intent)19