Fix settings timeout

This commit is contained in:
J-Jamet
2018-12-19 19:04:44 +01:00
parent 4fcb2de2bb
commit a535f123ff
10 changed files with 256 additions and 240 deletions

View File

@@ -424,7 +424,7 @@ public class EntryActivity extends LockingHideActivity {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
MenuUtil.contributionMenuInflater(inflater, menu);
MenuUtil.INSTANCE.contributionMenuInflater(inflater, menu);
inflater.inflate(R.menu.entry, menu);
inflater.inflate(R.menu.database_lock, menu);
@@ -468,7 +468,7 @@ public class EntryActivity extends LockingHideActivity {
public boolean onOptionsItemSelected(MenuItem item) {
switch ( item.getItemId() ) {
case R.id.menu_contribute:
return MenuUtil.onContributionItemSelected(this);
return MenuUtil.INSTANCE.onContributionItemSelected(this);
case R.id.menu_toggle_pass:
mShowPassword = !mShowPassword;

View File

@@ -471,7 +471,7 @@ public class EntryEditActivity extends LockingHideActivity
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
MenuUtil.contributionMenuInflater(inflater, menu);
MenuUtil.INSTANCE.contributionMenuInflater(inflater, menu);
return true;
}
@@ -479,7 +479,7 @@ public class EntryEditActivity extends LockingHideActivity
public boolean onOptionsItemSelected(MenuItem item) {
switch ( item.getItemId() ) {
case R.id.menu_contribute:
return MenuUtil.onContributionItemSelected(this);
return MenuUtil.INSTANCE.onContributionItemSelected(this);
case android.R.id.home:
finish();

View File

@@ -892,7 +892,7 @@ public class GroupActivity extends LockingActivity
});
}
MenuUtil.contributionMenuInflater(inflater, menu);
MenuUtil.INSTANCE.contributionMenuInflater(inflater, menu);
inflater.inflate(R.menu.default_menu, menu);
super.onCreateOptionsMenu(menu);
@@ -956,7 +956,7 @@ public class GroupActivity extends LockingActivity
return true;
default:
// Check the time lock before launching settings
MenuUtil.onDefaultMenuOptionsItemSelected(this, item, getReadOnly(), true);
MenuUtil.INSTANCE.onDefaultMenuOptionsItemSelected(this, item, getReadOnly(), true);
return super.onOptionsItemSelected(item);
}
}

View File

@@ -43,6 +43,8 @@ abstract class LockingActivity : StylishActivity() {
const val RESULT_EXIT_LOCK = 1450
}
protected var timeoutEnable: Boolean = true
private var lockReceiver: LockReceiver? = null
private var exitLock: Boolean = false
@@ -51,14 +53,15 @@ abstract class LockingActivity : StylishActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (timeoutEnable) {
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
@@ -76,6 +79,8 @@ abstract class LockingActivity : StylishActivity() {
override fun onResume() {
super.onResume()
if (timeoutEnable) {
// After the first creation
// or If simply swipe with another application
// If the time is out -> close the Activity
@@ -84,6 +89,7 @@ abstract class LockingActivity : StylishActivity() {
if (!exitLock)
TimeoutHelper.recordTime(this)
}
}
override fun onSaveInstanceState(outState: Bundle) {
ReadOnlyHelper.onSaveInstanceState(outState, readOnly)
@@ -92,9 +98,12 @@ abstract class LockingActivity : StylishActivity() {
override fun onPause() {
super.onPause()
if (timeoutEnable) {
// If the time is out during our navigation in activity -> close the Activity
TimeoutHelper.checkTime(this)
}
}
override fun onDestroy() {
super.onDestroy()
@@ -141,9 +150,13 @@ abstract class LockingActivity : StylishActivity() {
}
override fun onBackPressed() {
if (timeoutEnable) {
TimeoutHelper.lockOrResetTimeout(this) {
super.onBackPressed()
}
} else {
super.onBackPressed()
}
}
}

View File

@@ -697,13 +697,13 @@ public class FileSelectActivity extends StylishActivity implements
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuUtil.defaultMenuInflater(getMenuInflater(), menu);
MenuUtil.INSTANCE.defaultMenuInflater(getMenuInflater(), menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
return MenuUtil.onDefaultMenuOptionsItemSelected(this, item)
return MenuUtil.INSTANCE.onDefaultMenuOptionsItemSelected(this, item)
&& super.onOptionsItemSelected(item);
}
}

View File

@@ -1027,7 +1027,7 @@ public class PasswordActivity extends StylishActivity
inflater.inflate(R.menu.open_file, menu);
changeOpenFileReadIcon(menu.findItem(R.id.menu_open_file_read_mode_key));
MenuUtil.defaultMenuInflater(inflater, menu);
MenuUtil.INSTANCE.defaultMenuInflater(inflater, menu);
// Fingerprint menu
if (!fingerprintMustBeConfigured
@@ -1069,7 +1069,7 @@ public class PasswordActivity extends StylishActivity
}
break;
default:
return MenuUtil.onDefaultMenuOptionsItemSelected(this, item);
return MenuUtil.INSTANCE.onDefaultMenuOptionsItemSelected(this, item);
}
return super.onOptionsItemSelected(item);

View File

@@ -1,126 +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.settings;
import android.app.Activity;
import android.app.backup.BackupManager;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import com.kunzisoft.keepass.R;
import com.kunzisoft.keepass.activities.ReadOnlyHelper;
import com.kunzisoft.keepass.activities.lock.LockingActivity;
import com.kunzisoft.keepass.timeout.TimeoutHelper;
public class SettingsActivity extends LockingActivity implements MainPreferenceFragment.Callback {
private static final String TAG_NESTED = "TAG_NESTED";
private BackupManager backupManager;
private Toolbar toolbar;
public static void launch(Activity activity, boolean readOnly) {
Intent intent = new Intent(activity, SettingsActivity.class);
ReadOnlyHelper.putReadOnlyInIntent(intent, readOnly);
activity.startActivity(intent);
}
public static void launch(Activity activity, boolean readOnly, boolean checkLock) {
// To avoid flickering when launch settings in a LockingActivity
if (!checkLock)
launch(activity, readOnly);
else if (TimeoutHelper.INSTANCE.checkTime(activity)) {
launch(activity, readOnly);
}
}
/**
* Retrieve the main fragment to show in first
* @return The main fragment
*/
protected Fragment retrieveMainFragment() {
return new MainPreferenceFragment();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_toolbar);
toolbar = findViewById(R.id.toolbar);
toolbar.setTitle(R.string.settings);
setSupportActionBar(toolbar);
assert getSupportActionBar() != null;
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, retrieveMainFragment())
.commit();
}
backupManager = new BackupManager(this);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch ( item.getItemId() ) {
case android.R.id.home:
onBackPressed();
break;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onStop() {
backupManager.dataChanged();
super.onStop();
}
@Override
public void onBackPressed() {
// this if statement is necessary to navigate through nested and main fragments
if (getSupportFragmentManager().getBackStackEntryCount() == 0) {
super.onBackPressed();
} else {
getSupportFragmentManager().popBackStack();
}
toolbar.setTitle(R.string.settings);
}
@Override
public void onNestedPreferenceSelected(NestedSettingsFragment.Screen key) {
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, getReadOnly()), TAG_NESTED)
.addToBackStack(TAG_NESTED)
.commit();
toolbar.setTitle(NestedSettingsFragment.retrieveTitle(getResources(), key));
}
}

View File

@@ -0,0 +1,133 @@
/*
* 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.settings
import android.app.Activity
import android.app.backup.BackupManager
import android.content.Intent
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v7.widget.Toolbar
import android.view.MenuItem
import com.kunzisoft.keepass.R
import com.kunzisoft.keepass.activities.ReadOnlyHelper
import com.kunzisoft.keepass.activities.lock.LockingActivity
import com.kunzisoft.keepass.app.App
import com.kunzisoft.keepass.timeout.TimeoutHelper
open class SettingsActivity : LockingActivity(), MainPreferenceFragment.Callback {
private var backupManager: BackupManager? = null
private var toolbar: Toolbar? = null
companion object {
private const val TAG_NESTED = "TAG_NESTED"
fun launch(activity: Activity, readOnly: Boolean) {
val intent = Intent(activity, SettingsActivity::class.java)
ReadOnlyHelper.putReadOnlyInIntent(intent, readOnly)
activity.startActivity(intent)
}
fun launch(activity: Activity, readOnly: Boolean, checkLock: Boolean) {
// To avoid flickering when launch settings in a LockingActivity
if (!checkLock)
launch(activity, readOnly)
else if (TimeoutHelper.checkTime(activity)) {
launch(activity, readOnly)
}
}
}
/**
* Retrieve the main fragment to show in first
* @return The main fragment
*/
protected open fun retrieveMainFragment(): Fragment {
return MainPreferenceFragment()
}
override fun onCreate(savedInstanceState: Bundle?) {
timeoutEnable = App.getDB().loaded
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_toolbar)
toolbar = findViewById(R.id.toolbar)
toolbar?.setTitle(R.string.settings)
setSupportActionBar(toolbar)
supportActionBar?.setDisplayHomeAsUpEnabled(true)
if (savedInstanceState == null) {
supportFragmentManager.beginTransaction()
.add(R.id.fragment_container, retrieveMainFragment())
.commit()
}
backupManager = BackupManager(this)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
android.R.id.home -> onBackPressed()
}
return super.onOptionsItemSelected(item)
}
override fun onStop() {
backupManager?.dataChanged()
super.onStop()
}
override fun onBackPressed() {
// this if statement is necessary to navigate through nested and main fragments
if (supportFragmentManager.backStackEntryCount == 0) {
super.onBackPressed()
} else {
supportFragmentManager.popBackStack()
}
toolbar?.setTitle(R.string.settings)
}
private fun replaceFragment(key: NestedSettingsFragment.Screen) {
supportFragmentManager.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)
.addToBackStack(TAG_NESTED)
.commit()
toolbar?.title = NestedSettingsFragment.retrieveTitle(resources, key)
}
override fun onNestedPreferenceSelected(key: NestedSettingsFragment.Screen) {
if (timeoutEnable)
TimeoutHelper.lockOrResetTimeout(this) {
replaceFragment(key)
}
else
replaceFragment(key)
}
}

View File

@@ -1,86 +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.utils;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
import com.kunzisoft.keepass.BuildConfig;
import com.kunzisoft.keepass.R;
import com.kunzisoft.keepass.activities.AboutActivity;
import com.kunzisoft.keepass.settings.SettingsActivity;
import com.kunzisoft.keepass.stylish.StylishActivity;
import static com.kunzisoft.keepass.activities.ReadOnlyHelper.READ_ONLY_DEFAULT;
public class MenuUtil {
public static void contributionMenuInflater(MenuInflater inflater, Menu menu) {
if(!(BuildConfig.FULL_VERSION && BuildConfig.CLOSED_STORE))
inflater.inflate(R.menu.contribution, menu);
}
public static void defaultMenuInflater(MenuInflater inflater, Menu menu) {
contributionMenuInflater(inflater, menu);
inflater.inflate(R.menu.default_menu, menu);
}
public static boolean onContributionItemSelected(StylishActivity activity) {
try {
Util.gotoUrl(activity, R.string.contribution_url);
} catch (ActivityNotFoundException e) {
Toast.makeText(activity, R.string.error_failed_to_launch_link, Toast.LENGTH_LONG).show();
return false;
}
return true;
}
public static boolean onDefaultMenuOptionsItemSelected(StylishActivity activity, MenuItem item) {
return onDefaultMenuOptionsItemSelected(activity, item, READ_ONLY_DEFAULT, false);
}
/*
* @param checkLock Check the time lock before launch settings in LockingActivity
*/
public static boolean onDefaultMenuOptionsItemSelected(StylishActivity activity, MenuItem item, boolean readOnly, boolean checkLock) {
switch (item.getItemId()) {
case R.id.menu_contribute:
return onContributionItemSelected(activity);
case R.id.menu_app_settings:
// To avoid flickering when launch settings in a LockingActivity
SettingsActivity.launch(activity, readOnly, checkLock);
return true;
case R.id.menu_about:
Intent intent = new Intent(activity, AboutActivity.class);
activity.startActivity(intent);
return true;
default:
return true;
}
}
}

View File

@@ -0,0 +1,82 @@
/*
* 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.utils
import android.content.ActivityNotFoundException
import android.content.Intent
import android.view.Menu
import android.view.MenuInflater
import android.view.MenuItem
import android.widget.Toast
import com.kunzisoft.keepass.BuildConfig
import com.kunzisoft.keepass.R
import com.kunzisoft.keepass.activities.AboutActivity
import com.kunzisoft.keepass.activities.ReadOnlyHelper.READ_ONLY_DEFAULT
import com.kunzisoft.keepass.settings.SettingsActivity
import com.kunzisoft.keepass.stylish.StylishActivity
object MenuUtil {
fun contributionMenuInflater(inflater: MenuInflater, menu: Menu) {
if (!(BuildConfig.FULL_VERSION && BuildConfig.CLOSED_STORE))
inflater.inflate(R.menu.contribution, menu)
}
fun defaultMenuInflater(inflater: MenuInflater, menu: Menu) {
contributionMenuInflater(inflater, menu)
inflater.inflate(R.menu.default_menu, menu)
}
fun onContributionItemSelected(activity: StylishActivity): Boolean {
try {
Util.gotoUrl(activity, R.string.contribution_url)
} catch (e: ActivityNotFoundException) {
Toast.makeText(activity, R.string.error_failed_to_launch_link, Toast.LENGTH_LONG).show()
return false
}
return true
}
/*
* @param checkLock Check the time lock before launch settings in LockingActivity
*/
@JvmOverloads
fun onDefaultMenuOptionsItemSelected(activity: StylishActivity, item: MenuItem, readOnly: Boolean = READ_ONLY_DEFAULT, checkLock: Boolean = false): Boolean {
when (item.itemId) {
R.id.menu_contribute -> return onContributionItemSelected(activity)
R.id.menu_app_settings -> {
// To avoid flickering when launch settings in a LockingActivity
SettingsActivity.launch(activity, readOnly, checkLock)
return true
}
R.id.menu_about -> {
val intent = Intent(activity, AboutActivity::class.java)
activity.startActivity(intent)
return true
}
else -> return true
}
}
}