use of org.liberty.android.fantastischmemo.dao.CardDao in project AnyMemo by helloworld1.
the class QuizletDownloadHelper method downloadCardset.
/**
* Download cardsets list from Quizlet and save to a db file
*
* @param setId
* cardset ID
* @param authToken
* oauth token
* @return The path of saved db file
* @throws IOException
* IOException If http response code is not 2xx
* @throws JSONException
* If the response is invalid JSON
*/
public String downloadCardset(String setId, String authToken) throws IOException, JSONException {
URL url;
// needs authtoken
if (authToken != null) {
url = new URL(AMEnv.QUIZLET_API_ENDPOINT + "/sets/" + setId);
} else {
String urlString = String.format(AMEnv.QUIZLET_API_ENDPOINT + "/sets/" + "%1$s?client_id=%2$s", URLEncoder.encode(setId, "UTF-8"), URLEncoder.encode(AMEnv.QUIZLET_CLIENT_ID, "UTF-8"));
url = new URL(urlString);
}
String response = makeApiCall(url, authToken);
JSONObject rootObject = new JSONObject(response);
JSONArray flashcardsArray = rootObject.getJSONArray("terms");
int termCount = rootObject.getInt("term_count");
boolean hasImage = rootObject.getBoolean("has_images");
List<Card> cardList = new ArrayList<Card>(termCount);
// handle image
String dbname = downloaderUtils.validateDBName(rootObject.getString("title")) + ".db";
String imagePath = AMEnv.DEFAULT_IMAGE_PATH + dbname + "/";
if (hasImage) {
FileUtils.forceMkdir(new File(imagePath));
}
for (int i = 0; i < flashcardsArray.length(); i++) {
JSONObject jsonItem = flashcardsArray.getJSONObject(i);
String question = jsonItem.getString("term");
String answer = jsonItem.getString("definition");
// Download images, ignore image downloading error.
try {
if (jsonItem.has("image") && !jsonItem.isNull("image") && hasImage) {
JSONObject imageItem = jsonItem.getJSONObject("image");
String imageUrl = imageItem.getString("url");
String downloadFilename = Uri.parse(imageUrl).getLastPathSegment();
downloaderUtils.downloadFile(imageUrl, imagePath + downloadFilename);
answer += "<br /><img src=\"" + downloadFilename + "\"/>";
}
} catch (Exception e) {
Log.e(TAG, "Error downloading image.", e);
}
Card card = new Card();
card.setQuestion(question);
card.setAnswer(answer);
card.setCategory(new Category());
card.setLearningData(new LearningData());
cardList.add(card);
}
/* Make a valid dbname from the title */
String dbpath = AMEnv.DEFAULT_ROOT_PATH;
String fullpath = dbpath + dbname;
amFileUtil.deleteFileWithBackup(fullpath);
AnyMemoDBOpenHelper helper = AnyMemoDBOpenHelperManager.getHelper(fullpath);
try {
CardDao cardDao = helper.getCardDao();
cardDao.createCards(cardList);
long count = helper.getCardDao().getTotalCount(null);
if (count <= 0L) {
throw new RuntimeException("Downloaded empty db.");
}
} finally {
AnyMemoDBOpenHelperManager.releaseHelper(helper);
}
return fullpath;
}
use of org.liberty.android.fantastischmemo.dao.CardDao in project AnyMemo by helloworld1.
the class CardProvider method query.
/**
* The query returns null if the db is not valid.
*/
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
List<String> uriSegments = uri.getPathSegments();
String dbPath = AMEnv.DEFAULT_ROOT_PATH + uriSegments.get(0);
if (!new File(dbPath).exists()) {
return null;
}
AnyMemoDBOpenHelper helper = AnyMemoDBOpenHelperManager.getHelper(getContext(), dbPath);
try {
CardDao cardDao = helper.getCardDao();
Cursor resultCursor = null;
switch(sUriMatcher.match(uri)) {
case COUNT_URI:
{
long count = cardDao.getTotalCount(null);
resultCursor = buildCursorFromCount(count);
break;
}
case RANDOM_URI:
{
int count = Integer.valueOf(uriSegments.get(2));
List<Card> cards = cardDao.getRandomCards(null, count);
resultCursor = buildCursorFromCards(cards);
break;
}
case ORD_URI:
{
int ord = Integer.valueOf(uriSegments.get(2));
Card card = cardDao.getByOrdinal(ord);
resultCursor = buildCursorFromCard(card);
break;
}
case ID_URI:
{
int id = Integer.valueOf(uriSegments.get(2));
Card card = cardDao.getById(id);
resultCursor = buildCursorFromCard(card);
break;
}
case START_URI:
{
int start = Integer.valueOf(uriSegments.get(2));
int count = Integer.valueOf(uriSegments.get(4));
List<Card> cards = cardDao.getCardsByOrdinalAndSize(start, count);
resultCursor = buildCursorFromCards(cards);
break;
}
case ALL_URI:
{
List<Card> cards = cardDao.getAllCards(null);
resultCursor = buildCursorFromCards(cards);
break;
}
default:
throw new IllegalArgumentException("No matching handler for uri: " + uri);
}
if (resultCursor == null) {
Log.e(TAG, "No case matched for uri: " + uri);
}
return resultCursor;
} finally {
AnyMemoDBOpenHelperManager.releaseHelper(helper);
}
}
use of org.liberty.android.fantastischmemo.dao.CardDao in project AnyMemo by helloworld1.
the class LearnQueueManager method refill.
private synchronized void refill() {
final AnyMemoDBOpenHelper dbOpenHelper = AnyMemoDBOpenHelperManager.getHelper(context, dbPath);
final CardDao cardDao = dbOpenHelper.getCardDao();
dumpLearnQueue();
List<Card> exclusionList = new ArrayList<Card>(learnQueue.size() + dirtyCache.size());
exclusionList.addAll(learnQueue);
exclusionList.addAll(dirtyCache);
try {
if (newCache.size() == 0) {
List<Card> cs = cardDao.getNewCards(filterCategory, exclusionList, cacheSize);
if (cs.size() > 0) {
newCache.addAll(cs);
}
}
if (reviewCache.size() == 0) {
List<Card> cs = cardDao.getCardsForReview(filterCategory, exclusionList, cacheSize, reviewOrdering);
if (cs.size() > 0) {
reviewCache.addAll(cs);
}
}
while (learnQueue.size() < learnQueueSize && !reviewCache.isEmpty()) {
learnQueue.add(reviewCache.get(0));
reviewCache.remove(0);
}
while (learnQueue.size() < learnQueueSize && !newCache.isEmpty()) {
learnQueue.add(newCache.get(0));
newCache.remove(0);
}
} finally {
AnyMemoDBOpenHelperManager.releaseHelper(dbOpenHelper);
}
flushDirtyCache();
dumpLearnQueue();
}
use of org.liberty.android.fantastischmemo.dao.CardDao in project AnyMemo by helloworld1.
the class ShareScreen method onClick.
@Override
public void onClick(View v) {
try {
String dbpath = dbnameView.getText().toString();
Log.v(TAG, dbpath);
if (v == saveButton || v == savePrevButton) {
AnyMemoDBOpenHelper helper = AnyMemoDBOpenHelperManager.getHelper(this, dbpath);
CardDao cardDao = helper.getCardDao();
try {
Card card = new Card();
card.setQuestion(questionView.getText().toString());
card.setAnswer(answerView.getText().toString());
card.setNote(noteView.getText().toString());
card.setCategory(new Category());
card.setLearningData(new LearningData());
cardDao.createCard(card);
if (v == savePrevButton) {
Intent myIntent = new Intent(this, PreviewEditActivity.class);
/* This should be the newly created id */
myIntent.putExtra("id", card.getId());
myIntent.putExtra(PreviewEditActivity.EXTRA_DBPATH, dbpath);
startActivity(myIntent);
}
finish();
} finally {
AnyMemoDBOpenHelperManager.releaseHelper(helper);
}
} else if (v == cancelButton) {
finish();
} else if (v == dbnameView) {
Intent myIntent = new Intent(this, FileBrowserActivity.class);
myIntent.putExtra(FileBrowserActivity.EXTRA_FILE_EXTENSIONS, ".db");
startActivityForResult(myIntent, ACTIVITY_FB);
}
} catch (Exception e) {
AMGUIUtility.displayError(this, getString(R.string.error_text), "", e);
}
}
use of org.liberty.android.fantastischmemo.dao.CardDao in project AnyMemo by helloworld1.
the class WidgetRemoteViewsFactory method getViewAt.
public RemoteViews getViewAt(int position) {
RemoteViews rv = new RemoteViews(mContext.getPackageName(), R.layout.widget_item);
if (position < allPath.length) {
AnyMemoDBOpenHelper helper = AnyMemoDBOpenHelperManager.getHelper(mContext, allPath[position]);
CardDao dao = helper.getCardDao();
String dbName = FilenameUtils.getName(allPath[position]);
long totalCount = dao.getTotalCount(null);
long revCount = dao.getScheduledCardCount(null);
long newCount = dao.getNewCardCount(null);
String detail = mContext.getString(R.string.stat_total) + totalCount + " " + mContext.getString(R.string.stat_new) + newCount + " " + mContext.getString(R.string.stat_scheduled) + revCount;
rv.setTextViewText(R.id.widget_db_name, dbName);
rv.setTextViewText(R.id.widget_db_detail, detail);
Intent fillInIntent = new Intent();
fillInIntent.putExtra(StudyActivity.EXTRA_DBPATH, allPath[position]);
rv.setOnClickFillInIntent(R.id.widget_item, fillInIntent);
}
return rv;
}
Aggregations