Skip to content

Commit

Permalink
Introduce preference options to control the behavior when autofilling…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
PhilippC committed Jan 4, 2024
1 parent 5edc070 commit 059280e
Show file tree
Hide file tree
Showing 11 changed files with 212 additions and 96 deletions.
2 changes: 1 addition & 1 deletion src/keepass2android/QueryCredentialsActivity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ private void StartQuery()
//will return the results later
Intent i = new Intent(this, typeof (SelectCurrentDbActivity));
//don't show user notifications when an entry is opened.
var task = new SearchUrlTask() {UrlToSearchFor = _requestedUrl, ShowUserNotifications = ShowUserNotificationsMode.WhenTotp};
var task = new SearchUrlTask() {UrlToSearchFor = _requestedUrl, ShowUserNotifications = ActivationCondition.WhenTotp, ActivateKeyboard = ActivationCondition.Never };
task.ToIntent(i);
StartActivityForResult(i, RequestCodeQuery);
_startedQuery = true;
Expand Down
1 change: 1 addition & 0 deletions src/keepass2android/Resources/values/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
<string name="oi_filemanager_web">https://openintents.googlecode.com/files/FileManager-2.0.2.apk</string>
<string name="permission_desc2">KP2A Search</string>
<string name="permission_desc3">KP2A Choose autofill dataset</string>
<string name="AutoFillTotp_prefs_screen_key">AutoFillTotp_prefs_screen_key</string>


<!-- Preference settings -->
Expand Down
10 changes: 10 additions & 0 deletions src/keepass2android/Resources/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,16 @@
<string name="ShowSeparateNotifications_summary">Show separate notifications for copying username and password to clipboard and activating the keyboard.</string>
<string name="AccServiceAutoFill_prefs">AutoFill Accessibility-Service</string>
<string name="AutoFill_prefs">AutoFill Service</string>
<string name="AutoFillTotp_prefs_ShowNotification_summary">When autofilling an entry with TOTP, show the entry notification with a Copy TOTP button</string>
<string name="AutoFillTotp_prefs_ShowNotification_title">Show entry notification</string>
<string name="AutoFillTotp_prefs_title">Autofill for TOTP entries</string>
<string name="AutoFillTotp_prefs_CopyTotpToClipboard_title">Copy TOTP to clipboard</string>
<string name="AutoFillTotp_prefs_CopyTotpToClipboard_summary">When autofilling an entry with TOTP, copy the TOTP to the clipboard</string>
<string name="AutoFillTotp_prefs_ActivateKeyboard_summary">When autofilling an entry with TOTP, activate the built-in keyboard. The keyboard has a TOTP button.</string>
<string name="AutoFillTotp_prefs_ActivateKeyboard_title">Activate built-in keyboard</string>

<string name="TotpCopiedToClipboard">Copied TOTP to clipboard</string>

<string name="ShowKp2aKeyboardNotification_title">KP2A keyboard notification</string>
<string name="ShowKp2aKeyboardNotification_summary">Make full entry accessible through the KP2A keyboard (recommended).</string>
<string name="OpenKp2aKeyboardAutomatically_title">Switch keyboard</string>
Expand Down
35 changes: 34 additions & 1 deletion src/keepass2android/Resources/xml/preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,8 @@
android:defaultValue="false"
android:title="@string/LogAutofillView_title"
android:key="@string/LogAutofillView_key" />




<CheckBoxPreference
android:enabled="true"
Expand All @@ -476,6 +477,38 @@
android:summary="@string/AutofillDisabledQueriesPreference_summary"
android:persistent="false"
android:key="AutofillDisabledQueriesPreference_key"/>

<PreferenceScreen
android:key="@string/AutoFillTotp_prefs_screen_key"
android:title="@string/AutoFillTotp_prefs_title"
>
<keepass2android.ToolbarPreference
android:key="@string/AutoFillTotp_prefs_screen_key"
android:title="@string/AutoFillTotp_prefs_title" />

<CheckBoxPreference android:key="AutoFillTotp_prefs_ShowNotification_key"
android:enabled="true"
android:persistent="true"
android:summary="@string/AutoFillTotp_prefs_ShowNotification_summary"
android:defaultValue="true"
android:title="@string/AutoFillTotp_prefs_ShowNotification_title"
/>
<CheckBoxPreference android:key="AutoFillTotp_prefs_CopyTotpToClipboard_key"
android:enabled="true"
android:persistent="true"
android:summary="@string/AutoFillTotp_prefs_CopyTotpToClipboard_summary"
android:defaultValue="true"
android:title="@string/AutoFillTotp_prefs_CopyTotpToClipboard_title"
/>
<CheckBoxPreference android:key="AutoFillTotp_prefs_ActivateKeyboard_key"
android:enabled="true"
android:persistent="true"
android:summary="@string/AutoFillTotp_prefs_ActivateKeyboard_summary"
android:defaultValue="false"
android:title="@string/AutoFillTotp_prefs_ActivateKeyboard_title"
/>

</PreferenceScreen>

</PreferenceScreen>

Expand Down
23 changes: 22 additions & 1 deletion src/keepass2android/SelectCurrentDbActivity.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;

Expand Down Expand Up @@ -306,7 +307,27 @@ protected override void OnCreate(Bundle savedInstanceState)
}
else if (Intent.Action == Intent.ActionSend)
{
AppTask = new SearchUrlTask { UrlToSearchFor = Intent.GetStringExtra(Intent.ExtraText) };
ActivationCondition activationCondition = ActivationCondition.Never;
ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(this);
if (prefs.GetBoolean("kp2a_switch_rooted", false))
{
activationCondition = ActivationCondition.Always;
}
else
{
//if the app is about to be closed again (e.g. after searching for a URL and returning to the browser:
// automatically bring up the Keyboard selection dialog
if (prefs.GetBoolean(this.GetString(Resource.String.OpenKp2aKeyboardAutomatically_key), this.Resources.GetBoolean(Resource.Boolean.OpenKp2aKeyboardAutomatically_default)))
{
activationCondition = ActivationCondition.Always;
}
}

AppTask = new SearchUrlTask()
{
UrlToSearchFor = Intent.GetStringExtra(Intent.ExtraText),
ActivateKeyboard = activationCondition
};
}
}

Expand Down
21 changes: 12 additions & 9 deletions src/keepass2android/ShareUrlResults.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,20 +82,15 @@ protected override void OnCreate(Bundle savedInstanceState)
//if user presses back to leave this activity:
SetResult(Result.Canceled);


UpdateBottomBarElementVisibility(Resource.Id.select_other_entry, true);
UpdateBottomBarElementVisibility(Resource.Id.add_url_entry, true);


if (App.Kp2a.DatabaseIsUnlocked)
{

Query();
Query();
}
// else: LockCloseListActivity.OnResume will trigger a broadcast (LockDatabase) which will cause the activity to be finished.




}

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

var newTask = new SearchUrlTask() { AutoReturnFromQuery = false, UrlToSearchFor = searchUrl };
SearchUrlTask newTask;
if (AppTask is SelectEntryTask currentSelectTask)
{
newTask = new SearchUrlTask() { AutoReturnFromQuery = false, UrlToSearchFor = searchUrl, ActivateKeyboard = currentSelectTask.ActivateKeyboard };
newTask.ShowUserNotifications = currentSelectTask.ShowUserNotifications;
newTask.ActivateKeyboard = currentSelectTask.ActivateKeyboard;
newTask.CopyTotpToClipboard = currentSelectTask.CopyTotpToClipboard;
}
else
newTask = new SearchUrlTask() { AutoReturnFromQuery = false, UrlToSearchFor = searchUrl, ActivateKeyboard = ActivationCondition.Never };


selectOtherEntry.Click += (sender, e) => {
GroupActivity.Launch(this, newTask, new ActivityLaunchModeRequestCode(0));
Expand All @@ -179,7 +182,7 @@ private void Query()
createUrlEntry.Visibility = ViewStates.Visible;
createUrlEntry.Click += (sender, e) =>
{
GroupActivity.Launch(this, new CreateEntryThenCloseTask { Url = searchUrl, ShowUserNotifications = (AppTask as SelectEntryTask)?.ShowUserNotifications ?? ShowUserNotificationsMode.Always }, new ActivityLaunchModeRequestCode(0));
GroupActivity.Launch(this, new CreateEntryThenCloseTask { Url = searchUrl, ShowUserNotifications = (AppTask as SelectEntryTask)?.ShowUserNotifications ?? ActivationCondition.Always }, new ActivityLaunchModeRequestCode(0));
Toast.MakeText(this, GetString(Resource.String.select_group_then_add, new Java.Lang.Object[] { GetString(Resource.String.add_entry) }), ToastLength.Long).Show();
};
}
Expand Down
3 changes: 2 additions & 1 deletion src/keepass2android/Totp/Kp2aTotp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ namespace keepass2android
{
class Kp2aTotp
{
public const string TotpKey = "TOTP";

readonly ITotpPluginAdapter[] _pluginAdapters = new ITotpPluginAdapter[]
readonly ITotpPluginAdapter[] _pluginAdapters = new ITotpPluginAdapter[]
{
new TrayTotpPluginAdapter(),
new KeeOtpPluginAdapter(),
Expand Down
2 changes: 1 addition & 1 deletion src/keepass2android/Totp/UpdateTotpTimerTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace PluginTOTP
{
class UpdateTotpTimerTask: TimerTask
{
public const string TotpKey = "TOTP";
public const string TotpKey = Kp2aTotp.TotpKey;
private readonly Context _context;
private readonly ITotpPluginAdapter _adapter;

Expand Down
Loading

0 comments on commit 059280e

Please sign in to comment.