2013-02-23 11:43:42 -05:00
|
|
|
/*
|
|
|
|
This file is part of Keepass2Android, Copyright 2013 Philipp Crocoll. This file is based on Keepassdroid, Copyright Brian Pellin.
|
|
|
|
|
|
|
|
Keepass2Android 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 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Keepass2Android 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 Keepass2Android. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using Android.App;
|
|
|
|
using Android.Content;
|
|
|
|
using Android.Preferences;
|
2013-06-18 15:12:34 -04:00
|
|
|
using Android.Util;
|
2013-02-23 11:43:42 -05:00
|
|
|
using KeePassLib.Serialization;
|
|
|
|
|
|
|
|
namespace keepass2android
|
|
|
|
{
|
2013-06-15 16:02:48 -04:00
|
|
|
/// <summary>
|
|
|
|
/// Helper class to simplify usage of timeout (lock after idle time) from the activities
|
|
|
|
/// </summary>
|
2013-02-23 11:43:42 -05:00
|
|
|
public class TimeoutHelper {
|
2013-06-15 16:02:48 -04:00
|
|
|
|
2013-07-23 23:58:33 -04:00
|
|
|
private static class Timeout
|
2013-06-15 16:02:48 -04:00
|
|
|
{
|
|
|
|
private const int RequestId = 0;
|
|
|
|
private const long DefaultTimeout = 5 * 60 * 1000; // 5 minutes
|
2013-07-23 23:58:33 -04:00
|
|
|
|
2013-06-15 16:02:48 -04:00
|
|
|
private static PendingIntent BuildIntent(Context ctx)
|
|
|
|
{
|
|
|
|
Intent intent = new Intent(Intents.Timeout);
|
|
|
|
PendingIntent sender = PendingIntent.GetBroadcast(ctx, RequestId, intent, PendingIntentFlags.CancelCurrent);
|
|
|
|
|
|
|
|
return sender;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void Start(Context ctx)
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(ctx);
|
|
|
|
String sTimeout = prefs.GetString(ctx.GetString(Resource.String.app_timeout_key), ctx.GetString(Resource.String.clipboard_timeout_default));
|
|
|
|
|
|
|
|
long timeout;
|
|
|
|
if (!long.TryParse(sTimeout, out timeout))
|
|
|
|
{
|
|
|
|
timeout = DefaultTimeout;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (timeout == -1)
|
|
|
|
{
|
|
|
|
// No timeout don't start timeout service
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.StartService(new Intent(ctx, typeof(TimeoutService)));
|
|
|
|
|
|
|
|
long triggerTime = Java.Lang.JavaSystem.CurrentTimeMillis() + timeout;
|
|
|
|
AlarmManager am = (AlarmManager)ctx.GetSystemService(Context.AlarmService);
|
|
|
|
|
2013-07-23 23:58:33 -04:00
|
|
|
Kp2aLog.Log("Timeout start");
|
2013-06-15 16:02:48 -04:00
|
|
|
am.Set(AlarmType.Rtc, triggerTime, BuildIntent(ctx));
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void Cancel(Context ctx)
|
|
|
|
{
|
|
|
|
AlarmManager am = (AlarmManager)ctx.GetSystemService(Context.AlarmService);
|
|
|
|
|
2013-07-23 23:58:33 -04:00
|
|
|
Kp2aLog.Log("Timeout cancel");
|
2013-06-15 16:02:48 -04:00
|
|
|
am.Cancel(BuildIntent(ctx));
|
|
|
|
|
|
|
|
ctx.StopService(new Intent(ctx, typeof(TimeoutService)));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
public static void Pause(Activity act) {
|
2013-02-23 11:43:42 -05:00
|
|
|
// Record timeout time in case timeout service is killed
|
|
|
|
long time = Java.Lang.JavaSystem.CurrentTimeMillis();
|
|
|
|
|
|
|
|
ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(act);
|
|
|
|
ISharedPreferencesEditor edit = prefs.Edit();
|
|
|
|
edit.PutLong(act.GetString(Resource.String.timeout_key), time);
|
2013-07-23 23:58:33 -04:00
|
|
|
|
|
|
|
Kp2aLog.Log("Pause: start at " + time);
|
2013-02-23 11:43:42 -05:00
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
EditorCompat.Apply(edit);
|
2013-02-23 11:43:42 -05:00
|
|
|
|
2013-06-14 00:14:50 -04:00
|
|
|
if ( App.Kp2a.GetDb().Open ) {
|
2013-06-15 06:40:01 -04:00
|
|
|
Timeout.Start(act);
|
2013-02-23 11:43:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
public static void Resume(Activity act) {
|
2013-06-14 00:14:50 -04:00
|
|
|
if ( App.Kp2a.GetDb().Loaded ) {
|
2013-06-15 06:40:01 -04:00
|
|
|
Timeout.Cancel(act);
|
2013-02-23 11:43:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Check whether the timeout has expired
|
2013-06-15 06:40:01 -04:00
|
|
|
long curTime = Java.Lang.JavaSystem.CurrentTimeMillis();
|
2013-02-23 11:43:42 -05:00
|
|
|
|
|
|
|
ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(act);
|
2013-06-15 06:40:01 -04:00
|
|
|
long timeoutStart = prefs.GetLong(act.GetString(Resource.String.timeout_key), -1);
|
2013-07-23 23:58:33 -04:00
|
|
|
Kp2aLog.Log("timeoutStart=" + timeoutStart);
|
2013-02-23 11:43:42 -05:00
|
|
|
// The timeout never started
|
2013-06-15 06:40:01 -04:00
|
|
|
if (timeoutStart == -1) {
|
2013-02-23 11:43:42 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
String sTimeout = prefs.GetString(act.GetString(Resource.String.app_timeout_key), act.GetString(Resource.String.clipboard_timeout_default));
|
2013-06-15 06:40:01 -04:00
|
|
|
long timeout;
|
2013-07-23 23:58:33 -04:00
|
|
|
if (!long.TryParse(sTimeout, out timeout) || (timeout == -1))
|
2013-06-15 06:40:01 -04:00
|
|
|
{
|
2013-07-23 23:58:33 -04:00
|
|
|
Kp2aLog.Log("exit with timeout=" + timeout + "/"+sTimeout);
|
2013-06-15 06:40:01 -04:00
|
|
|
// We are set to never timeout
|
2013-02-23 11:43:42 -05:00
|
|
|
return;
|
|
|
|
}
|
2013-06-15 06:40:01 -04:00
|
|
|
|
|
|
|
long diff = curTime - timeoutStart;
|
2013-07-23 23:58:33 -04:00
|
|
|
if (diff >= timeout)
|
|
|
|
{
|
2013-02-23 11:43:42 -05:00
|
|
|
// We have timed out
|
2013-07-23 23:58:33 -04:00
|
|
|
Kp2aLog.Log("Shutdown due to " + diff + ">=" + timeout);
|
|
|
|
App.Kp2a.SetShutdown();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Kp2aLog.Log("No shutdown due to " + diff + "<" + timeout);
|
2013-02-23 11:43:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
static bool IocChanged(IOConnectionInfo ioc, IOConnectionInfo other)
|
2013-02-23 11:43:42 -05:00
|
|
|
{
|
|
|
|
if ((ioc == null) || (other == null)) return false;
|
|
|
|
return ioc.GetDisplayName() != other.GetDisplayName();
|
|
|
|
}
|
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
public static bool CheckShutdown(Activity act, IOConnectionInfo ioc) {
|
|
|
|
if (( App.Kp2a.GetDb().Loaded && (App.Kp2a.IsShutdown() || App.Kp2a.GetDb().Locked) )
|
|
|
|
|| (IocChanged(ioc, App.Kp2a.GetDb().Ioc))) //file was changed from ActionSend-Intent
|
2013-02-23 11:43:42 -05:00
|
|
|
{
|
2013-06-15 06:40:01 -04:00
|
|
|
act.SetResult(KeePass.ExitLock);
|
2013-02-23 11:43:42 -05:00
|
|
|
act.Finish();
|
2013-03-15 02:07:45 -04:00
|
|
|
return true;
|
2013-02-23 11:43:42 -05:00
|
|
|
}
|
2013-03-15 02:07:45 -04:00
|
|
|
return false;
|
2013-02-23 11:43:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|