From e8d0d03ad9f360a1bcb039bc56dbf2a88e44b5e9 Mon Sep 17 00:00:00 2001 From: J-Jamet Date: Sat, 27 Oct 2018 13:56:08 +0200 Subject: [PATCH] Add keyboard Timeout --- .../KeyboardEntryNotificationService.java | 71 ++++++++++++------- .../keepass/magikeyboard/MagikIME.java | 8 ++- .../keepass/timeout/TimeoutHelper.java | 11 +-- app/src/main/res/values/donottranslate.xml | 2 +- .../main/res/xml/application_preferences.xml | 2 +- app/src/main/res/xml/keyboard_preferences.xml | 9 ++- 6 files changed, 66 insertions(+), 37 deletions(-) diff --git a/app/src/main/java/com/kunzisoft/keepass/magikeyboard/KeyboardEntryNotificationService.java b/app/src/main/java/com/kunzisoft/keepass/magikeyboard/KeyboardEntryNotificationService.java index 6a540197a..602f02fb2 100644 --- a/app/src/main/java/com/kunzisoft/keepass/magikeyboard/KeyboardEntryNotificationService.java +++ b/app/src/main/java/com/kunzisoft/keepass/magikeyboard/KeyboardEntryNotificationService.java @@ -7,20 +7,24 @@ import android.app.Service; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; +import android.content.SharedPreferences; import android.os.Build; import android.os.IBinder; import android.support.v4.app.NotificationCompat; +import android.support.v7.preference.PreferenceManager; import android.util.Log; import com.kunzisoft.keepass.R; import com.kunzisoft.keepass.magikeyboard.receiver.LockBroadcastReceiver; import com.kunzisoft.keepass.magikeyboard.receiver.NotificationDeleteBroadcastReceiver; +import com.kunzisoft.keepass.timeout.TimeoutHelper; -import static android.content.ContentValues.TAG; import static com.kunzisoft.keepass.magikeyboard.receiver.LockBroadcastReceiver.LOCK_ACTION; public class KeyboardEntryNotificationService extends Service { + private static final String TAG = KeyboardEntryNotificationService.class.getName(); + private static final String CHANNEL_ID_KEYBOARD = "com.kunzisoft.keyboard.notification.entry.channel"; private static final String CHANNEL_NAME_KEYBOARD = "Magikeyboard notification"; @@ -100,30 +104,40 @@ public class KeyboardEntryNotificationService extends Service { notificationManager.cancel(notificationId); notificationManager.notify(notificationId, builder.build()); - } - // TODO Get timeout - /* - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); - notificationTimeoutMilliSecs = prefs.getInt(getString(R.string.entry_timeout_key), 100000); - */ - - /* - stopTask(cleanNotificationTimer); - cleanNotificationTimer = new Thread(new Runnable() { - @Override - public void run() { - try { - Thread.sleep(notificationTimeoutMilliSecs); - } catch (InterruptedException e) { - cleanNotificationTimer = null; - return; - } - notificationManager.cancel(notificationId); + // Timeout + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); + String keyboardTimeout = prefs.getString(getString(R.string.keyboard_entry_timeout_key), + getString(R.string.timeout_default)); + try { + notificationTimeoutMilliSecs = Long.parseLong(keyboardTimeout); + } catch (NumberFormatException e) { + notificationTimeoutMilliSecs = TimeoutHelper.DEFAULT_TIMEOUT; } - }); - cleanNotificationTimer.start(); - */ + + if (notificationTimeoutMilliSecs != TimeoutHelper.TIMEOUT_NEVER) { + stopTask(cleanNotificationTimer); + cleanNotificationTimer = new Thread(() -> { + int maxPos = 100; + long posDurationMills = notificationTimeoutMilliSecs / maxPos; + for (int pos = maxPos; pos > 0; --pos) { + builder.setProgress(maxPos, pos, false); + notificationManager.notify(notificationId, builder.build()); + try { + Thread.sleep(posDurationMills); + } catch (InterruptedException e) { + break; + } + } + try { + pendingDeleteIntent.send(); + } catch (PendingIntent.CanceledException e) { + Log.e(TAG, e.getLocalizedMessage()); + } + }); + cleanNotificationTimer.start(); + } + } } private void stopTask(Thread task) { @@ -131,12 +145,19 @@ public class KeyboardEntryNotificationService extends Service { task.interrupt(); } - @Override - public void onDestroy() { + private void destroyKeyboardNotification() { + stopTask(cleanNotificationTimer); + cleanNotificationTimer = null; unregisterReceiver(lockBroadcastReceiver); pendingDeleteIntent.cancel(); notificationManager.cancel(notificationId); + } + + @Override + public void onDestroy() { + + destroyKeyboardNotification(); super.onDestroy(); } diff --git a/app/src/main/java/com/kunzisoft/keepass/magikeyboard/MagikIME.java b/app/src/main/java/com/kunzisoft/keepass/magikeyboard/MagikIME.java index a77aba062..4fb7c1b55 100644 --- a/app/src/main/java/com/kunzisoft/keepass/magikeyboard/MagikIME.java +++ b/app/src/main/java/com/kunzisoft/keepass/magikeyboard/MagikIME.java @@ -143,8 +143,10 @@ public class MagikIME extends InputMethodService if (keyboard_entry != null) keyboardView.setKeyboard(keyboard_entry); } else { - if (keyboard != null) + if (keyboard != null) { + dismissCustomKeys(); keyboardView.setKeyboard(keyboard); + } } } } @@ -192,7 +194,6 @@ public class MagikIME extends InputMethodService if (imeManager != null) imeManager.showInputMethodPicker(); } - // TODO Add a long press to choose the keyboard break; case KEY_CHANGE_KEYBOARD: InputMethodManager imeManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); @@ -203,6 +204,7 @@ public class MagikIME extends InputMethodService // TODO Unlock key break; case KEY_ENTRY: + deleteEntryKey(this); Intent intent = new Intent(this, EntryRetrieverActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); @@ -285,8 +287,8 @@ public class MagikIME extends InputMethodService @Override public void onDestroy() { - unregisterReceiver(lockBroadcastReceiver); dismissCustomKeys(); + unregisterReceiver(lockBroadcastReceiver); super.onDestroy(); } } \ No newline at end of file diff --git a/app/src/main/java/com/kunzisoft/keepass/timeout/TimeoutHelper.java b/app/src/main/java/com/kunzisoft/keepass/timeout/TimeoutHelper.java index a9e7f51f0..f37c07bd1 100644 --- a/app/src/main/java/com/kunzisoft/keepass/timeout/TimeoutHelper.java +++ b/app/src/main/java/com/kunzisoft/keepass/timeout/TimeoutHelper.java @@ -31,8 +31,9 @@ public class TimeoutHelper { private static final String TAG = "TimeoutHelper"; - private static final long DEFAULT_TIMEOUT = 5 * 60 * 1000; // 5 minutes - + public static final long DEFAULT_TIMEOUT = 5 * 60 * 1000; // 5 minutes + public static final long TIMEOUT_NEVER = -1; // Infinite + public static void recordTime(Activity act) { // Record timeout time in case timeout service is killed long time = System.currentTimeMillis(); @@ -56,9 +57,9 @@ public class TimeoutHelper { long cur_time = System.currentTimeMillis(); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(act); - long timeout_start = prefs.getLong(act.getString(R.string.timeout_key), -1); + long timeout_start = prefs.getLong(act.getString(R.string.timeout_key), TIMEOUT_NEVER); // The timeout never started - if (timeout_start == -1) { + if (timeout_start == TIMEOUT_NEVER) { return true; } @@ -71,7 +72,7 @@ public class TimeoutHelper { } // We are set to never timeout - if (timeout == -1) { + if (timeout == TIMEOUT_NEVER) { return true; } diff --git a/app/src/main/res/values/donottranslate.xml b/app/src/main/res/values/donottranslate.xml index 05acfad4e..83164db6e 100644 --- a/app/src/main/res/values/donottranslate.xml +++ b/app/src/main/res/values/donottranslate.xml @@ -144,7 +144,7 @@ true false - 300000 + 300000 60000 diff --git a/app/src/main/res/xml/application_preferences.xml b/app/src/main/res/xml/application_preferences.xml index c445cea78..152e7c253 100644 --- a/app/src/main/res/xml/application_preferences.xml +++ b/app/src/main/res/xml/application_preferences.xml @@ -57,7 +57,7 @@ android:entries="@array/timeout_options" android:entryValues="@array/timeout_values" android:dialogTitle="@string/app_timeout" - android:defaultValue="@string/app_timeout_default"/> + android:defaultValue="@string/timeout_default"/> - + android:entries="@array/timeout_options" + android:entryValues="@array/timeout_values" + android:dialogTitle="@string/keyboard_entry_timeout_title" + android:defaultValue="@string/timeout_default"/> +