use of com.ichi2.libanki.Collection in project Anki-Android by Ramblurr.
the class DeckPicker method sync.
/**
* The mother of all syncing attempts. This might be called from sync() as first attempt to sync a collection OR
* from the mSyncConflictResolutionListener if the first attempt determines that a full-sync is required. In the
* second case, we have passed the mediaUsn that was obtained during the first attempt.
*
* @param syncConflictResolution Either "upload" or "download", depending on the user's choice.
* @param syncMediaUsn The media Usn, as determined during the prior sync() attempt that determined that full
* syncing was required.
*/
private void sync(String syncConflictResolution, int syncMediaUsn) {
SharedPreferences preferences = AnkiDroidApp.getSharedPrefs(getBaseContext());
String hkey = preferences.getString("hkey", "");
if (hkey.length() == 0) {
showDialog(DIALOG_USER_NOT_LOGGED_IN_SYNC);
} else {
Connection.sync(mSyncListener, new Connection.Payload(new Object[] { hkey, preferences.getBoolean("syncFetchesMedia", true), syncConflictResolution, syncMediaUsn }));
}
}
use of com.ichi2.libanki.Collection in project Anki-Android by Ramblurr.
the class Info method onCreate.
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.i(AnkiDroidApp.TAG, "Info - onCreate()");
Themes.applyTheme(this);
super.onCreate(savedInstanceState);
Resources res = getResources();
mType = getIntent().getIntExtra(TYPE_EXTRA, TYPE_ABOUT);
setTitle(getTitleString());
setContentView(R.layout.info);
mWebView = (WebView) findViewById(R.id.info);
mWebView.setBackgroundColor(res.getColor(Themes.getBackgroundColor()));
Themes.setWallpaper((View) mWebView.getParent().getParent().getParent());
TextView termsAndConditionsView = (TextView) findViewById(R.id.info_terms_and_conditions);
termsAndConditionsView.setMovementMethod(LinkMovementMethod.getInstance());
Button continueButton = (Button) findViewById(R.id.info_continue);
continueButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
if (mType == TYPE_ABOUT) {
Info.this.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=com.ichi2.anki")));
return;
}
setResult(RESULT_OK);
switch(mType) {
case TYPE_WELCOME:
AnkiDroidApp.getSharedPrefs(Info.this.getBaseContext()).edit().putLong("lastTimeOpened", System.currentTimeMillis()).commit();
break;
case TYPE_NEW_VERSION:
AnkiDroidApp.getSharedPrefs(Info.this.getBaseContext()).edit().putString("lastVersion", AnkiDroidApp.getPkgVersionName()).commit();
break;
case TYPE_UPGRADE_DECKS:
break;
}
finishWithAnimation();
}
});
StringBuilder sb = new StringBuilder();
switch(mType) {
case TYPE_ABOUT:
String[] content = res.getStringArray(R.array.about_content);
sb.append("<html><body text=\"#000000\" link=\"#E37068\" alink=\"#E37068\" vlink=\"#E37068\">");
sb.append(String.format(content[0], res.getString(R.string.app_name), res.getString(R.string.link_anki))).append("<br/><br/>");
sb.append(String.format(content[1], res.getString(R.string.link_issue_tracker), res.getString(R.string.link_wiki), res.getString(R.string.link_forum))).append("<br/><br/>");
sb.append(String.format(content[2], res.getString(R.string.link_wikipedia_open_source), res.getString(R.string.link_contribution), res.getString(R.string.link_contribution_contributors))).append(" ");
sb.append(String.format(content[3], res.getString(R.string.link_translation), res.getString(R.string.link_donation))).append("<br/><br/>");
sb.append(String.format(content[4], res.getString(R.string.licence_wiki), res.getString(R.string.link_source))).append("<br/><br/>");
sb.append("</body></html>");
mWebView.loadDataWithBaseURL("", sb.toString(), "text/html", "utf-8", null);
((Button) findViewById(R.id.info_continue)).setText(res.getString(R.string.info_rate));
break;
case TYPE_WELCOME:
// title
sb.append("<html><body text=\"#000000\" link=\"#E37068\" alink=\"#E37068\" vlink=\"#E37068\">");
sb.append("<big><b>");
sb.append(res.getString(R.string.studyoptions_welcome_title));
sb.append("</big></b><br><br>");
// message
sb.append(res.getString(R.string.welcome_message).replace("\n", "<br>"));
sb.append("</body></html>");
mWebView.loadDataWithBaseURL("", sb.toString(), "text/html", "utf-8", null);
// add tutorial button
Button tutBut = (Button) findViewById(R.id.info_tutorial);
tutBut.setVisibility(View.VISIBLE);
tutBut.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
setResult(RESULT_OK);
Editor edit = AnkiDroidApp.getSharedPrefs(Info.this.getBaseContext()).edit();
edit.putLong("lastTimeOpened", System.currentTimeMillis());
edit.putBoolean("createTutorial", true);
edit.commit();
finishWithAnimation();
}
});
break;
case TYPE_NEW_VERSION:
sb.append(res.getString(R.string.new_version_message));
sb.append("<ul>");
String[] features = res.getStringArray(R.array.new_version_features);
for (int i = 0; i < features.length; i++) {
sb.append("<li>");
sb.append(features[i]);
sb.append("</li>");
}
sb.append("</ul>");
sb.append("</body></html>");
mWebView.loadDataWithBaseURL("", sb.toString(), "text/html", "utf-8", null);
// reactivating ssl check for every new version
AnkiDroidApp.getSharedPrefs(Info.this.getBaseContext()).edit().putBoolean("sslAcceptAll", false).commit();
break;
case TYPE_SHARED_DECKS:
mLoadingLayer = (RelativeLayout) findViewById(R.id.info_loading_layer);
mLoadingLayer.setVisibility(View.VISIBLE);
try {
mShareDecksTemplate = Utils.convertStreamToString(getAssets().open("shared_decks_template.html"));
} catch (IOException e) {
e.printStackTrace();
}
mWebView.setWebViewClient(new MobileAnkiWebview());
mWebView.loadUrl(res.getString(R.string.shared_decks_url));
mWebView.getSettings().setJavaScriptEnabled(true);
termsAndConditionsView.setVisibility(View.VISIBLE);
continueButton.setText(res.getString(R.string.download_button_return));
break;
case TYPE_UPGRADE_DECKS:
setTitle(R.string.deck_upgrade_title);
sb.append("<html><body>");
// add upgrade button
Button but = (Button) findViewById(R.id.info_tutorial);
but.setVisibility(View.VISIBLE);
// add sync button
Button syncButton = (Button) findViewById(R.id.info_sync);
syncButton.setVisibility(View.VISIBLE);
mUpgradeStage = getIntent().getIntExtra(TYPE_UPGRADE_STAGE, -1);
boolean reupgrading = false;
switch(mUpgradeStage) {
case UPGRADE_SCREEN_BASIC1:
sb.append(getString(R.string.deck_upgrade_major_warning));
but.setText(R.string.later);
but.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Themes.showThemedToast(Info.this, getString(R.string.deck_upgrade_start_again_to_upgrade_toast), false);
setResult(RESULT_CANCELED);
finish();
}
});
syncButton.setText(R.string.more_options);
syncButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent result = new Intent();
result.putExtra(TYPE_UPGRADE_STAGE, UPGRADE_SCREEN_MORE_OPTIONS);
setResult(RESULT_OK, result);
finishWithAnimation();
}
});
continueButton.setText(R.string.now);
continueButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent result = new Intent();
result.putExtra(TYPE_UPGRADE_STAGE, UPGRADE_SCREEN_BASIC2);
setResult(RESULT_OK, result);
finishWithAnimation();
}
});
break;
case UPGRADE_SCREEN_BASIC2:
sb.append(getString(R.string.deck_upgrade_recommended_method));
but.setText(R.string.back);
but.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent result = new Intent();
result.putExtra(TYPE_UPGRADE_STAGE, UPGRADE_SCREEN_BASIC1);
result.putExtra(TYPE_ANIMATION_RIGHT, true);
setResult(RESULT_OK, result);
finishWithAnimation(false);
}
});
syncButton.setEnabled(false);
syncButton.setText("N/A");
syncButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent result = new Intent();
result.putExtra(TYPE_UPGRADE_STAGE, UPGRADE_SCREEN_WEB_UPGRADE);
setResult(RESULT_OK, result);
finishWithAnimation();
}
});
continueButton.setText(getString(R.string.pc));
continueButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent result = new Intent();
result.putExtra(TYPE_UPGRADE_STAGE, UPGRADE_SCREEN_PC_UPGRADE);
setResult(RESULT_OK, result);
finishWithAnimation();
}
});
break;
case UPGRADE_SCREEN_MORE_OPTIONS:
// If re-upgrading a collection exists already, so don't offer to make a new one
if (new File(AnkiDroidApp.getCollectionPath()).exists()) {
setTitle(getString(R.string.exit_wizard));
reupgrading = true;
sb.append(getString(R.string.deck_upgrade_more_options_exit));
} else {
sb.append(getString(R.string.deck_upgrade_more_options_new_collection));
}
sb.append(getString(R.string.deck_upgrade_more_options_downgrade));
but.setText(R.string.upgrade_decks_button);
but.setText(R.string.back);
but.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent result = new Intent();
result.putExtra(TYPE_UPGRADE_STAGE, UPGRADE_SCREEN_BASIC1);
result.putExtra(TYPE_ANIMATION_RIGHT, true);
setResult(RESULT_OK, result);
finishWithAnimation(false);
}
});
if (reupgrading) {
syncButton.setText(getString(R.string.upgrade_deck_dont_upgrade));
syncButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent result = new Intent();
result.putExtra(TYPE_UPGRADE_STAGE, UPGRADE_CONTINUE);
setResult(RESULT_OK, result);
finishWithAnimation();
}
});
} else {
syncButton.setText(R.string.deck_upgrade_create_new_collection);
syncButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
StyledDialog.Builder builder = new StyledDialog.Builder(Info.this);
builder.setTitle(R.string.deck_upgrade_create_new_collection_title);
builder.setIcon(R.drawable.ic_dialog_alert);
builder.setMessage(R.string.deck_upgrade_not_import_warning);
builder.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent result = new Intent();
result.putExtra(TYPE_UPGRADE_STAGE, UPGRADE_CONTINUE);
setResult(RESULT_OK, result);
finishWithAnimation();
}
});
builder.setNegativeButton(R.string.no, null);
builder.show();
}
});
}
continueButton.setVisibility(View.GONE);
break;
case UPGRADE_SCREEN_WEB_UPGRADE:
sb.append(getString(R.string.deck_upgrade_via_web));
but.setText(R.string.back);
but.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent result = new Intent();
result.putExtra(TYPE_UPGRADE_STAGE, UPGRADE_SCREEN_BASIC2);
result.putExtra(TYPE_ANIMATION_RIGHT, true);
setResult(RESULT_OK, result);
finishWithAnimation(false);
}
});
syncButton.setVisibility(View.GONE);
continueButton.setText(R.string.yes);
continueButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
lockScreenOrientation();
Connection.upgradeDecks(mWebUpgradeListener, new Connection.Payload(new Object[] { AnkiDroidApp.getCurrentAnkiDroidDirectory() }));
}
});
break;
case UPGRADE_SCREEN_PC_UPGRADE:
sb.append(getString(R.string.deck_upgrade_via_anki_desktop));
but.setText(R.string.back);
but.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent result = new Intent();
result.putExtra(TYPE_UPGRADE_STAGE, UPGRADE_SCREEN_BASIC2);
result.putExtra(TYPE_ANIMATION_RIGHT, true);
setResult(RESULT_OK, result);
finishWithAnimation(false);
}
});
syncButton.setText(R.string.usb);
syncButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent result = new Intent();
result.putExtra(TYPE_UPGRADE_STAGE, UPGRADE_SCREEN_MANUAL_UPGRADE);
setResult(RESULT_OK, result);
finishWithAnimation();
}
});
continueButton.setEnabled(false);
continueButton.setText("N/A");
continueButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent result = new Intent();
result.putExtra(TYPE_UPGRADE_STAGE, UPGRADE_SCREEN_AUTO_UPGRADE);
setResult(RESULT_OK, result);
finishWithAnimation();
}
});
break;
case UPGRADE_SCREEN_MANUAL_UPGRADE:
sb.append(getString(R.string.deck_upgrade_manual));
but.setText(R.string.back);
but.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent result = new Intent();
result.putExtra(TYPE_UPGRADE_STAGE, UPGRADE_SCREEN_BASIC2);
result.putExtra(TYPE_ANIMATION_RIGHT, true);
setResult(RESULT_OK, result);
finishWithAnimation(false);
}
});
syncButton.setText(R.string._import);
syncButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
File apkgFile = new File(AnkiDroidApp.getCurrentAnkiDroidDirectory(), DeckPicker.IMPORT_REPLACE_COLLECTION_NAME);
List<File> importables = Utils.getImportableDecks();
if (importables == null || !importables.contains(apkgFile)) {
Themes.showThemedToast(Info.this, getResources().getString(R.string.upgrade_import_no_file_found, DeckPicker.IMPORT_REPLACE_COLLECTION_NAME), false);
} else {
lockScreenOrientation();
DeckTask.launchDeckTask(DeckTask.TASK_TYPE_IMPORT_REPLACE, mUpgradeImportListener, new DeckTask.TaskData(AnkiDroidApp.getCol(), apkgFile.getAbsolutePath()));
}
}
});
continueButton.setVisibility(View.GONE);
break;
case UPGRADE_SCREEN_AUTO_UPGRADE:
sb.append(getString(R.string.deck_upgrade_auto_upgrade));
but.setText(R.string.back);
but.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent result = new Intent();
result.putExtra(TYPE_UPGRADE_STAGE, UPGRADE_SCREEN_PC_UPGRADE);
result.putExtra(TYPE_ANIMATION_RIGHT, true);
setResult(RESULT_OK, result);
finishWithAnimation(false);
}
});
syncButton.setText(getString(R.string.upgrade_deck_sync_from_ankiweb));
syncButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
StyledDialog.Builder builder = new StyledDialog.Builder(Info.this);
builder.setTitle(R.string.upgrade_deck_sync_from_ankiweb);
builder.setIcon(R.drawable.ic_dialog_alert);
builder.setMessage(getString(R.string.upgrade_deck_have_you_synced));
builder.setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
lockScreenOrientation();
downloadCollection();
}
});
builder.setNegativeButton(R.string.back, null);
builder.show();
}
});
continueButton.setVisibility(View.GONE);
break;
}
// File[] fileList = (new File(AnkiDroidApp.getCurrentAnkiDroidDirectory())).listFiles(new OldAnkiDeckFilter());
// StringBuilder fsb = new StringBuilder();
// fsb.append("<ul>");
// for (File f : fileList) {
// fsb.append("<li>").append(f.getName().replace(".anki", "")).append("</li>");
// }
// fsb.append("</ul>");
// sb.append(res.getString(R.string.upgrade_decks_message, fsb.toString()));
// sb.append("<ul><li>");
// sb.append(res.getString(R.string.upgrade_decks_message_pos1,
// AnkiDroidApp.getCurrentAnkiDroidDirectory()));
// sb.append("</li><li>");
// sb.append(res.getString(R.string.upgrade_decks_message_pos2, res.getString(R.string.link_anki)));
// sb.append("</li><li>");
// sb.append(res.getString(R.string.upgrade_decks_message_pos3));
// sb.append("</li></ul>");
// sb.append(res.getString(R.string.upgrade_decks_message_finish));
sb.append("</body></html>");
mWebView.loadDataWithBaseURL("", sb.toString(), "text/html", "utf-8", null);
StyledDialog.Builder builder2 = new StyledDialog.Builder(this);
builder2.setTitle(res.getString(R.string.connection_error_title));
builder2.setIcon(R.drawable.ic_dialog_alert);
builder2.setMessage(res.getString(R.string.connection_needed));
builder2.setPositiveButton(res.getString(R.string.ok), null);
mNoConnectionAlert = builder2.create();
break;
default:
finish();
break;
}
}
use of com.ichi2.libanki.Collection in project Anki-Android by Ramblurr.
the class DeckPicker method showStartupScreensAndDialogs.
private void showStartupScreensAndDialogs(SharedPreferences preferences, int skip) {
if (skip < 1 && preferences.getLong("lastTimeOpened", 0) == 0) {
Intent infoIntent = new Intent(this, Info.class);
infoIntent.putExtra(Info.TYPE_EXTRA, Info.TYPE_WELCOME);
startActivityForResult(infoIntent, SHOW_INFO_WELCOME);
if (skip != 0 && AnkiDroidApp.SDK_VERSION > 4) {
ActivityTransitionAnimation.slide(this, ActivityTransitionAnimation.LEFT);
}
} else if (skip < 2 && !preferences.getString("lastVersion", "").equals(AnkiDroidApp.getPkgVersionName())) {
preferences.edit().putBoolean("showBroadcastMessageToday", true).commit();
Intent infoIntent = new Intent(this, Info.class);
infoIntent.putExtra(Info.TYPE_EXTRA, Info.TYPE_NEW_VERSION);
startActivityForResult(infoIntent, SHOW_INFO_NEW_VERSION);
if (skip != 0 && AnkiDroidApp.SDK_VERSION > 4) {
ActivityTransitionAnimation.slide(this, ActivityTransitionAnimation.LEFT);
}
} else if (skip < 3 && upgradeNeeded()) {
// Note that the "upgrade needed" refers to upgrading Anki 1.x decks, not to newer
// versions of AnkiDroid.
AnkiDroidApp.getSharedPrefs(AnkiDroidApp.getInstance().getBaseContext()).edit().putInt("lastUpgradeVersion", AnkiDroidApp.getPkgVersionCode()).commit();
showUpgradeScreen(skip != 0, Info.UPGRADE_SCREEN_BASIC1);
} else if (skip < 4 && hasErrorFiles()) {
Intent i = new Intent(this, Feedback.class);
startActivityForResult(i, REPORT_ERROR);
if (skip != 0 && AnkiDroidApp.SDK_VERSION > 4) {
ActivityTransitionAnimation.slide(this, ActivityTransitionAnimation.LEFT);
}
} else if (!AnkiDroidApp.isSdCardMounted()) {
showDialog(DIALOG_SD_CARD_NOT_MOUNTED);
} else if (!BackupManager.enoughDiscSpace(mPrefDeckPath)) {
// && !preferences.getBoolean("dontShowLowMemory",
// false)) {
showDialog(DIALOG_NO_SPACE_LEFT);
} else if (preferences.getBoolean("noSpaceLeft", false)) {
showDialog(DIALOG_BACKUP_NO_SPACE_LEFT);
preferences.edit().putBoolean("noSpaceLeft", false).commit();
} else if (mImportPath != null && AnkiDroidApp.colIsOpen()) {
showDialog(DIALOG_IMPORT);
} else {
// AnkiDroid is being updated and a collection already exists. We check if we are upgrading
// to a version that contains additions to the database integrity check routine that we would
// like to run on all collections. A missing version number is assumed to be a fresh
// installation of AnkiDroid and we don't run the check.
int current = AnkiDroidApp.getPkgVersionCode();
// a non-final variable, for intermediate calculations
int previousTemp;
if (!preferences.contains("lastUpgradeVersion")) {
// Fresh install
previousTemp = current;
} else {
try {
previousTemp = preferences.getInt("lastUpgradeVersion", current);
} catch (ClassCastException e) {
// Previous versions stored this as a string.
String s = preferences.getString("lastUpgradeVersion", "");
// check.
if (s.equals("2.0.2")) {
previousTemp = 40;
} else {
previousTemp = 0;
}
}
}
final int previous = previousTemp;
preferences.edit().putInt("lastUpgradeVersion", current).commit();
if (previous < AnkiDroidApp.CHECK_DB_AT_VERSION || previous < AnkiDroidApp.CHECK_PREFERENCES_AT_VERSION) {
DeckTask.launchDeckTask(DeckTask.TASK_TYPE_OPEN_COLLECTION, new Listener() {
@Override
public void onPostExecute(DeckTask task, TaskData result) {
mOpenCollectionHandler.onPostExecute(result);
if (previous < AnkiDroidApp.CHECK_DB_AT_VERSION) {
integrityCheck();
}
if (previous < AnkiDroidApp.CHECK_PREFERENCES_AT_VERSION) {
upgradePreferences(previous);
}
}
@Override
public void onPreExecute(DeckTask task) {
}
@Override
public void onProgressUpdate(DeckTask task, TaskData... values) {
}
}, new DeckTask.TaskData(AnkiDroidApp.getCollectionPath()));
} else {
loadCollection();
}
}
}
use of com.ichi2.libanki.Collection in project Anki-Android by Ramblurr.
the class BackupManager method repairDeck.
public static boolean repairDeck(String deckPath) {
File deckFile = new File(deckPath);
Collection col = AnkiDroidApp.getCol();
if (col != null) {
col.close();
}
AnkiDatabaseManager.closeDatabase(deckPath);
// repair file
String execString = "sqlite3 " + deckPath + " .dump | sqlite3 " + deckPath + ".tmp";
Log.i(AnkiDroidApp.TAG, "repairDeck - Execute: " + execString);
try {
String[] cmd = { "/system/bin/sh", "-c", execString };
Process process = Runtime.getRuntime().exec(cmd);
process.waitFor();
if (!new File(deckPath + ".tmp").exists()) {
return false;
}
if (!moveDatabaseToBrokenFolder(deckPath, false)) {
return false;
}
Log.i(AnkiDroidApp.TAG, "repairDeck - moved corrupt file to broken folder");
File repairedFile = new File(deckPath + ".tmp");
if (!repairedFile.renameTo(deckFile)) {
return false;
}
return true;
} catch (IOException e) {
Log.e("AnkiDroidApp.TAG", "repairCollection - error: " + e);
} catch (InterruptedException e) {
Log.e("AnkiDroidApp.TAG", "repairCollection - error: " + e);
}
return false;
}
use of com.ichi2.libanki.Collection in project Anki-Android by Ramblurr.
the class StudyOptionsFragment method onCreateDialog.
protected StyledDialog onCreateDialog(int id) {
StyledDialog dialog = null;
Resources res = getResources();
StyledDialog.Builder builder1 = new StyledDialog.Builder(this.getActivity());
switch(id) {
case DIALOG_STATISTIC_TYPE:
dialog = ChartBuilder.getStatisticsDialog(getActivity(), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
DeckTask.launchDeckTask(DeckTask.TASK_TYPE_LOAD_STATISTICS, mLoadStatisticsHandler, new DeckTask.TaskData(AnkiDroidApp.getCol(), which, false));
}
}, mFragmented);
break;
case DIALOG_CUSTOM_STUDY:
builder1.setTitle(res.getString(R.string.custom_study));
builder1.setIcon(android.R.drawable.ic_menu_sort_by_size);
builder1.setItems(res.getStringArray(R.array.custom_study_options_labels), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
mCustomDialogChoice = which;
if (which == CUSTOM_STUDY_TAGS - 1) {
/*
* There is a special Dialog for CUSTOM STUDY, where instead of only collecting
* a number, it is necessary to collect a list of tags. This case handles the
* creation of that Dialog.
*/
showDialog(DIALOG_CUSTOM_STUDY_TAGS);
return;
}
showDialog(DIALOG_CUSTOM_STUDY_DETAILS);
}
});
builder1.setCancelable(true);
dialog = builder1.create();
break;
case DIALOG_CUSTOM_STUDY_TAGS:
/*
* This handles the case where we want to create a Custom Study Deck using tags.
* This dialog needs to be different from the normal Custom Study dialogs, because
* more information is required:
* --List of Tags to select.
* --Which cards to select
* --(New cards, Due cards, or all cards, as in the desktop version)
*/
if (!AnkiDroidApp.colIsOpen()) {
//TODO how should this error be handled?
}
Context context = getActivity().getBaseContext();
/*
* The following RadioButtons and RadioGroup are to select the category of cards
* to select for the Custom Study Deck (New, Due or All cards).
*/
RadioGroup rg = formatRGCardType(context, res);
mSelectWhichCards = rg;
builder1.setView(rg, false, true);
//Here we add the list of tags for the whole collection.
Collection col;
col = AnkiDroidApp.getCol();
allTags = col.getTags().all();
builder1.setTitle(R.string.studyoptions_limit_select_tags);
builder1.setMultiChoiceItems(allTags, new boolean[allTags.length], new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String tag = allTags[which];
if (mSelectedTags.contains(tag)) {
Log.i(AnkiDroidApp.TAG, "unchecked tag: " + tag);
mSelectedTags.remove(tag);
} else {
Log.i(AnkiDroidApp.TAG, "checked tag: " + tag);
mSelectedTags.add(tag);
}
}
});
/*
* Here's the method that gathers the final selection of tags, type of cards
* and generates the search screen for the custom study deck.
*/
builder1.setPositiveButton(res.getString(R.string.select), new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
mCustomStudyEditText.setText("");
String tags = mSelectedTags.toString();
mCustomStudyEditText.setHint(getResources().getString(R.string.card_browser_tags_shown, tags.substring(1, tags.length() - 1)));
StringBuilder sb = new StringBuilder();
switch(mSelectedOption) {
case 1:
sb.append("is:new ");
break;
case 2:
sb.append("is:due ");
break;
default:
// Logging here might be appropriate : )
break;
}
int i = 0;
for (String tag : mSelectedTags) {
if (i != 0) {
sb.append("or ");
} else {
//Only if we really have selected tags
sb.append("(");
}
sb.append("tag:").append(tag).append(" ");
i++;
}
if (i > 0) {
//Only if we added anything to the tag list
sb.append(")");
}
mSearchTerms = sb.toString();
createFilteredDeck(new JSONArray(), new Object[] { mSearchTerms, 9999, Sched.DYN_RANDOM }, false);
}
});
builder1.setNegativeButton(res.getString(R.string.cancel), new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
mSelectedTags.clear();
}
});
builder1.setOnCancelListener(new OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
mSelectedTags.clear();
}
});
dialog = builder1.create();
break;
case DIALOG_CUSTOM_STUDY_DETAILS:
/*
* This is the normal case for creating a custom study deck, where the dialog
* requires only a numeric input.
*/
builder1.setContentView(mCustomStudyDetailsView);
builder1.setCancelable(true);
builder1.setNegativeButton(R.string.cancel, null);
builder1.setPositiveButton(R.string.ok, null);
dialog = builder1.create();
break;
default:
dialog = null;
break;
}
dialog.setOwnerActivity(getActivity());
return dialog;
}
Aggregations