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.Runtime;
|
2013-06-14 00:14:50 -04:00
|
|
|
using KeePassLib.Serialization;
|
|
|
|
using Android.Preferences;
|
2013-02-23 11:43:42 -05:00
|
|
|
|
|
|
|
namespace keepass2android
|
|
|
|
{
|
2013-02-27 16:11:44 -05:00
|
|
|
#if NoNet
|
2013-06-15 16:02:48 -04:00
|
|
|
/// <summary>
|
|
|
|
/// Static strings containing App names for the Offline ("nonet") release
|
|
|
|
/// </summary>
|
2013-02-27 16:11:44 -05:00
|
|
|
public static class AppNames
|
2013-02-27 00:23:22 -05:00
|
|
|
{
|
2013-02-27 16:11:44 -05:00
|
|
|
public const string AppName = "@string/app_name_nonet";
|
|
|
|
public const string AppNameShort = "@string/short_app_name_nonet";
|
|
|
|
public const string AppLauncherTitle = "@string/short_app_name_nonet";
|
2013-05-05 23:28:13 -04:00
|
|
|
public const string PackagePart = "keepass2android_nonet";
|
2013-02-27 00:23:22 -05:00
|
|
|
}
|
2013-02-27 16:11:44 -05:00
|
|
|
#else
|
2013-06-15 16:02:48 -04:00
|
|
|
/// <summary>
|
|
|
|
/// Static strings containing App names for the Online release
|
|
|
|
/// </summary>
|
2013-02-27 16:11:44 -05:00
|
|
|
public static class AppNames
|
|
|
|
{
|
|
|
|
public const string AppName = "@string/app_name";
|
|
|
|
public const string AppNameShort = "@string/short_app_name";
|
|
|
|
public const string AppLauncherTitle = "@string/app_name";
|
2013-05-05 23:28:13 -04:00
|
|
|
public const string PackagePart = "keepass2android";
|
2013-02-27 16:11:44 -05:00
|
|
|
}
|
|
|
|
#endif
|
2013-06-18 15:12:34 -04:00
|
|
|
/// <summary>
|
|
|
|
/// Main implementation of the IKp2aApp interface for usage in the real app.
|
|
|
|
/// </summary>
|
2013-06-14 00:14:50 -04:00
|
|
|
public class Kp2aApp: IKp2aApp
|
|
|
|
{
|
2013-06-15 06:40:01 -04:00
|
|
|
public bool IsShutdown()
|
2013-06-14 00:14:50 -04:00
|
|
|
{
|
2013-06-15 06:40:01 -04:00
|
|
|
return _shutdown;
|
2013-06-14 00:14:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public void SetShutdown()
|
|
|
|
{
|
2013-06-15 06:40:01 -04:00
|
|
|
_shutdown = true;
|
2013-06-14 00:14:50 -04:00
|
|
|
}
|
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
public void ClearShutdown()
|
2013-06-14 00:14:50 -04:00
|
|
|
{
|
2013-06-15 06:40:01 -04:00
|
|
|
_shutdown = false;
|
2013-06-14 00:14:50 -04:00
|
|
|
}
|
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
private Database _db;
|
|
|
|
private bool _shutdown;
|
2013-06-14 00:14:50 -04:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// See comments to EntryEditActivityState.
|
|
|
|
/// </summary>
|
2013-06-15 06:40:01 -04:00
|
|
|
internal EntryEditActivityState EntryEditActivityState = null;
|
2013-06-14 00:14:50 -04:00
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
public FileDbHelper FileDbHelper;
|
2013-06-14 00:14:50 -04:00
|
|
|
|
|
|
|
public Database GetDb()
|
|
|
|
{
|
2013-06-15 06:40:01 -04:00
|
|
|
if (_db == null)
|
2013-06-14 00:14:50 -04:00
|
|
|
{
|
2013-06-15 06:40:01 -04:00
|
|
|
_db = CreateNewDatabase();
|
2013-06-14 00:14:50 -04:00
|
|
|
}
|
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
return _db;
|
2013-06-14 00:14:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public bool GetBooleanPreference(PreferenceKey key)
|
|
|
|
{
|
|
|
|
Context ctx = Application.Context;
|
|
|
|
ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(ctx);
|
|
|
|
switch (key)
|
|
|
|
{
|
|
|
|
case PreferenceKey.remember_keyfile:
|
|
|
|
return prefs.GetBoolean(ctx.Resources.GetString(Resource.String.keyfile_key), ctx.Resources.GetBoolean(Resource.Boolean.keyfile_default));
|
|
|
|
case PreferenceKey.UseFileTransactions:
|
|
|
|
return prefs.GetBoolean(ctx.Resources.GetString(Resource.String.UseFileTransactions_key), true);
|
|
|
|
default:
|
|
|
|
throw new Exception("unexpected key!");
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void CheckForOpenFileChanged(Activity activity)
|
|
|
|
{
|
2013-06-15 06:40:01 -04:00
|
|
|
if (_db.DidOpenFileChange())
|
2013-06-14 00:14:50 -04:00
|
|
|
{
|
2013-06-15 06:40:01 -04:00
|
|
|
if (_db.ReloadRequested)
|
2013-06-14 00:14:50 -04:00
|
|
|
{
|
2013-06-15 06:40:01 -04:00
|
|
|
activity.SetResult(KeePass.ExitReloadDb);
|
2013-06-14 00:14:50 -04:00
|
|
|
activity.Finish();
|
|
|
|
}
|
|
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
|
|
|
|
builder.SetTitle(activity.GetString(Resource.String.AskReloadFile_title));
|
|
|
|
|
|
|
|
builder.SetMessage(activity.GetString(Resource.String.AskReloadFile));
|
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
builder.SetPositiveButton(activity.GetString(Android.Resource.String.Yes),
|
|
|
|
(dlgSender, dlgEvt) =>
|
|
|
|
{
|
|
|
|
_db.ReloadRequested = true;
|
|
|
|
activity.SetResult(KeePass.ExitReloadDb);
|
|
|
|
activity.Finish();
|
2013-06-14 00:14:50 -04:00
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
});
|
2013-06-14 00:14:50 -04:00
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
builder.SetNegativeButton(activity.GetString(Android.Resource.String.No), (dlgSender, dlgEvt) =>
|
|
|
|
{
|
2013-06-14 00:14:50 -04:00
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
});
|
2013-06-14 00:14:50 -04:00
|
|
|
|
|
|
|
|
|
|
|
Dialog dialog = builder.Create();
|
|
|
|
dialog.Show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void StoreOpenedFileAsRecent(IOConnectionInfo ioc, string keyfile)
|
|
|
|
{
|
2013-06-15 06:40:01 -04:00
|
|
|
FileDbHelper.CreateFile(ioc, keyfile);
|
2013-06-14 00:14:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public string GetResourceString(UiStringKey key)
|
|
|
|
{
|
|
|
|
var field = typeof (Resource.String).GetField(key.ToString());
|
|
|
|
if (field == null)
|
|
|
|
throw new Exception("Invalid key " + key);
|
2013-06-15 06:40:01 -04:00
|
|
|
return Application.Context.GetString((int)field.GetValue(null));
|
2013-06-14 00:14:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public void AskYesNoCancel(UiStringKey titleKey, UiStringKey messageKey,
|
|
|
|
EventHandler<DialogClickEventArgs> yesHandler,
|
|
|
|
EventHandler<DialogClickEventArgs> noHandler,
|
|
|
|
EventHandler<DialogClickEventArgs> cancelHandler,
|
|
|
|
Context ctx)
|
|
|
|
{
|
|
|
|
|
|
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
|
|
|
|
builder.SetTitle(GetResourceString(titleKey));
|
|
|
|
|
|
|
|
builder.SetMessage(GetResourceString(messageKey));
|
|
|
|
|
|
|
|
builder.SetPositiveButton(Resource.String.yes, yesHandler);
|
|
|
|
|
|
|
|
builder.SetNegativeButton(Resource.String.no, noHandler);
|
|
|
|
|
|
|
|
builder.SetNeutralButton(ctx.GetString(Android.Resource.String.Cancel),
|
|
|
|
cancelHandler);
|
|
|
|
|
|
|
|
Dialog dialog = builder.Create();
|
|
|
|
dialog.Show();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
internal void OnTerminate()
|
|
|
|
{
|
2013-06-15 06:40:01 -04:00
|
|
|
if (_db != null)
|
2013-06-14 00:14:50 -04:00
|
|
|
{
|
2013-06-15 06:40:01 -04:00
|
|
|
_db.Clear();
|
2013-06-14 00:14:50 -04:00
|
|
|
}
|
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
if (FileDbHelper != null && FileDbHelper.IsOpen())
|
2013-06-14 00:14:50 -04:00
|
|
|
{
|
2013-06-15 06:40:01 -04:00
|
|
|
FileDbHelper.Close();
|
2013-06-14 00:14:50 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
internal void OnCreate(Application app)
|
|
|
|
{
|
2013-06-15 06:40:01 -04:00
|
|
|
FileDbHelper = new FileDbHelper(app);
|
|
|
|
FileDbHelper.Open();
|
2013-06-14 00:14:50 -04:00
|
|
|
|
|
|
|
#if DEBUG
|
|
|
|
foreach (UiStringKey key in Enum.GetValues(typeof(UiStringKey)))
|
|
|
|
{
|
|
|
|
GetResourceString(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
this should case a compiler error when switching to release (and thus ensure DEBUG is defined in DEBUG)
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Database CreateNewDatabase()
|
|
|
|
{
|
2013-06-15 06:40:01 -04:00
|
|
|
_db = new Database(new DrawableFactory(), this);
|
|
|
|
return _db;
|
2013-06-14 00:14:50 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///Application class for Keepass2Android: Contains static Database variable to be used by all components.
|
2013-02-27 00:23:22 -05:00
|
|
|
#if NoNet
|
2013-02-27 16:11:44 -05:00
|
|
|
[Application(Debuggable=false, Label=AppNames.AppName)]
|
2013-02-23 11:43:42 -05:00
|
|
|
#else
|
2013-06-14 00:14:50 -04:00
|
|
|
#if RELEASE
|
2013-02-27 16:11:44 -05:00
|
|
|
[Application(Debuggable=false, Label=AppNames.AppName)]
|
2013-06-14 00:14:50 -04:00
|
|
|
#else
|
|
|
|
[Application(Debuggable = true, Label = AppNames.AppName)]
|
|
|
|
#endif
|
2013-02-23 11:43:42 -05:00
|
|
|
#endif
|
|
|
|
public class App : Application {
|
|
|
|
|
|
|
|
public App (IntPtr javaReference, JniHandleOwnership transfer)
|
|
|
|
: base(javaReference, transfer)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
public static readonly Kp2aApp Kp2a = new Kp2aApp();
|
|
|
|
|
2013-02-23 11:43:42 -05:00
|
|
|
public override void OnCreate() {
|
|
|
|
base.OnCreate();
|
|
|
|
|
|
|
|
Android.Util.Log.Debug("DEBUG","Creating application");
|
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
Kp2a.OnCreate(this);
|
2013-02-23 11:43:42 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void OnTerminate() {
|
|
|
|
base.OnTerminate();
|
|
|
|
Android.Util.Log.Debug("DEBUG","Terminating application");
|
2013-06-15 06:40:01 -04:00
|
|
|
Kp2a.OnTerminate();
|
2013-02-23 11:43:42 -05:00
|
|
|
}
|
|
|
|
|
2013-06-14 00:14:50 -04:00
|
|
|
|
|
|
|
}
|
2013-02-23 11:43:42 -05:00
|
|
|
}
|
|
|
|
|