Search in sources :

Example 1 with Result

use of com.ichi2.anki.multimediacard.googleimagesearch.json.Result in project Anki-Android by Ramblurr.

the class DeckPicker method onActivityResult.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);
    mDontSaveOnStop = false;
    if (resultCode == RESULT_MEDIA_EJECTED) {
        showDialog(DIALOG_SD_CARD_NOT_MOUNTED);
        return;
    } else if (resultCode == RESULT_DB_ERROR) {
        handleDbError();
        return;
    }
    if (requestCode == SHOW_STUDYOPTIONS && resultCode == RESULT_OK) {
        loadCounts();
    } else if (requestCode == ADD_NOTE && resultCode != RESULT_CANCELED) {
        loadCounts();
    } else if (requestCode == BROWSE_CARDS && (resultCode == Activity.RESULT_OK || resultCode == Activity.RESULT_CANCELED)) {
        loadCounts();
    } else if (requestCode == ADD_CRAM_DECK) {
        // TODO: check, if ok has been clicked
        loadCounts();
    } else if (requestCode == REPORT_ERROR) {
        showStartupScreensAndDialogs(AnkiDroidApp.getSharedPrefs(getBaseContext()), 4);
    } else if (requestCode == SHOW_INFO_UPGRADE_DECKS) {
        if (intent != null && intent.hasExtra(Info.TYPE_UPGRADE_STAGE)) {
            int type = intent.getIntExtra(Info.TYPE_UPGRADE_STAGE, Info.UPGRADE_SCREEN_BASIC1);
            if (type == Info.UPGRADE_CONTINUE) {
                showStartupScreensAndDialogs(AnkiDroidApp.getSharedPrefs(getBaseContext()), 3);
            } else {
                showUpgradeScreen(true, type, !intent.hasExtra(Info.TYPE_ANIMATION_RIGHT));
            }
        } else {
            if (resultCode == RESULT_OK) {
                if (mOpenCollectionDialog != null && mOpenCollectionDialog.isShowing()) {
                    mOpenCollectionDialog.dismiss();
                }
                if (AnkiDroidApp.colIsOpen()) {
                    AnkiDroidApp.closeCollection(true);
                }
                AnkiDroidApp.openCollection(AnkiDroidApp.getCollectionPath());
                loadCounts();
            } else {
                finishWithAnimation();
            }
        }
    } else if (requestCode == SHOW_INFO_WELCOME || requestCode == SHOW_INFO_NEW_VERSION) {
        if (resultCode == RESULT_OK) {
            showStartupScreensAndDialogs(AnkiDroidApp.getSharedPrefs(getBaseContext()), requestCode == SHOW_INFO_WELCOME ? 1 : 2);
        } else {
            finishWithAnimation();
        }
    } else if (requestCode == PREFERENCES_UPDATE) {
        String oldPath = mPrefDeckPath;
        SharedPreferences pref = restorePreferences();
        String newLanguage = pref.getString("language", "");
        if (AnkiDroidApp.setLanguage(newLanguage)) {
            mInvalidateMenu = true;
        }
        if (mNotMountedDialog != null && mNotMountedDialog.isShowing() && pref.getBoolean("internalMemory", false)) {
            showStartupScreensAndDialogs(pref, 0);
        } else if (!mPrefDeckPath.equals(oldPath)) {
            loadCollection();
        }
    // if (resultCode == StudyOptions.RESULT_RESTART) {
    // setResult(StudyOptions.RESULT_RESTART);
    // finishWithAnimation();
    // } else {
    // SharedPreferences preferences = PrefSettings.getSharedPrefs(getBaseContext());
    // BackupManager.initBackup();
    // if (!mPrefDeckPath.equals(preferences.getString("deckPath", AnkiDroidApp.getStorageDirectory())) ||
    // mPrefDeckOrder != Integer.parseInt(preferences.getString("deckOrder", "0"))) {
    // // populateDeckList(preferences.getString("deckPath", AnkiDroidApp.getStorageDirectory()));
    // }
    // }
    } else if (requestCode == REPORT_FEEDBACK && resultCode == RESULT_OK) {
    } else if (requestCode == LOG_IN_FOR_SYNC && resultCode == RESULT_OK) {
        sync();
    } else if (requestCode == LOG_IN_FOR_SHARED_DECK && resultCode == RESULT_OK) {
        addSharedDeck();
    } else if (requestCode == ADD_SHARED_DECKS) {
        if (intent != null) {
            mImportPath = intent.getStringExtra("importPath");
        }
        if (AnkiDroidApp.colIsOpen() && mImportPath != null) {
            DeckTask.launchDeckTask(DeckTask.TASK_TYPE_IMPORT, mImportAddListener, new TaskData(AnkiDroidApp.getCol(), mImportPath, true));
            mImportPath = null;
        }
    } else if (requestCode == REQUEST_REVIEW) {
        Log.i(AnkiDroidApp.TAG, "Result code = " + resultCode);
        switch(resultCode) {
            default:
                // do not reload counts, if activity is created anew because it has been before destroyed by android
                loadCounts();
                break;
            case Reviewer.RESULT_NO_MORE_CARDS:
                mDontSaveOnStop = true;
                Intent i = new Intent();
                i.setClass(this, StudyOptionsActivity.class);
                i.putExtra("onlyFnsMsg", true);
                startActivityForResult(i, SHOW_STUDYOPTIONS);
                if (AnkiDroidApp.SDK_VERSION > 4) {
                    ActivityTransitionAnimation.slide(this, ActivityTransitionAnimation.RIGHT);
                }
                break;
        }
    }
    // workaround for hidden dialog on return
    BroadcastMessages.showDialog();
}
Also used : SharedPreferences(android.content.SharedPreferences) Intent(android.content.Intent) TaskData(com.ichi2.async.DeckTask.TaskData)

Example 2 with Result

use of com.ichi2.anki.multimediacard.googleimagesearch.json.Result in project Anki-Android by Ramblurr.

the class DeckPicker method onCreateDialog.

@Override
protected Dialog onCreateDialog(int id) {
    StyledDialog dialog;
    Resources res = getResources();
    StyledDialog.Builder builder = new StyledDialog.Builder(this);
    switch(id) {
        case DIALOG_OK:
            builder.setPositiveButton(R.string.ok, null);
            dialog = builder.create();
            break;
        case DIALOG_NO_SDCARD:
            builder.setMessage("The SD card could not be read. Please, turn off USB storage.");
            builder.setPositiveButton(R.string.ok, null);
            dialog = builder.create();
            break;
        case DIALOG_SELECT_HELP:
            builder.setTitle(res.getString(R.string.help_title));
            builder.setItems(new String[] { res.getString(R.string.help_tutorial), res.getString(R.string.help_online), res.getString(R.string.help_faq) }, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface arg0, int arg1) {
                    if (arg1 == 0) {
                        createTutorialDeck();
                    } else {
                        if (Utils.isIntentAvailable(DeckPicker.this, "android.intent.action.VIEW")) {
                            Intent intent = new Intent("android.intent.action.VIEW", Uri.parse(getResources().getString(arg1 == 1 ? R.string.link_help : R.string.link_faq)));
                            startActivity(intent);
                        } else {
                            startActivity(new Intent(DeckPicker.this, Info.class));
                        }
                    }
                }
            });
            dialog = builder.create();
            break;
        case DIALOG_CONNECTION_ERROR:
            builder.setTitle(res.getString(R.string.connection_error_title));
            builder.setIcon(R.drawable.ic_dialog_alert);
            builder.setMessage(res.getString(R.string.connection_error_message));
            builder.setPositiveButton(res.getString(R.string.retry), new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    sync();
                }
            });
            builder.setNegativeButton(res.getString(R.string.cancel), null);
            dialog = builder.create();
            break;
        case DIALOG_SYNC_CONFLICT_RESOLUTION:
            builder.setTitle(res.getString(R.string.sync_conflict_title));
            builder.setIcon(android.R.drawable.ic_input_get);
            builder.setMessage(res.getString(R.string.sync_conflict_message));
            builder.setPositiveButton(res.getString(R.string.sync_conflict_local), mSyncConflictResolutionListener);
            builder.setNeutralButton(res.getString(R.string.sync_conflict_remote), mSyncConflictResolutionListener);
            builder.setNegativeButton(res.getString(R.string.sync_conflict_cancel), mSyncConflictResolutionListener);
            builder.setCancelable(true);
            dialog = builder.create();
            break;
        case DIALOG_LOAD_FAILED:
            builder.setMessage(res.getString(R.string.open_collection_failed_message, BackupManager.BROKEN_DECKS_SUFFIX, res.getString(R.string.repair_deck)));
            builder.setTitle(R.string.open_collection_failed_title);
            builder.setIcon(R.drawable.ic_dialog_alert);
            builder.setPositiveButton(res.getString(R.string.error_handling_options), new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    showDialog(DIALOG_ERROR_HANDLING);
                }
            });
            builder.setNegativeButton(res.getString(R.string.close), new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    finishWithAnimation();
                }
            });
            builder.setOnCancelListener(new OnCancelListener() {

                @Override
                public void onCancel(DialogInterface dialog) {
                    finishWithAnimation();
                }
            });
            dialog = builder.create();
            break;
        case DIALOG_DB_ERROR:
            builder.setMessage(R.string.answering_error_message);
            builder.setTitle(R.string.answering_error_title);
            builder.setIcon(R.drawable.ic_dialog_alert);
            builder.setPositiveButton(res.getString(R.string.error_handling_options), new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    showDialog(DIALOG_ERROR_HANDLING);
                }
            });
            builder.setNeutralButton(res.getString(R.string.answering_error_report), new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Intent i = new Intent(DeckPicker.this, Feedback.class);
                    i.putExtra("request", RESULT_DB_ERROR);
                    dialog.dismiss();
                    startActivityForResult(i, REPORT_ERROR);
                    if (AnkiDroidApp.SDK_VERSION > 4) {
                        ActivityTransitionAnimation.slide(DeckPicker.this, ActivityTransitionAnimation.RIGHT);
                    }
                }
            });
            builder.setNegativeButton(res.getString(R.string.close), new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if (!AnkiDroidApp.colIsOpen()) {
                        finishWithAnimation();
                    }
                }
            });
            builder.setCancelable(true);
            dialog = builder.create();
            break;
        case DIALOG_ERROR_HANDLING:
            builder.setTitle(res.getString(R.string.error_handling_title));
            builder.setIcon(R.drawable.ic_dialog_alert);
            builder.setSingleChoiceItems(new String[] { "1" }, 0, null);
            builder.setOnCancelListener(new OnCancelListener() {

                @Override
                public void onCancel(DialogInterface dialog) {
                    if (mLoadFailed) {
                        // dialog has been called because collection could not be opened
                        showDialog(DIALOG_LOAD_FAILED);
                    } else {
                        // dialog has been called because a db error happened
                        showDialog(DIALOG_DB_ERROR);
                    }
                }
            });
            builder.setNegativeButton(res.getString(R.string.cancel), new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if (mLoadFailed) {
                        // dialog has been called because collection could not be opened
                        showDialog(DIALOG_LOAD_FAILED);
                    } else {
                        // dialog has been called because a db error happened
                        showDialog(DIALOG_DB_ERROR);
                    }
                }
            });
            dialog = builder.create();
            break;
        case DIALOG_USER_NOT_LOGGED_IN_ADD_SHARED_DECK:
            builder.setTitle(res.getString(R.string.connection_error_title));
            builder.setIcon(R.drawable.ic_dialog_alert);
            builder.setMessage(res.getString(R.string.no_user_password_error_message));
            builder.setNegativeButton(res.getString(R.string.cancel), null);
            builder.setPositiveButton(res.getString(R.string.log_in), new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Intent myAccount = new Intent(DeckPicker.this, MyAccount.class);
                    myAccount.putExtra("notLoggedIn", true);
                    startActivityForResult(myAccount, LOG_IN_FOR_SHARED_DECK);
                    if (AnkiDroidApp.SDK_VERSION > 4) {
                        ActivityTransitionAnimation.slide(DeckPicker.this, ActivityTransitionAnimation.FADE);
                    }
                }
            });
            dialog = builder.create();
            break;
        case DIALOG_USER_NOT_LOGGED_IN_SYNC:
            builder.setTitle(res.getString(R.string.connection_error_title));
            builder.setIcon(R.drawable.ic_dialog_alert);
            builder.setMessage(res.getString(R.string.no_user_password_error_message));
            builder.setNegativeButton(res.getString(R.string.cancel), null);
            builder.setPositiveButton(res.getString(R.string.log_in), new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Intent myAccount = new Intent(DeckPicker.this, MyAccount.class);
                    myAccount.putExtra("notLoggedIn", true);
                    startActivityForResult(myAccount, LOG_IN_FOR_SYNC);
                    if (AnkiDroidApp.SDK_VERSION > 4) {
                        ActivityTransitionAnimation.slide(DeckPicker.this, ActivityTransitionAnimation.FADE);
                    }
                }
            });
            dialog = builder.create();
            break;
        case DIALOG_NO_CONNECTION:
            builder.setTitle(res.getString(R.string.connection_error_title));
            builder.setIcon(R.drawable.ic_dialog_alert);
            builder.setMessage(res.getString(R.string.connection_needed));
            builder.setPositiveButton(res.getString(R.string.ok), null);
            dialog = builder.create();
            break;
        case DIALOG_DELETE_DECK:
            if (!AnkiDroidApp.colIsOpen() || mDeckList == null) {
                return null;
            }
            // Message is set in onPrepareDialog
            builder.setTitle(res.getString(R.string.delete_deck_title));
            builder.setIcon(R.drawable.ic_dialog_alert);
            builder.setPositiveButton(res.getString(R.string.yes), new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    DeckTask.launchDeckTask(DeckTask.TASK_TYPE_DELETE_DECK, new DeckTask.TaskListener() {

                        @Override
                        public void onPreExecute() {
                            mProgressDialog = StyledProgressDialog.show(DeckPicker.this, "", getResources().getString(R.string.delete_deck), true);
                        }

                        @Override
                        public void onPostExecute(TaskData result) {
                            if (result == null) {
                                return;
                            }
                            Object[] res = result.getObjArray();
                            updateDecksList((TreeSet<Object[]>) res[0], (Integer) res[1], (Integer) res[2]);
                            if (mFragmented) {
                                selectDeck(AnkiDroidApp.getCol().getDecks().selected());
                            }
                            if (mProgressDialog.isShowing()) {
                                try {
                                    mProgressDialog.dismiss();
                                } catch (Exception e) {
                                    Log.e(AnkiDroidApp.TAG, "onPostExecute - Dialog dismiss Exception = " + e.getMessage());
                                }
                            }
                        }

                        @Override
                        public void onProgressUpdate(TaskData... values) {
                        }
                    }, new TaskData(AnkiDroidApp.getCol(), mCurrentDid));
                }
            });
            builder.setNegativeButton(res.getString(R.string.cancel), null);
            dialog = builder.create();
            break;
        case DIALOG_SELECT_STATISTICS_TYPE:
            dialog = ChartBuilder.getStatisticsDialog(this, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    boolean muh = mFragmented ? AnkiDroidApp.getSharedPrefs(AnkiDroidApp.getInstance().getBaseContext()).getBoolean("statsRange", true) : true;
                    DeckTask.launchDeckTask(DeckTask.TASK_TYPE_LOAD_STATISTICS, mLoadStatisticsHandler, new DeckTask.TaskData(AnkiDroidApp.getCol(), which, mFragmented ? AnkiDroidApp.getSharedPrefs(AnkiDroidApp.getInstance().getBaseContext()).getBoolean("statsRange", true) : true));
                }
            }, mFragmented);
            break;
        case DIALOG_CONTEXT_MENU:
            String[] entries = new String[3];
            // entries[CONTEXT_MENU_DECK_SUMMARY] =
            // "XXXsum";//res.getStringArray(R.array.statistics_type_labels)[0];
            // entries[CONTEXT_MENU_CUSTOM_DICTIONARY] =
            // res.getString(R.string.contextmenu_deckpicker_set_custom_dictionary);
            // entries[CONTEXT_MENU_RESET_LANGUAGE] =
            // res.getString(R.string.contextmenu_deckpicker_reset_language_assignments);
            entries[CONTEXT_MENU_COLLAPSE_DECK] = res.getString(R.string.contextmenu_deckpicker_collapse_deck);
            entries[CONTEXT_MENU_RENAME_DECK] = res.getString(R.string.contextmenu_deckpicker_rename_deck);
            entries[CONTEXT_MENU_DELETE_DECK] = res.getString(R.string.contextmenu_deckpicker_delete_deck);
            builder.setTitle("Context Menu");
            builder.setIcon(R.drawable.ic_menu_manage);
            builder.setItems(entries, mContextMenuListener);
            dialog = builder.create();
            break;
        case DIALOG_REPAIR_COLLECTION:
            builder.setTitle(res.getString(R.string.backup_repair_deck));
            builder.setMessage(res.getString(R.string.repair_deck_dialog, BackupManager.BROKEN_DECKS_SUFFIX));
            builder.setIcon(R.drawable.ic_dialog_alert);
            builder.setPositiveButton(res.getString(R.string.yes), new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    DeckTask.launchDeckTask(DeckTask.TASK_TYPE_REPAIR_DECK, mRepairDeckHandler, new DeckTask.TaskData(AnkiDroidApp.getCol(), AnkiDroidApp.getCollectionPath()));
                }
            });
            builder.setNegativeButton(res.getString(R.string.no), new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    showDialog(DIALOG_ERROR_HANDLING);
                }
            });
            builder.setOnCancelListener(new OnCancelListener() {

                @Override
                public void onCancel(DialogInterface arg0) {
                    showDialog(DIALOG_ERROR_HANDLING);
                }
            });
            dialog = builder.create();
            break;
        case DIALOG_SYNC_SANITY_ERROR:
            builder.setPositiveButton(getString(R.string.sync_sanity_local), mSyncSanityFailListener);
            builder.setNeutralButton(getString(R.string.sync_sanity_remote), mSyncSanityFailListener);
            builder.setNegativeButton(res.getString(R.string.sync_conflict_cancel), mSyncSanityFailListener);
            builder.setTitle(res.getString(R.string.sync_log_title));
            dialog = builder.create();
            break;
        case DIALOG_SYNC_UPGRADE_REQUIRED:
            builder.setMessage(res.getString(R.string.upgrade_required, res.getString(R.string.link_anki)));
            builder.setPositiveButton(res.getString(R.string.retry), new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    sync("download", mSyncMediaUsn);
                }
            });
            builder.setNegativeButton(res.getString(R.string.cancel), new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if (mLoadFailed) {
                        // dialog has been called because collection could not be opened
                        showDialog(DIALOG_LOAD_FAILED);
                    } else {
                        // dialog has been called because a db error happened
                        showDialog(DIALOG_DB_ERROR);
                    }
                }
            });
            builder.setOnCancelListener(new OnCancelListener() {

                @Override
                public void onCancel(DialogInterface arg0) {
                    if (mLoadFailed) {
                        // dialog has been called because collection could not be opened
                        showDialog(DIALOG_LOAD_FAILED);
                    } else {
                        // dialog has been called because a db error happened
                        showDialog(DIALOG_DB_ERROR);
                    }
                }
            });
            builder.setTitle(res.getString(R.string.sync_log_title));
            dialog = builder.create();
            break;
        case DIALOG_SYNC_LOG:
            builder.setTitle(res.getString(R.string.sync_log_title));
            builder.setPositiveButton(res.getString(R.string.ok), null);
            dialog = builder.create();
            break;
        case DIALOG_BACKUP_NO_SPACE_LEFT:
            builder.setTitle(res.getString(R.string.attention));
            builder.setMessage(res.getString(R.string.backup_deck_no_space_left));
            builder.setPositiveButton(res.getString(R.string.ok), new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    loadCollection();
                }
            });
            // builder.setNegativeButton(res.getString(R.string.dont_show_again), new
            // DialogInterface.OnClickListener() {
            // @Override
            // public void onClick(DialogInterface arg0, int arg1) {
            // PrefSettings.getSharedPrefs(getBaseContext()).edit().putBoolean("dontShowLowMemory", true).commit();
            // }
            // });
            builder.setCancelable(true);
            builder.setOnCancelListener(new OnCancelListener() {

                @Override
                public void onCancel(DialogInterface arg0) {
                    loadCollection();
                }
            });
            dialog = builder.create();
            break;
        case DIALOG_SD_CARD_NOT_MOUNTED:
            if (mNotMountedDialog == null || !mNotMountedDialog.isShowing()) {
                mNotMountedDialog = StyledOpenCollectionDialog.show(DeckPicker.this, getResources().getString(R.string.sd_card_not_mounted), new OnCancelListener() {

                    @Override
                    public void onCancel(DialogInterface arg0) {
                        finishWithAnimation();
                    }
                }, new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        startActivityForResult(new Intent(DeckPicker.this, Preferences.class), PREFERENCES_UPDATE);
                    }
                });
            }
            dialog = null;
            break;
        case DIALOG_IMPORT:
            builder.setTitle(res.getString(R.string.import_title));
            builder.setMessage(res.getString(R.string.import_message, mImportPath));
            builder.setPositiveButton(res.getString(R.string.import_message_add), new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    DeckTask.launchDeckTask(DeckTask.TASK_TYPE_IMPORT, mImportAddListener, new TaskData(AnkiDroidApp.getCol(), mImportPath, false));
                    mImportPath = null;
                }
            });
            builder.setNeutralButton(res.getString(R.string.import_message_replace), new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Resources res = getResources();
                    StyledDialog.Builder builder = new StyledDialog.Builder(DeckPicker.this);
                    builder.setTitle(res.getString(R.string.import_title));
                    builder.setMessage(res.getString(R.string.import_message_replace_confirm, mImportPath));
                    builder.setPositiveButton(res.getString(R.string.yes), new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            DeckTask.launchDeckTask(DeckTask.TASK_TYPE_IMPORT_REPLACE, mImportReplaceListener, new TaskData(AnkiDroidApp.getCol(), mImportPath));
                            mImportPath = null;
                        }
                    });
                    builder.setNegativeButton(res.getString(R.string.no), null);
                    builder.show();
                }
            });
            builder.setNegativeButton(res.getString(R.string.cancel), null);
            builder.setCancelable(true);
            dialog = builder.create();
            break;
        case DIALOG_IMPORT_SELECT:
            builder.setTitle(res.getString(R.string.import_title));
            dialog = builder.create();
            break;
        case DIALOG_IMPORT_HINT:
            builder.setTitle(res.getString(R.string.import_title));
            builder.setMessage(res.getString(R.string.import_hint, AnkiDroidApp.getCurrentAnkiDroidDirectory()));
            builder.setPositiveButton(res.getString(R.string.ok), new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    showDialog(DIALOG_IMPORT_SELECT);
                }
            });
            builder.setNegativeButton(res.getString(R.string.cancel), null);
            dialog = builder.create();
            break;
        case DIALOG_IMPORT_LOG:
            builder.setIcon(R.drawable.ic_dialog_alert);
            builder.setTitle(res.getString(R.string.import_title));
            builder.setPositiveButton(res.getString(R.string.ok), null);
            dialog = builder.create();
            break;
        case DIALOG_NO_SPACE_LEFT:
            builder.setTitle(res.getString(R.string.attention));
            builder.setMessage(res.getString(R.string.sd_space_warning, BackupManager.MIN_FREE_SPACE));
            builder.setPositiveButton(res.getString(R.string.ok), new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    finishWithAnimation();
                }
            });
            // builder.setNegativeButton(res.getString(R.string.dont_show_again), new
            // DialogInterface.OnClickListener() {
            // @Override
            // public void onClick(DialogInterface arg0, int arg1) {
            // PrefSettings.getSharedPrefs(getBaseContext()).edit().putBoolean("dontShowLowMemory", true).commit();
            // }
            // });
            builder.setCancelable(true);
            builder.setOnCancelListener(new OnCancelListener() {

                @Override
                public void onCancel(DialogInterface arg0) {
                    finishWithAnimation();
                }
            });
            dialog = builder.create();
            break;
        case DIALOG_RESTORE_BACKUP:
            File[] files = BackupManager.getBackups(new File(AnkiDroidApp.getCollectionPath()));
            mBackups = new File[files.length];
            for (int i = 0; i < files.length; i++) {
                mBackups[i] = files[files.length - 1 - i];
            }
            if (mBackups.length == 0) {
                builder.setTitle(getResources().getString(R.string.backup_restore));
                builder.setMessage(res.getString(R.string.backup_restore_no_backups));
                builder.setPositiveButton(res.getString(R.string.ok), new Dialog.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        showDialog(DIALOG_ERROR_HANDLING);
                    }
                });
                builder.setCancelable(true).setOnCancelListener(new OnCancelListener() {

                    @Override
                    public void onCancel(DialogInterface arg0) {
                        showDialog(DIALOG_ERROR_HANDLING);
                    }
                });
            } else {
                String[] dates = new String[mBackups.length];
                for (int i = 0; i < mBackups.length; i++) {
                    dates[i] = mBackups[i].getName().replaceAll(".*-(\\d{4}-\\d{2}-\\d{2})-(\\d{2})-(\\d{2}).anki2", "$1 ($2:$3 h)");
                }
                builder.setTitle(res.getString(R.string.backup_restore_select_title));
                builder.setIcon(android.R.drawable.ic_input_get);
                builder.setSingleChoiceItems(dates, dates.length, new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        DeckTask.launchDeckTask(DeckTask.TASK_TYPE_RESTORE_DECK, mRestoreDeckHandler, new DeckTask.TaskData(new Object[] { AnkiDroidApp.getCol(), AnkiDroidApp.getCollectionPath(), mBackups[which].getPath() }));
                    }
                });
                builder.setCancelable(true).setOnCancelListener(new OnCancelListener() {

                    @Override
                    public void onCancel(DialogInterface arg0) {
                        showDialog(DIALOG_ERROR_HANDLING);
                    }
                });
            }
            dialog = builder.create();
            break;
        case DIALOG_NEW_COLLECTION:
            builder.setTitle(res.getString(R.string.backup_new_collection));
            builder.setMessage(res.getString(R.string.backup_del_collection_question));
            builder.setPositiveButton(res.getString(R.string.ok), new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    AnkiDroidApp.closeCollection(false);
                    String path = AnkiDroidApp.getCollectionPath();
                    AnkiDatabaseManager.closeDatabase(path);
                    if (BackupManager.moveDatabaseToBrokenFolder(path, false)) {
                        loadCollection();
                    } else {
                        showDialog(DIALOG_ERROR_HANDLING);
                    }
                }
            });
            builder.setNegativeButton(res.getString(R.string.no), new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface arg0, int arg1) {
                    showDialog(DIALOG_ERROR_HANDLING);
                }
            });
            builder.setCancelable(true);
            builder.setOnCancelListener(new OnCancelListener() {

                @Override
                public void onCancel(DialogInterface arg0) {
                    showDialog(DIALOG_ERROR_HANDLING);
                }
            });
            dialog = builder.create();
            break;
        case DIALOG_FULL_SYNC_FROM_SERVER:
            builder.setTitle(res.getString(R.string.backup_full_sync_from_server));
            builder.setMessage(res.getString(R.string.backup_full_sync_from_server_question));
            builder.setPositiveButton(res.getString(R.string.ok), new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    sync("download", mSyncMediaUsn);
                }
            });
            builder.setNegativeButton(res.getString(R.string.no), new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface arg0, int arg1) {
                    showDialog(DIALOG_ERROR_HANDLING);
                }
            });
            builder.setCancelable(true);
            builder.setOnCancelListener(new OnCancelListener() {

                @Override
                public void onCancel(DialogInterface arg0) {
                    showDialog(DIALOG_ERROR_HANDLING);
                }
            });
            dialog = builder.create();
            break;
        default:
            dialog = null;
    }
    if (dialog != null) {
        dialog.setOwnerActivity(this);
    }
    return dialog;
}
Also used : DialogInterface(android.content.DialogInterface) ChartBuilder(com.ichi2.charts.ChartBuilder) StyledDialog(com.ichi2.themes.StyledDialog) TaskData(com.ichi2.async.DeckTask.TaskData) DeckTask(com.ichi2.async.DeckTask) TaskData(com.ichi2.async.DeckTask.TaskData) StyledOpenCollectionDialog(com.ichi2.themes.StyledOpenCollectionDialog) Dialog(android.app.Dialog) StyledProgressDialog(com.ichi2.themes.StyledProgressDialog) StyledDialog(com.ichi2.themes.StyledDialog) SharedPreferences(android.content.SharedPreferences) OnCancelListener(android.content.DialogInterface.OnCancelListener) Intent(android.content.Intent) View(android.view.View) AdapterView(android.widget.AdapterView) TextView(android.widget.TextView) ListView(android.widget.ListView) JSONException(org.json.JSONException) NotFoundException(android.content.res.Resources.NotFoundException) SQLException(android.database.SQLException) OnClickListener(android.view.View.OnClickListener) JSONObject(org.json.JSONObject) Resources(android.content.res.Resources) File(java.io.File)

Example 3 with Result

use of com.ichi2.anki.multimediacard.googleimagesearch.json.Result in project Anki-Android by Ramblurr.

the class Connection method doInBackgroundUpgradeDecks.

private Payload doInBackgroundUpgradeDecks(Payload data) {
    // Enable http request canceller
    mCancelCallback = new CancelCallback();
    String path = (String) data.data[0];
    File ankiDir = new File(path);
    if (!ankiDir.isDirectory()) {
        data.success = false;
        data.data = new Object[] { "wrong anki directory" };
        return data;
    }
    // step 1: gather all .anki files into a zip, without media.
    // we must store them as 1.anki, 2.anki and provide a map so we don't run into
    // encoding issues with the zip file.
    File[] fileList = ankiDir.listFiles(new OldAnkiDeckFilter());
    List<String> corruptFiles = new ArrayList<String>();
    JSONObject map = new JSONObject();
    byte[] buf = new byte[1024];
    String zipFilename = path + "/upload.zip";
    String colFilename = path + AnkiDroidApp.COLLECTION_PATH;
    try {
        ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFilename));
        int n = 1;
        for (File f : fileList) {
            String deckPath = f.getAbsolutePath();
            // set journal mode to delete
            try {
                AnkiDb d = AnkiDatabaseManager.getDatabase(deckPath);
            } catch (SQLiteDatabaseCorruptException e) {
                // ignore invalid .anki files
                corruptFiles.add(f.getName());
                continue;
            } finally {
                AnkiDatabaseManager.closeDatabase(deckPath);
            }
            // zip file
            String tmpName = n + ".anki";
            FileInputStream in = new FileInputStream(deckPath);
            ZipEntry ze = new ZipEntry(tmpName);
            zos.putNextEntry(ze);
            int len;
            while ((len = in.read(buf)) >= 0) {
                zos.write(buf, 0, len);
            }
            zos.closeEntry();
            map.put(tmpName, f.getName());
            n++;
        }
        // if all .anki files were found corrupted, abort
        if (fileList.length == corruptFiles.size()) {
            data.success = false;
            data.data = new Object[] { sContext.getString(R.string.upgrade_deck_web_upgrade_failed) };
            return data;
        }
        ZipEntry ze = new ZipEntry("map.json");
        zos.putNextEntry(ze);
        InputStream in = new ByteArrayInputStream(Utils.jsonToString(map).getBytes("UTF-8"));
        int len;
        while ((len = in.read(buf)) >= 0) {
            zos.write(buf, 0, len);
        }
        zos.closeEntry();
        zos.close();
    } catch (FileNotFoundException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    } catch (JSONException e) {
        throw new RuntimeException(e);
    }
    File zipFile = new File(zipFilename);
    // step 1.1: if it's over 50MB compressed, it must be upgraded by the user
    if (zipFile.length() > 50 * 1024 * 1024) {
        data.success = false;
        data.data = new Object[] { sContext.getString(R.string.upgrade_deck_web_upgrade_exceeds) };
        return data;
    }
    // step 2: upload zip file to upgrade service and get token
    BasicHttpSyncer h = new BasicHttpSyncer(null, null);
    // note: server doesn't expect it to be gzip compressed, because the zip file is compressed
    // enable cancelling
    publishProgress(R.string.upgrade_decks_upload, null, true);
    try {
        HttpResponse resp = h.req("upgrade/upload", new FileInputStream(zipFile), 0, false, null, mCancelCallback);
        if (resp == null && !isCancelled()) {
            data.success = false;
            data.data = new Object[] { sContext.getString(R.string.upgrade_deck_web_upgrade_failed) };
            return data;
        }
        String result;
        String key = null;
        if (!isCancelled()) {
            result = h.stream2String(resp.getEntity().getContent());
            if (result != null && result.startsWith("ok:")) {
                key = result.split(":")[1];
            } else {
                data.success = false;
                data.data = new Object[] { sContext.getString(R.string.upgrade_deck_web_upgrade_failed) };
                return data;
            }
        }
        while (!isCancelled()) {
            result = h.stream2String(h.req("upgrade/status?key=" + key).getEntity().getContent());
            if (result.equals("error")) {
                data.success = false;
                data.data = new Object[] { "error" };
                return data;
            } else if (result.startsWith("waiting:")) {
                publishProgress(R.string.upgrade_decks_upload, result.split(":")[1]);
            } else if (result.equals("upgrading")) {
                publishProgress(new Object[] { R.string.upgrade_decks_upgrade_started });
            } else if (result.equals("ready")) {
                break;
            } else {
                data.success = false;
                data.data = new Object[] { sContext.getString(R.string.upgrade_deck_web_upgrade_failed) };
                return data;
            }
            Thread.sleep(1000);
        }
        // gzip compression if the client says it can handle it
        if (!isCancelled()) {
            publishProgress(new Object[] { R.string.upgrade_decks_downloading });
            resp = h.req("upgrade/download?key=" + key, null, 6, true, null, mCancelCallback);
        // uploads/downloads have finished so disable cancelling
        }
        publishProgress(R.string.upgrade_decks_downloading, null, false);
        if (isCancelled()) {
            return null;
        }
        if (resp == null) {
            data.success = false;
            data.data = new Object[] { sContext.getString(R.string.upgrade_deck_web_upgrade_failed) };
            return data;
        }
        // step 5: check the received file is valid
        InputStream cont = resp.getEntity().getContent();
        if (!h.writeToFile(cont, colFilename)) {
            data.success = false;
            data.data = new Object[] { sContext.getString(R.string.upgrade_deck_web_upgrade_sdcard, new File(colFilename).length() / 1048576 + 1) };
            (new File(colFilename)).delete();
            return data;
        }
        // check the received file is ok
        publishProgress(new Object[] { R.string.sync_check_download_file });
        publishProgress(R.string.sync_check_download_file);
        try {
            AnkiDb d = AnkiDatabaseManager.getDatabase(colFilename);
            if (!d.queryString("PRAGMA integrity_check").equalsIgnoreCase("ok")) {
                data.success = false;
                data.data = new Object[] { sContext.getResources() };
                return data;
            }
        } finally {
            AnkiDatabaseManager.closeDatabase(colFilename);
        }
        Collection col = AnkiDroidApp.openCollection(colFilename);
        ArrayList<String> decks = col.getDecks().allNames(false);
        ArrayList<String> failed = new ArrayList<String>();
        ArrayList<File> mediaDirs = new ArrayList<File>();
        for (File f : fileList) {
            String name = f.getName().replaceFirst("\\.anki$", "");
            if (!decks.contains(name)) {
                failed.add(name);
            } else {
                mediaDirs.add(new File(f.getAbsolutePath().replaceFirst("\\.anki$", ".media")));
            }
        }
        File newMediaDir = new File(col.getMedia().getDir());
        // step 6. move media files to new media directory
        publishProgress(new Object[] { R.string.upgrade_decks_media });
        ArrayList<String> failedMedia = new ArrayList<String>();
        File curMediaDir = null;
        for (File mediaDir : mediaDirs) {
            curMediaDir = mediaDir;
            // Check if media directory exists and is local
            if (!curMediaDir.exists() || !curMediaDir.isDirectory()) {
                // If not try finding it in dropbox 1.2.x
                curMediaDir = new File(AnkiDroidApp.getDropboxDir(), mediaDir.getName());
                if (!curMediaDir.exists() || !curMediaDir.isDirectory()) {
                    // No media for this deck
                    continue;
                }
            }
            // Found media dir, copy files
            for (File m : curMediaDir.listFiles()) {
                try {
                    Utils.copyFile(m, new File(newMediaDir, m.getName()));
                } catch (IOException e) {
                    failedMedia.add(curMediaDir.getName().replaceFirst("\\.media$", ".anki"));
                    break;
                }
            }
        }
        data.data = new Object[] { failed, failedMedia, newMediaDir.getAbsolutePath() };
        data.success = true;
        return data;
    } catch (FileNotFoundException e) {
        throw new RuntimeException(e);
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    } catch (IllegalStateException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        (new File(zipFilename)).delete();
    }
}
Also used : ZipEntry(java.util.zip.ZipEntry) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) SQLiteDatabaseCorruptException(android.database.sqlite.SQLiteDatabaseCorruptException) AnkiDb(com.ichi2.anki.AnkiDb) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) JSONException(org.json.JSONException) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) BasicHttpSyncer(com.ichi2.libanki.sync.BasicHttpSyncer) JSONObject(org.json.JSONObject) ByteArrayInputStream(java.io.ByteArrayInputStream) ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) Collection(com.ichi2.libanki.Collection) JSONObject(org.json.JSONObject) File(java.io.File)

Example 4 with Result

use of com.ichi2.anki.multimediacard.googleimagesearch.json.Result in project Anki-Android by Ramblurr.

the class DeckTask method doInBackgroundCheckDatabase.

private TaskData doInBackgroundCheckDatabase(TaskData... params) {
    Log.i(AnkiDroidApp.TAG, "doInBackgroundCheckDatabase");
    Collection col = params[0].getCollection();
    long result = col.fixIntegrity();
    if (result == -1) {
        return new TaskData(false);
    } else {
        return new TaskData(0, result, true);
    }
}
Also used : Collection(com.ichi2.libanki.Collection)

Example 5 with Result

use of com.ichi2.anki.multimediacard.googleimagesearch.json.Result in project Anki-Android by Ramblurr.

the class DeckTask method doInBackgroundImportAdd.

private TaskData doInBackgroundImportAdd(TaskData... params) {
    Log.i(AnkiDroidApp.TAG, "doInBackgroundImportAdd");
    Resources res = AnkiDroidApp.getInstance().getBaseContext().getResources();
    Collection col = params[0].getCollection();
    String path = params[0].getString();
    boolean sharedDeckImport = params[0].getBoolean();
    ProgressCallback pc = null;
    // don't report progress on shared deck import (or maybe should we?)
    if (!sharedDeckImport) {
        pc = new ProgressCallback(this, res);
    }
    int addedCount = -1;
    try {
        Anki2Importer imp = new Anki2Importer(col, path, pc);
        AnkiDb ankiDB = col.getDb();
        ankiDB.getDatabase().beginTransaction();
        try {
            addedCount = imp.run();
            ankiDB.getDatabase().setTransactionSuccessful();
        } finally {
            ankiDB.getDatabase().endTransaction();
            if (sharedDeckImport) {
                File tmpFile = new File(path);
                tmpFile.delete();
            }
        }
        if (addedCount >= 0) {
            ankiDB.execute("VACUUM");
            ankiDB.execute("ANALYZE");
        }
        publishProgress(new TaskData(res.getString(R.string.import_update_counts)));
        // Update the counts
        DeckTask.TaskData result = doInBackgroundLoadDeckCounts(new TaskData(col));
        if (result == null) {
            return null;
        }
        return new TaskData(addedCount, result.getObjArray(), true);
    } catch (RuntimeException e) {
        Log.e(AnkiDroidApp.TAG, "doInBackgroundImportAdd - RuntimeException on importing cards: ", e);
        AnkiDroidApp.saveExceptionReportFile(e, "doInBackgroundImportAdd");
        return new TaskData(false);
    } catch (IOException e) {
        Log.e(AnkiDroidApp.TAG, "doInBackgroundImportAdd - IOException on importing cards: ", e);
        AnkiDroidApp.saveExceptionReportFile(e, "doInBackgroundImportAdd");
        return new TaskData(false);
    }
}
Also used : Anki2Importer(com.ichi2.libanki.importer.Anki2Importer) AnkiDb(com.ichi2.anki.AnkiDb) IOException(java.io.IOException) Collection(com.ichi2.libanki.Collection) Resources(android.content.res.Resources) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Aggregations

IOException (java.io.IOException)7 Intent (android.content.Intent)6 Resources (android.content.res.Resources)6 DeckTask (com.ichi2.async.DeckTask)6 Collection (com.ichi2.libanki.Collection)6 JSONException (org.json.JSONException)6 DialogInterface (android.content.DialogInterface)5 TaskData (com.ichi2.async.DeckTask.TaskData)5 File (java.io.File)5 OnCancelListener (android.content.DialogInterface.OnCancelListener)4 SharedPreferences (android.content.SharedPreferences)4 OnClickListener (android.view.View.OnClickListener)4 NotFoundException (android.content.res.Resources.NotFoundException)3 View (android.view.View)3 Payload (com.ichi2.async.Connection.Payload)3 StyledDialog (com.ichi2.themes.StyledDialog)3 FileNotFoundException (java.io.FileNotFoundException)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 JSONObject (org.json.JSONObject)3