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 System.Collections.Generic;
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
using Android.App;
|
|
|
|
using Android.Content;
|
|
|
|
using Android.OS;
|
|
|
|
using Android.Views;
|
|
|
|
using Android.Widget;
|
|
|
|
using KeePassLib;
|
|
|
|
using KeePassLib.Utility;
|
|
|
|
using Android.Preferences;
|
|
|
|
using Android.Text.Method;
|
|
|
|
using System.Globalization;
|
|
|
|
using Android.Content.PM;
|
|
|
|
using KeePassLib.Security;
|
|
|
|
using Android.Webkit;
|
2013-04-26 04:10:46 -04:00
|
|
|
using Android.Graphics;
|
2013-04-28 16:46:31 -04:00
|
|
|
using Java.IO;
|
2013-02-23 11:43:42 -05:00
|
|
|
|
|
|
|
namespace keepass2android
|
|
|
|
{
|
|
|
|
[Activity (Label = "@string/app_name", ConfigurationChanges=ConfigChanges.Orientation|ConfigChanges.KeyboardHidden, Theme="@style/NoTitleBar")]
|
|
|
|
public class EntryActivity : LockCloseActivity {
|
2013-06-15 06:40:01 -04:00
|
|
|
public const String KeyEntry = "entry";
|
|
|
|
public const String KeyRefreshPos = "refresh_pos";
|
|
|
|
public const String KeyCloseAfterCreate = "close_after_create";
|
2013-02-23 11:43:42 -05:00
|
|
|
|
|
|
|
|
2013-05-30 02:29:08 -04:00
|
|
|
public static void Launch(Activity act, PwEntry pw, int pos, AppTask appTask) {
|
2013-06-15 06:40:01 -04:00
|
|
|
Intent i = new Intent(act, typeof(EntryActivity));
|
2013-02-23 11:43:42 -05:00
|
|
|
|
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
i.PutExtra(KeyEntry, pw.Uuid.ToHexString());
|
|
|
|
i.PutExtra(KeyRefreshPos, pos);
|
2013-05-30 00:54:25 -04:00
|
|
|
|
|
|
|
appTask.ToIntent(i);
|
2013-02-23 11:43:42 -05:00
|
|
|
|
|
|
|
act.StartActivityForResult(i,0);
|
|
|
|
}
|
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
protected PwEntry Entry;
|
2013-02-23 11:43:42 -05:00
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
private bool _showPassword;
|
|
|
|
private int _pos;
|
2013-02-23 11:43:42 -05:00
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
AppTask _appTask;
|
2013-02-23 11:43:42 -05:00
|
|
|
|
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
protected void SetEntryView() {
|
2013-02-23 11:43:42 -05:00
|
|
|
SetContentView(Resource.Layout.entry_view);
|
|
|
|
}
|
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
protected void SetupEditButtons() {
|
2013-04-12 23:28:15 -04:00
|
|
|
View edit = FindViewById(Resource.Id.entry_edit);
|
2013-02-23 11:43:42 -05:00
|
|
|
edit.Click += (sender, e) => {
|
2013-06-15 06:40:01 -04:00
|
|
|
EntryEditActivity.Launch(this, Entry,_appTask);
|
2013-02-23 11:43:42 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void OnCreate(Bundle savedInstanceState)
|
|
|
|
{
|
|
|
|
ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(this);
|
2013-03-06 15:25:30 -05:00
|
|
|
|
|
|
|
long usageCount = prefs.GetLong(GetString(Resource.String.UsageCount_key), 0);
|
|
|
|
|
|
|
|
ISharedPreferencesEditor edit = prefs.Edit();
|
|
|
|
edit.PutLong(GetString(Resource.String.UsageCount_key), usageCount+1);
|
2013-06-15 06:40:01 -04:00
|
|
|
EditorCompat.Apply(edit);
|
2013-03-06 15:25:30 -05:00
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
_showPassword = ! prefs.GetBoolean(GetString(Resource.String.maskpass_key), Resources.GetBoolean(Resource.Boolean.maskpass_default));
|
2013-02-23 11:43:42 -05:00
|
|
|
|
|
|
|
base.OnCreate(savedInstanceState);
|
2013-06-15 06:40:01 -04:00
|
|
|
SetEntryView();
|
2013-02-23 11:43:42 -05:00
|
|
|
|
2013-06-14 00:14:50 -04:00
|
|
|
Database db = App.Kp2a.GetDb();
|
2013-02-23 11:43:42 -05:00
|
|
|
// Likely the app has been killed exit the activity
|
|
|
|
if (! db.Loaded)
|
|
|
|
{
|
|
|
|
Finish();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
SetResult(KeePass.ExitNormal);
|
2013-02-23 11:43:42 -05:00
|
|
|
|
|
|
|
Intent i = Intent;
|
2013-06-15 06:40:01 -04:00
|
|
|
PwUuid uuid = new PwUuid(MemUtil.HexStringToByteArray(i.GetStringExtra(KeyEntry)));
|
|
|
|
_pos = i.GetIntExtra(KeyRefreshPos, -1);
|
2013-02-23 11:43:42 -05:00
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
_appTask = AppTask.GetTaskInOnCreate(savedInstanceState, Intent);
|
2013-05-30 00:54:25 -04:00
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
bool closeAfterCreate = _appTask.CloseEntryActivityAfterCreate;
|
2013-02-23 11:43:42 -05:00
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
Entry = db.Entries [uuid];
|
2013-02-23 11:43:42 -05:00
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
// Refresh Menu contents in case onCreateMenuOptions was called before _entry was set
|
|
|
|
ActivityCompat.InvalidateOptionsMenu(this);
|
2013-02-23 11:43:42 -05:00
|
|
|
|
|
|
|
// Update last access time.
|
2013-06-15 06:40:01 -04:00
|
|
|
Entry.Touch(false);
|
2013-02-23 11:43:42 -05:00
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
if (PwDefs.IsTanEntry(Entry) && prefs.GetBoolean(GetString(Resource.String.TanExpiresOnUse_key), Resources.GetBoolean(Resource.Boolean.TanExpiresOnUse_default)) && ((Entry.Expires == false) || Entry.ExpiryTime > DateTime.Now))
|
2013-02-23 11:43:42 -05:00
|
|
|
{
|
2013-06-15 06:40:01 -04:00
|
|
|
PwEntry backupEntry = Entry.CloneDeep();
|
|
|
|
Entry.ExpiryTime = DateTime.Now;
|
|
|
|
Entry.Expires = true;
|
|
|
|
Entry.Touch(true);
|
|
|
|
RequiresRefresh();
|
|
|
|
UpdateEntry update = new UpdateEntry(this, App.Kp2a.GetDb(), backupEntry, Entry, null);
|
2013-06-14 00:14:50 -04:00
|
|
|
ProgressTask pt = new ProgressTask(App.Kp2a, this, update, UiStringKey.saving_database);
|
2013-06-18 15:12:34 -04:00
|
|
|
pt.Run();
|
2013-02-23 11:43:42 -05:00
|
|
|
}
|
2013-06-15 06:40:01 -04:00
|
|
|
FillData(false);
|
2013-02-23 11:43:42 -05:00
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
SetupEditButtons();
|
2013-02-23 11:43:42 -05:00
|
|
|
|
|
|
|
Intent showNotIntent = new Intent(this, typeof(CopyToClipboardService));
|
2013-06-15 06:40:01 -04:00
|
|
|
Intent.SetAction(Intents.ShowNotification);
|
|
|
|
showNotIntent.PutExtra(KeyEntry, Entry.Uuid.ToHexString());
|
|
|
|
showNotIntent.PutExtra(KeyCloseAfterCreate, closeAfterCreate);
|
2013-02-23 11:43:42 -05:00
|
|
|
|
|
|
|
StartService(showNotIntent);
|
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
Android.Util.Log.Debug("DEBUG", "Requesting copy to clipboard for Uuid=" + Entry.Uuid.ToHexString());
|
2013-02-23 11:43:42 -05:00
|
|
|
|
2013-06-14 00:14:50 -04:00
|
|
|
/*foreach (PwUuid key in App.Kp2a.GetDb().entries.Keys)
|
2013-02-23 11:43:42 -05:00
|
|
|
{
|
2013-06-14 00:14:50 -04:00
|
|
|
Android.Util.Log.Debug("DEBUG",key.ToHexString() + " -> " + App.Kp2a.GetDb().entries[key].Uuid.ToHexString());
|
2013-02-23 11:43:42 -05:00
|
|
|
}*/
|
|
|
|
|
|
|
|
if (closeAfterCreate)
|
|
|
|
{
|
2013-06-15 06:40:01 -04:00
|
|
|
SetResult(KeePass.ExitCloseAfterTaskComplete);
|
2013-02-23 11:43:42 -05:00
|
|
|
Finish();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
private String getDateTime(DateTime dt) {
|
2013-02-23 11:43:42 -05:00
|
|
|
return dt.ToString ("g", CultureInfo.CurrentUICulture);
|
|
|
|
}
|
|
|
|
|
|
|
|
String concatTags(List<string> tags)
|
|
|
|
{
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
foreach (string tag in tags)
|
|
|
|
{
|
|
|
|
sb.Append(tag);
|
|
|
|
sb.Append(", ");
|
|
|
|
}
|
|
|
|
if (tags.Count > 0)
|
|
|
|
sb.Remove(sb.Length-2,2);
|
|
|
|
return sb.ToString();
|
|
|
|
}
|
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
void PopulateExtraStrings(bool trimList)
|
2013-02-23 11:43:42 -05:00
|
|
|
{
|
|
|
|
ViewGroup extraGroup = (ViewGroup)FindViewById(Resource.Id.extra_strings);
|
|
|
|
if (trimList)
|
|
|
|
{
|
|
|
|
extraGroup.RemoveAllViews();
|
|
|
|
}
|
|
|
|
bool hasExtraFields = false;
|
2013-06-15 06:40:01 -04:00
|
|
|
foreach (KeyValuePair<string, ProtectedString> pair in Entry.Strings)
|
2013-02-23 11:43:42 -05:00
|
|
|
{
|
|
|
|
String key = pair.Key;
|
|
|
|
if (!PwDefs.IsStandardField(key))
|
|
|
|
{
|
2013-04-26 04:10:46 -04:00
|
|
|
//View view = new EntrySection(this, null, key, pair.Value.ReadString());
|
|
|
|
View view = CreateEditSection(key, pair.Value.ReadString());
|
2013-02-23 11:43:42 -05:00
|
|
|
extraGroup.AddView(view);
|
|
|
|
hasExtraFields = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
FindViewById(Resource.Id.entry_extra_strings_label).Visibility = hasExtraFields ? ViewStates.Visible : ViewStates.Gone;
|
|
|
|
}
|
|
|
|
|
2013-04-26 04:10:46 -04:00
|
|
|
View CreateEditSection(string key, string value)
|
|
|
|
{
|
2013-06-15 06:40:01 -04:00
|
|
|
LinearLayout layout = new LinearLayout(this, null) {Orientation = Orientation.Vertical};
|
|
|
|
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FillParent, ViewGroup.LayoutParams.WrapContent);
|
2013-04-26 04:10:46 -04:00
|
|
|
layoutParams.SetMargins(10,0,0,0);
|
|
|
|
layout.LayoutParameters = layoutParams;
|
|
|
|
View viewInflated = LayoutInflater.Inflate(Resource.Layout.entry_extrastring_title,null);
|
|
|
|
TextView keyView = (TextView)viewInflated;
|
|
|
|
if (key != null)
|
|
|
|
keyView.Text = key;
|
|
|
|
|
|
|
|
layout.AddView(keyView);
|
|
|
|
TextView valueView = (TextView)LayoutInflater.Inflate(Resource.Layout.entry_extrastring_value, null);
|
|
|
|
if (value != null)
|
|
|
|
valueView.Text = value;
|
|
|
|
valueView.Typeface = Typeface.Monospace;
|
2013-04-28 16:46:31 -04:00
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
if ((int)Build.VERSION.SdkInt >= 11)
|
2013-04-28 16:46:31 -04:00
|
|
|
valueView.SetTextIsSelectable(true);
|
2013-04-26 04:10:46 -04:00
|
|
|
layout.AddView(valueView);
|
|
|
|
return layout;
|
|
|
|
}
|
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
Android.Net.Uri WriteBinaryToFile(string key, bool writeToCacheDirectory)
|
2013-02-23 11:43:42 -05:00
|
|
|
{
|
2013-06-15 06:40:01 -04:00
|
|
|
ProtectedBinary pb = Entry.Binaries.Get(key);
|
2013-04-28 16:46:31 -04:00
|
|
|
System.Diagnostics.Debug.Assert(pb != null);
|
|
|
|
if (pb == null)
|
|
|
|
throw new ArgumentException();
|
2013-02-23 11:43:42 -05:00
|
|
|
|
|
|
|
ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(this);
|
2013-04-28 16:46:31 -04:00
|
|
|
string binaryDirectory = prefs.GetString(GetString(Resource.String.BinaryDirectory_key), GetString(Resource.String.BinaryDirectory_default));
|
|
|
|
if (writeToCacheDirectory)
|
|
|
|
binaryDirectory = CacheDir.Path;
|
2013-02-23 11:43:42 -05:00
|
|
|
|
2013-06-01 15:44:17 -04:00
|
|
|
string filepart = key;
|
|
|
|
if (writeToCacheDirectory)
|
|
|
|
filepart = filepart.Replace(" ", "");
|
2013-06-15 06:40:01 -04:00
|
|
|
var targetFile = new File(binaryDirectory, filepart);
|
2013-02-23 11:43:42 -05:00
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
File parent = targetFile.ParentFile;
|
2013-02-23 11:43:42 -05:00
|
|
|
|
|
|
|
if (parent == null || (parent.Exists() && ! parent.IsDirectory))
|
|
|
|
{
|
|
|
|
Toast.MakeText(this,
|
|
|
|
Resource.String.error_invalid_path,
|
|
|
|
ToastLength.Long).Show();
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! parent.Exists())
|
|
|
|
{
|
2013-04-28 16:46:31 -04:00
|
|
|
// Create parent directory
|
2013-02-23 11:43:42 -05:00
|
|
|
if (! parent.Mkdirs())
|
|
|
|
{
|
|
|
|
Toast.MakeText(this,
|
|
|
|
Resource.String.error_could_not_create_parent,
|
|
|
|
ToastLength.Long).Show();
|
|
|
|
return null;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
string filename = targetFile.AbsolutePath;
|
2013-04-28 16:46:31 -04:00
|
|
|
Android.Net.Uri fileUri = Android.Net.Uri.FromFile(targetFile);
|
2013-02-23 11:43:42 -05:00
|
|
|
|
|
|
|
byte[] pbData = pb.ReadData();
|
2013-04-28 16:46:31 -04:00
|
|
|
try
|
|
|
|
{
|
|
|
|
System.IO.File.WriteAllBytes(filename, pbData);
|
|
|
|
} catch (Exception exWrite)
|
2013-02-23 11:43:42 -05:00
|
|
|
{
|
|
|
|
Toast.MakeText(this, GetString(Resource.String.SaveAttachment_Failed, new Java.Lang.Object[]{ filename})
|
2013-04-28 16:46:31 -04:00
|
|
|
+ exWrite.Message, ToastLength.Long).Show();
|
2013-02-23 11:43:42 -05:00
|
|
|
return null;
|
2013-04-28 16:46:31 -04:00
|
|
|
} finally
|
2013-02-23 11:43:42 -05:00
|
|
|
{
|
|
|
|
MemUtil.ZeroByteArray(pbData);
|
|
|
|
}
|
2013-04-28 16:46:31 -04:00
|
|
|
Toast.MakeText(this, GetString(Resource.String.SaveAttachment_doneMessage, new Java.Lang.Object[]{filename}), ToastLength.Short).Show();
|
|
|
|
if (writeToCacheDirectory)
|
|
|
|
{
|
2013-06-15 06:40:01 -04:00
|
|
|
return Android.Net.Uri.Parse("content://" + AttachmentContentProvider.Authority + "/"
|
2013-04-28 16:46:31 -04:00
|
|
|
+ filename);
|
|
|
|
}
|
2013-06-15 06:40:01 -04:00
|
|
|
return fileUri;
|
2013-02-23 11:43:42 -05:00
|
|
|
}
|
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
void OpenBinaryFile(Android.Net.Uri uri)
|
2013-02-23 11:43:42 -05:00
|
|
|
{
|
2013-06-15 06:40:01 -04:00
|
|
|
String theMimeType = GetMimeType(uri.Path);
|
|
|
|
if (theMimeType != null)
|
2013-02-23 11:43:42 -05:00
|
|
|
{
|
|
|
|
Intent theIntent = new Intent(Intent.ActionView);
|
|
|
|
theIntent.AddFlags(ActivityFlags.NewTask | ActivityFlags.ExcludeFromRecents);
|
2013-06-15 06:40:01 -04:00
|
|
|
theIntent.SetDataAndType(uri, theMimeType);
|
2013-02-23 11:43:42 -05:00
|
|
|
try
|
|
|
|
{
|
|
|
|
StartActivity(theIntent);
|
|
|
|
}
|
2013-06-15 06:40:01 -04:00
|
|
|
catch (ActivityNotFoundException)
|
2013-02-23 11:43:42 -05:00
|
|
|
{
|
|
|
|
//ignore
|
|
|
|
Toast.MakeText(this, "Couldn't open file", ToastLength.Short).Show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
void PopulateBinaries(bool trimList)
|
2013-02-23 11:43:42 -05:00
|
|
|
{
|
|
|
|
ViewGroup binariesGroup = (ViewGroup)FindViewById(Resource.Id.binaries);
|
|
|
|
if (trimList)
|
|
|
|
{
|
|
|
|
binariesGroup.RemoveAllViews();
|
|
|
|
}
|
2013-06-15 06:40:01 -04:00
|
|
|
foreach (KeyValuePair<string, ProtectedBinary> pair in Entry.Binaries)
|
2013-02-23 11:43:42 -05:00
|
|
|
{
|
|
|
|
String key = pair.Key;
|
|
|
|
Button binaryButton = new Button(this);
|
2013-06-15 06:40:01 -04:00
|
|
|
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.FillParent, ViewGroup.LayoutParams.WrapContent);
|
2013-02-23 11:43:42 -05:00
|
|
|
binaryButton.Text = key;
|
|
|
|
binaryButton.SetCompoundDrawablesWithIntrinsicBounds( Resources.GetDrawable(Android.Resource.Drawable.IcMenuSave),null, null, null);
|
2013-06-15 06:40:01 -04:00
|
|
|
binaryButton.Click += (sender, e) =>
|
2013-02-23 11:43:42 -05:00
|
|
|
{
|
|
|
|
Button btnSender = (Button)(sender);
|
|
|
|
|
2013-04-28 16:46:31 -04:00
|
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(this);
|
|
|
|
builder.SetTitle(GetString(Resource.String.SaveAttachmentDialog_title));
|
|
|
|
|
|
|
|
builder.SetMessage(GetString(Resource.String.SaveAttachmentDialog_text));
|
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
builder.SetPositiveButton(GetString(Resource.String.SaveAttachmentDialog_save), (dlgSender, dlgEvt) =>
|
|
|
|
{
|
|
|
|
WriteBinaryToFile(btnSender.Text, false);
|
|
|
|
});
|
2013-04-28 16:46:31 -04:00
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
builder.SetNegativeButton(GetString(Resource.String.SaveAttachmentDialog_open), (dlgSender, dlgEvt) =>
|
2013-04-28 16:46:31 -04:00
|
|
|
{
|
2013-06-15 06:40:01 -04:00
|
|
|
Android.Net.Uri newUri = WriteBinaryToFile(btnSender.Text, true);
|
|
|
|
if (newUri != null)
|
|
|
|
{
|
|
|
|
OpenBinaryFile(newUri);
|
|
|
|
}
|
|
|
|
});
|
2013-04-28 16:46:31 -04:00
|
|
|
|
|
|
|
Dialog dialog = builder.Create();
|
|
|
|
dialog.Show();
|
|
|
|
|
2013-02-23 11:43:42 -05:00
|
|
|
|
|
|
|
};
|
|
|
|
binariesGroup.AddView(binaryButton,layoutParams);
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2013-06-15 06:40:01 -04:00
|
|
|
FindViewById(Resource.Id.entry_binaries_label).Visibility = Entry.Binaries.UCount > 0 ? ViewStates.Visible : ViewStates.Gone;
|
2013-02-23 11:43:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// url = file path or whatever suitable URL you want.
|
2013-06-15 06:40:01 -04:00
|
|
|
public static String GetMimeType(String url)
|
2013-02-23 11:43:42 -05:00
|
|
|
{
|
|
|
|
String type = null;
|
|
|
|
String extension = MimeTypeMap.GetFileExtensionFromUrl(url);
|
|
|
|
if (extension != null) {
|
|
|
|
MimeTypeMap mime = MimeTypeMap.Singleton;
|
|
|
|
type = mime.GetMimeTypeFromExtension(extension);
|
|
|
|
}
|
|
|
|
return type;
|
|
|
|
}
|
2013-05-17 00:53:58 -04:00
|
|
|
|
|
|
|
public override void OnBackPressed()
|
|
|
|
{
|
|
|
|
base.OnBackPressed();
|
|
|
|
OverridePendingTransition(Resource.Animation.anim_enter_back, Resource.Animation.anim_leave_back);
|
|
|
|
}
|
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
protected void FillData(bool trimList)
|
2013-02-23 11:43:42 -05:00
|
|
|
{
|
|
|
|
ImageView iv = (ImageView)FindViewById(Resource.Id.entry_icon);
|
2013-04-12 23:28:15 -04:00
|
|
|
if (iv != null)
|
|
|
|
{
|
2013-06-18 15:12:34 -04:00
|
|
|
App.Kp2a.GetDb().DrawableFactory.AssignDrawableTo(iv, Resources, App.Kp2a.GetDb().KpDatabase, Entry.IconId, Entry.CustomIconUuid);
|
2013-04-12 23:28:15 -04:00
|
|
|
}
|
2013-02-23 11:43:42 -05:00
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
//populateText(Resource.Id.entry_title, _entry.Strings.ReadSafe(PwDefs.TitleField));
|
2013-02-23 11:43:42 -05:00
|
|
|
var button = ((Button)FindViewById(Resource.Id.entry_title));
|
2013-04-12 23:28:15 -04:00
|
|
|
if (button != null)
|
|
|
|
{
|
2013-06-15 06:40:01 -04:00
|
|
|
button.Text = Entry.Strings.ReadSafe(PwDefs.TitleField);
|
|
|
|
button.Click += (sender, e) => {
|
2013-02-23 11:43:42 -05:00
|
|
|
Finish(); };
|
2013-04-12 23:28:15 -04:00
|
|
|
}
|
2013-04-28 16:46:31 -04:00
|
|
|
if (Util.HasActionBar(this))
|
2013-04-26 04:10:46 -04:00
|
|
|
{
|
2013-06-15 06:40:01 -04:00
|
|
|
ActionBar.Title = Entry.Strings.ReadSafe(PwDefs.TitleField);
|
2013-04-26 04:10:46 -04:00
|
|
|
ActionBar.SetDisplayHomeAsUpEnabled(true);
|
|
|
|
}
|
2013-06-15 06:40:01 -04:00
|
|
|
PopulateText(Resource.Id.entry_user_name, Resource.Id.entry_user_name_label, Entry.Strings.ReadSafe(PwDefs.UserNameField));
|
2013-02-23 11:43:42 -05:00
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
PopulateText(Resource.Id.entry_url, Resource.Id.entry_url_label, Entry.Strings.ReadSafe(PwDefs.UrlField));
|
|
|
|
PopulateText(Resource.Id.entry_password, Resource.Id.entry_password_label, Entry.Strings.ReadSafe(PwDefs.PasswordField));
|
|
|
|
SetPasswordStyle();
|
2013-02-23 11:43:42 -05:00
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
PopulateText(Resource.Id.entry_created, Resource.Id.entry_created_label, getDateTime(Entry.CreationTime));
|
|
|
|
PopulateText(Resource.Id.entry_modified, Resource.Id.entry_modified_label, getDateTime(Entry.LastModificationTime));
|
|
|
|
PopulateText(Resource.Id.entry_accessed, Resource.Id.entry_accessed_label, getDateTime(Entry.LastAccessTime));
|
2013-02-23 11:43:42 -05:00
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
if (Entry.Expires)
|
2013-02-23 11:43:42 -05:00
|
|
|
{
|
2013-06-15 06:40:01 -04:00
|
|
|
PopulateText(Resource.Id.entry_expires, Resource.Id.entry_expires_label, getDateTime(Entry.ExpiryTime));
|
2013-02-23 11:43:42 -05:00
|
|
|
} else
|
|
|
|
{
|
2013-06-15 06:40:01 -04:00
|
|
|
PopulateText(Resource.Id.entry_expires, Resource.Id.entry_expires_label, Resource.String.never);
|
2013-02-23 11:43:42 -05:00
|
|
|
}
|
2013-06-15 06:40:01 -04:00
|
|
|
PopulateText(Resource.Id.entry_comment, Resource.Id.entry_comment_label, Entry.Strings.ReadSafe(PwDefs.NotesField));
|
2013-02-23 11:43:42 -05:00
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
PopulateText(Resource.Id.entry_tags, Resource.Id.entry_tags_label, concatTags(Entry.Tags));
|
2013-02-23 11:43:42 -05:00
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
PopulateText(Resource.Id.entry_override_url, Resource.Id.entry_override_url_label, Entry.OverrideUrl);
|
2013-02-23 11:43:42 -05:00
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
PopulateExtraStrings(trimList);
|
2013-02-23 11:43:42 -05:00
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
PopulateBinaries(trimList);
|
2013-02-23 11:43:42 -05:00
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
private void PopulateText(int viewId, int headerViewId,int resId) {
|
2013-02-23 11:43:42 -05:00
|
|
|
View header = FindViewById(headerViewId);
|
|
|
|
TextView tv = (TextView)FindViewById(viewId);
|
|
|
|
|
|
|
|
header.Visibility = tv.Visibility = ViewStates.Visible;
|
|
|
|
tv.SetText (resId);
|
|
|
|
}
|
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
private void PopulateText(int viewId, int headerViewId, String text)
|
2013-02-23 11:43:42 -05:00
|
|
|
{
|
|
|
|
View header = FindViewById(headerViewId);
|
|
|
|
TextView tv = (TextView)FindViewById(viewId);
|
|
|
|
if (String.IsNullOrEmpty(text))
|
|
|
|
{
|
|
|
|
header.Visibility = tv.Visibility = ViewStates.Gone;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
header.Visibility = tv.Visibility = ViewStates.Visible;
|
|
|
|
tv.Text = text;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
void RequiresRefresh ()
|
2013-02-23 11:43:42 -05:00
|
|
|
{
|
|
|
|
Intent ret = new Intent ();
|
2013-06-15 06:40:01 -04:00
|
|
|
ret.PutExtra (KeyRefreshPos, _pos);
|
|
|
|
SetResult (KeePass.ExitRefresh, ret);
|
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
|
|
|
if ( resultCode == KeePass.ExitRefresh || resultCode == KeePass.ExitRefreshTitle ) {
|
|
|
|
FillData(true);
|
|
|
|
if ( resultCode == KeePass.ExitRefreshTitle ) {
|
|
|
|
RequiresRefresh ();
|
2013-02-23 11:43:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public override bool OnCreateOptionsMenu(IMenu menu) {
|
|
|
|
base.OnCreateOptionsMenu(menu);
|
|
|
|
|
|
|
|
MenuInflater inflater = MenuInflater;
|
|
|
|
inflater.Inflate(Resource.Menu.entry, menu);
|
|
|
|
|
|
|
|
IMenuItem togglePassword = menu.FindItem(Resource.Id.menu_toggle_pass);
|
2013-06-15 06:40:01 -04:00
|
|
|
if ( _showPassword ) {
|
2013-02-23 11:43:42 -05:00
|
|
|
togglePassword.SetTitle(Resource.String.menu_hide_password);
|
|
|
|
} else {
|
|
|
|
togglePassword.SetTitle(Resource.String.show_password);
|
|
|
|
}
|
|
|
|
|
|
|
|
IMenuItem gotoUrl = menu.FindItem(Resource.Id.menu_goto_url);
|
|
|
|
//Disabled IMenuItem copyUser = menu.FindItem(Resource.Id.menu_copy_user);
|
|
|
|
//Disabled IMenuItem copyPass = menu.FindItem(Resource.Id.menu_copy_pass);
|
|
|
|
|
|
|
|
// In API >= 11 onCreateOptionsMenu may be called before onCreate completes
|
2013-06-15 06:40:01 -04:00
|
|
|
// so _entry may not be set
|
|
|
|
if (Entry == null) {
|
2013-02-23 11:43:42 -05:00
|
|
|
gotoUrl.SetVisible(false);
|
|
|
|
//Disabled copyUser.SetVisible(false);
|
|
|
|
//Disabled copyPass.SetVisible(false);
|
|
|
|
}
|
|
|
|
else {
|
2013-06-15 06:40:01 -04:00
|
|
|
String url = Entry.Strings.ReadSafe (PwDefs.UrlField);
|
2013-02-23 11:43:42 -05:00
|
|
|
if (String.IsNullOrEmpty(url)) {
|
|
|
|
// disable button if url is not available
|
|
|
|
gotoUrl.SetVisible(false);
|
|
|
|
}
|
2013-06-15 06:40:01 -04:00
|
|
|
if ( String.IsNullOrEmpty(Entry.Strings.ReadSafe(PwDefs.UserNameField ))) {
|
2013-02-23 11:43:42 -05:00
|
|
|
// disable button if username is not available
|
|
|
|
//Disabled copyUser.SetVisible(false);
|
|
|
|
}
|
2013-06-15 06:40:01 -04:00
|
|
|
if ( String.IsNullOrEmpty(Entry.Strings.ReadSafe(PwDefs.PasswordField ))) {
|
2013-02-23 11:43:42 -05:00
|
|
|
// disable button if password is not available
|
|
|
|
//Disabled copyPass.SetVisible(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
private void SetPasswordStyle() {
|
2013-02-23 11:43:42 -05:00
|
|
|
TextView password = (TextView) FindViewById(Resource.Id.entry_password);
|
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
if ( _showPassword ) {
|
2013-02-23 11:43:42 -05:00
|
|
|
password.TransformationMethod = null;
|
|
|
|
} else {
|
|
|
|
password.TransformationMethod = PasswordTransformationMethod.Instance;
|
|
|
|
}
|
|
|
|
}
|
2013-04-28 16:46:31 -04:00
|
|
|
protected override void OnResume()
|
|
|
|
{
|
|
|
|
ClearCache();
|
|
|
|
base.OnResume();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void ClearCache() {
|
|
|
|
try {
|
|
|
|
File dir = CacheDir;
|
|
|
|
if (dir != null && dir.IsDirectory) {
|
2013-06-15 06:40:01 -04:00
|
|
|
DeleteDir(dir);
|
2013-04-28 16:46:31 -04:00
|
|
|
}
|
2013-06-15 06:40:01 -04:00
|
|
|
} catch (Exception) {
|
2013-04-28 16:46:31 -04:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2013-02-23 11:43:42 -05:00
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
public static bool DeleteDir(File dir) {
|
2013-04-28 16:46:31 -04:00
|
|
|
if (dir != null && dir.IsDirectory) {
|
|
|
|
String[] children = dir.List();
|
|
|
|
for (int i = 0; i < children.Length; i++) {
|
2013-06-15 06:40:01 -04:00
|
|
|
bool success = DeleteDir(new File(dir, children[i]));
|
2013-04-28 16:46:31 -04:00
|
|
|
if (!success) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The directory is now empty so delete it
|
|
|
|
return dir.Delete();
|
|
|
|
}
|
|
|
|
|
2013-02-23 11:43:42 -05:00
|
|
|
public override bool OnOptionsItemSelected(IMenuItem item) {
|
|
|
|
switch ( item.ItemId ) {
|
2013-04-06 14:26:16 -04:00
|
|
|
case Resource.Id.menu_donate:
|
2013-02-23 11:43:42 -05:00
|
|
|
try {
|
2013-04-09 16:59:33 -04:00
|
|
|
Util.gotoDonateUrl(this);
|
2013-02-23 11:43:42 -05:00
|
|
|
} catch (ActivityNotFoundException) {
|
|
|
|
Toast.MakeText(this, Resource.String.error_failed_to_launch_link, ToastLength.Long).Show();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-04-06 14:26:16 -04:00
|
|
|
return true;
|
2013-02-23 11:43:42 -05:00
|
|
|
case Resource.Id.menu_toggle_pass:
|
2013-06-15 06:40:01 -04:00
|
|
|
if ( _showPassword ) {
|
2013-02-23 11:43:42 -05:00
|
|
|
item.SetTitle(Resource.String.show_password);
|
2013-06-15 06:40:01 -04:00
|
|
|
_showPassword = false;
|
2013-02-23 11:43:42 -05:00
|
|
|
} else {
|
|
|
|
item.SetTitle(Resource.String.menu_hide_password);
|
2013-06-15 06:40:01 -04:00
|
|
|
_showPassword = true;
|
2013-02-23 11:43:42 -05:00
|
|
|
}
|
2013-06-15 06:40:01 -04:00
|
|
|
SetPasswordStyle();
|
2013-02-23 11:43:42 -05:00
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case Resource.Id.menu_goto_url:
|
2013-06-15 06:40:01 -04:00
|
|
|
string url = Entry.Strings.ReadSafe (PwDefs.UrlField);
|
2013-02-23 11:43:42 -05:00
|
|
|
|
|
|
|
// Default http:// if no protocol specified
|
|
|
|
if ( ! url.Contains("://") ) {
|
|
|
|
url = "http://" + url;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
Util.gotoUrl(this, url);
|
|
|
|
} catch (ActivityNotFoundException) {
|
|
|
|
Toast.MakeText(this, Resource.String.no_url_handler, ToastLength.Long).Show();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
/* TODO: required?
|
|
|
|
case Resource.Id.menu_copy_user:
|
2013-06-15 06:40:01 -04:00
|
|
|
timeoutCopyToClipboard(_entry.Strings.ReadSafe (PwDefs.UserNameField));
|
2013-02-23 11:43:42 -05:00
|
|
|
return true;
|
|
|
|
|
|
|
|
case Resource.Id.menu_copy_pass:
|
2013-06-15 06:40:01 -04:00
|
|
|
timeoutCopyToClipboard(_entry.Strings.ReadSafe (PwDefs.UserNameField));
|
2013-02-23 11:43:42 -05:00
|
|
|
return true;
|
|
|
|
*/
|
2013-03-23 16:19:37 -04:00
|
|
|
case Resource.Id.menu_rate:
|
|
|
|
try {
|
|
|
|
Util.gotoMarket(this);
|
|
|
|
} catch (ActivityNotFoundException) {
|
|
|
|
Toast.MakeText(this, Resource.String.no_url_handler, ToastLength.Long).Show();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
case Resource.Id.menu_suggest_improvements:
|
|
|
|
try {
|
|
|
|
Util.gotoUrl(this, Resource.String.SuggestionsURL);
|
|
|
|
} catch (ActivityNotFoundException) {
|
|
|
|
Toast.MakeText(this, Resource.String.no_url_handler, ToastLength.Long).Show();
|
|
|
|
}
|
|
|
|
return true;
|
2013-02-23 11:43:42 -05:00
|
|
|
case Resource.Id.menu_lock:
|
2013-06-14 00:14:50 -04:00
|
|
|
App.Kp2a.SetShutdown();
|
2013-06-15 06:40:01 -04:00
|
|
|
SetResult(KeePass.ExitLock);
|
2013-02-23 11:43:42 -05:00
|
|
|
Finish();
|
|
|
|
return true;
|
2013-03-23 16:19:37 -04:00
|
|
|
case Resource.Id.menu_translate:
|
|
|
|
try {
|
|
|
|
Util.gotoUrl(this, Resource.String.TranslationURL);
|
|
|
|
} catch (ActivityNotFoundException) {
|
|
|
|
Toast.MakeText(this, Resource.String.no_url_handler, ToastLength.Long).Show();
|
|
|
|
}
|
|
|
|
return true;
|
2013-04-26 04:10:46 -04:00
|
|
|
case Android.Resource.Id.Home:
|
|
|
|
//Currently the action bar only displays the home button when we come from a previous activity.
|
|
|
|
//So we can simply Finish. See this page for information on how to do this in more general (future?) cases:
|
|
|
|
//http://developer.android.com/training/implementing-navigation/ancestral.html
|
|
|
|
Finish();
|
2013-05-17 00:53:58 -04:00
|
|
|
OverridePendingTransition(Resource.Animation.anim_enter_back, Resource.Animation.anim_leave_back);
|
|
|
|
|
2013-03-23 16:19:37 -04:00
|
|
|
return true;
|
2013-02-23 11:43:42 -05:00
|
|
|
}
|
2013-03-23 16:19:37 -04:00
|
|
|
|
2013-02-23 11:43:42 -05:00
|
|
|
|
|
|
|
return base.OnOptionsItemSelected(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|