Search in sources :

Example 31 with R

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

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() {
    initializeNoteTypeList();
    mModelNameInput = new FixedEditText(this);
    mModelNameInput.setSingleLine(true);
    mModelNameInput.setText(mModels.get(mModelListPosition).getString("name"));
    mModelNameInput.setSelection(mModelNameInput.getText().length());
    new MaterialEditTextDialog.Builder(this, mModelNameInput).title(R.string.rename_model).positiveText(R.string.rename).negativeText(R.string.dialog_cancel).onPositive((dialog, which) -> {
        Model model = mModels.get(mModelListPosition);
        String deckName = mModelNameInput.getText().toString().replaceAll("[\"\\n\\r]", "");
        if (mExistingModelNames.contains(deckName)) {
            deckName = randomizeName(deckName);
        }
        if (deckName.length() > 0) {
            model.put("name", deckName);
            mCol.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 : Context(android.content.Context) Bundle(android.os.Bundle) TaskManager(com.ichi2.async.TaskManager) NonNull(androidx.annotation.NonNull) Pair(android.util.Pair) 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) ActionBar(androidx.appcompat.app.ActionBar) WidgetStatus(com.ichi2.widget.WidgetStatus) Menu(android.view.Menu) View(android.view.View) Model(com.ichi2.libanki.Model) START(com.ichi2.anim.ActivityTransitionAnimation.Direction.START) ConfirmModSchemaException(com.ichi2.anki.exception.ConfirmModSchemaException) StdModels(com.ichi2.libanki.StdModels) ActivityResultLauncher(androidx.activity.result.ActivityResultLauncher) 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) List(java.util.List) Nullable(androidx.annotation.Nullable) TextView(android.widget.TextView) ActivityResultContracts(androidx.activity.result.contract.ActivityResultContracts) TaskListenerWithContext(com.ichi2.async.TaskListenerWithContext) Utils.checksum(com.ichi2.libanki.Utils.checksum) ListView(android.widget.ListView) MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) FixedEditText(com.ichi2.ui.FixedEditText) Activity(android.app.Activity) EditText(android.widget.EditText) Model(com.ichi2.libanki.Model) FixedEditText(com.ichi2.ui.FixedEditText)

Example 32 with R

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

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 33 with R

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

the class ZipFile method exportFiltered.

private JSONObject exportFiltered(ZipFile z, String path, Context context) throws IOException, JSONException, ImportExportException {
    // export into the anki2 file
    String colfile = path.replace(".apkg", ".anki2");
    super.exportInto(colfile, context);
    z.write(colfile, CollectionHelper.COLLECTION_FILENAME);
    // and media
    prepareMedia();
    JSONObject media = _exportMedia(z, mMediaFiles, mCol.getMedia().dir());
    // tidy up intermediate files
    SQLiteDatabase.deleteDatabase(new File(colfile));
    SQLiteDatabase.deleteDatabase(new File(path.replace(".apkg", ".media.ad.db2")));
    String tempPath = path.replace(".apkg", ".media");
    File file = new File(tempPath);
    if (file.exists()) {
        String deleteCmd = "rm -r " + tempPath;
        Runtime runtime = Runtime.getRuntime();
        try {
            runtime.exec(deleteCmd);
        } catch (IOException ignored) {
            Timber.w(ignored);
        }
    }
    return media;
}
Also used : JSONObject(com.ichi2.utils.JSONObject) IOException(java.io.IOException) File(java.io.File)

Example 34 with R

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

the class HttpTest method testLogin.

@Test
// #7108: AsyncTask
@SuppressWarnings("deprecation")
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