Search in sources :

Example 1 with Sound

use of com.ichi2.libanki.Sound in project AnkiChinaAndroid by ankichinateam.

the class ImportTest method testAnki2Mediadupes.

@Test
public void testAnki2Mediadupes() throws IOException, JSONException, ImportExportException {
    List<String> expected;
    List<String> actual;
    // add a note that references a sound
    Note n = testCol.newNote();
    n.setField(0, "[sound:foo.mp3]");
    long mid = n.model().getLong("id");
    testCol.addNote(n);
    // add that sound to the media folder
    FileOutputStream os;
    os = new FileOutputStream(new File(testCol.getMedia().dir(), "foo.mp3"), false);
    os.write("foo".getBytes());
    os.close();
    testCol.close();
    // it should be imported correctly into an empty deck
    Collection empty = Shared.getEmptyCol(InstrumentationRegistry.getInstrumentation().getTargetContext());
    Importer imp = new Anki2Importer(empty, testCol.getPath());
    imp.run();
    expected = Collections.singletonList("foo.mp3");
    actual = Arrays.asList(new File(empty.getMedia().dir()).list());
    actual.retainAll(expected);
    assertEquals(expected.size(), actual.size());
    // and importing again will not duplicate, as the file content matches
    empty.remCards(empty.getDb().queryLongList("select id from cards"));
    imp = new Anki2Importer(empty, testCol.getPath());
    imp.run();
    expected = Collections.singletonList("foo.mp3");
    actual = Arrays.asList(new File(empty.getMedia().dir()).list());
    actual.retainAll(expected);
    assertEquals(expected.size(), actual.size());
    n = empty.getNote(empty.getDb().queryLongScalar("select id from notes"));
    assertTrue(n.getFields()[0].contains("foo.mp3"));
    // if the local file content is different, and import should trigger a rename
    empty.remCards(empty.getDb().queryLongList("select id from cards"));
    os = new FileOutputStream(new File(empty.getMedia().dir(), "foo.mp3"), false);
    os.write("bar".getBytes());
    os.close();
    imp = new Anki2Importer(empty, testCol.getPath());
    imp.run();
    expected = Arrays.asList("foo.mp3", String.format("foo_%s.mp3", mid));
    actual = Arrays.asList(new File(empty.getMedia().dir()).list());
    actual.retainAll(expected);
    assertEquals(expected.size(), actual.size());
    n = empty.getNote(empty.getDb().queryLongScalar("select id from notes"));
    assertTrue(n.getFields()[0].contains("_"));
    // if the localized media file already exists, we rewrite the note and media
    empty.remCards(empty.getDb().queryLongList("select id from cards"));
    os = new FileOutputStream(new File(empty.getMedia().dir(), "foo.mp3"));
    os.write("bar".getBytes());
    os.close();
    imp = new Anki2Importer(empty, testCol.getPath());
    imp.run();
    expected = Arrays.asList("foo.mp3", String.format("foo_%s.mp3", mid));
    actual = Arrays.asList(new File(empty.getMedia().dir()).list());
    actual.retainAll(expected);
    assertEquals(expected.size(), actual.size());
    n = empty.getNote(empty.getDb().queryLongScalar("select id from notes"));
    assertTrue(n.getFields()[0].contains("_"));
    empty.close();
}
Also used : Anki2Importer(com.ichi2.libanki.importer.Anki2Importer) Note(com.ichi2.libanki.Note) FileOutputStream(java.io.FileOutputStream) Collection(com.ichi2.libanki.Collection) File(java.io.File) Anki2Importer(com.ichi2.libanki.importer.Anki2Importer) Importer(com.ichi2.libanki.importer.Importer) AnkiPackageImporter(com.ichi2.libanki.importer.AnkiPackageImporter) NoteImporter(com.ichi2.libanki.importer.NoteImporter) TextImporter(com.ichi2.libanki.importer.TextImporter) Test(org.junit.Test)

Example 2 with Sound

use of com.ichi2.libanki.Sound in project AnkiChinaAndroid by ankichinateam.

the class VideoPlayer method onCreate.

/**
 * Called when the activity is first created.
 */
@Override
protected void onCreate(Bundle savedInstanceState) {
    Timber.i("onCreate");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.video_player);
    mPath = getIntent().getStringExtra("path");
    Timber.i("Video Player intent had path: %s", mPath);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    mVideoView = findViewById(R.id.video_surface);
    mVideoView.getHolder().addCallback(this);
    mSoundPlayer = new Sound();
}
Also used : Sound(com.ichi2.libanki.Sound)

Example 3 with Sound

use of com.ichi2.libanki.Sound in project AnkiChinaAndroid by ankichinateam.

the class NoteService method updateMultimediaNoteFromFields.

public static void updateMultimediaNoteFromFields(Collection col, String[] fields, long modelId, MultimediaEditableNote mmNote) {
    for (int i = 0; i < fields.length; i++) {
        String value = fields[i];
        IField field = null;
        if (value.startsWith("<img")) {
            field = new ImageField();
        } else if (value.startsWith("[sound:") && value.contains("rec")) {
            field = new AudioRecordingField();
        } else if (value.startsWith("[sound:")) {
            field = new AudioClipField();
        } else {
            field = new TextField();
        }
        field.setFormattedString(col, value);
        mmNote.setField(i, field);
    }
    mmNote.setModelId(modelId);
// TODO: set current id of the note as well
}
Also used : ImageField(com.ichi2.anki.multimediacard.fields.ImageField) AudioClipField(com.ichi2.anki.multimediacard.fields.AudioClipField) TextField(com.ichi2.anki.multimediacard.fields.TextField) IField(com.ichi2.anki.multimediacard.fields.IField) AudioRecordingField(com.ichi2.anki.multimediacard.fields.AudioRecordingField)

Example 4 with Sound

use of com.ichi2.libanki.Sound in project AnkiChinaAndroid by ankichinateam.

the class Media method filesInStr.

/**
 * Extract media filenames from an HTML string.
 *
 * @param string        The string to scan for media filenames ([sound:...] or <img...>).
 * @param includeRemote If true will also include external http/https/ftp urls.
 * @return A list containing all the sound and image filenames found in the input string.
 */
public static List<String> filesInStr(Collection mCol, Long mid, String string, boolean includeRemote) {
    List<String> l = new ArrayList<>();
    JSONObject model = mCol.getModels().get(mid);
    List<String> strings = new ArrayList<>();
    if (model.getInt("type") == Consts.MODEL_CLOZE && string.contains("{{c")) {
        // if the field has clozes in it, we'll need to expand the
        // possibilities so we can render latex
        strings = _expandClozes(string);
    } else {
        strings.add(string);
    }
    for (String s : strings) {
        // handle latex
        s = LaTeX.mungeQA(s, mCol, model);
        // extract filenames
        Matcher m;
        for (Pattern p : mRegexps) {
            // NOTE: python uses the named group 'fname'. Java doesn't have named groups, so we have to determine
            // the index based on which pattern we are using
            int fnameIdx = p.equals(fSoundRegexps) ? 2 : p.equals(fImgRegExpU) ? 2 : 3;
            m = p.matcher(s);
            while (m.find()) {
                String fname = m.group(fnameIdx);
                boolean isLocal = !fRemotePattern.matcher(fname.toLowerCase(Locale.US)).find();
                if (isLocal || includeRemote) {
                    l.add(fname);
                }
            }
        }
    }
    return l;
}
Also used : Pattern(java.util.regex.Pattern) JSONObject(com.ichi2.utils.JSONObject) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList)

Example 5 with Sound

use of com.ichi2.libanki.Sound in project Anki-Android by ankidroid.

the class ImportTest method testAnki2Mediadupes.

@Test
public void testAnki2Mediadupes() throws IOException, JSONException, ImportExportException {
    // add a note that references a sound
    Note n = mTestCol.newNote();
    n.setField(0, "[sound:foo.mp3]");
    long mid = n.model().getLong("id");
    mTestCol.addNote(n);
    // add that sound to the media directory
    FileOutputStream os = new FileOutputStream(new File(mTestCol.getMedia().dir(), "foo.mp3"), false);
    os.write("foo".getBytes());
    os.close();
    mTestCol.close();
    // it should be imported correctly into an empty deck
    Collection empty = getEmptyCol();
    Importer imp = new Anki2Importer(empty, mTestCol.getPath());
    imp.run();
    List<String> expected = Collections.singletonList("foo.mp3");
    List<String> actual = Arrays.asList(new File(empty.getMedia().dir()).list());
    actual.retainAll(expected);
    assertEquals(expected.size(), actual.size());
    // and importing again will not duplicate, as the file content matches
    empty.remCards(empty.getDb().queryLongList("select id from cards"));
    imp = new Anki2Importer(empty, mTestCol.getPath());
    imp.run();
    expected = Collections.singletonList("foo.mp3");
    actual = Arrays.asList(new File(empty.getMedia().dir()).list());
    actual.retainAll(expected);
    assertEquals(expected.size(), actual.size());
    n = empty.getNote(empty.getDb().queryLongScalar("select id from notes"));
    assertTrue(n.getFields()[0].contains("foo.mp3"));
    // if the local file content is different, and import should trigger a rename
    empty.remCards(empty.getDb().queryLongList("select id from cards"));
    os = new FileOutputStream(new File(empty.getMedia().dir(), "foo.mp3"), false);
    os.write("bar".getBytes());
    os.close();
    imp = new Anki2Importer(empty, mTestCol.getPath());
    imp.run();
    expected = Arrays.asList("foo.mp3", String.format("foo_%s.mp3", mid));
    actual = Arrays.asList(new File(empty.getMedia().dir()).list());
    actual.retainAll(expected);
    assertEquals(expected.size(), actual.size());
    n = empty.getNote(empty.getDb().queryLongScalar("select id from notes"));
    assertTrue(n.getFields()[0].contains("_"));
    // if the localized media file already exists, we rewrite the note and media
    empty.remCards(empty.getDb().queryLongList("select id from cards"));
    os = new FileOutputStream(new File(empty.getMedia().dir(), "foo.mp3"));
    os.write("bar".getBytes());
    os.close();
    imp = new Anki2Importer(empty, mTestCol.getPath());
    imp.run();
    expected = Arrays.asList("foo.mp3", String.format("foo_%s.mp3", mid));
    actual = Arrays.asList(new File(empty.getMedia().dir()).list());
    actual.retainAll(expected);
    assertEquals(expected.size(), actual.size());
    n = empty.getNote(empty.getDb().queryLongScalar("select id from notes"));
    assertTrue(n.getFields()[0].contains("_"));
    empty.close();
}
Also used : Anki2Importer(com.ichi2.libanki.importer.Anki2Importer) Note(com.ichi2.libanki.Note) FileOutputStream(java.io.FileOutputStream) Collection(com.ichi2.libanki.Collection) File(java.io.File) Anki2Importer(com.ichi2.libanki.importer.Anki2Importer) Importer(com.ichi2.libanki.importer.Importer) AnkiPackageImporter(com.ichi2.libanki.importer.AnkiPackageImporter) NoteImporter(com.ichi2.libanki.importer.NoteImporter) TextImporter(com.ichi2.libanki.importer.TextImporter) Test(org.junit.Test) InstrumentedTest(com.ichi2.anki.tests.InstrumentedTest)

Aggregations

Test (org.junit.Test)5 Uri (android.net.Uri)3 Note (com.ichi2.libanki.Note)3 MediaPlayer (android.media.MediaPlayer)2 AbstractFlashcardViewer (com.ichi2.anki.AbstractFlashcardViewer)2 RobolectricTest (com.ichi2.anki.RobolectricTest)2 IField (com.ichi2.anki.multimediacard.fields.IField)2 ImageField (com.ichi2.anki.multimediacard.fields.ImageField)2 TextField (com.ichi2.anki.multimediacard.fields.TextField)2 Collection (com.ichi2.libanki.Collection)2 Sound (com.ichi2.libanki.Sound)2 Anki2Importer (com.ichi2.libanki.importer.Anki2Importer)2 AnkiPackageImporter (com.ichi2.libanki.importer.AnkiPackageImporter)2 Importer (com.ichi2.libanki.importer.Importer)2 NoteImporter (com.ichi2.libanki.importer.NoteImporter)2 TextImporter (com.ichi2.libanki.importer.TextImporter)2 File (java.io.File)2 FileOutputStream (java.io.FileOutputStream)2 ArrayList (java.util.ArrayList)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2