Search in sources :

Example 1 with EXPORT_APKG

use of com.ichi2.async.CollectionTask.TASK_TYPE.EXPORT_APKG in project AnkiChinaAndroid by ankichinateam.

the class AnkiActivity method exportApkg.

@Override
public void exportApkg(String filename, Long did, boolean includeSched, boolean includeMedia, boolean exportCard, boolean exportApkg) {
    File exportDir = new File(getExternalCacheDir(), "export");
    exportDir.mkdirs();
    File exportPath;
    String timeStampSuffix = "-" + TimeUtils.getTimestamp(getCol().getTime());
    if (filename != null) {
        // filename has been explicitly specified
        exportPath = new File(exportDir, filename);
    } else if (did != null) {
        // filename not explicitly specified, but a deck has been specified so use deck name
        exportPath = new File(exportDir, getCol().getDecks().get(did).getString("name").replaceAll("\\W+", "_") + timeStampSuffix + ".apkg");
    } else if (!includeSched) {
        // full export without scheduling is assumed to be shared with someone else -- use "All Decks.apkg"
        exportPath = new File(exportDir, "All Decks" + timeStampSuffix + ".apkg");
    } else {
        // full collection export -- use "collection.colpkg"
        File colPath = new File(getCol().getPath());
        String newFileName = colPath.getName().replace(".anki2", timeStampSuffix + ".colpkg");
        exportPath = new File(exportDir, newFileName);
    }
    // add input arguments to new generic structure
    Object[] inputArgs = new Object[7];
    inputArgs[0] = getCol();
    inputArgs[1] = exportPath.getPath();
    inputArgs[2] = did;
    inputArgs[3] = includeSched;
    inputArgs[4] = includeMedia;
    inputArgs[5] = exportApkg;
    inputArgs[6] = exportCard;
    try {
        CollectionTask.launchCollectionTask(EXPORT_APKG, exportListener(), new TaskData(inputArgs));
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : JSONObject(com.ichi2.utils.JSONObject) File(java.io.File) JSONException(com.ichi2.utils.JSONException) IOException(java.io.IOException) ActivityNotFoundException(android.content.ActivityNotFoundException) DeckRenameException(com.ichi2.anki.exception.DeckRenameException) TaskData(com.ichi2.async.TaskData)

Example 2 with EXPORT_APKG

use of com.ichi2.async.CollectionTask.TASK_TYPE.EXPORT_APKG in project AnkiChinaAndroid by ankichinateam.

the class ExportDialog method onCreateDialog.

@Override
public MaterialDialog onCreateDialog(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Resources res = getResources();
    final Long did = getArguments().getLong("did", -1L);
    Integer[] checked;
    final String[] items;
    if (did != -1L) {
        mIncludeSched = false;
        checked = new Integer[] { EXPORT_APKG };
        items = new String[] { res.getString(R.string.export_include_schedule), res.getString(R.string.export_include_media), "Apkg格式", "Card格式" };
    } else {
        mIncludeSched = true;
        checked = new Integer[] { INCLUDE_SCHED };
        items = new String[] { res.getString(R.string.export_include_schedule), res.getString(R.string.export_include_media) };
    }
    MaterialDialog.Builder builder = new MaterialDialog.Builder(getActivity()).title(R.string.export).content(getArguments().getString("dialogMessage")).positiveText(android.R.string.ok).negativeText(android.R.string.cancel).cancelable(true).items(items).alwaysCallMultiChoiceCallback().itemsCallbackMultiChoice(checked, (materialDialog, integers, charSequences) -> {
        mIncludeMedia = false;
        mIncludeSched = false;
        mExportApkg = false;
        mExportCard = false;
        for (Integer integer : integers) {
            switch(integer) {
                case INCLUDE_SCHED:
                    mIncludeSched = true;
                    break;
                case INCLUDE_MEDIA:
                    mIncludeMedia = true;
                    break;
                case EXPORT_CARD:
                    mExportCard = true;
                    // mExportApkg = false;
                    break;
                case EXPORT_APKG:
                    mExportApkg = true;
                    // mExportCard = false;
                    break;
            }
        }
        return true;
    }).onPositive((dialog, which) -> {
        if (mExportApkg && mExportCard) {
            Toast.makeText(getContext(), "不能同时导出Apkg和Card格式", Toast.LENGTH_SHORT).show();
            return;
        }
        if (did != -1L && !mExportApkg && !mExportCard) {
            Toast.makeText(getContext(), "请至少选择一种导出格式", Toast.LENGTH_SHORT).show();
            return;
        }
        listener.exportApkg(null, did != -1L ? did : null, mIncludeSched, mIncludeMedia, mExportCard, mExportApkg);
        dismissAllDialogFragments();
    }).onNegative((dialog, which) -> dismissAllDialogFragments());
    return builder.show();
}
Also used : Bundle(android.os.Bundle) NonNull(androidx.annotation.NonNull) AnalyticsDialogFragment(com.ichi2.anki.analytics.AnalyticsDialogFragment) Toast(android.widget.Toast) R(com.ichi2.anki.R) View(android.view.View) DialogAction(com.afollestad.materialdialogs.DialogAction) MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) Resources(android.content.res.Resources) MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) Resources(android.content.res.Resources)

Aggregations

ActivityNotFoundException (android.content.ActivityNotFoundException)1 Resources (android.content.res.Resources)1 Bundle (android.os.Bundle)1 View (android.view.View)1 Toast (android.widget.Toast)1 NonNull (androidx.annotation.NonNull)1 DialogAction (com.afollestad.materialdialogs.DialogAction)1 MaterialDialog (com.afollestad.materialdialogs.MaterialDialog)1 R (com.ichi2.anki.R)1 AnalyticsDialogFragment (com.ichi2.anki.analytics.AnalyticsDialogFragment)1 DeckRenameException (com.ichi2.anki.exception.DeckRenameException)1 TaskData (com.ichi2.async.TaskData)1 JSONException (com.ichi2.utils.JSONException)1 JSONObject (com.ichi2.utils.JSONObject)1 File (java.io.File)1 IOException (java.io.IOException)1