Implemented option to add URL to entry selected after Search URL

This commit is contained in:
Philipp Crocoll 2014-01-26 06:51:16 -08:00
parent dc4088e7c7
commit 89546c1427
6 changed files with 493 additions and 352 deletions

View File

@ -1400,20 +1400,19 @@ public class KP2AKeyboard extends InputMethodService
} }
StringForTyping openOrChangeEntry = new StringForTyping();
if (keepass2android.kbbridge.KeyboardData.entryName == null) if (keepass2android.kbbridge.KeyboardData.entryName == null)
{ {
StringForTyping openEntry = new StringForTyping(); openOrChangeEntry.displayName = openOrChangeEntry.key = getString(R.string.open_entry);
openEntry.displayName = openEntry.key = getString(R.string.open_entry);
openEntry.value = "KP2ASPECIAL_SelectEntryTask";
items.add(openEntry);
} }
else else
{ {
StringForTyping changeEntry = new StringForTyping(); openOrChangeEntry.displayName = openOrChangeEntry.key = getString(R.string.change_entry);
changeEntry.displayName = changeEntry.key = getString(R.string.change_entry);
changeEntry.value = "KP2ASPECIAL_SelectEntryTask";
items.add(changeEntry);
} }
openOrChangeEntry.value = "KP2ASPECIAL_SelectEntryTask";
items.add(openOrChangeEntry);
final String clientPackageName = attribute.packageName; final String clientPackageName = attribute.packageName;

View File

@ -120,8 +120,6 @@ namespace keepass2android
_appTask = AppTask.GetTaskInOnCreate(savedInstanceState, Intent); _appTask = AppTask.GetTaskInOnCreate(savedInstanceState, Intent);
bool closeAfterCreate = _appTask.CloseEntryActivityAfterCreate;
Entry = db.Entries [uuid]; Entry = db.Entries [uuid];
// Refresh Menu contents in case onCreateMenuOptions was called before _entry was set // Refresh Menu contents in case onCreateMenuOptions was called before _entry was set
@ -145,9 +143,17 @@ namespace keepass2android
SetupEditButtons(); SetupEditButtons();
//depending on the app task, the final things to do might be delayed, so let the appTask call CompleteOnCreate when appropriate
_appTask.OnCompleteCreateEntryActivity(this);
}
public void CompleteOnCreate()
{
Intent showNotIntent = new Intent(this, typeof (CopyToClipboardService)); Intent showNotIntent = new Intent(this, typeof (CopyToClipboardService));
Intent.SetAction(Intents.ShowNotification); Intent.SetAction(Intents.ShowNotification);
showNotIntent.PutExtra(KeyEntry, Entry.Uuid.ToHexString()); showNotIntent.PutExtra(KeyEntry, Entry.Uuid.ToHexString());
bool closeAfterCreate = _appTask.CloseEntryActivityAfterCreate;
showNotIntent.PutExtra(KeyCloseAfterCreate, closeAfterCreate); showNotIntent.PutExtra(KeyCloseAfterCreate, closeAfterCreate);
StartService(showNotIntent); StartService(showNotIntent);
@ -647,6 +653,73 @@ namespace keepass2android
return base.OnOptionsItemSelected(item); return base.OnOptionsItemSelected(item);
} }
/// <summary>
/// brings up a dialog asking the user whether he wants to add the given URL to the entry for automatic finding
/// </summary>
public void AskAddUrlThenCompleteCreate(string url)
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.SetTitle(GetString(Resource.String.AddUrlToEntryDialog_title));
builder.SetMessage(GetString(Resource.String.AddUrlToEntryDialog_text, new Java.Lang.Object[] { url } ));
builder.SetPositiveButton(GetString(Resource.String.yes), (dlgSender, dlgEvt) =>
{
AddUrlToEntryThenCompleteCreate(url);
});
builder.SetNegativeButton(GetString(Resource.String.no), (dlgSender, dlgEvt) =>
{
CompleteOnCreate();
});
Dialog dialog = builder.Create();
dialog.Show();
}
private void AddUrlToEntryThenCompleteCreate(string url)
{
PwEntry initialEntry = Entry.CloneDeep();
PwEntry newEntry = Entry;
newEntry.History = newEntry.History.CloneDeep();
newEntry.CreateBackup(null);
newEntry.Touch(true, false); // Touch *after* backup
//if there is no URL in the entry, set that field. If it's already in use, use an additional (not existing) field
if (String.IsNullOrEmpty(newEntry.Strings.ReadSafe(PwDefs.UrlField)))
{
newEntry.Strings.Set(PwDefs.UrlField, new ProtectedString(false, url));
}
else
{
int c = 1;
while (newEntry.Strings.Get("KP2A_URL_" + c) != null)
{
c++;
}
newEntry.Strings.Set("KP2A_URL_" + c, new ProtectedString(false, url));
}
//save the entry:
ActionOnFinish closeOrShowError = new ActionOnFinish((success, message) =>
{
OnFinish.DisplayMessage(this, message);
CompleteOnCreate();
});
RunnableOnFinish runnable = new UpdateEntry(this, App.Kp2a, initialEntry, newEntry, closeOrShowError);
ProgressTask pt = new ProgressTask(App.Kp2a, this, runnable);
pt.Run();
}
} }
} }

File diff suppressed because it is too large Load Diff

View File

@ -224,6 +224,10 @@
<string name="SaveAttachment_doneMessage">Saved file to %1$s.</string> <string name="SaveAttachment_doneMessage">Saved file to %1$s.</string>
<string name="SaveAttachment_Failed">Could not save attachment to %1$s.</string> <string name="SaveAttachment_Failed">Could not save attachment to %1$s.</string>
<string name="AddUrlToEntryDialog_title">Remember search text?</string>
<string name="AddUrlToEntryDialog_text">Would you like to store the search text "%1$s" in the selected entry in order to find it automatically next time?</string>
<string name="error_invalid_expiry_date">Invalid date/time format for expiry date!</string> <string name="error_invalid_expiry_date">Invalid date/time format for expiry date!</string>
<string name="error_string_key">A field name is required for each string.</string> <string name="error_string_key">A field name is required for each string.</string>
<string name="field_name">Field Name</string> <string name="field_name">Field Name</string>

View File

@ -150,7 +150,7 @@ namespace keepass2android
View selectOtherEntry = FindViewById (Resource.Id.select_other_entry); View selectOtherEntry = FindViewById (Resource.Id.select_other_entry);
selectOtherEntry.Click += (sender, e) => { selectOtherEntry.Click += (sender, e) => {
GroupActivity.Launch (this, new SelectEntryTask()); GroupActivity.Launch (this, new SelectEntryForUrlTask(url));
}; };

View File

@ -229,6 +229,11 @@ namespace keepass2android
act.Intent.RemoveExtra(AppTaskKey); act.Intent.RemoveExtra(AppTaskKey);
} }
public virtual void OnCompleteCreateEntryActivity(EntryActivity entryActivity)
{
entryActivity.CompleteOnCreate();
}
} }
/// <summary> /// <summary>
@ -290,6 +295,60 @@ namespace keepass2android
} }
} }
/// <summary>
/// User is about to select an entry. When selected, ask whether the url he was searching for earlier should be stored
/// in the selected entry for later use.
/// </summary>
public class SelectEntryForUrlTask: AppTask
{
/// <summary>
/// default constructor for creating from Bundle
/// </summary>
public SelectEntryForUrlTask()
{
}
public SelectEntryForUrlTask(string url)
{
UrlToSearchFor = url;
}
public const String UrlToSearchKey = "UrlToSearch";
public string UrlToSearchFor
{
get;
set;
}
public override void Setup(Bundle b)
{
UrlToSearchFor = b.GetString(UrlToSearchKey);
}
public override IEnumerable<IExtra> Extras
{
get
{
yield return new StringExtra { Key = UrlToSearchKey, Value = UrlToSearchFor };
}
}
public override bool CloseEntryActivityAfterCreate
{
get { return true; }
}
public override void OnCompleteCreateEntryActivity(EntryActivity entryActivity)
{
//if the database is readonly, don't offer to modify the URL
if (App.Kp2a.GetDb().CanWrite == false)
base.OnCompleteCreateEntryActivity(entryActivity);
entryActivity.AskAddUrlThenCompleteCreate(UrlToSearchFor);
}
}
/// <summary> /// <summary>
/// User is about to move an entry or group to another group /// User is about to move an entry or group to another group
/// </summary> /// </summary>