Search in sources :

Example 1 with StyledDialog

use of com.ichi2.themes.StyledDialog 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 2 with StyledDialog

use of com.ichi2.themes.StyledDialog in project Anki-Android by Ramblurr.

the class DeckPicker method onPrepareDialog.

@Override
protected void onPrepareDialog(int id, Dialog dialog) {
    Resources res = getResources();
    StyledDialog ad = (StyledDialog) dialog;
    switch(id) {
        case DIALOG_DELETE_DECK:
            if (!AnkiDroidApp.colIsOpen() || mDeckList == null || mDeckList.size() == 0) {
                return;
            }
            boolean isDyn = AnkiDroidApp.getCol().getDecks().isDyn(mCurrentDid);
            if (isDyn) {
                ad.setMessage(String.format(res.getString(R.string.delete_cram_deck_message), "\'" + AnkiDroidApp.getCol().getDecks().name(mCurrentDid) + "\'"));
            } else {
                ad.setMessage(String.format(res.getString(R.string.delete_deck_message), "\'" + AnkiDroidApp.getCol().getDecks().name(mCurrentDid) + "\'"));
            }
            break;
        case DIALOG_CONTEXT_MENU:
            if (!AnkiDroidApp.colIsOpen() || mDeckList == null || mDeckList.size() == 0) {
                return;
            }
            mCurrentDid = Long.parseLong(mDeckList.get(mContextMenuPosition).get("did"));
            try {
                ad.changeListItem(CONTEXT_MENU_COLLAPSE_DECK, getResources().getString(AnkiDroidApp.getCol().getDecks().get(mCurrentDid).getBoolean("collapsed") ? R.string.contextmenu_deckpicker_inflate_deck : R.string.contextmenu_deckpicker_collapse_deck));
            } catch (NotFoundException e) {
            // do nothing
            } catch (JSONException e) {
            // do nothing
            }
            ad.setTitle(AnkiDroidApp.getCol().getDecks().name(mCurrentDid));
            break;
        case DIALOG_IMPORT_LOG:
        case DIALOG_SYNC_LOG:
        case DIALOG_SYNC_SANITY_ERROR:
            // If both have text, separate them by a new line.
            if (!TextUtils.isEmpty(mDialogMessage) && !TextUtils.isEmpty(mSyncMessage)) {
                ad.setMessage(mDialogMessage + "\n\n" + mSyncMessage);
            } else if (!TextUtils.isEmpty(mDialogMessage)) {
                ad.setMessage(mDialogMessage);
            } else {
                ad.setMessage(mSyncMessage);
            }
            break;
        case DIALOG_DB_ERROR:
            mLoadFailed = false;
            ad.getButton(Dialog.BUTTON3).setEnabled(hasErrorFiles());
            break;
        case DIALOG_LOAD_FAILED:
            mLoadFailed = true;
            if (mOpenCollectionDialog != null && mOpenCollectionDialog.isShowing()) {
                mOpenCollectionDialog.setMessage(res.getString(R.string.col_load_failed));
            }
            break;
        case DIALOG_ERROR_HANDLING:
            ArrayList<String> options = new ArrayList<String>();
            ArrayList<Integer> values = new ArrayList<Integer>();
            if (AnkiDroidApp.getCol() == null) {
                // retry
                options.add(res.getString(R.string.backup_retry_opening));
                values.add(0);
            } else {
                // fix integrity
                options.add(res.getString(R.string.check_db));
                values.add(1);
            }
            // repair db with sqlite
            options.add(res.getString(R.string.backup_error_menu_repair));
            values.add(2);
            // // restore from backup
            options.add(res.getString(R.string.backup_restore));
            values.add(3);
            // delete old collection and build new one
            options.add(res.getString(R.string.backup_full_sync_from_server));
            values.add(4);
            // delete old collection and build new one
            options.add(res.getString(R.string.backup_del_collection));
            values.add(5);
            String[] titles = new String[options.size()];
            mRepairValues = new int[options.size()];
            for (int i = 0; i < options.size(); i++) {
                titles[i] = options.get(i);
                mRepairValues[i] = values.get(i);
            }
            ad.setItems(titles, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    switch(mRepairValues[which]) {
                        case 0:
                            loadCollection();
                            return;
                        case 1:
                            integrityCheck();
                            return;
                        case 2:
                            showDialog(DIALOG_REPAIR_COLLECTION);
                            return;
                        case 3:
                            showDialog(DIALOG_RESTORE_BACKUP);
                            return;
                        case 4:
                            showDialog(DIALOG_FULL_SYNC_FROM_SERVER);
                            return;
                        case 5:
                            showDialog(DIALOG_NEW_COLLECTION);
                            return;
                    }
                }
            });
            break;
        case DIALOG_IMPORT_SELECT:
            List<File> fileList = Utils.getImportableDecks();
            if (fileList.size() == 0) {
                Themes.showThemedToast(DeckPicker.this, getResources().getString(R.string.upgrade_import_no_file_found), false);
            }
            ad.setEnabled(fileList.size() != 0);
            String[] tts = new String[fileList.size()];
            mImportValues = new String[fileList.size()];
            for (int i = 0; i < tts.length; i++) {
                tts[i] = fileList.get(i).getName().replace(".apkg", "");
                mImportValues[i] = fileList.get(i).getAbsolutePath();
            }
            ad.setItems(tts, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    mImportPath = mImportValues[which];
                    //If the apkg file is called "collection.apkg", we assume the collection will be replaced
                    if (mImportPath.split("/")[mImportPath.split("/").length - 1].equals("collection.apkg")) {
                        mImportMethod = IMPORT_METHOD_REPLACE;
                    }
                    switch(mImportMethod) {
                        case IMPORT_METHOD_ADD:
                            DeckTask.launchDeckTask(DeckTask.TASK_TYPE_IMPORT, mImportAddListener, new TaskData(AnkiDroidApp.getCol(), mImportPath, false));
                            mImportPath = null;
                            break;
                        case IMPORT_METHOD_REPLACE:
                            DeckTask.launchDeckTask(DeckTask.TASK_TYPE_IMPORT_REPLACE, mImportReplaceListener, new TaskData(AnkiDroidApp.getCol(), mImportPath));
                            mImportPath = null;
                            break;
                        case IMPORT_METHOD_ASK:
                        default:
                            showDialog(DIALOG_IMPORT);
                    }
                    mImportMethod = IMPORT_METHOD_ASK;
                }
            });
            break;
    }
}
Also used : DialogInterface(android.content.DialogInterface) StyledDialog(com.ichi2.themes.StyledDialog) ArrayList(java.util.ArrayList) NotFoundException(android.content.res.Resources.NotFoundException) JSONException(org.json.JSONException) TaskData(com.ichi2.async.DeckTask.TaskData) Resources(android.content.res.Resources) File(java.io.File)

Example 3 with StyledDialog

use of com.ichi2.themes.StyledDialog in project Anki-Android by Ramblurr.

the class CardBrowser method onPrepareDialog.

@Override
protected void onPrepareDialog(int id, Dialog dialog) {
    Resources res = getResources();
    StyledDialog ad = (StyledDialog) dialog;
    switch(id) {
        case DIALOG_ORDER:
            for (int i = 0; i < mOrderByFields.length; ++i) {
                if (i != CARD_ORDER_NONE && i == mOrder) {
                    if (mOrderAsc) {
                        ad.changeListItem(i, mOrderByFields[i] + " (▲)");
                    } else {
                        ad.changeListItem(i, mOrderByFields[i] + " (▼)");
                    }
                } else {
                    ad.changeListItem(i, mOrderByFields[i]);
                }
            }
            break;
        case DIALOG_CONTEXT_MENU:
            HashMap<String, String> card = mCards.get(mPositionInCardsList);
            int flags = Integer.parseInt(card.get("flags"));
            if (flags == 2 || flags == 3) {
                ad.changeListItem(CONTEXT_MENU_MARK, res.getString(R.string.card_browser_unmark_card));
                Log.d(AnkiDroidApp.TAG, "Selected Card is currently marked");
            } else {
                ad.changeListItem(CONTEXT_MENU_MARK, res.getString(R.string.card_browser_mark_card));
            }
            if (flags == 1 || flags == 3) {
                ad.changeListItem(CONTEXT_MENU_SUSPEND, res.getString(R.string.card_browser_unsuspend_card));
                Log.d(AnkiDroidApp.TAG, "Selected Card is currently suspended");
            } else {
                ad.changeListItem(CONTEXT_MENU_SUSPEND, res.getString(R.string.card_browser_suspend_card));
            }
            ad.setTitle(card.get("sfld"));
            break;
        case DIALOG_TAGS:
            mSelectedTags.clear();
            ad.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.d(AnkiDroidApp.TAG, "unchecked tag: " + tag);
                        mSelectedTags.remove(tag);
                    } else {
                        Log.d(AnkiDroidApp.TAG, "checked tag: " + tag);
                        mSelectedTags.add(tag);
                    }
                }
            });
            break;
    }
}
Also used : OnClickListener(android.content.DialogInterface.OnClickListener) DialogInterface(android.content.DialogInterface) StyledDialog(com.ichi2.themes.StyledDialog) Resources(android.content.res.Resources)

Example 4 with StyledDialog

use of com.ichi2.themes.StyledDialog in project Anki-Android by Ramblurr.

the class CardBrowser method onCreateDialog.

@Override
protected Dialog onCreateDialog(int id) {
    StyledDialog dialog = null;
    Resources res = getResources();
    StyledDialog.Builder builder = new StyledDialog.Builder(this);
    switch(id) {
        case DIALOG_ORDER:
            builder.setTitle(res.getString(R.string.card_browser_change_display_order_title));
            builder.setMessage(res.getString(R.string.card_browser_change_display_order_reverse));
            builder.setIcon(android.R.drawable.ic_menu_sort_by_size);
            builder.setSingleChoiceItems(res.getStringArray(R.array.card_browser_order_labels), mOrder, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface arg0, int which) {
                    if (which != mOrder) {
                        mOrder = which;
                        mOrderAsc = false;
                        try {
                            if (mOrder == 0) {
                                mCol.getConf().put("sortType", fSortTypes[1]);
                                AnkiDroidApp.getSharedPrefs(getBaseContext()).edit().putBoolean("cardBrowserNoSorting", true).commit();
                            } else {
                                mCol.getConf().put("sortType", fSortTypes[mOrder]);
                                AnkiDroidApp.getSharedPrefs(getBaseContext()).edit().putBoolean("cardBrowserNoSorting", false).commit();
                            }
                            // default to descending for non-text fields
                            if (fSortTypes[mOrder].equals("noteFld")) {
                                mOrderAsc = true;
                            }
                            mCol.getConf().put("sortBackwards", mOrderAsc);
                        } catch (JSONException e) {
                            throw new RuntimeException(e);
                        }
                        searchCards();
                    } else if (which != CARD_ORDER_NONE) {
                        mOrderAsc = !mOrderAsc;
                        try {
                            mCol.getConf().put("sortBackwards", mOrderAsc);
                        } catch (JSONException e) {
                            throw new RuntimeException(e);
                        }
                        Collections.reverse(mCards);
                        updateList();
                    }
                }
            });
            dialog = builder.create();
            break;
        case DIALOG_CONTEXT_MENU:
            // FIXME:
            String[] entries = new String[4];
            @SuppressWarnings("unused") MenuItem item;
            entries[CONTEXT_MENU_MARK] = res.getString(R.string.card_browser_mark_card);
            entries[CONTEXT_MENU_SUSPEND] = res.getString(R.string.card_browser_suspend_card);
            entries[CONTEXT_MENU_DELETE] = res.getString(R.string.card_browser_delete_card);
            entries[CONTEXT_MENU_DETAILS] = res.getString(R.string.card_browser_card_details);
            builder.setTitle("contextmenu");
            builder.setIcon(R.drawable.ic_menu_manage);
            builder.setItems(entries, mContextMenuListener);
            dialog = builder.create();
            break;
        case DIALOG_TAGS:
            allTags = mCol.getTags().all();
            builder.setTitle(R.string.studyoptions_limit_select_tags);
            builder.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);
                    }
                }
            });
            builder.setPositiveButton(res.getString(R.string.select), new OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    mSearchEditText.setText("");
                    String tags = mSelectedTags.toString();
                    mSearchEditText.setHint(getResources().getString(R.string.card_browser_tags_shown, tags.substring(1, tags.length() - 1)));
                    StringBuilder sb = new StringBuilder();
                    for (String tag : mSelectedTags) {
                        sb.append("tag:").append(tag).append(" ");
                    }
                    mSearchTerms = sb.toString();
                    searchCards();
                }
            });
            builder.setNegativeButton(res.getString(R.string.cancel), new OnClickListener() {

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

                @Override
                public void onCancel(DialogInterface dialog) {
                    mSelectedTags.clear();
                }
            });
            dialog = builder.create();
            break;
    }
    return dialog;
}
Also used : DialogInterface(android.content.DialogInterface) StyledDialog(com.ichi2.themes.StyledDialog) JSONException(org.json.JSONException) MenuItem(android.view.MenuItem) OnClickListener(android.content.DialogInterface.OnClickListener) OnClickListener(android.content.DialogInterface.OnClickListener) Resources(android.content.res.Resources) OnCancelListener(android.content.DialogInterface.OnCancelListener)

Example 5 with StyledDialog

use of com.ichi2.themes.StyledDialog in project Anki-Android by Ramblurr.

the class CardEditor method onPrepareDialog.

@Override
protected void onPrepareDialog(int id, Dialog dialog) {
    StyledDialog ad = (StyledDialog) dialog;
    switch(id) {
        case DIALOG_TAGS_SELECT:
            if (mEditorNote == null) {
                dialog = null;
                return;
            }
            selectedTags = new ArrayList<String>();
            for (String s : mEditorNote.getTags()) {
                selectedTags.add(s);
            }
            actualizeTagDialog(ad);
            break;
        case DIALOG_INTENT_INFORMATION:
            // dirty fix for dialog listview not being actualized
            mIntentInformationDialog = createDialogIntentInformation(new StyledDialog.Builder(this), getResources());
            ad = mIntentInformationDialog;
            break;
    }
}
Also used : Builder(com.ichi2.themes.StyledDialog.Builder) StyledDialog(com.ichi2.themes.StyledDialog)

Aggregations

StyledDialog (com.ichi2.themes.StyledDialog)14 DialogInterface (android.content.DialogInterface)12 Resources (android.content.res.Resources)10 JSONException (org.json.JSONException)7 JSONObject (org.json.JSONObject)7 OnClickListener (android.content.DialogInterface.OnClickListener)6 Intent (android.content.Intent)4 ArrayList (java.util.ArrayList)4 OnCancelListener (android.content.DialogInterface.OnCancelListener)3 View (android.view.View)3 AdapterView (android.widget.AdapterView)3 ListView (android.widget.ListView)3 TextView (android.widget.TextView)3 TaskData (com.ichi2.async.DeckTask.TaskData)3 JSONArray (org.json.JSONArray)3 SuppressLint (android.annotation.SuppressLint)2 NotFoundException (android.content.res.Resources.NotFoundException)2 EditText (android.widget.EditText)2 ImageView (android.widget.ImageView)2 RadioGroup (android.widget.RadioGroup)2