From 5b830e20368c5fb95be4b3652b98fd2efc8d0c56 Mon Sep 17 00:00:00 2001 From: Daniel Weigl Date: Thu, 19 Nov 2020 19:41:10 +0100 Subject: [PATCH] Adds feature to share the current OTP via an Intent with the special mime-type "text/keystrokes" Other apps (like https://invent.kde.org/network/kdeconnect-android/-/merge_requests/220 ) could listen for this intents and pass the date on to a connected desktop or other device where the 2FA code is needed --- .../flocke/andotp/Utilities/Constants.java | 2 +- .../andotp/View/EntriesCardAdapter.java | 25 +++++++++++++++++++ app/src/main/res/menu/menu_popup.xml | 4 +++ app/src/main/res/values-de/strings_main.xml | 2 ++ .../main/res/values-de/strings_settings.xml | 1 + app/src/main/res/values/settings.xml | 1 + app/src/main/res/values/strings_main.xml | 1 + app/src/main/res/values/strings_settings.xml | 1 + 8 files changed, 36 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/org/shadowice/flocke/andotp/Utilities/Constants.java b/app/src/main/java/org/shadowice/flocke/andotp/Utilities/Constants.java index d57c55b6..ee5bacf6 100644 --- a/app/src/main/java/org/shadowice/flocke/andotp/Utilities/Constants.java +++ b/app/src/main/java/org/shadowice/flocke/andotp/Utilities/Constants.java @@ -61,7 +61,7 @@ public class Constants { } public enum TapMode { - NOTHING, REVEAL, COPY, COPY_BACKGROUND + NOTHING, REVEAL, COPY, COPY_BACKGROUND, SEND_KEYSTROKES } public enum LabelDisplay { diff --git a/app/src/main/java/org/shadowice/flocke/andotp/View/EntriesCardAdapter.java b/app/src/main/java/org/shadowice/flocke/andotp/View/EntriesCardAdapter.java index 52ce4ff9..c7921988 100644 --- a/app/src/main/java/org/shadowice/flocke/andotp/View/EntriesCardAdapter.java +++ b/app/src/main/java/org/shadowice/flocke/andotp/View/EntriesCardAdapter.java @@ -26,6 +26,7 @@ import android.annotation.SuppressLint; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; +import android.content.Intent; import android.graphics.Bitmap; import android.graphics.drawable.BitmapDrawable; import android.net.Uri; @@ -319,6 +320,10 @@ public class EntriesCardAdapter extends RecyclerView.Adapter establishPinIfNeeded(position); copyHandler(position, text, true); break; + case SEND_KEYSTROKES: + establishPinIfNeeded(position); + sendKeystrokes(position); + break; default: // If tap-to-reveal is disabled a single tab still needs to establish the PIN if (!settings.getTapToReveal()) @@ -342,6 +347,10 @@ public class EntriesCardAdapter extends RecyclerView.Adapter establishPinIfNeeded(position); copyHandler(position, text, true); break; + case SEND_KEYSTROKES: + establishPinIfNeeded(position); + sendKeystrokes(position); + break; default: break; } @@ -671,6 +680,19 @@ public class EntriesCardAdapter extends RecyclerView.Adapter .show(); } + // sends the current OTP code via a "Send Action" with the MIME type "text/x-keystrokes" + // other apps (eg. https://github.com/KDE/kdeconnect-android ) can listen for this and handle + // the current code on their own (eg. sending it to a connected device/browser/...) + private void sendKeystrokes(final int pos) { + String otp = displayedEntries.get(pos).getCurrentOTP(); + Intent sendIntent = new Intent(Intent.ACTION_SEND); + sendIntent.setType("text/x-keystrokes"); + sendIntent.putExtra(Intent.EXTRA_TEXT, otp); + if (sendIntent.resolveActivity(this.context.getPackageManager()) != null) { + this.context.startActivity(sendIntent); + } + } + private void showQRCode(final int pos) { Uri uri = displayedEntries.get(pos).toUri(); if (uri != null) { @@ -729,6 +751,9 @@ public class EntriesCardAdapter extends RecyclerView.Adapter } else if (id == R.id.menu_popup_show_qr_code) { showQRCode(pos); return true; + } else if (id == R.id.menu_send_keystrokes) { + sendKeystrokes(pos); + return true; } else { return false; } diff --git a/app/src/main/res/menu/menu_popup.xml b/app/src/main/res/menu/menu_popup.xml index 95a0088e..541a889b 100644 --- a/app/src/main/res/menu/menu_popup.xml +++ b/app/src/main/res/menu/menu_popup.xml @@ -22,4 +22,8 @@ android:id="@+id/menu_popup_remove" android:title="@string/menu_popup_remove" /> + + \ No newline at end of file diff --git a/app/src/main/res/values-de/strings_main.xml b/app/src/main/res/values-de/strings_main.xml index 751398f3..176ce808 100644 --- a/app/src/main/res/values-de/strings_main.xml +++ b/app/src/main/res/values-de/strings_main.xml @@ -45,6 +45,8 @@ Bild ändern Entfernen Zeige QR-Code + Als Keystrokes teilen + Weitere Optionen Weitere Optionen für %1$s diff --git a/app/src/main/res/values-de/strings_settings.xml b/app/src/main/res/values-de/strings_settings.xml index 84878ba4..6ee40bc8 100644 --- a/app/src/main/res/values-de/strings_settings.xml +++ b/app/src/main/res/values-de/strings_settings.xml @@ -137,6 +137,7 @@ Anzeigen/Verstecken Kopieren Kopieren und in den Hintergrund wechseln + Als Keystrokes teilen Beschriftung diff --git a/app/src/main/res/values/settings.xml b/app/src/main/res/values/settings.xml index 944249e4..30518aa7 100644 --- a/app/src/main/res/values/settings.xml +++ b/app/src/main/res/values/settings.xml @@ -133,6 +133,7 @@ reveal copy copy_background + send_keystrokes diff --git a/app/src/main/res/values/strings_main.xml b/app/src/main/res/values/strings_main.xml index c19999fe..2f067254 100644 --- a/app/src/main/res/values/strings_main.xml +++ b/app/src/main/res/values/strings_main.xml @@ -62,6 +62,7 @@ Remove Show QR Code Enter pin + Share as Keystrokes More options diff --git a/app/src/main/res/values/strings_settings.xml b/app/src/main/res/values/strings_settings.xml index b41f6ee3..61d71ea7 100644 --- a/app/src/main/res/values/strings_settings.xml +++ b/app/src/main/res/values/strings_settings.xml @@ -205,6 +205,7 @@ Reveal/Hide Copy Copy and go to background + Share as Keystrokes