Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge branch 'master' of https://github.com/nicolas-raoul/Anki-Android

Conflicts:
	src/com/ichi2/libanki/Media.java
  • Loading branch information...
commit bbf794f5ab4b0b02ab03cad43a8a8b4266658655 2 parents 3d249ae + 86d9878
marek-jasovsky authored
BIN  assets/collection.media.db
Binary file not shown
8 pull.sh
... ... @@ -0,0 +1,8 @@
  1 +#!/bin/sh
  2 +# Build all needed files for ant compilation
  3 +# Norbert Nagold (2011)
  4 +# You must:
  5 +# - Have installed the Android SDK,
  6 +# - Have added android to the PATH system variable.
  7 +
  8 +git pull https://github.com/nicolas-raoul/Anki-Android.git master
95 src/com/ichi2/libanki/Media.java
@@ -21,18 +21,26 @@
21 21 import android.util.Log;
22 22
23 23 import java.io.File;
  24 +import java.io.FileInputStream;
  25 +import java.io.FileOutputStream;
  26 +import java.io.IOException;
  27 +import java.io.InputStream;
  28 +import java.nio.channels.FileChannel;
24 29 import java.util.ArrayList;
25 30 import java.util.HashMap;
26   -import java.util.HashSet;
  31 +import java.io.InputStream;mport java.util.HashSet;
27 32 import java.util.Map;
28 33 import java.util.Map.Entry;
29 34 import java.util.Set;
30 35 import java.util.regex.Pattern;
31 36 import java.util.regex.Matcher;
32 37
  38 +import com.ichi2.anki.AnkiDatabaseManager;
  39 +import com.ichi2.anki.AnkiDb;
33 40 import com.ichi2.anki.AnkiDroidApp;
34 41
35 42 /**
  43 +/**
36 44 * Class with static functions related with media handling (images and sounds).
37 45 */
38 46 public class Media {
@@ -44,6 +52,7 @@
44 52 };
45 53
46 54 private String mDir;
  55 + private AnkiDb mMediaDb;
47 56
48 57 public Media(Collection col) {
49 58 mDir = col.getPath().replaceFirst("\\.anki2$", ".media");
@@ -59,13 +68,95 @@ public Media(Collection col) {
59 68
60 69 private void connect() {
61 70 String path = mDir + ".db";
62   - // TODO: media db methods
  71 + File mediaDbFile = new File(path);
  72 + if (!mediaDbFile.exists()) {
  73 + // Copy an empty collection file from the assets to the SD card.
  74 + InputStream stream;
  75 + try {
  76 + stream = AnkiDroidApp.getAppResources().getAssets().open("collection.media.db");
  77 + Utils.writeToFile(stream, path);
  78 + stream.close();
  79 + } catch (IOException e) {
  80 + Log.e(AnkiDroidApp.TAG, "Error initialising " + path, e);
  81 + }
  82 + }
  83 + mMediaDb = AnkiDatabaseManager.getDatabase(path);
  84 + }
  85 + private void close() {
  86 +
  87 + mMediaDb.closeDatabase();
  88 + mMediaDb = null;
63 89 }
64 90
65 91 public String getDir() {
66 92 return mDir;
67 93 }
68 94
  95 + // Adding media
  96 + ///////////////
  97 +
  98 + /**
  99 + * Copy PATH to MEDIADIR and return new filename.
  100 + * If the same name exists, compare checksums.
  101 + *
  102 + * @param opath The path where the media file exists before adding it.
  103 + * @return The filename of the resulting file.
  104 + */
  105 + private String addFile(String opath) {
  106 + String mdir = getDir();
  107 + // remove any dangerous characters
  108 + String base = new File(opath).getName().replaceAll("[][<>:/\\&]", "");
  109 + String dst = mdir + base;
  110 + // if it doesn't exist, copy it directly
  111 + File newMediaFile = new File(dst);
  112 + if (!newMediaFile.exists()) {
  113 + try {
  114 + Utils.copyFile(new File(opath), newMediaFile);
  115 + } catch (IOException e) {
  116 + Log.e(AnkiDroidApp.TAG, "Could not copy file " + opath + " to location " + dst, e);
  117 + }
  118 + return base;
  119 + }
  120 + if (filesIdentical(opath, dst)) {
  121 + return base;
  122 + }
  123 + // otherwise, find a unique name
  124 + String root, ext;
  125 + int extIndex = base.lastIndexOf('.');
  126 + if (extIndex == 0 || extIndex == -1) {
  127 + root = base;
  128 + ext = "";
  129 + } else {
  130 + root = base.substring(0, extIndex);
  131 + ext = base.substring(extIndex);
  132 + }
  133 + int num = 1;
  134 + StringBuilder sb = new StringBuilder(mdir);
  135 + sb.append("/").append(root).append(" (");
  136 + do {
  137 + StringBuilder sb2 = new StringBuilder(sb);
  138 + sb2.append(num).append(ext);
  139 + newMediaFile = new File(sb2.toString());
  140 + num += 1;
  141 + } while (newMediaFile.exists());
  142 + try {
  143 + Utils.copyFile(new File(opath), newMediaFile);
  144 + } catch (IOException e) {
  145 + Log.e(AnkiDroidApp.TAG, "Could not copy file " + opath + " to location " + newMediaFile.getAbsolutePath(), e);
  146 + }
  147 + return newMediaFile.getName();
  148 + }
  149 +
  150 + /**
  151 + * Checks if two files are identical
  152 + * @param filepath1 The path of the first file to be checked
  153 + * @param filepath2 The path of the second file to be checked
  154 + * @return True if both files have the same contents
  155 + */
  156 + private boolean filesIdentical(String filepath1, String filepath2) {
  157 + return (Utils.fileChecksum(filepath1) == Utils.fileChecksum(filepath2));
  158 + }
  159 +
69 160 // private static final Pattern regPattern = Pattern.compile("\\((\\d+)\\)$");
70 161 //
71 162 // // File Handling

0 comments on commit bbf794f

Please sign in to comment.
Something went wrong with that request. Please try again.