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.OS;
|
|
|
|
using Android.Runtime;
|
|
|
|
using Android.Views;
|
|
|
|
using Android.Widget;
|
|
|
|
using Java.Net;
|
|
|
|
using Android.Preferences;
|
|
|
|
using Java.IO;
|
|
|
|
using Android.Text;
|
|
|
|
using Android.Content.PM;
|
|
|
|
using KeePassLib.Keys;
|
|
|
|
using KeePassLib.Serialization;
|
|
|
|
|
|
|
|
namespace keepass2android
|
|
|
|
{
|
2013-03-20 17:40:28 -04:00
|
|
|
[Activity (Label = "@string/app_name",
|
|
|
|
ConfigurationChanges=ConfigChanges.Orientation|ConfigChanges.KeyboardHidden,
|
|
|
|
Theme="@style/Base")]
|
|
|
|
|
2013-02-23 11:43:42 -05:00
|
|
|
public class PasswordActivity : LockingActivity {
|
2013-06-15 06:40:01 -04:00
|
|
|
bool _showPassword;
|
2013-02-23 11:43:42 -05:00
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
public const String KeyDefaultFilename = "defaultFileName";
|
2013-02-23 11:43:42 -05:00
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
public const String KeyFilename = "fileName";
|
|
|
|
private const String KeyKeyfile = "keyFile";
|
|
|
|
private const String KeyServerusername = "serverCredUser";
|
|
|
|
private const String KeyServerpassword = "serverCredPwd";
|
|
|
|
private const String KeyServercredmode = "serverCredRememberMode";
|
2013-02-23 11:43:42 -05:00
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
private const String ViewIntent = "android.intent.action.VIEW";
|
2013-02-23 11:43:42 -05:00
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
private IOConnectionInfo _ioConnection;
|
|
|
|
private String _keyFile;
|
|
|
|
private bool _rememberKeyfile;
|
|
|
|
ISharedPreferences _prefs;
|
2013-02-23 11:43:42 -05:00
|
|
|
|
|
|
|
public PasswordActivity (IntPtr javaReference, JniHandleOwnership transfer)
|
|
|
|
: base(javaReference, transfer)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public PasswordActivity()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
static void PutIoConnectionToIntent(IOConnectionInfo ioc, Intent i)
|
2013-02-23 11:43:42 -05:00
|
|
|
{
|
2013-06-15 06:40:01 -04:00
|
|
|
i.PutExtra(KeyFilename, ioc.Path);
|
|
|
|
i.PutExtra(KeyServerusername, ioc.UserName);
|
|
|
|
i.PutExtra(KeyServerpassword, ioc.Password);
|
|
|
|
i.PutExtra(KeyServercredmode, (int)ioc.CredSaveMode);
|
2013-02-23 11:43:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public static void SetIoConnectionFromIntent(IOConnectionInfo ioc, Intent i)
|
|
|
|
{
|
2013-06-15 06:40:01 -04:00
|
|
|
ioc.Path = i.GetStringExtra(KeyFilename);
|
|
|
|
ioc.UserName = i.GetStringExtra(KeyServerusername) ?? "";
|
|
|
|
ioc.Password = i.GetStringExtra(KeyServerpassword) ?? "";
|
|
|
|
ioc.CredSaveMode = (IOCredSaveMode)i.GetIntExtra(KeyServercredmode, (int) IOCredSaveMode.NoSave);
|
2013-02-23 11:43:42 -05:00
|
|
|
}
|
|
|
|
|
2013-05-30 02:29:08 -04:00
|
|
|
public static void Launch(Activity act, String fileName, AppTask appTask) {
|
2013-06-15 06:40:01 -04:00
|
|
|
File dbFile = new File(fileName);
|
2013-02-23 11:43:42 -05:00
|
|
|
if ( ! dbFile.Exists() ) {
|
2013-06-15 06:40:01 -04:00
|
|
|
throw new FileNotFoundException();
|
2013-02-23 11:43:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Intent i = new Intent(act, typeof(PasswordActivity));
|
2013-06-15 06:40:01 -04:00
|
|
|
i.PutExtra(KeyFilename, fileName);
|
2013-05-30 00:54:25 -04:00
|
|
|
appTask.ToIntent(i);
|
2013-02-23 11:43:42 -05:00
|
|
|
act.StartActivityForResult(i, 0);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static void Launch(Activity act, String fileName) {
|
|
|
|
Launch(act, IOConnectionInfo.FromPath(fileName), null);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-30 02:29:08 -04:00
|
|
|
public static void Launch(Activity act, IOConnectionInfo ioc, AppTask appTask)
|
2013-02-23 11:43:42 -05:00
|
|
|
{
|
|
|
|
if (ioc.IsLocalFile())
|
|
|
|
{
|
2013-05-30 00:54:25 -04:00
|
|
|
Launch(act, ioc.Path, appTask);
|
2013-02-23 11:43:42 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Intent i = new Intent(act, typeof(PasswordActivity));
|
|
|
|
PutIoConnectionToIntent(ioc, i);
|
|
|
|
|
2013-05-30 00:54:25 -04:00
|
|
|
appTask.ToIntent(i);
|
2013-02-23 11:43:42 -05:00
|
|
|
|
|
|
|
act.StartActivityForResult(i, 0);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public void LaunchNextActivity()
|
|
|
|
{
|
2013-06-15 06:40:01 -04:00
|
|
|
AppTask.AfterUnlockDatabase(this);
|
2013-02-23 11:43:42 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
void UnloadDatabase()
|
2013-02-23 11:43:42 -05:00
|
|
|
{
|
2013-06-14 00:14:50 -04:00
|
|
|
App.Kp2a.GetDb().Clear();
|
2013-02-23 11:43:42 -05:00
|
|
|
StopService(new Intent(this, typeof(QuickUnlockForegroundService)));
|
|
|
|
}
|
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
void LockDatabase()
|
2013-02-23 11:43:42 -05:00
|
|
|
{
|
2013-06-15 06:40:01 -04:00
|
|
|
SetResult(KeePass.ExitLock);
|
|
|
|
SetEditText(Resource.Id.password, "");
|
2013-06-14 00:14:50 -04:00
|
|
|
if (App.Kp2a.GetDb().QuickUnlockEnabled)
|
|
|
|
App.Kp2a.GetDb().Locked = true;
|
2013-02-23 11:43:42 -05:00
|
|
|
else
|
|
|
|
{
|
2013-06-15 06:40:01 -04:00
|
|
|
UnloadDatabase();
|
2013-02-23 11:43:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
void LockAndClose()
|
2013-02-23 11:43:42 -05:00
|
|
|
{
|
2013-06-15 06:40:01 -04:00
|
|
|
LockDatabase();
|
2013-02-23 11:43:42 -05:00
|
|
|
Finish();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
bool TryStartQuickUnlock()
|
2013-02-23 11:43:42 -05:00
|
|
|
{
|
2013-06-14 00:14:50 -04:00
|
|
|
if (!App.Kp2a.GetDb().QuickUnlockEnabled)
|
2013-02-23 11:43:42 -05:00
|
|
|
return false;
|
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
if (App.Kp2a.GetDb().KpDatabase.MasterKey.ContainsType(typeof(KcpPassword)) == false)
|
2013-02-23 11:43:42 -05:00
|
|
|
return false;
|
2013-06-15 06:40:01 -04:00
|
|
|
KcpPassword kcpPassword = (KcpPassword)App.Kp2a.GetDb().KpDatabase.MasterKey.GetUserKey(typeof(KcpPassword));
|
2013-02-23 11:43:42 -05:00
|
|
|
String password = kcpPassword.Password.ReadString();
|
|
|
|
|
|
|
|
if (password.Length == 0)
|
|
|
|
return false;
|
|
|
|
|
2013-06-14 00:14:50 -04:00
|
|
|
App.Kp2a.GetDb().Locked = true;
|
2013-02-23 11:43:42 -05:00
|
|
|
|
|
|
|
Intent i = new Intent(this, typeof(QuickUnlock));
|
2013-06-15 06:40:01 -04:00
|
|
|
PutIoConnectionToIntent(_ioConnection, i);
|
2013-07-06 10:11:38 -04:00
|
|
|
Kp2aLog.Log("Starting QuickUnlock");
|
2013-02-23 11:43:42 -05:00
|
|
|
StartActivityForResult(i,0);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void StartQuickUnlockForegroundService()
|
|
|
|
{
|
2013-06-14 00:14:50 -04:00
|
|
|
if (App.Kp2a.GetDb().QuickUnlockEnabled)
|
2013-02-23 11:43:42 -05:00
|
|
|
{
|
|
|
|
StartService(new Intent(this, typeof(QuickUnlockForegroundService)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
bool _startedWithActivityResult;
|
2013-02-23 11:43:42 -05:00
|
|
|
|
|
|
|
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
|
|
|
|
{
|
|
|
|
base.OnActivityResult(requestCode, resultCode, data);
|
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
_startedWithActivityResult = true;
|
2013-07-06 10:11:38 -04:00
|
|
|
Kp2aLog.Log("PasswordActivity.OnActivityResult "+resultCode+"/"+requestCode);
|
2013-02-23 11:43:42 -05:00
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
if (resultCode != KeePass.ExitCloseAfterTaskComplete)
|
2013-02-23 11:43:42 -05:00
|
|
|
{
|
|
|
|
//Stop service when app activity is left
|
|
|
|
StopService(new Intent(this, typeof(CopyToClipboardService)));
|
|
|
|
}
|
|
|
|
|
|
|
|
//NOTE: original code from k eepassdroid used switch ((Android.App.Result)requestCode) { (but doesn't work here, although k eepassdroid works)
|
|
|
|
switch(resultCode) {
|
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
case KeePass.ExitNormal:
|
|
|
|
if (!TryStartQuickUnlock())
|
2013-02-23 11:43:42 -05:00
|
|
|
{
|
2013-06-15 06:40:01 -04:00
|
|
|
SetEditText(Resource.Id.password, "");
|
2013-02-23 11:43:42 -05:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
case KeePass.ExitLock:
|
|
|
|
if (!TryStartQuickUnlock())
|
2013-02-23 11:43:42 -05:00
|
|
|
{
|
2013-06-15 06:40:01 -04:00
|
|
|
LockAndClose();
|
2013-02-23 11:43:42 -05:00
|
|
|
}
|
|
|
|
break;
|
2013-06-15 06:40:01 -04:00
|
|
|
case KeePass.ExitForceLock:
|
|
|
|
SetEditText(Resource.Id.password, "");
|
|
|
|
UnloadDatabase();
|
2013-02-23 11:43:42 -05:00
|
|
|
break;
|
2013-06-15 06:40:01 -04:00
|
|
|
case KeePass.ExitForceLockAndChangeDb:
|
|
|
|
UnloadDatabase();
|
2013-02-23 11:43:42 -05:00
|
|
|
Finish();
|
|
|
|
break;
|
2013-06-15 06:40:01 -04:00
|
|
|
case KeePass.ExitChangeDb:
|
|
|
|
LockAndClose();
|
2013-02-23 11:43:42 -05:00
|
|
|
break;
|
2013-06-15 06:40:01 -04:00
|
|
|
case KeePass.ExitCloseAfterTaskComplete:
|
|
|
|
SetResult(KeePass.ExitCloseAfterTaskComplete);
|
2013-02-23 11:43:42 -05:00
|
|
|
Finish();
|
|
|
|
break;
|
2013-06-15 06:40:01 -04:00
|
|
|
case KeePass.ExitQuickUnlock:
|
2013-06-14 00:14:50 -04:00
|
|
|
App.Kp2a.GetDb().Locked = false;
|
2013-02-23 11:43:42 -05:00
|
|
|
LaunchNextActivity();
|
|
|
|
break;
|
2013-06-15 06:40:01 -04:00
|
|
|
case KeePass.ExitReloadDb:
|
2013-03-15 02:07:45 -04:00
|
|
|
//if the activity was killed, fill password/keyfile so the user can directly hit load again
|
2013-06-14 00:14:50 -04:00
|
|
|
if (App.Kp2a.GetDb().Loaded)
|
2013-03-15 02:07:45 -04:00
|
|
|
{
|
2013-06-15 06:40:01 -04:00
|
|
|
if (App.Kp2a.GetDb().KpDatabase.MasterKey.ContainsType(typeof(KcpPassword)))
|
2013-03-15 02:07:45 -04:00
|
|
|
{
|
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
KcpPassword kcpPassword = (KcpPassword)App.Kp2a.GetDb().KpDatabase.MasterKey.GetUserKey(typeof(KcpPassword));
|
2013-03-15 02:07:45 -04:00
|
|
|
String password = kcpPassword.Password.ReadString();
|
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
SetEditText(Resource.Id.password, password);
|
2013-03-15 02:07:45 -04:00
|
|
|
|
|
|
|
}
|
2013-06-15 06:40:01 -04:00
|
|
|
if (App.Kp2a.GetDb().KpDatabase.MasterKey.ContainsType(typeof(KcpKeyFile)))
|
2013-03-15 02:07:45 -04:00
|
|
|
{
|
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
KcpKeyFile kcpKeyfile = (KcpKeyFile)App.Kp2a.GetDb().KpDatabase.MasterKey.GetUserKey(typeof(KcpKeyFile));
|
2013-02-23 11:43:42 -05:00
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
SetEditText(Resource.Id.pass_keyfile, kcpKeyfile.Path);
|
2013-03-15 02:07:45 -04:00
|
|
|
}
|
|
|
|
}
|
2013-06-15 06:40:01 -04:00
|
|
|
UnloadDatabase();
|
2013-03-15 02:07:45 -04:00
|
|
|
break;
|
2013-06-15 06:40:01 -04:00
|
|
|
case Result.Ok:
|
|
|
|
if (requestCode == Intents.RequestCodeFileBrowseForKeyfile) {
|
2013-04-26 06:43:06 -04:00
|
|
|
string filename = Util.IntentToFilename(data);
|
2013-02-23 11:43:42 -05:00
|
|
|
if (filename != null) {
|
|
|
|
if (filename.StartsWith("file://")) {
|
|
|
|
filename = filename.Substring(7);
|
|
|
|
}
|
|
|
|
|
|
|
|
filename = URLDecoder.Decode(filename);
|
|
|
|
|
|
|
|
EditText fn = (EditText) FindViewById(Resource.Id.pass_keyfile);
|
|
|
|
fn.Text = filename;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
internal AppTask AppTask;
|
2013-02-23 11:43:42 -05:00
|
|
|
|
|
|
|
protected override void OnCreate(Bundle savedInstanceState)
|
|
|
|
{
|
|
|
|
base.OnCreate(savedInstanceState);
|
|
|
|
|
|
|
|
Intent i = Intent;
|
|
|
|
String action = i.Action;
|
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
_prefs = PreferenceManager.GetDefaultSharedPreferences(this);
|
|
|
|
_rememberKeyfile = _prefs.GetBoolean(GetString(Resource.String.keyfile_key), Resources.GetBoolean(Resource.Boolean.keyfile_default));
|
2013-02-23 11:43:42 -05:00
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
_ioConnection = new IOConnectionInfo();
|
2013-02-23 11:43:42 -05:00
|
|
|
|
2013-03-20 20:01:02 -04:00
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
if (action != null && action.Equals(ViewIntent))
|
2013-02-23 11:43:42 -05:00
|
|
|
{
|
2013-06-15 06:40:01 -04:00
|
|
|
_ioConnection.Path = i.DataString;
|
2013-02-23 11:43:42 -05:00
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
if (! _ioConnection.Path.Substring(0, 7).Equals("file://"))
|
2013-02-23 11:43:42 -05:00
|
|
|
{
|
|
|
|
//TODO: this might no longer be required as we can handle http(s) and ftp as well (but we need server credentials therefore)
|
|
|
|
Toast.MakeText(this, Resource.String.error_can_not_handle_uri, ToastLength.Long).Show();
|
|
|
|
Finish();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
_ioConnection.Path = URLDecoder.Decode(_ioConnection.Path.Substring(7));
|
2013-02-23 11:43:42 -05:00
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
if (_ioConnection.Path.Length == 0)
|
2013-02-23 11:43:42 -05:00
|
|
|
{
|
|
|
|
// No file name
|
|
|
|
Toast.MakeText(this, Resource.String.FileNotFound, ToastLength.Long).Show();
|
|
|
|
Finish();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
File dbFile = new File(_ioConnection.Path);
|
2013-02-23 11:43:42 -05:00
|
|
|
if (! dbFile.Exists())
|
|
|
|
{
|
|
|
|
// File does not exist
|
|
|
|
Toast.MakeText(this, Resource.String.FileNotFound, ToastLength.Long).Show();
|
|
|
|
Finish();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
_keyFile = GetKeyFile(_ioConnection.Path);
|
2013-02-23 11:43:42 -05:00
|
|
|
|
|
|
|
} else
|
|
|
|
{
|
2013-06-15 06:40:01 -04:00
|
|
|
SetIoConnectionFromIntent(_ioConnection, i);
|
|
|
|
_keyFile = i.GetStringExtra(KeyKeyfile);
|
|
|
|
if (string.IsNullOrEmpty(_keyFile))
|
2013-02-23 11:43:42 -05:00
|
|
|
{
|
2013-06-15 06:40:01 -04:00
|
|
|
_keyFile = GetKeyFile(_ioConnection.Path);
|
2013-02-23 11:43:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
AppTask = AppTask.GetTaskInOnCreate(savedInstanceState, Intent);
|
2013-02-23 11:43:42 -05:00
|
|
|
|
|
|
|
SetContentView(Resource.Layout.password);
|
2013-06-15 06:40:01 -04:00
|
|
|
PopulateView();
|
2013-06-01 13:24:42 -04:00
|
|
|
|
|
|
|
EditText passwordEdit = FindViewById<EditText>(Resource.Id.password);
|
|
|
|
|
|
|
|
|
|
|
|
passwordEdit.RequestFocus();
|
|
|
|
Window.SetSoftInputMode(SoftInput.StateVisible);
|
|
|
|
|
2013-02-23 11:43:42 -05:00
|
|
|
Button confirmButton = (Button)FindViewById(Resource.Id.pass_ok);
|
2013-06-15 06:40:01 -04:00
|
|
|
confirmButton.Click += (sender, e) =>
|
2013-02-23 11:43:42 -05:00
|
|
|
{
|
|
|
|
String pass = GetEditText(Resource.Id.password);
|
|
|
|
String key = GetEditText(Resource.Id.pass_keyfile);
|
|
|
|
if (pass.Length == 0 && key.Length == 0)
|
|
|
|
{
|
2013-06-15 06:40:01 -04:00
|
|
|
ErrorMessage(Resource.String.error_nopass);
|
2013-02-23 11:43:42 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear before we load
|
2013-06-15 06:40:01 -04:00
|
|
|
UnloadDatabase();
|
2013-02-23 11:43:42 -05:00
|
|
|
|
|
|
|
// Clear the shutdown flag
|
2013-06-15 06:40:01 -04:00
|
|
|
App.Kp2a.ClearShutdown();
|
2013-02-23 11:43:42 -05:00
|
|
|
|
|
|
|
CheckBox cbQuickUnlock = (CheckBox)FindViewById(Resource.Id.enable_quickunlock);
|
2013-06-14 00:14:50 -04:00
|
|
|
App.Kp2a.GetDb().QuickUnlockEnabled = cbQuickUnlock.Checked;
|
2013-06-15 06:40:01 -04:00
|
|
|
App.Kp2a.GetDb().QuickUnlockKeyLength = int.Parse(_prefs.GetString(GetString(Resource.String.QuickUnlockLength_key), GetString(Resource.String.QuickUnlockLength_default)));
|
2013-02-23 11:43:42 -05:00
|
|
|
|
|
|
|
Handler handler = new Handler();
|
2013-06-15 06:40:01 -04:00
|
|
|
LoadDb task = new LoadDb(App.Kp2a, _ioConnection, pass, key, new AfterLoad(handler, this));
|
2013-07-09 03:59:17 -04:00
|
|
|
ProgressTask pt = new ProgressTask(App.Kp2a, this, task);
|
2013-06-15 16:02:48 -04:00
|
|
|
pt.Run();
|
2013-02-23 11:43:42 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
/*CheckBox checkBox = (CheckBox) FindViewById(Resource.Id.show_password);
|
|
|
|
// Show or hide password
|
|
|
|
checkBox.CheckedChange += delegate(object sender, CompoundButton.CheckedChangeEventArgs e) {
|
|
|
|
|
|
|
|
TextView password = (TextView) FindViewById(Resource.Id.password);
|
|
|
|
if ( e.IsChecked ) {
|
|
|
|
password.InputType = InputTypes.ClassText | InputTypes.TextVariationVisiblePassword;
|
|
|
|
} else {
|
|
|
|
password.InputType = InputTypes.ClassText | InputTypes.TextVariationPassword;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
*/
|
|
|
|
ImageButton btnTogglePassword = (ImageButton)FindViewById(Resource.Id.toggle_password);
|
2013-06-15 06:40:01 -04:00
|
|
|
btnTogglePassword.Click += (sender, e) => {
|
|
|
|
_showPassword = !_showPassword;
|
2013-02-23 11:43:42 -05:00
|
|
|
TextView password = (TextView)FindViewById(Resource.Id.password);
|
2013-06-15 06:40:01 -04:00
|
|
|
if (_showPassword)
|
2013-02-23 11:43:42 -05:00
|
|
|
{
|
|
|
|
password.InputType = InputTypes.ClassText | InputTypes.TextVariationVisiblePassword;
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
password.InputType = InputTypes.ClassText | InputTypes.TextVariationPassword;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
CheckBox defaultCheck = (CheckBox)FindViewById(Resource.Id.default_database);
|
2013-06-15 06:40:01 -04:00
|
|
|
//Don't allow the current file to be the default if we don't have stored credentials
|
|
|
|
if ((_ioConnection.IsLocalFile() == false) && (_ioConnection.CredSaveMode != IOCredSaveMode.SaveCred))
|
2013-02-23 11:43:42 -05:00
|
|
|
{
|
|
|
|
defaultCheck.Enabled = false;
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
defaultCheck.Enabled = true;
|
|
|
|
}
|
2013-06-15 06:40:01 -04:00
|
|
|
defaultCheck.CheckedChange += (sender, e) =>
|
2013-02-23 11:43:42 -05:00
|
|
|
{
|
|
|
|
String newDefaultFileName;
|
|
|
|
|
|
|
|
if (e.IsChecked)
|
|
|
|
{
|
2013-06-15 06:40:01 -04:00
|
|
|
newDefaultFileName = _ioConnection.Path;
|
2013-02-23 11:43:42 -05:00
|
|
|
} else
|
|
|
|
{
|
|
|
|
newDefaultFileName = "";
|
|
|
|
}
|
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
ISharedPreferencesEditor editor = _prefs.Edit();
|
|
|
|
editor.PutString(KeyDefaultFilename, newDefaultFileName);
|
|
|
|
EditorCompat.Apply(editor);
|
2013-02-23 11:43:42 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
ImageButton browse = (ImageButton)FindViewById(Resource.Id.browse_button);
|
2013-06-15 06:40:01 -04:00
|
|
|
browse.Click += (sender, evt) =>
|
2013-02-23 11:43:42 -05:00
|
|
|
{
|
2013-04-26 06:43:06 -04:00
|
|
|
string filename = null;
|
2013-06-15 06:40:01 -04:00
|
|
|
if (!String.IsNullOrEmpty(_ioConnection.Path))
|
2013-02-23 11:43:42 -05:00
|
|
|
{
|
2013-06-15 06:40:01 -04:00
|
|
|
File keyfile = new File(_ioConnection.Path);
|
2013-04-26 06:43:06 -04:00
|
|
|
File parent = keyfile.ParentFile;
|
|
|
|
if (parent != null)
|
2013-02-23 11:43:42 -05:00
|
|
|
{
|
2013-04-26 06:43:06 -04:00
|
|
|
filename = parent.AbsolutePath;
|
2013-02-23 11:43:42 -05:00
|
|
|
}
|
|
|
|
}
|
2013-06-15 06:40:01 -04:00
|
|
|
Util.showBrowseDialog(filename, this, Intents.RequestCodeFileBrowseForKeyfile, false);
|
2013-04-26 06:43:06 -04:00
|
|
|
|
2013-02-23 11:43:42 -05:00
|
|
|
};
|
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
RetrieveSettings();
|
2013-02-23 11:43:42 -05:00
|
|
|
|
|
|
|
|
|
|
|
}
|
2013-05-30 00:54:25 -04:00
|
|
|
|
|
|
|
protected override void OnSaveInstanceState(Bundle outState)
|
|
|
|
{
|
|
|
|
base.OnSaveInstanceState(outState);
|
2013-06-15 06:40:01 -04:00
|
|
|
AppTask.ToBundle(outState);
|
2013-05-30 00:54:25 -04:00
|
|
|
}
|
2013-02-23 11:43:42 -05:00
|
|
|
|
|
|
|
protected override void OnResume() {
|
|
|
|
base.OnResume();
|
|
|
|
|
|
|
|
// If the application was shutdown make sure to clear the password field, if it
|
|
|
|
// was saved in the instance state
|
2013-06-15 06:40:01 -04:00
|
|
|
if (App.Kp2a.IsShutdown()) {
|
|
|
|
LockDatabase();
|
2013-02-23 11:43:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Clear the shutdown flag
|
2013-06-15 06:40:01 -04:00
|
|
|
App.Kp2a.ClearShutdown();
|
2013-02-23 11:43:42 -05:00
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
if (_startedWithActivityResult)
|
2013-02-23 11:43:42 -05:00
|
|
|
return;
|
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
if (App.Kp2a.GetDb().Loaded && (App.Kp2a.GetDb().Ioc != null)
|
|
|
|
&& (_ioConnection != null) && (App.Kp2a.GetDb().Ioc.GetDisplayName() == _ioConnection.GetDisplayName()))
|
2013-02-23 11:43:42 -05:00
|
|
|
{
|
2013-06-14 00:14:50 -04:00
|
|
|
if (App.Kp2a.GetDb().Locked == false)
|
2013-02-23 11:43:42 -05:00
|
|
|
{
|
|
|
|
LaunchNextActivity();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-15 06:40:01 -04:00
|
|
|
TryStartQuickUnlock();
|
2013-02-23 11:43:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
private void RetrieveSettings() {
|
|
|
|
String defaultFilename = _prefs.GetString(KeyDefaultFilename, "");
|
|
|
|
if (!String.IsNullOrEmpty(_ioConnection.Path) && _ioConnection.Path.Equals(defaultFilename)) {
|
2013-02-23 11:43:42 -05:00
|
|
|
CheckBox checkbox = (CheckBox) FindViewById(Resource.Id.default_database);
|
|
|
|
checkbox.Checked = true;
|
|
|
|
}
|
|
|
|
CheckBox cbQuickUnlock = (CheckBox)FindViewById(Resource.Id.enable_quickunlock);
|
2013-06-15 06:40:01 -04:00
|
|
|
cbQuickUnlock.Checked = _prefs.GetBoolean(GetString(Resource.String.QuickUnlockDefaultEnabled_key), true);
|
2013-02-23 11:43:42 -05:00
|
|
|
}
|
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
private String GetKeyFile(String filename) {
|
|
|
|
if ( _rememberKeyfile ) {
|
|
|
|
FileDbHelper dbHelp = App.Kp2a.FileDbHelper;
|
2013-02-23 11:43:42 -05:00
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
String keyfile = dbHelp.GetFileByName(filename);
|
2013-02-23 11:43:42 -05:00
|
|
|
|
|
|
|
return keyfile;
|
|
|
|
} else {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
private void PopulateView() {
|
|
|
|
SetEditText(Resource.Id.filename, _ioConnection.GetDisplayName());
|
|
|
|
SetEditText(Resource.Id.pass_keyfile, _keyFile);
|
2013-02-23 11:43:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
private void errorMessage(CharSequence text)
|
|
|
|
{
|
|
|
|
Toast.MakeText(this, text, ToastLength.Long).Show();
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
private void ErrorMessage(int resId)
|
2013-02-23 11:43:42 -05:00
|
|
|
{
|
|
|
|
Toast.MakeText(this, resId, ToastLength.Long).Show();
|
|
|
|
}
|
|
|
|
|
|
|
|
private String GetEditText(int resId) {
|
2013-06-25 15:27:41 -04:00
|
|
|
return Util.GetEditText(this, resId);
|
2013-02-23 11:43:42 -05:00
|
|
|
}
|
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
private void SetEditText(int resId, String str) {
|
2013-02-23 11:43:42 -05:00
|
|
|
TextView te = (TextView) FindViewById(resId);
|
|
|
|
//assert(te == null);
|
|
|
|
|
|
|
|
if (te != null) {
|
|
|
|
te.Text = str;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public override bool OnCreateOptionsMenu(IMenu menu) {
|
|
|
|
base.OnCreateOptionsMenu(menu);
|
|
|
|
|
|
|
|
MenuInflater inflate = MenuInflater;
|
|
|
|
inflate.Inflate(Resource.Menu.password, menu);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public override bool OnOptionsItemSelected(IMenuItem item) {
|
|
|
|
switch ( item.ItemId ) {
|
|
|
|
case Resource.Id.menu_about:
|
|
|
|
AboutDialog dialog = new AboutDialog(this);
|
|
|
|
dialog.Show();
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case Resource.Id.menu_app_settings:
|
|
|
|
AppSettingsActivity.Launch(this);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return base.OnOptionsItemSelected(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
private class AfterLoad : OnFinish {
|
2013-06-15 06:40:01 -04:00
|
|
|
readonly PasswordActivity _act;
|
2013-02-23 11:43:42 -05:00
|
|
|
public AfterLoad(Handler handler, PasswordActivity act):base(handler) {
|
2013-06-15 16:02:48 -04:00
|
|
|
_act = act;
|
2013-02-23 11:43:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
public override void Run() {
|
|
|
|
if ( Success ) {
|
|
|
|
_act.StartQuickUnlockForegroundService();
|
|
|
|
_act.LaunchNextActivity();
|
2013-02-23 11:43:42 -05:00
|
|
|
} else {
|
2013-06-15 06:40:01 -04:00
|
|
|
DisplayMessage(_act);
|
2013-02-23 11:43:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|