Lock since keyboard and refactor

This commit is contained in:
J-Jamet committed 2018-06-08 17:59:18 +02:00
1 parent d8a8005d70
commit b4b215fe30
8 files changed
+50 -138

No files matched your search

@@ -249,6 +249,7 @@ public class Database {
mUri = uri;
}
// TODO Clear database when lock broadcast is receive in backstage
public void clear() {
drawFactory.clearCache();
@@ -1,97 +0,0 @@
/*
* Copyright 2018 Jeremy Jamet / Kunzisoft.
*
* This file is part of KeePass DX.
*
* KeePass DX is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* KeePass DX is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with KeePass DX. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.kunzisoft.keepass.lock;
import android.app.NotificationManager;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
import com.kunzisoft.keepass.app.App;
public class LockService extends Service {
private static final String TAG = LockService.class.getName();
public static final String LOCK_ACTION = "com.kunzisoft.keepass.LOCK";
private BroadcastReceiver mIntentReceiver;
@Override
public void onCreate() {
super.onCreate();
mIntentReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if ( action != null &&
action.equals(LOCK_ACTION)
) {
lock();
}
}
};
IntentFilter filter = new IntentFilter();
filter.addAction(LOCK_ACTION);
registerReceiver(mIntentReceiver, filter);
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Log.d(TAG, "Lock service started");
}
private void lock() {
Log.d(TAG, "Lock");
App.setShutdown();
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (nm != null)
nm.cancelAll();
stopSelf();
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "Lock service stopped");
unregisterReceiver(mIntentReceiver);
}
public class LockBinder extends Binder {
public LockService getService() {
return LockService.this;
}
}
private final IBinder mBinder = new LockBinder();
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
}
@@ -20,6 +20,7 @@
package com.kunzisoft.keepass.lock;
import android.app.Activity;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
@@ -37,11 +38,11 @@ public abstract class LockingActivity extends StylishActivity {
private static final String TAG = LockingActivity.class.getName();
public static final String LOCK_ACTION = "com.kunzisoft.keepass.LOCK";
public static final int RESULT_EXIT_LOCK = 1450;
private static final String AT_LEAST_SECOND_SHOWN_KEY = "AT_LEAST_SECOND_SHOWN_KEY";
private ScreenReceiver screenReceiver;
private LockReceiver lockReceiver;
private boolean exitLock;
@@ -62,29 +63,36 @@ public abstract class LockingActivity extends StylishActivity {
super.onCreate(savedInstanceState);
if (PreferencesUtil.isLockDatabaseWhenScreenShutOffEnable(this)) {
screenReceiver = new ScreenReceiver();
registerReceiver(screenReceiver, new IntentFilter((Intent.ACTION_SCREEN_OFF)));
lockReceiver = new LockReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
intentFilter.addAction(LOCK_ACTION);
registerReceiver(lockReceiver, new IntentFilter(intentFilter));
} else
screenReceiver = null;
lockReceiver = null;
exitLock = false;
// WARNING TODO recordTime is not called after a back if was in backstack
}
public static void checkShutdown(Activity act) {
public static void checkShutdown(Activity activity) {
if (App.isShutdown() && App.getDB().getLoaded()) {
Log.i(TAG, "Shutdown " + act.getLocalClassName() +
" after inactivity or manual lock");
act.setResult(RESULT_EXIT_LOCK);
act.finish();
lockAndExit(activity);
}
}
protected void lockAndExit() {
private static void lockAndExit(Activity activity) {
App.setShutdown();
setResult(LockingActivity.RESULT_EXIT_LOCK);
finish();
Log.i(TAG, "Shutdown " + activity.getLocalClassName() +
" after inactivity or manual lock");
NotificationManager nm = (NotificationManager) activity.getSystemService(NOTIFICATION_SERVICE);
if (nm != null)
nm.cancelAll();
activity.setResult(LockingActivity.RESULT_EXIT_LOCK);
activity.finish();
}
protected void lockAndExit() {
lockAndExit(this);
}
@Override
@@ -118,27 +126,25 @@ public abstract class LockingActivity extends StylishActivity {
@Override
protected void onDestroy() {
super.onDestroy();
if(screenReceiver != null)
unregisterReceiver(screenReceiver);
if(lockReceiver != null)
unregisterReceiver(lockReceiver);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean(AT_LEAST_SECOND_SHOWN_KEY, true);
}
public class ScreenReceiver extends BroadcastReceiver {
public class LockReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction() != null) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
if (PreferencesUtil.isLockDatabaseWhenScreenShutOffEnable(LockingActivity.this)) {
App.setShutdown();
checkShutdown(LockingActivity.this);
}
String action = intent.getAction();
if(action != null) {
switch (action) {
case Intent.ACTION_SCREEN_OFF:
if (PreferencesUtil.isLockDatabaseWhenScreenShutOffEnable(LockingActivity.this)) {
lockAndExit();
}
break;
case LOCK_ACTION:
lockAndExit();
break;
}
}
}
@@ -28,7 +28,7 @@ import android.preference.PreferenceManager;
import android.util.Log;
import com.kunzisoft.keepass.R;
import com.kunzisoft.keepass.lock.LockService;
import com.kunzisoft.keepass.lock.LockingActivity;
public class Timeout {
@@ -37,7 +37,7 @@ public class Timeout {
private static String TAG = "KeePass Timeout";
private static PendingIntent buildIntent(Context ctx) {
Intent intent = new Intent(LockService.LOCK_ACTION);
Intent intent = new Intent(LockingActivity.LOCK_ACTION);
return PendingIntent.getBroadcast(ctx, REQUEST_ID, intent, PendingIntent.FLAG_CANCEL_CURRENT);
}
@@ -56,8 +56,6 @@ public class Timeout {
// No timeout don't start timeout service
return;
}
ctx.startService(new Intent(ctx, LockService.class));
long triggerTime = System.currentTimeMillis() + timeout;
AlarmManager am = (AlarmManager) ctx.getSystemService(Context.ALARM_SERVICE);
@@ -75,8 +73,6 @@ public class Timeout {
if (am != null) {
am.cancel(buildIntent(ctx));
}
ctx.stopService(new Intent(ctx, LockService.class));
}
}