Search in sources :

Example 6 with Pair

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

the class Media method _changes.

private Pair<List<String>, List<String>> _changes() {
    Map<String, Pair<String, Long>> cache = new HashMap<String, Pair<String, Long>>();
    Map<String, Boolean> used = new HashMap<String, Boolean>();
    Cursor cur = null;
    try {
        cur = mMediaDb.getDatabase().query("media", new String[] { "fname", "csum", "mod" }, null, null, null, null, null);
        while (cur.moveToNext()) {
            cache.put(cur.getString(0), new Pair<String, Long>(cur.getString(1), cur.getLong(1)));
            used.put(cur.getString(0), false);
        }
    } catch (SQLException e) {
        throw new RuntimeException(e);
    } finally {
        if (cur != null) {
            cur.close();
        }
    }
    List<String> added = new ArrayList<String>();
    List<String> removed = new ArrayList<String>();
    File mediaDir = new File(getDir());
    for (File f : mediaDir.listFiles()) {
        // ignore folders and thumbs.db
        if (f.isDirectory()) {
            continue;
        }
        String fname = f.getName();
        if (fname.compareTo("thumbs.db") == 0) {
            continue;
        }
        // and files with invalid chars
        boolean bad = false;
        for (String c : new String[] { "\0", "/", "\\", ":" }) {
            if (fname.contains(c)) {
                bad = true;
                break;
            }
        }
        if (bad) {
            continue;
        }
        // empty files are invalid; clean them up and continue
        if (f.length() == 0) {
            f.delete();
            continue;
        }
        // newly added?
        if (!cache.containsKey(fname)) {
            added.add(fname);
        } else {
            // modified since last time?
            if ((f.lastModified() / 1000) != cache.get(fname).second) {
                // and has different checksum?
                if (_checksum(f.getAbsolutePath()).compareTo(cache.get(fname).first) != 0) {
                    added.add(fname);
                }
            }
            // mark as used
            used.put(fname, true);
        }
    }
    // look for any entries in the cache that no longer exist on disk
    for (String fname : used.keySet()) {
        if (!used.get(fname)) {
            removed.add(fname);
        }
    }
    return new Pair<List<String>, List<String>>(added, removed);
}
Also used : HashMap(java.util.HashMap) SQLException(android.database.SQLException) ArrayList(java.util.ArrayList) Cursor(android.database.Cursor) ZipFile(java.util.zip.ZipFile) File(java.io.File) Pair(com.ichi2.anki.Pair)

Example 7 with Pair

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

the class GoogleTranslaterFilter method filter.

public Pair<String, String> filter(Pair<String, String> messages, SharedPreferences preferences) {
    Pair<String, String> result = new Pair<String, String>(messages.first, messages.second);
    Pattern pattern = Pattern.compile(SEARCH_PATTERN);
    Matcher matcher = pattern.matcher(getSearchText(messages));
    if (isCanBeExecuted(messages, preferences) && matcher.find()) {
        String translate = matcher.group(1);
        if (matcher.find()) {
            result = new Pair<String, String>(matcher.group(1), translate);
        }
    }
    return result;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) Pair(com.ichi2.anki.Pair)

Example 8 with Pair

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

the class Sched method progressToday.

/** returns today's progress
     *
     * @param counts (if empty, cached version will be used if any)
     * @param card
     * @return [progressCurrentDeck, progressAllDecks, leftCards, eta]
     */
public float[] progressToday(TreeSet<Object[]> counts, Card card, boolean eta) {
    try {
        int doneCurrent = 0;
        int[] leftCurrent = new int[] { 0, 0, 0 };
        String[] cs = new String[] { "new", "lrn", "rev" };
        long currentDid = 0;
        // current selected deck
        if (counts == null) {
            JSONObject deck = mCol.getDecks().current();
            currentDid = deck.getLong("id");
            for (String s : cs) {
                doneCurrent += deck.getJSONArray(s + "Today").getInt(1);
            }
            if (card != null) {
                int idx = countIdx(card);
                leftCurrent[idx] += idx == 1 ? card.getLeft() / 1000 : 1;
            } else {
                reset();
            }
            leftCurrent[0] += mNewCount;
            leftCurrent[1] += mLrnCount;
            leftCurrent[2] += mRevCount;
        }
        // refresh deck progresses with fresh counts if necessary
        if (counts != null || mCachedDeckCounts == null) {
            if (mCachedDeckCounts == null) {
                mCachedDeckCounts = new HashMap<Long, Pair<String[], long[]>>();
            }
            mCachedDeckCounts.clear();
            if (counts == null) {
                // reload counts
                counts = (TreeSet<Object[]>) deckCounts()[0];
            }
            for (Object[] d : counts) {
                int done = 0;
                JSONObject deck = mCol.getDecks().get((Long) d[1]);
                for (String s : cs) {
                    done += deck.getJSONArray(s + "Today").getInt(1);
                }
                mCachedDeckCounts.put((Long) d[1], new Pair<String[], long[]>((String[]) d[0], new long[] { done, (Integer) d[2], (Integer) d[3], (Integer) d[4] }));
            }
        }
        int doneAll = 0;
        int[] leftAll = new int[] { 0, 0, 0 };
        for (Map.Entry<Long, Pair<String[], long[]>> d : mCachedDeckCounts.entrySet()) {
            // || mCol.getDecks().isDyn(d.getKey());
            boolean exclude = d.getKey() == currentDid;
            if (d.getValue().first.length == 1) {
                if (exclude) {
                    // don't count cached version of current deck
                    continue;
                }
                long[] c = d.getValue().second;
                doneAll += c[0];
                leftAll[0] += c[1];
                leftAll[1] += c[2];
                leftAll[2] += c[3];
            } else if (exclude) {
                // exclude cached values for current deck in order to avoid double count
                long[] c = d.getValue().second;
                doneAll -= c[0];
                leftAll[0] -= c[1];
                leftAll[1] -= c[2];
                leftAll[2] -= c[3];
            }
        }
        doneAll += doneCurrent;
        leftAll[0] += leftCurrent[0];
        leftAll[1] += leftCurrent[1];
        leftAll[2] += leftCurrent[2];
        int totalAll = doneAll + leftAll[0] + leftAll[1] + leftAll[2];
        int totalCurrent = doneCurrent + leftCurrent[0] + leftCurrent[1] + leftCurrent[2];
        float progressCurrent = -1;
        if (totalCurrent != 0) {
            progressCurrent = (float) doneCurrent / (float) totalCurrent;
        }
        float progressTotal = -1;
        if (totalAll != 0) {
            progressTotal = (float) doneAll / (float) totalAll;
        }
        return new float[] { progressCurrent, progressTotal, totalAll - doneAll, eta ? eta(leftAll, false) : -1 };
    } catch (JSONException e) {
        throw new RuntimeException(e);
    }
}
Also used : JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) JSONObject(org.json.JSONObject) HashMap(java.util.HashMap) Map(java.util.Map) Pair(com.ichi2.anki.Pair)

Example 9 with Pair

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

the class Finder method findDupes.

public static List<Pair<String, List<Long>>> findDupes(Collection col, String fieldName, String search) {
    // limit search to notes with applicable field name
    if (search != null && search.length() > 0) {
        search = "(" + search + ") ";
    }
    search += "'" + fieldName + ":*'";
    // go through notes
    String sql = "select id, mid, flds from notes where id in " + Utils.ids2str(col.findNotes(search).toArray(new Long[] {}));
    Cursor cur = null;
    Map<Long, Integer> fields = new HashMap<Long, Integer>();
    Map<String, List<Long>> vals = new HashMap<String, List<Long>>();
    List<Pair<String, List<Long>>> dupes = new ArrayList<Pair<String, List<Long>>>();
    try {
        cur = col.getDb().getDatabase().rawQuery(sql, null);
        while (cur.moveToNext()) {
            long nid = cur.getLong(0);
            long mid = cur.getLong(1);
            String[] flds = Utils.splitFields(cur.getString(2));
            // inlined ordForMid(mid)
            if (!fields.containsKey(mid)) {
                JSONObject model = col.getModels().get(mid);
                fields.put(mid, col.getModels().fieldMap(model).get(fieldName).first);
            }
            String val = flds[fields.get(mid)];
            // empty does not count as duplicate
            if (val.equals("")) {
                continue;
            }
            if (!vals.containsKey(val)) {
                vals.put(val, new ArrayList<Long>());
            }
            vals.get(val).add(nid);
            if (vals.get(val).size() == 2) {
                dupes.add(new Pair<String, List<Long>>(val, vals.get(val)));
            }
        }
    } finally {
        if (cur != null) {
            cur.close();
        }
    }
    return dupes;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Cursor(android.database.Cursor) JSONObject(org.json.JSONObject) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) Pair(com.ichi2.anki.Pair)

Aggregations

Pair (com.ichi2.anki.Pair)8 HashMap (java.util.HashMap)5 JSONException (org.json.JSONException)5 JSONObject (org.json.JSONObject)5 ArrayList (java.util.ArrayList)4 Matcher (java.util.regex.Matcher)3 Cursor (android.database.Cursor)2 File (java.io.File)2 ZipFile (java.util.zip.ZipFile)2 Resources (android.content.res.Resources)1 SQLException (android.database.SQLException)1 Bundle (android.os.Bundle)1 FilterFacade (com.ichi2.filters.FilterFacade)1 MustacheException (com.samskivert.mustache.MustacheException)1 BufferedInputStream (java.io.BufferedInputStream)1 BufferedOutputStream (java.io.BufferedOutputStream)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1