Skip to content

Commit 059280e

Browse files
committed
Introduce preference options to control the behavior when autofilling TOTP entries (show entry notification, activate keyboard, copy TOTP to clipboard)
Required a bit of refactoring to decide when to bring up keyboard selection dialog. Also changed the code so it shows e.g. on Pixel 6a with Android 14, previously did not. closes #2308 closes #1286 as it now shows up the entry notificaton (unless disabled) and solves the underlying issue in the best found way
1 parent 5edc070 commit 059280e

File tree

11 files changed

+212
-96
lines changed

11 files changed

+212
-96
lines changed

src/keepass2android/QueryCredentialsActivity.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ private void StartQuery()
140140
//will return the results later
141141
Intent i = new Intent(this, typeof (SelectCurrentDbActivity));
142142
//don't show user notifications when an entry is opened.
143-
var task = new SearchUrlTask() {UrlToSearchFor = _requestedUrl, ShowUserNotifications = ShowUserNotificationsMode.WhenTotp};
143+
var task = new SearchUrlTask() {UrlToSearchFor = _requestedUrl, ShowUserNotifications = ActivationCondition.WhenTotp, ActivateKeyboard = ActivationCondition.Never };
144144
task.ToIntent(i);
145145
StartActivityForResult(i, RequestCodeQuery);
146146
_startedQuery = true;

src/keepass2android/Resources/values/config.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
<string name="oi_filemanager_web">https://openintents.googlecode.com/files/FileManager-2.0.2.apk</string>
4848
<string name="permission_desc2">KP2A Search</string>
4949
<string name="permission_desc3">KP2A Choose autofill dataset</string>
50+
<string name="AutoFillTotp_prefs_screen_key">AutoFillTotp_prefs_screen_key</string>
5051

5152

5253
<!-- Preference settings -->

src/keepass2android/Resources/values/strings.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,16 @@
402402
<string name="ShowSeparateNotifications_summary">Show separate notifications for copying username and password to clipboard and activating the keyboard.</string>
403403
<string name="AccServiceAutoFill_prefs">AutoFill Accessibility-Service</string>
404404
<string name="AutoFill_prefs">AutoFill Service</string>
405+
<string name="AutoFillTotp_prefs_ShowNotification_summary">When autofilling an entry with TOTP, show the entry notification with a Copy TOTP button</string>
406+
<string name="AutoFillTotp_prefs_ShowNotification_title">Show entry notification</string>
407+
<string name="AutoFillTotp_prefs_title">Autofill for TOTP entries</string>
408+
<string name="AutoFillTotp_prefs_CopyTotpToClipboard_title">Copy TOTP to clipboard</string>
409+
<string name="AutoFillTotp_prefs_CopyTotpToClipboard_summary">When autofilling an entry with TOTP, copy the TOTP to the clipboard</string>
410+
<string name="AutoFillTotp_prefs_ActivateKeyboard_summary">When autofilling an entry with TOTP, activate the built-in keyboard. The keyboard has a TOTP button.</string>
411+
<string name="AutoFillTotp_prefs_ActivateKeyboard_title">Activate built-in keyboard</string>
412+
413+
<string name="TotpCopiedToClipboard">Copied TOTP to clipboard</string>
414+
405415
<string name="ShowKp2aKeyboardNotification_title">KP2A keyboard notification</string>
406416
<string name="ShowKp2aKeyboardNotification_summary">Make full entry accessible through the KP2A keyboard (recommended).</string>
407417
<string name="OpenKp2aKeyboardAutomatically_title">Switch keyboard</string>

src/keepass2android/Resources/xml/preferences.xml

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,8 @@
461461
android:defaultValue="false"
462462
android:title="@string/LogAutofillView_title"
463463
android:key="@string/LogAutofillView_key" />
464-
464+
465+
465466

466467
<CheckBoxPreference
467468
android:enabled="true"
@@ -476,6 +477,38 @@
476477
android:summary="@string/AutofillDisabledQueriesPreference_summary"
477478
android:persistent="false"
478479
android:key="AutofillDisabledQueriesPreference_key"/>
480+
481+
<PreferenceScreen
482+
android:key="@string/AutoFillTotp_prefs_screen_key"
483+
android:title="@string/AutoFillTotp_prefs_title"
484+
>
485+
<keepass2android.ToolbarPreference
486+
android:key="@string/AutoFillTotp_prefs_screen_key"
487+
android:title="@string/AutoFillTotp_prefs_title" />
488+
489+
<CheckBoxPreference android:key="AutoFillTotp_prefs_ShowNotification_key"
490+
android:enabled="true"
491+
android:persistent="true"
492+
android:summary="@string/AutoFillTotp_prefs_ShowNotification_summary"
493+
android:defaultValue="true"
494+
android:title="@string/AutoFillTotp_prefs_ShowNotification_title"
495+
/>
496+
<CheckBoxPreference android:key="AutoFillTotp_prefs_CopyTotpToClipboard_key"
497+
android:enabled="true"
498+
android:persistent="true"
499+
android:summary="@string/AutoFillTotp_prefs_CopyTotpToClipboard_summary"
500+
android:defaultValue="true"
501+
android:title="@string/AutoFillTotp_prefs_CopyTotpToClipboard_title"
502+
/>
503+
<CheckBoxPreference android:key="AutoFillTotp_prefs_ActivateKeyboard_key"
504+
android:enabled="true"
505+
android:persistent="true"
506+
android:summary="@string/AutoFillTotp_prefs_ActivateKeyboard_summary"
507+
android:defaultValue="false"
508+
android:title="@string/AutoFillTotp_prefs_ActivateKeyboard_title"
509+
/>
510+
511+
</PreferenceScreen>
479512

480513
</PreferenceScreen>
481514

src/keepass2android/SelectCurrentDbActivity.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Diagnostics;
34
using System.Linq;
45
using System.Text;
56

@@ -306,7 +307,27 @@ protected override void OnCreate(Bundle savedInstanceState)
306307
}
307308
else if (Intent.Action == Intent.ActionSend)
308309
{
309-
AppTask = new SearchUrlTask { UrlToSearchFor = Intent.GetStringExtra(Intent.ExtraText) };
310+
ActivationCondition activationCondition = ActivationCondition.Never;
311+
ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(this);
312+
if (prefs.GetBoolean("kp2a_switch_rooted", false))
313+
{
314+
activationCondition = ActivationCondition.Always;
315+
}
316+
else
317+
{
318+
//if the app is about to be closed again (e.g. after searching for a URL and returning to the browser:
319+
// automatically bring up the Keyboard selection dialog
320+
if (prefs.GetBoolean(this.GetString(Resource.String.OpenKp2aKeyboardAutomatically_key), this.Resources.GetBoolean(Resource.Boolean.OpenKp2aKeyboardAutomatically_default)))
321+
{
322+
activationCondition = ActivationCondition.Always;
323+
}
324+
}
325+
326+
AppTask = new SearchUrlTask()
327+
{
328+
UrlToSearchFor = Intent.GetStringExtra(Intent.ExtraText),
329+
ActivateKeyboard = activationCondition
330+
};
310331
}
311332
}
312333

src/keepass2android/ShareUrlResults.cs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -82,20 +82,15 @@ protected override void OnCreate(Bundle savedInstanceState)
8282
//if user presses back to leave this activity:
8383
SetResult(Result.Canceled);
8484

85-
8685
UpdateBottomBarElementVisibility(Resource.Id.select_other_entry, true);
8786
UpdateBottomBarElementVisibility(Resource.Id.add_url_entry, true);
8887

89-
9088
if (App.Kp2a.DatabaseIsUnlocked)
9189
{
92-
93-
Query();
90+
Query();
9491
}
9592
// else: LockCloseListActivity.OnResume will trigger a broadcast (LockDatabase) which will cause the activity to be finished.
96-
97-
98-
93+
9994
}
10095

10196
protected override void OnSaveInstanceState(Bundle outState)
@@ -162,9 +157,17 @@ private void Query()
162157
String searchUrl = searchUrlTask.UrlToSearchFor;
163158
selectOtherEntry.Visibility = ViewStates.Visible;
164159

165-
var newTask = new SearchUrlTask() { AutoReturnFromQuery = false, UrlToSearchFor = searchUrl };
160+
SearchUrlTask newTask;
166161
if (AppTask is SelectEntryTask currentSelectTask)
162+
{
163+
newTask = new SearchUrlTask() { AutoReturnFromQuery = false, UrlToSearchFor = searchUrl, ActivateKeyboard = currentSelectTask.ActivateKeyboard };
167164
newTask.ShowUserNotifications = currentSelectTask.ShowUserNotifications;
165+
newTask.ActivateKeyboard = currentSelectTask.ActivateKeyboard;
166+
newTask.CopyTotpToClipboard = currentSelectTask.CopyTotpToClipboard;
167+
}
168+
else
169+
newTask = new SearchUrlTask() { AutoReturnFromQuery = false, UrlToSearchFor = searchUrl, ActivateKeyboard = ActivationCondition.Never };
170+
168171

169172
selectOtherEntry.Click += (sender, e) => {
170173
GroupActivity.Launch(this, newTask, new ActivityLaunchModeRequestCode(0));
@@ -179,7 +182,7 @@ private void Query()
179182
createUrlEntry.Visibility = ViewStates.Visible;
180183
createUrlEntry.Click += (sender, e) =>
181184
{
182-
GroupActivity.Launch(this, new CreateEntryThenCloseTask { Url = searchUrl, ShowUserNotifications = (AppTask as SelectEntryTask)?.ShowUserNotifications ?? ShowUserNotificationsMode.Always }, new ActivityLaunchModeRequestCode(0));
185+
GroupActivity.Launch(this, new CreateEntryThenCloseTask { Url = searchUrl, ShowUserNotifications = (AppTask as SelectEntryTask)?.ShowUserNotifications ?? ActivationCondition.Always }, new ActivityLaunchModeRequestCode(0));
183186
Toast.MakeText(this, GetString(Resource.String.select_group_then_add, new Java.Lang.Object[] { GetString(Resource.String.add_entry) }), ToastLength.Long).Show();
184187
};
185188
}

src/keepass2android/Totp/Kp2aTotp.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ namespace keepass2android
1010
{
1111
class Kp2aTotp
1212
{
13+
public const string TotpKey = "TOTP";
1314

14-
readonly ITotpPluginAdapter[] _pluginAdapters = new ITotpPluginAdapter[]
15+
readonly ITotpPluginAdapter[] _pluginAdapters = new ITotpPluginAdapter[]
1516
{
1617
new TrayTotpPluginAdapter(),
1718
new KeeOtpPluginAdapter(),

src/keepass2android/Totp/UpdateTotpTimerTask.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace PluginTOTP
1313
{
1414
class UpdateTotpTimerTask: TimerTask
1515
{
16-
public const string TotpKey = "TOTP";
16+
public const string TotpKey = Kp2aTotp.TotpKey;
1717
private readonly Context _context;
1818
private readonly ITotpPluginAdapter _adapter;
1919

0 commit comments

Comments
 (0)