Search in sources :

Example 16 with R

use of com.ichi2.anki.R in project Anki-Android by Ramblurr.

the class DeckPicker method onOptionsItemSelected.

/**
 * Handles item selections
 */
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    Resources res = getResources();
    switch(item.getItemId()) {
        case MENU_HELP:
            showDialog(DIALOG_SELECT_HELP);
            return true;
        case MENU_SYNC:
            sync();
            return true;
        case MENU_ADD_NOTE:
            addNote();
            return true;
        case MENU_STATISTICS:
            showDialog(DIALOG_SELECT_STATISTICS_TYPE);
            return true;
        case MENU_CARDBROWSER:
            openCardBrowser();
            return true;
        case MENU_CREATE_DECK:
            StyledDialog.Builder builder2 = new StyledDialog.Builder(DeckPicker.this);
            builder2.setTitle(res.getString(R.string.new_deck));
            mDialogEditText = (EditText) new EditText(DeckPicker.this);
            // mDialogEditText.setFilters(new InputFilter[] { mDeckNameFilter });
            builder2.setView(mDialogEditText, false, false);
            builder2.setPositiveButton(res.getString(R.string.create), new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    String deckName = mDialogEditText.getText().toString().replaceAll("[\'\"\\n\\r\\[\\]\\(\\)]", "");
                    // Log.i(AnkiDroidApp.TAG, "Creating deck: " + deckName);
                    AnkiDroidApp.getCol().getDecks().id(deckName, true);
                    loadCounts();
                }
            });
            builder2.setNegativeButton(res.getString(R.string.cancel), null);
            builder2.create().show();
            return true;
        case MENU_CREATE_DYNAMIC_DECK:
            StyledDialog.Builder builder3 = new StyledDialog.Builder(DeckPicker.this);
            builder3.setTitle(res.getString(R.string.new_deck));
            mDialogEditText = (EditText) new EditText(DeckPicker.this);
            ArrayList<String> names = AnkiDroidApp.getCol().getDecks().allNames();
            int n = 1;
            String cramDeckName = "Cram 1";
            while (names.contains(cramDeckName)) {
                n++;
                cramDeckName = "Cram " + n;
            }
            mDialogEditText.setText(cramDeckName);
            // mDialogEditText.setFilters(new InputFilter[] { mDeckNameFilter });
            builder3.setView(mDialogEditText, false, false);
            builder3.setPositiveButton(res.getString(R.string.create), new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    long id = AnkiDroidApp.getCol().getDecks().newDyn(mDialogEditText.getText().toString());
                    openStudyOptions(id, new Bundle());
                }
            });
            builder3.setNegativeButton(res.getString(R.string.cancel), null);
            builder3.create().show();
            return true;
        case MENU_ABOUT:
            startActivity(new Intent(DeckPicker.this, Info.class));
            if (AnkiDroidApp.SDK_VERSION > 4) {
                ActivityTransitionAnimation.slide(DeckPicker.this, ActivityTransitionAnimation.RIGHT);
            }
            return true;
        case MENU_ADD_SHARED_DECK:
            if (AnkiDroidApp.getCol() != null) {
                SharedPreferences preferences = AnkiDroidApp.getSharedPrefs(getBaseContext());
                String hkey = preferences.getString("hkey", "");
                if (hkey.length() == 0) {
                    showDialog(DIALOG_USER_NOT_LOGGED_IN_ADD_SHARED_DECK);
                } else {
                    addSharedDeck();
                }
            }
            return true;
        case MENU_IMPORT:
            showDialog(DIALOG_IMPORT_HINT);
            return true;
        case MENU_PREFERENCES:
            startActivityForResult(new Intent(DeckPicker.this, Preferences.class), PREFERENCES_UPDATE);
            return true;
        case MENU_FEEDBACK:
            Intent i = new Intent(DeckPicker.this, Feedback.class);
            i.putExtra("request", REPORT_FEEDBACK);
            startActivityForResult(i, REPORT_FEEDBACK);
            if (AnkiDroidApp.SDK_VERSION > 4) {
                ActivityTransitionAnimation.slide(this, ActivityTransitionAnimation.RIGHT);
            }
            return true;
        case CHECK_DATABASE:
            integrityCheck();
            return true;
        case StudyOptionsActivity.MENU_ROTATE:
            if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
                this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            } else {
                this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            }
            return true;
        case StudyOptionsActivity.MENU_NIGHT:
            SharedPreferences preferences = AnkiDroidApp.getSharedPrefs(this);
            if (preferences.getBoolean("invertedColors", false)) {
                preferences.edit().putBoolean("invertedColors", false).commit();
                item.setIcon(R.drawable.ic_menu_night);
            } else {
                preferences.edit().putBoolean("invertedColors", true).commit();
                item.setIcon(R.drawable.ic_menu_night_checked);
            }
            return true;
        case MENU_REUPGRADE:
            restartUpgradeProcess();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}
Also used : EditText(android.widget.EditText) DialogInterface(android.content.DialogInterface) SharedPreferences(android.content.SharedPreferences) Bundle(android.os.Bundle) ChartBuilder(com.ichi2.charts.ChartBuilder) StyledDialog(com.ichi2.themes.StyledDialog) Intent(android.content.Intent) ActivityInfo(android.content.pm.ActivityInfo) ContextMenuInfo(android.view.ContextMenu.ContextMenuInfo) Resources(android.content.res.Resources) SharedPreferences(android.content.SharedPreferences)

Example 17 with R

use of com.ichi2.anki.R in project AnkiChinaAndroid by ankichinateam.

the class ModelBrowser method renameModelDialog.

/*
     * Displays a confirmation box asking if you want to rename the note type and then renames it if confirmed
     */
private void renameModelDialog() {
    mModelNameInput = new EditText(this);
    mModelNameInput.setSingleLine(true);
    mModelNameInput.setText(mModels.get(mModelListPosition).getString("name"));
    mModelNameInput.setSelection(mModelNameInput.getText().length());
    new MaterialDialog.Builder(this).title(R.string.rename_model).positiveText(R.string.rename).negativeText(R.string.dialog_cancel).customView(mModelNameInput, true).onPositive((dialog, which) -> {
        Model model = mModels.get(mModelListPosition);
        String deckName = mModelNameInput.getText().toString().replaceAll("[\"\\n\\r]", "");
        getCol().getDecks().id(deckName, false);
        if (deckName.length() > 0) {
            model.put("name", deckName);
            col.getModels().update(model);
            mModels.get(mModelListPosition).put("name", deckName);
            mModelDisplayList.set(mModelListPosition, new DisplayPair(mModels.get(mModelListPosition).getString("name"), mCardCounts.get(mModelListPosition)));
            refreshList();
        } else {
            showToast(getResources().getString(R.string.toast_empty_name));
        }
    }).show();
}
Also used : EditText(android.widget.EditText) Context(android.content.Context) TaskListener(com.ichi2.async.TaskListener) Bundle(android.os.Bundle) NonNull(androidx.annotation.NonNull) Intent(android.content.Intent) ModelBrowserContextMenu(com.ichi2.anki.dialogs.ModelBrowserContextMenu) Random(java.util.Random) Collection(com.ichi2.libanki.Collection) ConfirmationDialog(com.ichi2.anki.dialogs.ConfirmationDialog) MenuItem(android.view.MenuItem) ArrayList(java.util.ArrayList) TASK_TYPE(com.ichi2.async.CollectionTask.TASK_TYPE) ActionBar(androidx.appcompat.app.ActionBar) WidgetStatus(com.ichi2.widget.WidgetStatus) Toast(android.widget.Toast) Menu(android.view.Menu) View(android.view.View) Model(com.ichi2.libanki.Model) TaskData(com.ichi2.async.TaskData) AdapterView(android.widget.AdapterView) OnItemLongClickListener(android.widget.AdapterView.OnItemLongClickListener) ConfirmModSchemaException(com.ichi2.anki.exception.ConfirmModSchemaException) StdModels(com.ichi2.libanki.StdModels) LayoutInflater(android.view.LayoutInflater) CollectionTask(com.ichi2.async.CollectionTask) ViewGroup(android.view.ViewGroup) Timber(timber.log.Timber) Spinner(android.widget.Spinner) ArrayAdapter(android.widget.ArrayAdapter) Nullable(androidx.annotation.Nullable) TextView(android.widget.TextView) TaskListenerWithContext(com.ichi2.async.TaskListenerWithContext) ListView(android.widget.ListView) MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) ActivityTransitionAnimation(com.ichi2.anim.ActivityTransitionAnimation) EditText(android.widget.EditText) Model(com.ichi2.libanki.Model)

Example 18 with R

use of com.ichi2.anki.R in project AnkiChinaAndroid by ankichinateam.

the class ModelFieldEditor method renameFieldDialog.

/*
     * Creates a dialog to rename the currently selected field
     * Processing time is constant
     */
private void renameFieldDialog() {
    mFieldNameInput = new EditText(this);
    mFieldNameInput.setSingleLine(true);
    mFieldNameInput.setText(mFieldLabels.get(mCurrentPos));
    mFieldNameInput.setSelection(mFieldNameInput.getText().length());
    new MaterialDialog.Builder(this).title(R.string.rename_model).positiveText(R.string.rename).customView(mFieldNameInput, true).onPositive((dialog, which) -> {
        String fieldLabel = mFieldNameInput.getText().toString().replaceAll("[\\n\\r]", "");
        if (fieldLabel.length() == 0) {
            UIUtils.showThemedToast(this, getResources().getString(R.string.toast_empty_name), true);
        } else if (containsField(fieldLabel)) {
            UIUtils.showThemedToast(this, getResources().getString(R.string.toast_duplicate_field), true);
        } else {
            // Field is valid, now rename
            try {
                renameField();
            } catch (ConfirmModSchemaException e) {
                // Handler mod schema confirmation
                ConfirmationDialog c = new ConfirmationDialog();
                c.setArgs(getResources().getString(R.string.full_sync_confirmation));
                Runnable confirm = () -> {
                    mCol.modSchemaNoCheck();
                    try {
                        renameField();
                    } catch (ConfirmModSchemaException e1) {
                    // This should never be thrown
                    }
                    dismissContextMenu();
                };
                c.setConfirm(confirm);
                c.setCancel(mConfirmDialogCancel);
                ModelFieldEditor.this.showDialogFragment(c);
            }
        }
    }).negativeText(R.string.dialog_cancel).show();
}
Also used : EditText(android.widget.EditText) ConfirmModSchemaException(com.ichi2.anki.exception.ConfirmModSchemaException) ConfirmationDialog(com.ichi2.anki.dialogs.ConfirmationDialog)

Example 19 with R

use of com.ichi2.anki.R in project AnkiChinaAndroid by ankichinateam.

the class ModelFieldEditor method renameField.

/*
     * Renames the current field
     */
private void renameField() throws ConfirmModSchemaException {
    String fieldLabel = mFieldNameInput.getText().toString().replaceAll("[\\n\\r]", "");
    JSONObject field = mNoteFields.getJSONObject(mCurrentPos);
    mCol.getModels().renameField(mMod, field, fieldLabel);
    mCol.getModels().save();
    fullRefreshList();
}
Also used : JSONObject(com.ichi2.utils.JSONObject)

Example 20 with R

use of com.ichi2.anki.R in project AnkiChinaAndroid by ankichinateam.

the class HttpTest method testLogin.

@Test
public void testLogin() {
    String username = "AnkiDroidInstrumentedTestUser";
    String password = "AnkiDroidInstrumentedTestInvalidPass";
    Connection.Payload invalidPayload = new Connection.Payload(new Object[] { username, password, new HostNum(null) });
    TestTaskListener testListener = new TestTaskListener(invalidPayload);
    // We have to carefully run things on the main thread here or the threading protections in BaseAsyncTask throw
    // The first one is just to run the static initializer, really
    Runnable onlineRunnable = () -> {
        try {
            Class.forName("com.ichi2.async.Connection");
        } catch (Exception e) {
            Assert.fail("Unable to load Connection class: " + e.getMessage());
        }
    };
    InstrumentationRegistry.getInstrumentation().runOnMainSync(onlineRunnable);
    // TODO simulate offline programmatically - currently exercised by manually toggling an emulator offline pre-test
    if (!Connection.isOnline()) {
        Connection.login(testListener, invalidPayload);
        Assert.assertFalse("Successful login despite being offline", testListener.getPayload().success);
        Assert.assertTrue("onDisconnected not called despite being offline", testListener.mDisconnectedCalled);
        return;
    }
    Runnable r = () -> {
        Connection conn = Connection.login(testListener, invalidPayload);
        try {
            // This forces us to synchronously wait for the AsyncTask to do it's work
            conn.get();
        } catch (Exception e) {
            Assert.fail("Caught exception while trying to login: " + e.getMessage());
        }
    };
    InstrumentationRegistry.getInstrumentation().runOnMainSync(r);
    Assert.assertFalse("Successful login despite invalid credentials", testListener.getPayload().success);
}
Also used : Connection(com.ichi2.async.Connection) HostNum(com.ichi2.libanki.sync.HostNum) Test(org.junit.Test)

Aggregations

JSONObject (com.ichi2.utils.JSONObject)17 JSONArray (com.ichi2.utils.JSONArray)14 ArrayList (java.util.ArrayList)10 Test (org.junit.Test)7 ConfirmModSchemaException (com.ichi2.anki.exception.ConfirmModSchemaException)6 Model (com.ichi2.libanki.Model)6 EditText (android.widget.EditText)5 File (java.io.File)5 Random (java.util.Random)5 Nullable (androidx.annotation.Nullable)4 ConfirmationDialog (com.ichi2.anki.dialogs.ConfirmationDialog)4 Connection (com.ichi2.async.Connection)4 IOException (java.io.IOException)4 Intent (android.content.Intent)3 SharedPreferences (android.content.SharedPreferences)3 Cursor (android.database.Cursor)3 Bundle (android.os.Bundle)3 Timber (timber.log.Timber)3 Context (android.content.Context)2 LayoutInflater (android.view.LayoutInflater)2