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);
}
}
}
}
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;
}
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;
}
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;
}
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;
}
Aggregations