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;
|
2013-06-15 06:40:01 -04:00
|
|
|
using System.Globalization;
|
2013-02-23 11:43:42 -05:00
|
|
|
using Android.Content;
|
|
|
|
using Android.OS;
|
|
|
|
using Android.Views;
|
|
|
|
using Android.Widget;
|
|
|
|
using Android.Preferences;
|
|
|
|
using KeePassLib;
|
|
|
|
using Android.Util;
|
|
|
|
|
|
|
|
namespace keepass2android.settings
|
|
|
|
{
|
2013-06-15 16:02:48 -04:00
|
|
|
/// <summary>
|
|
|
|
/// Represents the setting for the number of key transformation rounds. Changing this requires to save the database.
|
|
|
|
/// </summary>
|
2013-02-23 11:43:42 -05:00
|
|
|
public class RoundsPreference : DialogPreference {
|
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
internal PwDatabase PwDatabase;
|
|
|
|
internal TextView RoundsView;
|
2013-02-23 11:43:42 -05:00
|
|
|
|
|
|
|
protected override View OnCreateDialogView() {
|
|
|
|
View view = base.OnCreateDialogView();
|
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
RoundsView = (TextView) view.FindViewById(Resource.Id.rounds);
|
2013-02-23 11:43:42 -05:00
|
|
|
|
2013-06-14 00:14:50 -04:00
|
|
|
Database db = App.Kp2a.GetDb();
|
2013-06-15 06:40:01 -04:00
|
|
|
PwDatabase = db.KpDatabase;
|
|
|
|
ulong numRounds = PwDatabase.KeyEncryptionRounds;
|
|
|
|
RoundsView.Text = numRounds.ToString(CultureInfo.InvariantCulture);
|
2013-02-23 11:43:42 -05:00
|
|
|
|
|
|
|
return view;
|
|
|
|
}
|
|
|
|
|
|
|
|
public RoundsPreference(Context context, IAttributeSet attrs):base(context, attrs) {
|
|
|
|
}
|
|
|
|
|
|
|
|
public RoundsPreference(Context context, IAttributeSet attrs, int defStyle): base(context, attrs, defStyle) {
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void OnDialogClosed(bool positiveResult) {
|
|
|
|
base.OnDialogClosed(positiveResult);
|
|
|
|
|
|
|
|
if ( positiveResult ) {
|
|
|
|
ulong rounds;
|
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
String strRounds = RoundsView.Text;
|
2013-02-23 11:43:42 -05:00
|
|
|
if (!(ulong.TryParse(strRounds,out rounds)))
|
|
|
|
{
|
|
|
|
Toast.MakeText(Context, Resource.String.error_rounds_not_number, ToastLength.Long).Show();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( rounds < 1 ) {
|
|
|
|
rounds = 1;
|
|
|
|
}
|
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
ulong oldRounds = PwDatabase.KeyEncryptionRounds;
|
2013-02-23 11:43:42 -05:00
|
|
|
|
|
|
|
if (oldRounds == rounds)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
PwDatabase.KeyEncryptionRounds = rounds;
|
2013-02-23 11:43:42 -05:00
|
|
|
|
|
|
|
Handler handler = new Handler();
|
2013-06-15 06:40:01 -04:00
|
|
|
SaveDb save = new SaveDb(Context, App.Kp2a.GetDb(), new AfterSave(Context, handler, oldRounds, this));
|
2013-06-14 00:14:50 -04:00
|
|
|
ProgressTask pt = new ProgressTask(App.Kp2a, Context, save, UiStringKey.saving_database);
|
2013-06-15 16:02:48 -04:00
|
|
|
pt.Run();
|
2013-02-23 11:43:42 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private class AfterSave : OnFinish {
|
2013-06-15 06:40:01 -04:00
|
|
|
private readonly ulong _oldRounds;
|
|
|
|
private readonly Context _ctx;
|
|
|
|
private readonly RoundsPreference _pref;
|
2013-02-23 11:43:42 -05:00
|
|
|
|
|
|
|
public AfterSave(Context ctx, Handler handler, ulong oldRounds, RoundsPreference pref):base(handler) {
|
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
_pref = pref;
|
|
|
|
_ctx = ctx;
|
|
|
|
_oldRounds = oldRounds;
|
2013-02-23 11:43:42 -05:00
|
|
|
}
|
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
public override void Run() {
|
|
|
|
if ( Success ) {
|
2013-02-23 11:43:42 -05:00
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
if ( _pref.OnPreferenceChangeListener != null ) {
|
|
|
|
_pref.OnPreferenceChangeListener.OnPreferenceChange(_pref, null);
|
2013-02-23 11:43:42 -05:00
|
|
|
}
|
|
|
|
} else {
|
2013-06-15 06:40:01 -04:00
|
|
|
DisplayMessage(_ctx);
|
|
|
|
_pref.PwDatabase.KeyEncryptionRounds = _oldRounds;
|
2013-02-23 11:43:42 -05:00
|
|
|
}
|
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
base.Run();
|
2013-02-23 11:43:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|