Add keyboard Timeout

This commit is contained in:
J-Jamet committed 2018-10-27 13:56:08 +02:00
1 parent 6cb97de793
commit e8d0d03ad9
6 files changed
+66 -37

No files matched your search

@@ -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();
}
@@ -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();
}
}
@@ -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;
}