keepass2android/src/keepass2android/GroupBaseActivity.cs

363 lines
9.3 KiB
C#
Raw Normal View History

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 KeePassLib;
using Android.Preferences;
using keepass2android.view;
using Android.Graphics.Drawables;
2013-02-23 11:43:42 -05:00
namespace keepass2android
{
public abstract class GroupBaseActivity : LockCloseListActivity {
public const String KeyEntry = "entry";
public const String KeyMode = "mode";
2013-02-23 11:43:42 -05:00
public virtual void LaunchActivityForEntry(PwEntry pwEntry, int pos)
2013-02-23 11:43:42 -05:00
{
EntryActivity.Launch(this, pwEntry, pos, AppTask);
2013-02-23 11:43:42 -05:00
}
protected GroupBaseActivity ()
2013-02-23 11:43:42 -05:00
{
}
protected GroupBaseActivity (IntPtr javaReference, JniHandleOwnership transfer)
2013-02-23 11:43:42 -05:00
: base(javaReference, transfer)
{
}
protected override void OnSaveInstanceState(Bundle outState)
{
base.OnSaveInstanceState(outState);
AppTask.ToBundle(outState);
}
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
if (resultCode == KeePass.ExitCloseAfterTaskComplete)
{
SetResult(KeePass.ExitCloseAfterTaskComplete);
Finish();
}
}
2013-02-23 11:43:42 -05:00
private ISharedPreferences _prefs;
2013-02-23 11:43:42 -05:00
protected PwGroup Group;
internal AppTask AppTask;
2013-02-23 11:43:42 -05:00
protected override void OnResume() {
base.OnResume();
RefreshIfDirty();
2013-02-23 11:43:42 -05:00
}
public override bool OnSearchRequested()
{
StartActivityForResult(typeof(SearchActivity), 0);
return true;
}
public void RefreshIfDirty() {
Database db = App.Kp2a.GetDb();
if ( db.Dirty.Contains(Group) ) {
db.Dirty.Remove(Group);
2013-02-23 11:43:42 -05:00
BaseAdapter adapter = (BaseAdapter) ListAdapter;
adapter.NotifyDataSetChanged();
}
}
protected override void OnListItemClick(ListView l, View v, int position, long id) {
base.OnListItemClick(l, v, position, id);
IListAdapter adapt = ListAdapter;
2013-02-23 11:43:42 -05:00
ClickView cv = (ClickView) adapt.GetView(position, null, null);
cv.OnClick();
}
protected override void OnCreate(Bundle savedInstanceState) {
base.OnCreate(savedInstanceState);
AppTask = AppTask.GetTaskInOnCreate(savedInstanceState, Intent);
2013-02-23 11:43:42 -05:00
// Likely the app has been killed exit the activity
if ( ! App.Kp2a.GetDb().Loaded ) {
2013-02-23 11:43:42 -05:00
Finish();
return;
}
_prefs = PreferenceManager.GetDefaultSharedPreferences(this);
2013-02-23 11:43:42 -05:00
SetContentView(new GroupViewOnlyView(this));
SetResult(KeePass.ExitNormal);
2013-02-23 11:43:42 -05:00
StyleScrollBars();
2013-02-23 11:43:42 -05:00
}
protected void StyleScrollBars() {
2013-02-23 11:43:42 -05:00
ListView lv = ListView;
lv.ScrollBarStyle =ScrollbarStyles.InsideInset;
lv.TextFilterEnabled = true;
}
protected void SetGroupTitle()
2013-02-23 11:43:42 -05:00
{
String name = Group.Name;
String titleText;
bool clickable = (Group != null) && (Group.IsVirtual == false) && (Group.ParentGroup != null);
if (!String.IsNullOrEmpty(name))
{
titleText = name;
} else
{
titleText = GetText(Resource.String.root);
}
//see if the button for SDK Version < 11 is there
2013-02-23 11:43:42 -05:00
Button tv = (Button)FindViewById(Resource.Id.group_name);
if (tv != null)
2013-02-23 11:43:42 -05:00
{
if (Group != null)
{
tv.Text = titleText;
}
if (clickable)
2013-02-23 11:43:42 -05:00
{
tv.Click += (sender, e) =>
{
Finish();
};
2013-02-23 11:43:42 -05:00
} else
{
tv.SetCompoundDrawables(null, null, null, null);
tv.Clickable = false;
2013-02-23 11:43:42 -05:00
}
}
//ICS?
if (Util.HasActionBar(this))
2013-02-23 11:43:42 -05:00
{
ActionBar.Title = titleText;
if (clickable)
ActionBar.SetDisplayHomeAsUpEnabled(true);
2013-02-23 11:43:42 -05:00
}
}
protected void SetGroupIcon() {
if (Group != null) {
Drawable drawable = App.Kp2a.GetDb().DrawableFactory.GetIconDrawable(Resources, App.Kp2a.GetDb().KpDatabase, Group.IconId, Group.CustomIconUuid);
2013-02-23 11:43:42 -05:00
ImageView iv = (ImageView) FindViewById(Resource.Id.icon);
if (iv != null)
iv.SetImageDrawable(drawable);
if (Util.HasActionBar(this))
ActionBar.SetIcon(drawable);
2013-02-23 11:43:42 -05:00
}
}
public override bool OnCreateOptionsMenu(IMenu menu) {
base.OnCreateOptionsMenu(menu);
MenuInflater inflater = MenuInflater;
inflater.Inflate(Resource.Menu.group, menu);
if (Util.HasActionBar(this))
{
var searchManager = (SearchManager) GetSystemService(Context.SearchService);
var searchView = (SearchView) menu.FindItem(Resource.Id.menu_search).ActionView;
2013-06-19 13:44:35 -04:00
searchView.SetSearchableInfo(searchManager.GetSearchableInfo(ComponentName));
}
2013-02-23 11:43:42 -05:00
return true;
}
private void SetSortMenuText(IMenu menu) {
bool sortByName = _prefs.GetBoolean(GetString(Resource.String.sort_key), Resources.GetBoolean(Resource.Boolean.sort_default));
2013-02-23 11:43:42 -05:00
int resId;
if ( sortByName ) {
resId = Resource.String.sort_db;
} else {
resId = Resource.String.sort_name;
}
menu.FindItem(Resource.Id.menu_sort).SetTitle(resId);
}
public override bool OnPrepareOptionsMenu(IMenu menu) {
if ( ! base.OnPrepareOptionsMenu(menu) ) {
return false;
}
SetSortMenuText(menu);
2013-02-23 11:43:42 -05:00
return true;
}
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 {
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_lock:
2013-07-25 08:47:05 -04:00
App.Kp2a.LockDatabase();
2013-02-23 11:43:42 -05:00
return true;
2013-06-19 13:44:35 -04:00
2013-02-23 11:43:42 -05:00
case Resource.Id.menu_search:
2013-06-19 13:44:35 -04:00
case Resource.Id.menu_search_advanced:
2013-02-23 11:43:42 -05:00
OnSearchRequested();
return true;
case Resource.Id.menu_app_settings:
DatabaseSettingsActivity.Launch(this);
2013-02-23 11:43:42 -05:00
return true;
case Resource.Id.menu_change_master_key:
SetPassword();
2013-02-23 11:43:42 -05:00
return true;
case Resource.Id.menu_sort:
ToggleSort();
2013-02-23 11:43:42 -05:00
return true;
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;
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;
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);
return true;
2013-02-23 11:43:42 -05:00
}
return base.OnOptionsItemSelected(item);
}
private void ToggleSort() {
2013-02-23 11:43:42 -05:00
// Toggle setting
String sortKey = GetString(Resource.String.sort_key);
bool sortByName = _prefs.GetBoolean(sortKey, Resources.GetBoolean(Resource.Boolean.sort_default));
ISharedPreferencesEditor editor = _prefs.Edit();
2013-02-23 11:43:42 -05:00
editor.PutBoolean(sortKey, ! sortByName);
EditorCompat.Apply(editor);
2013-02-23 11:43:42 -05:00
// Refresh menu titles
ActivityCompat.InvalidateOptionsMenu(this);
2013-02-23 11:43:42 -05:00
// Mark all groups as dirty now to refresh them on load
Database db = App.Kp2a.GetDb();
db.MarkAllGroupsAsDirty();
2013-02-23 11:43:42 -05:00
// We'll manually refresh this group so we can remove it
db.Dirty.Remove(Group);
2013-02-23 11:43:42 -05:00
// Tell the adapter to refresh it's list
BaseAdapter adapter = (BaseAdapter) ListAdapter;
adapter.NotifyDataSetChanged();
}
private void SetPassword() {
2013-02-23 11:43:42 -05:00
SetPasswordDialog dialog = new SetPasswordDialog(this);
dialog.Show();
}
public class RefreshTask : OnFinish {
readonly GroupBaseActivity _act;
2013-02-23 11:43:42 -05:00
public RefreshTask(Handler handler, GroupBaseActivity act):base(handler) {
_act = act;
2013-02-23 11:43:42 -05:00
}
public override void Run() {
if ( Success) {
_act.RefreshIfDirty();
2013-02-23 11:43:42 -05:00
} else {
DisplayMessage(_act);
2013-02-23 11:43:42 -05:00
}
}
}
public class AfterDeleteGroup : OnFinish {
readonly GroupBaseActivity _act;
2013-02-23 11:43:42 -05:00
public AfterDeleteGroup(Handler handler, GroupBaseActivity act):base(handler) {
_act = act;
2013-02-23 11:43:42 -05:00
}
public override void Run() {
if ( Success) {
_act.RefreshIfDirty();
2013-02-23 11:43:42 -05:00
} else {
Handler.Post( () => {
Toast.MakeText(_act, "Unrecoverable error: " + Message, ToastLength.Long).Show();
2013-02-23 11:43:42 -05:00
});
2013-07-25 08:47:05 -04:00
App.Kp2a.LockDatabase();
2013-02-23 11:43:42 -05:00
}
}
}
}
}