Refactor Lock with Kotlin

This commit is contained in:
J-Jamet committed 2018-12-17 19:00:08 +01:00
1 parent 6372099cb2
commit cbc8ec9880
10 files changed
+367 -412

No files matched your search

@@ -47,13 +47,13 @@ import com.kunzisoft.keepass.database.ExtraFields;
import com.kunzisoft.keepass.database.PwDatabase;
import com.kunzisoft.keepass.database.PwEntry;
import com.kunzisoft.keepass.database.security.ProtectedString;
import com.kunzisoft.keepass.lock.LockingActivity;
import com.kunzisoft.keepass.lock.LockingHideActivity;
import com.kunzisoft.keepass.notifications.NotificationCopyingService;
import com.kunzisoft.keepass.notifications.NotificationField;
import com.kunzisoft.keepass.settings.PreferencesUtil;
import com.kunzisoft.keepass.settings.SettingsAutofillActivity;
import com.kunzisoft.keepass.timeout.ClipboardHelper;
import com.kunzisoft.keepass.timeout.TimeoutHelper;
import com.kunzisoft.keepass.utils.EmptyUtils;
import com.kunzisoft.keepass.utils.MenuUtil;
import com.kunzisoft.keepass.utils.Types;
@@ -85,12 +85,12 @@ public class EntryActivity extends LockingHideActivity {
private int iconColor;
public static void launch(Activity act, PwEntry pw, boolean readOnly) {
if (LockingActivity.checkTimeIsAllowedOrFinish(act)) {
Intent intent = new Intent(act, EntryActivity.class);
public static void launch(Activity activity, PwEntry pw, boolean readOnly) {
if (TimeoutHelper.INSTANCE.checkTime(activity)) {
Intent intent = new Intent(activity, EntryActivity.class);
intent.putExtra(KEY_ENTRY, Types.UUIDtoBytes(pw.getUUID()));
ReadOnlyHelper.putReadOnlyInIntent(intent, readOnly);
act.startActivityForResult(intent, EntryEditActivity.ADD_OR_UPDATE_ENTRY_REQUEST_CODE);
activity.startActivityForResult(intent, EntryEditActivity.ADD_OR_UPDATE_ENTRY_REQUEST_CODE);
}
}
@@ -113,7 +113,7 @@ public class EntryActivity extends LockingHideActivity {
finish();
return;
}
readOnly = db.isReadOnly() || readOnly;
setReadOnly(db.isReadOnly() || getReadOnly());
mShowPassword = !PreferencesUtil.isPasswordMask(this);
@@ -428,7 +428,7 @@ public class EntryActivity extends LockingHideActivity {
inflater.inflate(R.menu.entry, menu);
inflater.inflate(R.menu.database_lock, menu);
if (readOnly) {
if (getReadOnly()) {
MenuItem edit = menu.findItem(R.id.menu_edit);
if (edit != null)
edit.setVisible(false);
@@ -56,11 +56,11 @@ import com.kunzisoft.keepass.database.action.node.UpdateEntryRunnable;
import com.kunzisoft.keepass.database.security.ProtectedString;
import com.kunzisoft.keepass.dialogs.GeneratePasswordDialogFragment;
import com.kunzisoft.keepass.dialogs.IconPickerDialogFragment;
import com.kunzisoft.keepass.lock.LockingActivity;
import com.kunzisoft.keepass.lock.LockingHideActivity;
import com.kunzisoft.keepass.settings.PreferencesUtil;
import com.kunzisoft.keepass.tasks.SaveDatabaseProgressTaskDialogFragment;
import com.kunzisoft.keepass.tasks.UpdateProgressTaskStatus;
import com.kunzisoft.keepass.timeout.TimeoutHelper;
import com.kunzisoft.keepass.utils.MenuUtil;
import com.kunzisoft.keepass.utils.Types;
import com.kunzisoft.keepass.utils.Util;
@@ -113,28 +113,28 @@ public class EntryEditActivity extends LockingHideActivity
/**
* Launch EntryEditActivity to update an existing entry
*
* @param act from activity
* @param pw Entry to update
* @param activity from activity
* @param pwEntry Entry to update
*/
public static void launch(Activity act, PwEntry pw) {
if (LockingActivity.checkTimeIsAllowedOrFinish(act)) {
Intent intent = new Intent(act, EntryEditActivity.class);
intent.putExtra(KEY_ENTRY, Types.UUIDtoBytes(pw.getUUID()));
act.startActivityForResult(intent, ADD_OR_UPDATE_ENTRY_REQUEST_CODE);
public static void launch(Activity activity, PwEntry pwEntry) {
if (TimeoutHelper.INSTANCE.checkTime(activity)) {
Intent intent = new Intent(activity, EntryEditActivity.class);
intent.putExtra(KEY_ENTRY, Types.UUIDtoBytes(pwEntry.getUUID()));
activity.startActivityForResult(intent, ADD_OR_UPDATE_ENTRY_REQUEST_CODE);
}
}
/**
* Launch EntryEditActivity to add a new entry
*
* @param act from activity
* @param activity from activity
* @param pwGroup Group who will contains new entry
*/
public static void launch(Activity act, PwGroup pwGroup) {
if (LockingActivity.checkTimeIsAllowedOrFinish(act)) {
Intent intent = new Intent(act, EntryEditActivity.class);
public static void launch(Activity activity, PwGroup pwGroup) {
if (TimeoutHelper.INSTANCE.checkTime(activity)) {
Intent intent = new Intent(activity, EntryEditActivity.class);
intent.putExtra(KEY_PARENT, pwGroup.getId());
act.startActivityForResult(intent, ADD_OR_UPDATE_ENTRY_REQUEST_CODE);
activity.startActivityForResult(intent, ADD_OR_UPDATE_ENTRY_REQUEST_CODE);
}
}
@@ -87,6 +87,7 @@ import com.kunzisoft.keepass.settings.PreferencesUtil;
import com.kunzisoft.keepass.tasks.SaveDatabaseProgressTaskDialogFragment;
import com.kunzisoft.keepass.tasks.UIToastTask;
import com.kunzisoft.keepass.tasks.UpdateProgressTaskStatus;
import com.kunzisoft.keepass.timeout.TimeoutHelper;
import com.kunzisoft.keepass.utils.MenuUtil;
import com.kunzisoft.keepass.view.AddNodeButtonView;
@@ -144,13 +145,13 @@ public class GroupActivity extends LockingActivity
}
public static void launch(Activity act, boolean readOnly) {
startRecordTime(act);
TimeoutHelper.INSTANCE.recordTime(act);
launch(act, null, readOnly);
}
private static void buildAndLaunchIntent(Activity activity, PwGroup group, boolean readOnly,
IntentBuildLauncher intentBuildLauncher) {
if (checkTimeIsAllowedOrFinish(activity)) {
if (TimeoutHelper.INSTANCE.checkTime(activity)) {
Intent intent = new Intent(activity, GroupActivity.class);
if (group != null) {
intent.putExtra(GROUP_ID_KEY, group.getId());
@@ -165,9 +166,9 @@ public class GroupActivity extends LockingActivity
(intent) -> activity.startActivityForResult(intent, 0));
}
public static void launchForKeyboardResult(Activity act, boolean readOnly) {
startRecordTime(act);
launchForKeyboardResult(act, null, readOnly);
public static void launchForKeyboardResult(Activity activity, boolean readOnly) {
TimeoutHelper.INSTANCE.recordTime(activity);
launchForKeyboardResult(activity, null, readOnly);
}
public static void launchForKeyboardResult(Activity activity, PwGroup group, boolean readOnly) {
@@ -179,12 +180,12 @@ public class GroupActivity extends LockingActivity
}
@RequiresApi(api = Build.VERSION_CODES.O)
public static void launchForAutofillResult(Activity act, AssistStructure assistStructure, boolean readOnly) {
public static void launchForAutofillResult(Activity activity, AssistStructure assistStructure, boolean readOnly) {
if ( assistStructure != null ) {
startRecordTime(act);
launchForAutofillResult(act, null, assistStructure, readOnly);
TimeoutHelper.INSTANCE.recordTime(activity);
launchForAutofillResult(activity, null, assistStructure, readOnly);
} else {
launch(act, readOnly);
launch(activity, readOnly);
}
}
@@ -231,7 +232,7 @@ public class GroupActivity extends LockingActivity
invalidateOptionsMenu();
// Get arg from intent or instance state
readOnly = ReadOnlyHelper.retrieveReadOnlyFromInstanceStateOrIntent(savedInstanceState, getIntent());
setReadOnly(ReadOnlyHelper.retrieveReadOnlyFromInstanceStateOrIntent(savedInstanceState, getIntent()));
// Retrieve elements after an orientation change
if (savedInstanceState != null) {
@@ -282,7 +283,7 @@ public class GroupActivity extends LockingActivity
listNodesFragment = (ListNodesFragment) getSupportFragmentManager()
.findFragmentByTag(fragmentTag);
if (listNodesFragment == null)
listNodesFragment = ListNodesFragment.newInstance(mCurrentGroup, readOnly, currentGroupIsASearch);
listNodesFragment = ListNodesFragment.newInstance(mCurrentGroup, getReadOnly(), currentGroupIsASearch);
// Attach fragment to content view
getSupportFragmentManager().beginTransaction().replace(
@@ -340,35 +341,34 @@ public class GroupActivity extends LockingActivity
}
private void openGroup(PwGroup group, boolean isASearch) {
// Check Timeout
if (checkTimeIsAllowedOrFinish(this)) {
startRecordTime(this);
// Check TimeoutHelper
TimeoutHelper.INSTANCE.touchToReinitTime(this, () -> {
// Open a group in a new fragment
ListNodesFragment newListNodeFragment = ListNodesFragment.newInstance(group, getReadOnly(), isASearch);
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
// Different animation
String fragmentTag;
if (isASearch) {
fragmentTransaction.setCustomAnimations(R.anim.slide_in_top, R.anim.slide_out_bottom,
R.anim.slide_in_bottom, R.anim.slide_out_top);
fragmentTag = SEARCH_FRAGMENT_TAG;
} else {
fragmentTransaction.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left,
R.anim.slide_in_left, R.anim.slide_out_right);
fragmentTag = LIST_NODES_FRAGMENT_TAG;
}
// Open a group in a new fragment
ListNodesFragment newListNodeFragment = ListNodesFragment.newInstance(group, readOnly, isASearch);
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
// Different animation
String fragmentTag;
if (isASearch) {
fragmentTransaction.setCustomAnimations(R.anim.slide_in_top, R.anim.slide_out_bottom,
R.anim.slide_in_bottom, R.anim.slide_out_top);
fragmentTag = SEARCH_FRAGMENT_TAG;
} else {
fragmentTransaction.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left,
R.anim.slide_in_left, R.anim.slide_out_right);
fragmentTag = LIST_NODES_FRAGMENT_TAG;
}
fragmentTransaction.replace(R.id.nodes_list_fragment_container,
newListNodeFragment,
fragmentTag);
fragmentTransaction.addToBackStack(fragmentTag);
fragmentTransaction.commit();
fragmentTransaction.replace(R.id.nodes_list_fragment_container,
newListNodeFragment,
fragmentTag);
fragmentTransaction.addToBackStack(fragmentTag);
fragmentTransaction.commit();
listNodesFragment = newListNodeFragment;
mCurrentGroup = group;
assignGroupViewElements();
}
listNodesFragment = newListNodeFragment;
mCurrentGroup = group;
assignGroupViewElements();
return null;
});
}
@Override
@@ -380,7 +380,7 @@ public class GroupActivity extends LockingActivity
outState.putParcelable(NODE_TO_COPY_KEY, nodeToCopy);
if (nodeToMove != null)
outState.putParcelable(NODE_TO_MOVE_KEY, nodeToMove);
ReadOnlyHelper.onSaveInstanceState(outState, readOnly);
ReadOnlyHelper.onSaveInstanceState(outState, getReadOnly());
super.onSaveInstanceState(outState);
}
@@ -401,7 +401,7 @@ public class GroupActivity extends LockingActivity
pwGroupId = intent.getParcelableExtra(GROUP_ID_KEY);
}
readOnly = database.isReadOnly() || readOnly; // Force read only if the database is like that
setReadOnly(database.isReadOnly() || getReadOnly()); // Force read only if the database is like that
Log.w(TAG, "Creating tree view");
PwGroup currentGroup;
@@ -463,8 +463,8 @@ public class GroupActivity extends LockingActivity
if (addNodeButtonView != null) {
// To enable add button
boolean addGroupEnabled = !readOnly && !currentGroupIsASearch;
boolean addEntryEnabled = !readOnly && !currentGroupIsASearch;
boolean addGroupEnabled = !getReadOnly() && !currentGroupIsASearch;
boolean addEntryEnabled = !getReadOnly() && !currentGroupIsASearch;
if (mCurrentGroup != null) {
boolean isRoot = (mCurrentGroup == rootGroup);
if (!mCurrentGroup.allowAddEntryIfIsRoot())
@@ -524,7 +524,7 @@ public class GroupActivity extends LockingActivity
openChildGroup((PwGroup) node);
break;
case ENTRY:
EntryActivity.launch(this, (PwEntry) node, readOnly);
EntryActivity.launch(this, (PwEntry) node, getReadOnly());
break;
}
}
@@ -858,7 +858,7 @@ public class GroupActivity extends LockingActivity
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.search, menu);
if (!readOnly)
if (!getReadOnly())
inflater.inflate(R.menu.database_master_key, menu);
inflater.inflate(R.menu.database_lock, menu);
@@ -945,7 +945,7 @@ public class GroupActivity extends LockingActivity
return true;
case R.id.menu_lock:
lockAndExit();
lockAndExit();
return true;
case R.id.menu_change_master_key:
@@ -953,7 +953,7 @@ public class GroupActivity extends LockingActivity
return true;
default:
// Check the time lock before launching settings
MenuUtil.onDefaultMenuOptionsItemSelected(this, item, readOnly, true);
MenuUtil.onDefaultMenuOptionsItemSelected(this, item, getReadOnly(), true);
return super.onOptionsItemSelected(item);
}
}
@@ -1197,17 +1197,13 @@ public class GroupActivity extends LockingActivity
@Override
public void onBackPressed() {
if (checkTimeIsAllowedOrFinish(this)) {
startRecordTime(this);
super.onBackPressed();
super.onBackPressed();
listNodesFragment = (ListNodesFragment) getSupportFragmentManager().findFragmentByTag(LIST_NODES_FRAGMENT_TAG);
// to refresh fragment
listNodesFragment.rebuildList();
mCurrentGroup = listNodesFragment.getMainGroup();
removeSearchInIntent(getIntent());
assignGroupViewElements();
}
listNodesFragment = (ListNodesFragment) getSupportFragmentManager().findFragmentByTag(LIST_NODES_FRAGMENT_TAG);
// to refresh fragment
listNodesFragment.rebuildList();
mCurrentGroup = listNodesFragment.getMainGroup();
removeSearchInIntent(getIntent());
assignGroupViewElements();
}
}
@@ -1,163 +0,0 @@
/*
* Copyright 2017 Brian Pellin, 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.Activity;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import com.kunzisoft.keepass.activities.ReadOnlyHelper;
import com.kunzisoft.keepass.app.App;
import com.kunzisoft.keepass.settings.PreferencesUtil;
import com.kunzisoft.keepass.stylish.StylishActivity;
import com.kunzisoft.keepass.timeout.TimeoutHelper;
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 LockReceiver lockReceiver;
private boolean exitLock;
protected boolean readOnly;
/**
* Called to start a record time,
* Generally used for a first launch or for a fragment change
*/
protected static void startRecordTime(Activity activity) {
TimeoutHelper.recordTime(activity);
}
protected static boolean checkTimeIsAllowedOrFinish(Activity activity) {
return TimeoutHelper.checkTime(activity);
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (PreferencesUtil.isLockDatabaseWhenScreenShutOffEnable(this)) {
lockReceiver = new LockReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
intentFilter.addAction(LOCK_ACTION);
registerReceiver(lockReceiver, new IntentFilter(intentFilter));
} else
lockReceiver = null;
exitLock = false;
readOnly = false;
readOnly = ReadOnlyHelper.retrieveReadOnlyFromInstanceStateOrIntent(savedInstanceState, getIntent());
}
public static void checkShutdown(Activity activity) {
if (App.isShutdown() && App.getDB().getLoaded()) {
lockAndExit(activity);
}
}
private static void lockAndExit(Activity activity) {
App.setShutdown();
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
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_EXIT_LOCK) {
exitLock = true;
checkShutdown(this);
}
}
@Override
protected void onResume() {
super.onResume();
// After the first creation
// or If simply swipe with another application
// If the time is out -> close the Activity
TimeoutHelper.checkTime(this);
// If onCreate already record time
if (!exitLock)
TimeoutHelper.recordTime(this);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
ReadOnlyHelper.onSaveInstanceState(outState, readOnly);
super.onSaveInstanceState(outState);
}
@Override
protected void onPause() {
super.onPause();
// If the time is out during our navigation in activity -> close the Activity
TimeoutHelper.checkTime(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
if(lockReceiver != null)
unregisterReceiver(lockReceiver);
}
public class LockReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
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;
}
}
}
}
}
@@ -0,0 +1,145 @@
/*
* Copyright 2017 Brian Pellin, 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.Activity
import android.app.NotificationManager
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Bundle
import android.util.Log
import com.kunzisoft.keepass.activities.ReadOnlyHelper
import com.kunzisoft.keepass.app.App
import com.kunzisoft.keepass.settings.PreferencesUtil
import com.kunzisoft.keepass.stylish.StylishActivity
import com.kunzisoft.keepass.timeout.TimeoutHelper
abstract class LockingActivity : StylishActivity() {
companion object {
const val LOCK_ACTION = "com.kunzisoft.keepass.LOCK"
const val RESULT_EXIT_LOCK = 1450
}
private var lockReceiver: LockReceiver? = null
private var exitLock: Boolean = false
protected var readOnly: Boolean = false
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (PreferencesUtil.isLockDatabaseWhenScreenShutOffEnable(this)) {
lockReceiver = LockReceiver()
val intentFilter = IntentFilter()
intentFilter.addAction(Intent.ACTION_SCREEN_OFF)
intentFilter.addAction(LOCK_ACTION)
registerReceiver(lockReceiver, IntentFilter(intentFilter))
} else
lockReceiver = null
exitLock = false
readOnly = false
readOnly = ReadOnlyHelper.retrieveReadOnlyFromInstanceStateOrIntent(savedInstanceState, intent)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == RESULT_EXIT_LOCK) {
exitLock = true
checkShutdown()
}
}
override fun onResume() {
super.onResume()
// After the first creation
// or If simply swipe with another application
// If the time is out -> close the Activity
TimeoutHelper.checkTime(this)
// If onCreate already record time
if (!exitLock)
TimeoutHelper.recordTime(this)
}
override fun onSaveInstanceState(outState: Bundle) {
ReadOnlyHelper.onSaveInstanceState(outState, readOnly)
super.onSaveInstanceState(outState)
}
override fun onPause() {
super.onPause()
// If the time is out during our navigation in activity -> close the Activity
TimeoutHelper.checkTime(this)
}
override fun onDestroy() {
super.onDestroy()
if (lockReceiver != null)
unregisterReceiver(lockReceiver)
}
inner class LockReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val action = intent.action
if (action != null) {
when (action) {
Intent.ACTION_SCREEN_OFF -> if (PreferencesUtil.isLockDatabaseWhenScreenShutOffEnable(this@LockingActivity)) {
lockAndExit()
}
LOCK_ACTION -> lockAndExit()
}
}
}
}
protected fun lockAndExit() {
lock()
}
private fun checkShutdown() {
if (App.isShutdown() && App.getDB().loaded) {
lockAndExit()
}
}
override fun onBackPressed() {
TimeoutHelper.touchToReinitTime(this) {
super.onBackPressed()
}
}
}
fun Activity.lock() {
App.setShutdown()
Log.i(Activity::class.java.name, "Shutdown " + localClassName +
" after inactivity or manual lock")
(getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager).apply {
cancelAll()
}
setResult(LockingActivity.RESULT_EXIT_LOCK)
finish()
}
@@ -30,6 +30,7 @@ import android.view.MenuItem;
import com.kunzisoft.keepass.R;
import com.kunzisoft.keepass.activities.ReadOnlyHelper;
import com.kunzisoft.keepass.lock.LockingActivity;
import com.kunzisoft.keepass.timeout.TimeoutHelper;
public class SettingsActivity extends LockingActivity implements MainPreferenceFragment.Callback {
@@ -50,7 +51,7 @@ public class SettingsActivity extends LockingActivity implements MainPreferenceF
// To avoid flickering when launch settings in a LockingActivity
if (!checkLock)
launch(activity, readOnly);
else if (LockingActivity.checkTimeIsAllowedOrFinish(activity)) {
else if (TimeoutHelper.INSTANCE.checkTime(activity)) {
launch(activity, readOnly);
}
}
@@ -116,7 +117,7 @@ public class SettingsActivity extends LockingActivity implements MainPreferenceF
getSupportFragmentManager().beginTransaction()
.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left,
R.anim.slide_in_left, R.anim.slide_out_right)
.replace(R.id.fragment_container, NestedSettingsFragment.newInstance(key, readOnly), TAG_NESTED)
.replace(R.id.fragment_container, NestedSettingsFragment.newInstance(key, getReadOnly()), TAG_NESTED)
.addToBackStack(TAG_NESTED)
.commit();
@@ -1,78 +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.timeout;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;
import com.kunzisoft.keepass.R;
import com.kunzisoft.keepass.lock.LockingActivity;
public class Timeout {
private static final int REQUEST_ID = 0;
private static final long DEFAULT_TIMEOUT = 5 * 60 * 1000; // 5 minutes
private static String TAG = "KeePass Timeout";
private static PendingIntent buildIntent(Context ctx) {
Intent intent = new Intent(LockingActivity.LOCK_ACTION);
return PendingIntent.getBroadcast(ctx, REQUEST_ID, intent, PendingIntent.FLAG_CANCEL_CURRENT);
}
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));
long timeout;
try {
timeout = Long.parseLong(sTimeout);
} catch (NumberFormatException e) {
timeout = DEFAULT_TIMEOUT;
}
if ( timeout == -1 ) {
// No timeout don't start timeout service
return;
}
long triggerTime = System.currentTimeMillis() + timeout;
AlarmManager am = (AlarmManager) ctx.getSystemService(Context.ALARM_SERVICE);
Log.d(TAG, "Timeout start");
if (am != null) {
am.set(AlarmManager.RTC, triggerTime, buildIntent(ctx));
}
}
public static void cancel(Context ctx) {
AlarmManager am = (AlarmManager) ctx.getSystemService(Context.ALARM_SERVICE);
Log.d(TAG, "Timeout cancel");
if (am != null) {
am.cancel(buildIntent(ctx));
}
}
}
@@ -1,91 +0,0 @@
/*
* Copyright 2017 Brian Pellin, 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.timeout;
import android.app.Activity;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import com.kunzisoft.keepass.R;
import com.kunzisoft.keepass.lock.LockingActivity;
import com.kunzisoft.keepass.app.App;
public class TimeoutHelper {
private static final String TAG = "TimeoutHelper";
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();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(act);
SharedPreferences.Editor edit = prefs.edit();
edit.putLong(act.getString(R.string.timeout_key), time);
edit.apply();
if ( App.getDB().getLoaded() ) {
Timeout.start(act);
}
}
public static boolean checkTime(Activity act) {
if ( App.getDB().getLoaded() ) {
Timeout.cancel(act);
}
// Check whether the timeout has expired
long cur_time = System.currentTimeMillis();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(act);
long timeout_start = prefs.getLong(act.getString(R.string.timeout_key), TIMEOUT_NEVER);
// The timeout never started
if (timeout_start == TIMEOUT_NEVER) {
return true;
}
String sTimeout = prefs.getString(act.getString(R.string.app_timeout_key), act.getString(R.string.clipboard_timeout_default));
long timeout;
try {
timeout = Long.parseLong(sTimeout);
} catch (NumberFormatException e) {
timeout = DEFAULT_TIMEOUT;
}
// We are set to never timeout
if (timeout == TIMEOUT_NEVER) {
return true;
}
long diff = cur_time - timeout_start;
if (diff >= timeout) {
// We have timed out
if ( App.getDB().getLoaded() ) {
App.setShutdown(act.getString(R.string.app_timeout));
LockingActivity.checkShutdown(act);
return false;
}
}
return true;
}
}
@@ -0,0 +1,145 @@
/*
* 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.timeout
import android.app.Activity
import android.app.AlarmManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.preference.PreferenceManager
import android.util.Log
import com.kunzisoft.keepass.R
import com.kunzisoft.keepass.app.App
import com.kunzisoft.keepass.lock.LockingActivity
import com.kunzisoft.keepass.lock.lock
object TimeoutHelper {
const val DEFAULT_TIMEOUT = (5 * 60 * 1000).toLong() // 5 minutes
const val TIMEOUT_NEVER: Long = -1 // Infinite
private const val REQUEST_ID = 140
private const val TAG = "TimeoutHelper"
private fun getLockPendingIntent(context: Context): PendingIntent {
return PendingIntent.getBroadcast(context,
REQUEST_ID,
Intent(LockingActivity.LOCK_ACTION),
PendingIntent.FLAG_CANCEL_CURRENT)
}
/**
* Record the current time to check it later with checkTime
*/
fun recordTime(context: Context) {
val prefs = PreferenceManager.getDefaultSharedPreferences(context)
// Record timeout time in case timeout service is killed
val time = System.currentTimeMillis()
val edit = prefs.edit()
edit.putLong(context.getString(R.string.timeout_backup_key), time)
edit.apply()
if (App.getDB().loaded) {
val timeout = try {
java.lang.Long.parseLong(prefs.getString(context.getString(R.string.app_timeout_key),
context.getString(R.string.clipboard_timeout_default)))
} catch (e: NumberFormatException) {
DEFAULT_TIMEOUT
}
// No timeout don't start timeout service
if (timeout != TIMEOUT_NEVER) {
val triggerTime = System.currentTimeMillis() + timeout
val am = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
Log.d(TAG, "TimeoutHelper start")
am.set(AlarmManager.RTC, triggerTime, getLockPendingIntent(context))
}
}
}
/**
* Check the time previously record with recordTime and lock the activity if timeout
*/
fun checkTime(activity: Activity): Boolean {
return checkTime(activity) {
if (App.isShutdown() && App.getDB().loaded)
activity.lock()
}
}
/**
* Check the time previously record with recordTime and do the shutdown action if timeout
*/
fun checkTime(context: Context, shutdown: (() -> Unit)): Boolean {
// Cancel the lock PendingIntent
if (App.getDB().loaded) {
val am = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
Log.d(TAG, "TimeoutHelper cancel")
am.cancel(getLockPendingIntent(context))
}
// Check whether the timeout has expired
val currentTime = System.currentTimeMillis()
// Retrieve the timeout programmatically backup
val prefs = PreferenceManager.getDefaultSharedPreferences(context)
val timeoutBackup = prefs.getLong(context.getString(R.string.timeout_backup_key),
TIMEOUT_NEVER)
// The timeout never started
if (timeoutBackup == TIMEOUT_NEVER) {
return true
}
// Retrieve the app timeout in settings
val appTimeout = try {
java.lang.Long.parseLong(prefs.getString(context.getString(R.string.app_timeout_key),
context.getString(R.string.clipboard_timeout_default)))
} catch (e: NumberFormatException) {
DEFAULT_TIMEOUT
}
// We are set to never timeout
if (appTimeout == TIMEOUT_NEVER) {
return true
}
// See if not a timeout
val diff = currentTime - timeoutBackup
if (diff >= appTimeout) {
// We have timed out
if (App.getDB().loaded) {
App.setShutdown(context.getString(R.string.app_timeout))
shutdown.invoke()
return false
}
}
return true
}
fun touchToReinitTime(activity: Activity, action: (() -> Unit)? = null) {
if (checkTime(activity)) {
recordTime(activity)
action?.invoke()
}
}
}
+1 -1
View File
@@ -48,6 +48,7 @@
<string name="encryption_algorithm_key" translatable="false">algorithm</string>
<string name="key_derivation_function_key" translatable="false">key_derivation_function_key</string>
<string name="app_key" translatable="false">app</string>
<string name="timeout_backup_key" translatable="false">timeout_backup_key</string>
<string name="app_timeout_key" translatable="false">app_timeout_key</string>
<string name="clipboard_timeout_key" translatable="false">clip_timeout_key</string>
<string name="db_key" translatable="false">db</string>
@@ -66,7 +67,6 @@
<string name="sort_group_before_key" translatable="false">sort_group_before_key</string>
<string name="sort_ascending_key" translatable="false">sort_ascending_key</string>
<string name="sort_recycle_bin_bottom_key" translatable="false">sort_recycle_bin_bottom_key</string>
<string name="timeout_key" translatable="false">timeout_key</string>
<string name="saf_key" translatable="false">storage_access_framework_key</string>
<string name="setting_style_key" translatable="false">setting_style_key</string>
<string name="setting_icon_pack_choose_key" translatable="false">setting_icon_pack_choose_key</string>