Search in sources :

Example 16 with JSONObject

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

the class BootService method scheduleDeckReminder.

private void scheduleDeckReminder(Context context) {
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    for (DeckConfig deckConfiguration : CollectionHelper.getInstance().getCol(context).getDecks().allConf()) {
        Collection col = CollectionHelper.getInstance().getCol(context);
        if (deckConfiguration.has("reminder")) {
            final JSONObject reminder = deckConfiguration.getJSONObject("reminder");
            if (reminder.getBoolean("enabled")) {
                final PendingIntent reminderIntent = PendingIntent.getBroadcast(context, (int) deckConfiguration.getLong("id"), new Intent(context, ReminderService.class).putExtra(ReminderService.EXTRA_DECK_OPTION_ID, deckConfiguration.getLong("id")), 0);
                final Calendar calendar = reminderToCalendar(col.getTime(), reminder);
                alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, reminderIntent);
            }
        }
    }
}
Also used : JSONObject(com.ichi2.utils.JSONObject) Calendar(java.util.Calendar) DeckOptions.reminderToCalendar(com.ichi2.anki.DeckOptions.reminderToCalendar) AlarmManager(android.app.AlarmManager) Collection(com.ichi2.libanki.Collection) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) PendingIntent(android.app.PendingIntent) DeckConfig(com.ichi2.libanki.DeckConfig)

Example 17 with JSONObject

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

the class ZipFile method _exportMedia.

private JSONObject _exportMedia(ZipFile z, File[] files, ValidateFiles validateFiles) throws IOException {
    int c = 0;
    JSONObject media = new JSONObject();
    for (File file : files) {
        // todo: deflate SVG files, as in dae/anki@a5b0852360b132c0d04094f5ca8f1933f64d7c7e
        if (validateFiles == ValidateFiles.VALIDATE && !file.exists()) {
            // Anki 2.1.30 does the same
            Timber.d("Skipping missing file %s", file);
            continue;
        }
        z.write(file.getPath(), Integer.toString(c));
        try {
            media.put(Integer.toString(c), file.getName());
            c++;
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    return media;
}
Also used : JSONObject(com.ichi2.utils.JSONObject) JSONException(com.ichi2.utils.JSONException) File(java.io.File)

Example 18 with JSONObject

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

the class ZipFile method exportFiltered.

private JSONObject exportFiltered(ZipFile z, String path, Context context) throws IOException, JSONException, ImportExportException {
    // export into the anki2 file
    String colfile = path.replace(".apkg", ".anki2");
    super.exportInto(colfile, context);
    z.write(colfile, CollectionHelper.COLLECTION_FILENAME);
    // and media
    prepareMedia();
    JSONObject media = _exportMedia(z, mMediaFiles, mCol.getMedia().dir());
    // tidy up intermediate files
    SQLiteDatabase.deleteDatabase(new File(colfile));
    SQLiteDatabase.deleteDatabase(new File(path.replace(".apkg", ".media.ad.db2")));
    String tempPath = path.replace(".apkg", ".media");
    File file = new File(tempPath);
    if (file.exists()) {
        String deleteCmd = "rm -r " + tempPath;
        Runtime runtime = Runtime.getRuntime();
        try {
            runtime.exec(deleteCmd);
        } catch (IOException e) {
        }
    }
    return media;
}
Also used : JSONObject(com.ichi2.utils.JSONObject) IOException(java.io.IOException) File(java.io.File)

Example 19 with JSONObject

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

the class Card method _getQA.

public HashMap<String, String> _getQA(boolean reload, boolean browser) {
    if (mQA == null || reload) {
        Note f = note(reload);
        Model m = model();
        JSONObject t = template();
        long did = mODid != 0L ? mODid : mDid;
        if (browser) {
            String bqfmt = t.getString("bqfmt");
            String bafmt = t.getString("bafmt");
            mQA = mCol._renderQA(mId, m, did, mOrd, f.stringTags(), f.getFields(), mFlags, browser, bqfmt, bafmt);
        } else {
            mQA = mCol._renderQA(mId, m, did, mOrd, f.stringTags(), f.getFields(), mFlags);
        }
    }
    return mQA;
}
Also used : JSONObject(com.ichi2.utils.JSONObject)

Example 20 with JSONObject

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

the class Connection method doInBackgroundLogin.

private Payload doInBackgroundLogin(Payload data) {
    String username = (String) data.data[0];
    String password = (String) data.data[1];
    HostNum hostNum = (HostNum) data.data[2];
    HttpSyncer server = new RemoteServer(this, null, hostNum);
    Response ret;
    try {
        ret = server.hostKey(username, password);
    } catch (UnknownHttpResponseException e) {
        data.success = false;
        data.result = new Object[] { "error", e.getResponseCode(), e.getMessage() };
        return data;
    } catch (Exception e2) {
        // Ask user to report all bugs which aren't timeout errors
        if (!timeoutOccurred(e2)) {
            AnkiDroidApp.sendExceptionReport(e2, "doInBackgroundLogin");
        }
        data.success = false;
        data.result = new Object[] { "connectionError", e2 };
        return data;
    }
    String hostkey = null;
    boolean valid = false;
    if (ret != null) {
        data.returnType = ret.code();
        Timber.d("doInBackgroundLogin - response from server: %d, (%s)", data.returnType, ret.message());
        if (data.returnType == 200) {
            try {
                JSONObject response = new JSONObject(ret.body().string());
                hostkey = response.getString("key");
                valid = (hostkey != null) && (hostkey.length() > 0);
            } catch (JSONException e) {
                valid = false;
            } catch (IllegalStateException | IOException | NullPointerException e) {
                throw new RuntimeException(e);
            }
        }
    } else {
        Timber.e("doInBackgroundLogin - empty response from server");
    }
    if (valid) {
        data.success = true;
        data.data = new String[] { username, hostkey };
    } else {
        data.success = false;
    }
    return data;
}
Also used : JSONException(com.ichi2.utils.JSONException) HostNum(com.ichi2.libanki.sync.HostNum) IOException(java.io.IOException) UnknownHttpResponseException(com.ichi2.anki.exception.UnknownHttpResponseException) JSONException(com.ichi2.utils.JSONException) NoEnoughServerSpaceException(com.ichi2.anki.exception.NoEnoughServerSpaceException) MediaSyncException(com.ichi2.anki.exception.MediaSyncException) IOException(java.io.IOException) UnknownHttpResponseException(com.ichi2.anki.exception.UnknownHttpResponseException) Response(okhttp3.Response) JSONObject(com.ichi2.utils.JSONObject) JSONObject(com.ichi2.utils.JSONObject) HttpSyncer(com.ichi2.libanki.sync.HttpSyncer) RemoteServer(com.ichi2.libanki.sync.RemoteServer)

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