Search in sources :

Example 1 with IField

use of com.ichi2.anki.multimediacard.fields.IField in project Anki-Android by Ramblurr.

the class MultimediaCardEditorActivity method createEditorUI.

/**
     * Creates the UI for editor area inside EditorLayout
     * 
     * @param note
     */
private void createEditorUI(IMultimediaEditableNote note) {
    try {
        if (mNote == null) {
            finishCancel();
            return;
        } else {
            for (int i = 0; i < mNote.getNumberOfFields(); ++i) {
                IField f = mNote.getField(i);
                if (f == null) {
                    finishCancel();
                    return;
                }
            }
        }
        LinearLayout linearLayout = mEditorLayout;
        linearLayout.removeAllViews();
        for (int i = 0; i < note.getNumberOfFields(); ++i) {
            createNewViewer(linearLayout, note.getField(i), i);
        }
        mModelButton.setText(gtxt(R.string.multimedia_editor_activity_note_type) + " : " + mEditorNote.model().getString("name"));
        mDeckButton.setText(gtxt(R.string.multimedia_editor_activity_deck) + " : " + mCol.getDecks().get(mCurrentDid).getString("name"));
    } catch (JSONException e) {
        Log.e("Multimedia Editor", e.getMessage());
        return;
    }
}
Also used : JSONException(org.json.JSONException) IField(com.ichi2.anki.multimediacard.fields.IField) SuppressLint(android.annotation.SuppressLint) LinearLayout(android.widget.LinearLayout)

Example 2 with IField

use of com.ichi2.anki.multimediacard.fields.IField in project Anki-Android by Ramblurr.

the class NoteService method importMediaToDirectory.

/**
     * Considering the field is new, if it has media handle it
     * 
     * @param field
     */
private static void importMediaToDirectory(IField field) {
    String tmpMediaPath = null;
    switch(field.getType()) {
        case AUDIO:
            tmpMediaPath = field.getAudioPath();
            break;
        case IMAGE:
            tmpMediaPath = field.getImagePath();
            break;
        case TEXT:
        default:
            break;
    }
    if (tmpMediaPath != null) {
        try {
            File inFile = new File(tmpMediaPath);
            if (inFile.exists()) {
                Collection col = AnkiDroidApp.getCol();
                String mediaDir = col.getMedia().getDir() + "/";
                File mediaDirFile = new File(mediaDir);
                File parent = inFile.getParentFile();
                // If already there.
                if (mediaDirFile.getAbsolutePath().contentEquals(parent.getAbsolutePath())) {
                    return;
                }
                File outFile = new File(mediaDir + inFile.getName());
                if (!outFile.exists()) {
                    if (field.hasTemporaryMedia()) {
                        // Move
                        inFile.renameTo(outFile);
                    } else {
                        // Copy
                        InputStream in = new FileInputStream(tmpMediaPath);
                        OutputStream out = new FileOutputStream(outFile.getAbsolutePath());
                        byte[] buf = new byte[1024];
                        int len;
                        while ((len = in.read(buf)) > 0) {
                            out.write(buf, 0, len);
                        }
                        in.close();
                        out.close();
                    }
                    switch(field.getType()) {
                        case AUDIO:
                            field.setAudioPath(outFile.getAbsolutePath());
                            break;
                        case IMAGE:
                            field.setImagePath(outFile.getAbsolutePath());
                            break;
                        default:
                            break;
                    }
                }
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) Collection(com.ichi2.libanki.Collection) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 3 with IField

use of com.ichi2.anki.multimediacard.fields.IField in project Anki-Android by Ramblurr.

the class NoteService method updateMultimediaNoteFromJsonNote.

public static void updateMultimediaNoteFromJsonNote(final Note editorNoteSrc, final IMultimediaEditableNote noteDst) {
    if (noteDst instanceof MultimediaEditableNote) {
        MultimediaEditableNote mmNote = (MultimediaEditableNote) noteDst;
        String[] values = editorNoteSrc.getFields();
        for (int i = 0; i < values.length; i++) {
            String value = values[i];
            IField field = null;
            if (value.startsWith("<img")) {
                field = new ImageField();
            } else if (value.startsWith("[sound:")) {
                field = new AudioField();
            } else {
                field = new TextField();
            }
            field.setFormattedString(value);
            mmNote.setField(i, field);
        }
        mmNote.setModelId(editorNoteSrc.getMid());
    // TODO: set current id of the note as well
    }
}
Also used : AudioField(com.ichi2.anki.multimediacard.fields.AudioField) ImageField(com.ichi2.anki.multimediacard.fields.ImageField) MultimediaEditableNote(com.ichi2.anki.multimediacard.impl.MultimediaEditableNote) IMultimediaEditableNote(com.ichi2.anki.multimediacard.IMultimediaEditableNote) TextField(com.ichi2.anki.multimediacard.fields.TextField) IField(com.ichi2.anki.multimediacard.fields.IField)

Example 4 with IField

use of com.ichi2.anki.multimediacard.fields.IField in project Anki-Android by Ramblurr.

the class NoteService method saveMedia.

/**
     * Saves the multimedia associated with this card to proper path inside anki folder. For each field associated with
     * the note it checks for the following condition a. The field content should have changed b. The field content does
     * not already point to a media inside anki media path If both condition satisfies then it copies the file inside
     * the media path and deletes the file referenced by the note
     * 
     * @param note
     */
public static void saveMedia(final MultimediaEditableNote noteNew) {
    // if (noteNew.getModelId() == noteOld.getModelId())
    // {
    // int fieldCount = noteNew.getNumberOfFields();
    // for (int i = 0; i < fieldCount; i++)
    // {
    // IField newField = noteNew.getField(i);
    // IField oldField = noteOld.getField(i);
    // if
    // (newField.getFormattedValue().equals(oldField.getFormattedValue()))
    // {
    // continue;
    // }
    // importMediaToDirectory(newField);
    // }
    // }
    // else
    // {
    int fieldCount = noteNew.getNumberOfFields();
    for (int i = 0; i < fieldCount; i++) {
        IField newField = noteNew.getField(i);
        importMediaToDirectory(newField);
    }
// }
}
Also used : IField(com.ichi2.anki.multimediacard.fields.IField)

Example 5 with IField

use of com.ichi2.anki.multimediacard.fields.IField in project Anki-Android by Ramblurr.

the class MultimediaCardEditorActivity method createNewViewer.

private void createNewViewer(LinearLayout linearLayout, final IField field, final int index) {
    final MultimediaCardEditorActivity context = this;
    switch(field.getType()) {
        case TEXT:
            // Create a text field and an edit button, opening editing for
            // the
            // text field
            TextView textView = new TextView(this);
            textView.setText(field.getText());
            linearLayout.addView(textView, LinearLayout.LayoutParams.MATCH_PARENT);
            break;
        case IMAGE:
            ImageView imgView = new ImageView(this);
            //
            // BitmapFactory.Options options = new BitmapFactory.Options();
            // options.inSampleSize = 2;
            // Bitmap bm = BitmapFactory.decodeFile(myJpgPath, options);
            // jpgView.setImageBitmap(bm);
            LinearLayout.LayoutParams p = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
            File f = new File(field.getImagePath());
            Bitmap b = BitmapUtil.decodeFile(f, getMaxImageSize());
            int currentapiVersion = android.os.Build.VERSION.SDK_INT;
            if (currentapiVersion >= android.os.Build.VERSION_CODES.ECLAIR) {
                b = ExifUtil.rotateFromCamera(f, b);
            }
            imgView.setImageBitmap(b);
            imgView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
            imgView.setAdjustViewBounds(true);
            DisplayMetrics metrics = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(metrics);
            int height = metrics.heightPixels;
            int width = metrics.widthPixels;
            imgView.setMaxHeight((int) Math.round(height * 0.6));
            imgView.setMaxWidth((int) Math.round(width * 0.7));
            linearLayout.addView(imgView, p);
            break;
        case AUDIO:
            AudioView audioView = AudioView.createPlayerInstance(this, R.drawable.av_play, R.drawable.av_pause, R.drawable.av_stop, field.getAudioPath());
            linearLayout.addView(audioView);
            break;
        default:
            Log.e("multimedia editor", "Unsupported field type found");
            break;
    }
    Button editButtonText = new Button(this);
    editButtonText.setText(gtxt(R.string.multimedia_editor_activity_edit_button));
    linearLayout.addView(editButtonText, LinearLayout.LayoutParams.MATCH_PARENT);
    editButtonText.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent i = new Intent(context, EditFieldActivity.class);
            putExtrasAndStartEditActivity(field, index, i);
        }
    });
}
Also used : LayoutParams(android.widget.LinearLayout.LayoutParams) OnClickListener(android.view.View.OnClickListener) Intent(android.content.Intent) DisplayMetrics(android.util.DisplayMetrics) ImageView(android.widget.ImageView) View(android.view.View) TextView(android.widget.TextView) AudioView(com.ichi2.anki.multimediacard.AudioView) SuppressLint(android.annotation.SuppressLint) LayoutParams(android.widget.LinearLayout.LayoutParams) AudioView(com.ichi2.anki.multimediacard.AudioView) Bitmap(android.graphics.Bitmap) Button(android.widget.Button) TextView(android.widget.TextView) ImageView(android.widget.ImageView) File(java.io.File) LinearLayout(android.widget.LinearLayout)

Aggregations

IField (com.ichi2.anki.multimediacard.fields.IField)4 SuppressLint (android.annotation.SuppressLint)3 View (android.view.View)2 OnClickListener (android.view.View.OnClickListener)2 Button (android.widget.Button)2 LinearLayout (android.widget.LinearLayout)2 File (java.io.File)2 Intent (android.content.Intent)1 Bitmap (android.graphics.Bitmap)1 DisplayMetrics (android.util.DisplayMetrics)1 ImageView (android.widget.ImageView)1 LayoutParams (android.widget.LinearLayout.LayoutParams)1 TextView (android.widget.TextView)1 AudioView (com.ichi2.anki.multimediacard.AudioView)1 IMultimediaEditableNote (com.ichi2.anki.multimediacard.IMultimediaEditableNote)1 PickStringDialogFragment (com.ichi2.anki.multimediacard.activity.PickStringDialogFragment)1 AudioField (com.ichi2.anki.multimediacard.fields.AudioField)1 ImageField (com.ichi2.anki.multimediacard.fields.ImageField)1 TextField (com.ichi2.anki.multimediacard.fields.TextField)1 MultimediaEditableNote (com.ichi2.anki.multimediacard.impl.MultimediaEditableNote)1