diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index 49cd05c18..1a9f76d8c 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -135,6 +135,10 @@
android:configChanges="orientation|keyboardHidden" />
+
0) {
- // only show notification if password is available
- Notification password = getNotification(Intents.COPY_PASSWORD, R.string.copy_password);
- mNM.notify(NOTIFY_PASSWORD, password);
- }
-
- if (mEntry.getUsername().length() > 0) {
- // only show notification if username is available
- Notification username = getNotification(Intents.COPY_USERNAME, R.string.copy_username);
- mNM.notify(NOTIFY_USERNAME, username);
- }
+ if (mEntry.getPassword().length() > 0) {
+ if (mEntry.getUsername().length() > 0) {
+ // username already copied, waiting for user's action before copy password.
+ Intent intent = new Intent(this, NotificationCopingService.class);
+ intent.setAction(NotificationCopingService.ACTION_NEW_NOTIFICATION);
+ intent.putExtra(NotificationCopingService.EXTRA_PASSWORD, mEntry.getPassword());
+ intent.putExtra(NotificationCopingService.EXTRA_USERNAME, mEntry.getUsername());
+ if (mEntry.getTitle() != null)
+ intent.putExtra(NotificationCopingService.EXTRA_ENTRY_TITLE, mEntry.getTitle());
+ startService(intent);
+ } else {
+ // username not copied, copy password immediately.
+ Intent intent = new Intent(this, NotificationCopingService.class);
+ intent.setAction(NotificationCopingService.ACTION_COPY_PASSWORD);
+ intent.putExtra(NotificationCopingService.EXTRA_PASSWORD, mEntry.getPassword());
+ startService(intent);
+ }
+ }
}
mIntentReceiver = new BroadcastReceiver() {
@@ -209,35 +206,9 @@ public class EntryActivity extends LockingHideActivity {
unregisterReceiver(mIntentReceiver);
}
- if ( mNM != null ) {
- try {
- mNM.cancelAll();
- } catch (SecurityException e) {
- // Some android devices give a SecurityException when trying to cancel notifications without the WAKE_LOCK permission,
- // we'll ignore these.
- }
- }
-
super.onDestroy();
}
- private Notification getNotification(String intentText, int descResId) {
-
- String desc = getString(descResId);
-
- Intent intent = new Intent(intentText);
- PendingIntent pending = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
-
- // no longer supported for api level >22
- // notify.setLatestEventInfo(this, getString(R.string.app_name), desc, pending);
- // so instead using compat builder and create new notification
- NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
- Notification notify = builder.setContentIntent(pending).setContentText(desc).setContentTitle(getString(R.string.app_name))
- .setSmallIcon(R.drawable.notify).setTicker(desc).setWhen(System.currentTimeMillis()).build();
-
- return notify;
- }
-
private void populateTitle(Drawable drawIcon, String text) {
titleIconView.setImageDrawable(drawIcon);
titleView.setText(text);
diff --git a/app/src/main/java/com/keepassdroid/services/NotificationCopingService.java b/app/src/main/java/com/keepassdroid/services/NotificationCopingService.java
new file mode 100755
index 000000000..25e237310
--- /dev/null
+++ b/app/src/main/java/com/keepassdroid/services/NotificationCopingService.java
@@ -0,0 +1,184 @@
+package com.keepassdroid.services;
+
+import android.app.Notification;
+import android.app.NotificationChannel;
+import android.app.NotificationManager;
+import android.app.PendingIntent;
+import android.app.Service;
+import android.content.ClipData;
+import android.content.ClipboardManager;
+import android.content.Intent;
+import android.content.SharedPreferences;
+import android.os.Build;
+import android.os.IBinder;
+import android.preference.PreferenceManager;
+import android.support.v4.app.NotificationCompat;
+import android.util.Log;
+import android.widget.Toast;
+
+import com.kunzisoft.keepass.R;
+
+public class NotificationCopingService extends Service {
+ private static final String TAG = NotificationCopingService.class.getName();
+ private static final String CHANNEL_ID_COPYING = "CHANNEL_ID_COPYING";
+ private static final String CHANNEL_NAME_COPYING = "Copy fields";
+ public static final String ACTION_CLEAN_CLIPBOARD = "ACTION_CLEAN_CLIPBOARD";
+ public static final String ACTION_COPY_PASSWORD = "ACTION_COPY_PASSWORD";
+ public static final String ACTION_NEW_NOTIFICATION = "ACTION_NEW_NOTIFICATION";
+ public static final String EXTRA_PASSWORD = "EXTRA_PASSWORD";
+ public static final String EXTRA_USERNAME = "EXTRA_USERNAME";
+ public static final String EXTRA_ENTRY_TITLE = "EXTRA_ENTRY_TITLE";
+
+ private NotificationManager notificationManager;
+ private ClipboardManager clipboardManager;
+ private Thread cleanNotificationTimer;
+ private Thread countingDownTask;
+ private int notificationId = 1;
+ private long notificationTimeoutMilliSecs;
+
+ public NotificationCopingService() {
+ }
+
+ @Override
+ public IBinder onBind(Intent intent) {
+ return null;
+ }
+
+ @Override
+ public void onCreate() {
+ super.onCreate();
+ notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
+ clipboardManager = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
+
+ // Create notification channel for Oreo+
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
+ NotificationChannel channel = new NotificationChannel(CHANNEL_ID_COPYING,
+ CHANNEL_NAME_COPYING,
+ NotificationManager.IMPORTANCE_LOW);
+ notificationManager.createNotificationChannel(channel);
+ }
+ }
+
+ @Override
+ public int onStartCommand(Intent intent, int flags, int startId) {
+ //Get settings
+ SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
+ String sClipClear = prefs.getString(getString(R.string.clipboard_timeout_key),
+ getString(R.string.clipboard_timeout_default));
+ notificationTimeoutMilliSecs = Long.parseLong(sClipClear);
+
+ if (intent == null) {
+ Log.w(TAG, "null intent");
+ } else if (ACTION_NEW_NOTIFICATION.equals(intent.getAction())) {
+ String title = intent.getStringExtra(EXTRA_ENTRY_TITLE);
+ String username = intent.getStringExtra(EXTRA_USERNAME);
+ String password = intent.getStringExtra(EXTRA_PASSWORD);
+ if (password == null) {
+ Log.e(TAG, "password is null");
+ return START_NOT_STICKY;
+ }
+ newNotification(title, username, password);
+ } else if (ACTION_COPY_PASSWORD.equals(intent.getAction())) {
+ String password = intent.getStringExtra(EXTRA_PASSWORD);
+ copyPassword(password);
+ } else if (ACTION_CLEAN_CLIPBOARD.equals(intent.getAction())) {
+ stopTask(countingDownTask);
+ cleanPassword();
+ } else {
+ Log.w(TAG, "unknown action");
+ }
+ return START_NOT_STICKY;
+ }
+
+ private PendingIntent getCopyPasswordPendingIntent(String field) {
+ Intent copyIntent = new Intent(this, NotificationCopingService.class);
+ copyIntent.setAction(ACTION_COPY_PASSWORD);
+ copyIntent.putExtra(EXTRA_PASSWORD, field);
+ return PendingIntent.getService(
+ this, 0, copyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
+ }
+
+ private void newNotification(String title, String username, String password) {
+ stopTask(countingDownTask);
+
+ copyToClipboard(getString(R.string.entry_user_name), username);
+ NotificationCompat.Builder builderPassword = new NotificationCompat.Builder(this, CHANNEL_ID_COPYING)
+ .setSmallIcon(R.drawable.notify)
+ .setContentTitle(createNotificationTitle(title));
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
+ builderPassword.setVisibility(Notification.VISIBILITY_SECRET);
+ builderPassword.setContentText(getString(R.string.copy_password));
+ builderPassword.setContentIntent(getCopyPasswordPendingIntent(password));
+
+ notificationManager.cancel(notificationId);
+ notificationManager.notify(++notificationId, builderPassword.build());
+
+ int myNotificationId = notificationId;
+ stopTask(cleanNotificationTimer);
+ cleanNotificationTimer = new Thread(() -> {
+ try {
+ Thread.sleep(notificationTimeoutMilliSecs);
+ } catch (InterruptedException e) {
+ cleanNotificationTimer = null;
+ return;
+ }
+ cleanPassword();
+ notificationManager.cancel(myNotificationId);
+ });
+ cleanNotificationTimer.start();
+ }
+
+ private String createNotificationTitle(String title) {
+ return getString(R.string.app_name) + " (" + title + ")";
+ }
+
+ private void copyPassword(String password) {
+ stopTask(cleanNotificationTimer);
+
+ String labelPassword = getString(R.string.password);
+ copyToClipboard(labelPassword, password);
+ NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID_COPYING)
+ .setSmallIcon(R.drawable.ic_key_white_24dp)
+ .setContentTitle(getString(R.string.copy_field, labelPassword))
+ .setContentText(getString(R.string.clipboard_swipe_clean));
+
+ Intent cleanIntent = new Intent(this, NotificationCopingService.class);
+ cleanIntent.setAction(ACTION_CLEAN_CLIPBOARD);
+ PendingIntent cleanPendingIntent = PendingIntent.getService(
+ this, 0, cleanIntent, PendingIntent.FLAG_UPDATE_CURRENT);
+ builder.setDeleteIntent(cleanPendingIntent);
+
+ int myNotificationId = notificationId;
+ countingDownTask = new Thread(() -> {
+ int maxPos = 500;
+ long posDurationMills = notificationTimeoutMilliSecs / maxPos;
+ for (int pos = maxPos; pos > 0; --pos) {
+ builder.setProgress(maxPos, pos, false);
+ notificationManager.notify(myNotificationId, builder.build());
+ try {
+ Thread.sleep(posDurationMills);
+ } catch (InterruptedException e) {
+ break;
+ }
+ }
+ countingDownTask = null;
+ notificationManager.cancel(myNotificationId);
+ cleanPassword();
+ });
+ countingDownTask.start();
+ }
+
+ private void copyToClipboard(String name, String value) {
+ clipboardManager.setPrimaryClip(ClipData.newPlainText(name, value));
+ Toast.makeText(this, getString(R.string.copy_field, name), Toast.LENGTH_SHORT).show();
+ }
+
+ private void cleanPassword() {
+ clipboardManager.setPrimaryClip(ClipData.newPlainText("",""));
+ }
+
+ private void stopTask(Thread task) {
+ if (task != null && task.isAlive())
+ task.interrupt();
+ }
+}
diff --git a/app/src/main/java/com/keepassdroid/timers/Timeout.java b/app/src/main/java/com/keepassdroid/timers/Timeout.java
index 9106d3312..a468b0723 100644
--- a/app/src/main/java/com/keepassdroid/timers/Timeout.java
+++ b/app/src/main/java/com/keepassdroid/timers/Timeout.java
@@ -8,9 +8,9 @@ import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;
-import com.kunzisoft.keepass.R;
import com.keepassdroid.intents.Intents;
import com.keepassdroid.services.TimeoutService;
+import com.kunzisoft.keepass.R;
public class Timeout {
private static final int REQUEST_ID = 0;
@@ -25,8 +25,6 @@ public class Timeout {
}
public static void start(Context ctx) {
-
-
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ctx);
String sTimeout = prefs.getString(ctx.getString(R.string.app_timeout_key), ctx.getString(R.string.clipboard_timeout_default));
@@ -58,7 +56,6 @@ public class Timeout {
am.cancel(buildIntent(ctx));
ctx.stopService(new Intent(ctx, TimeoutService.class));
-
}
}
diff --git a/app/src/main/java/com/keepassdroid/utils/Util.java b/app/src/main/java/com/keepassdroid/utils/Util.java
index 3a060441b..d9567d828 100644
--- a/app/src/main/java/com/keepassdroid/utils/Util.java
+++ b/app/src/main/java/com/keepassdroid/utils/Util.java
@@ -20,11 +20,11 @@
package com.keepassdroid.utils;
import android.content.ActivityNotFoundException;
+import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.net.Uri;
-import android.text.ClipboardManager;
import android.widget.TextView;
import com.keepassdroid.database.exception.SamsungClipboardException;
diff --git a/app/src/main/res/values-fr/strings.xml b/app/src/main/res/values-fr/strings.xml
index 808b22e8a..0ce7ce08d 100644
--- a/app/src/main/res/values-fr/strings.xml
+++ b/app/src/main/res/values-fr/strings.xml
@@ -48,6 +48,7 @@
Le vidage du presse-papier a échoué
Clipboard timeout
Temps avant le vidage du presse-papier après copie du nom d\'utilisateur ou du mot de passe
+ Balayez pour effacer le presse-papiers maintenant
Copier le nom d\'utilisateur dans le presse-papier
Copier le mot de passe dans le presse-papier
Création de la clé de base de données…
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
index fb95b33d7..9e75999ce 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -46,6 +46,7 @@
Clearing the clipboard failed
Clipboard timeout
Time before clearing clipboard after copying username or password
+ Swipe to clean clipboard now
Select to copy username to clipboard
Select to copy password to clipboard
Creating database key…