Skip to content

Plugin task to update an entry found by a key field - #3327

Open
cheblin wants to merge 1 commit into
PhilippC:mainfrom
cheblin:feature/update-entry-task
Open

cheblin wants to merge 1 commit into
PhilippC:mainfrom
cheblin:feature/update-entry-task

Conversation

@cheblin

@cheblin cheblin commented Oct 14, 2026

Copy link
Copy Markdown

📝 Description

A plugin or another app can already create an entry (CreateEntryThenCloseTask, Kp2aControl.getAddEntryIntent) and open or query one, but it cannot change an entry it created before. An app that keeps its credentials in KP2A therefore has two options when a secret changes: create a second entry next to the old one, or ask the user to find the entry and retype the new value. Both are error prone for values such as API tokens, which are long and get rotated.

This adds UpdateEntryThenCloseTask:

  • The entry is found by a key field: a field of the app's choice (for example MyApp id) holding a value the app generated when it created the entry, typically a random string. The match is exact, outside the recycle bin, in any open database. Title, URL and the other fields are not used for the lookup, so the user may freely rename or move the entry, and several entries may share a URL.
  • The entry opens in the normal editor with the new values already filled in, and a message names the changed fields. Nothing is written until the user taps Save, so the user gives the same consent as for creating an entry and can review or discard the change.
  • Fields that are not passed stay as they are. A field passed as JSON null is removed (a standard field is emptied instead).
  • If no entry has the key, the user is told and nothing changes. With UpdateEntry_CreateIfMissing set to true, the entry is created as with CreateEntryThenCloseTask, including the key field, so one call covers both the first run and later updates.

Kp2aControl.getUpdateEntryIntent builds the intent. Two virtual hooks are added to AppTask (PrepareEditEntry, AfterUpdateEntry), with empty defaults, so other tasks are not affected. EntryEditActivity calls them for existing entries only.

Usage example

An app that talks to a server stores the server address and an API token in one entry. On the first run it generates a random id and keeps it in its own preferences. When the token is rotated, or on the first run when there is no entry yet:

String id = prefs.getString("kp2a_entry_id", null);
boolean firstRun = id == null;
if (firstRun) {
    id = UUID.randomUUID().toString();
    prefs.edit().putString("kp2a_entry_id", id).apply();
}

HashMap<String, String> fields = new HashMap<>();
if (firstRun)
    fields.put(KeepassDefs.TitleField, "My server"); // later updates leave the title to the user
fields.put("API token", newToken);
fields.put("Old endpoint", null); // remove a field that is no longer used

ArrayList<String> protectedFields = new ArrayList<>();
protectedFields.add("API token");

startActivity(Kp2aControl.getUpdateEntryIntent("MyApp id", id, fields, protectedFields, true));

KP2A opens the entry (after unlocking the database if needed), shows "Fields changed by the request (saved only when you save): API token, Old endpoint", and the user taps Save. The existing entry keeps its group, history, attachments and all other fields. On the first run the new entry gets the field MyApp id with the generated value.

🔗 Related Issue

None found.

🛠️ Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update

✅ Checklist

  • My code follows the style guidelines of this project.
  • I have performed a self-review of my own code.
  • I have added/updated tests that prove my fix is effective or that my feature works. No test project covers the app tasks.
  • All new and existing tests passed locally.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have built the app locally, verified that it builds and tested the changes thoroughly. Built with the .NET 10 Android workload (Release, Net flavor).

UpdateEntryThenCloseTask finds the entry by a key field of the caller's
choice holding a value the caller generated, typically a random string,
so the user may change the title, URL or anything else. The entry opens
in the editor with the new field values filled in and the changed fields
named. Nothing is written until the user saves. A field passed as JSON
null is removed. If no entry has the key, the user is told, or, with
UpdateEntry_CreateIfMissing, the entry is created with the fields and
the key field as with CreateEntryThenCloseTask.
Kp2aControl.getUpdateEntryIntent builds the intent.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant