FileSelectActivity is only shown when necessary or when explicitly selected by user

This commit is contained in:
Philipp Crocoll 2013-08-09 22:31:30 +02:00
parent 3b1b10385f
commit eaa426db33
9 changed files with 494 additions and 481 deletions

View File

@ -24,6 +24,7 @@ using Android.Preferences;
using Android.Content.PM; using Android.Content.PM;
using Android.Text; using Android.Text;
using Android.Text.Method; using Android.Text.Method;
using KeePassLib.Serialization;
namespace keepass2android namespace keepass2android
{ {
@ -103,7 +104,7 @@ namespace keepass2android
Dialog dialog = builder.Create(); Dialog dialog = builder.Create();
dialog.DismissEvent += (sender, e) => dialog.DismissEvent += (sender, e) =>
{ {
StartFileSelect(); LaunchNextActivity();
}; };
dialog.Show(); dialog.Show();
TextView message = (TextView) dialog.FindViewById(Android.Resource.Id.Message); TextView message = (TextView) dialog.FindViewById(Android.Resource.Id.Message);
@ -116,7 +117,7 @@ namespace keepass2android
} else } else
{ {
StartFileSelect(); LaunchNextActivity();
} }
@ -154,12 +155,42 @@ namespace keepass2android
} }
private void StartFileSelect() { IOConnectionInfo LoadIoc(string defaultFileName)
Intent intent = new Intent(this, typeof(FileSelectActivity)); {
//TEST Intent intent = new Intent(this, typeof(EntryActivity)); return App.Kp2a.FileDbHelper.CursorToIoc(App.Kp2a.FileDbHelper.FetchFileByName(defaultFileName));
//Intent intent = new Intent(this, typeof(SearchActivity)); }
//Intent intent = new Intent(this, typeof(QuickUnlock));
private void LaunchNextActivity() {
if (!App.Kp2a.GetDb().Loaded)
{
// Load default database
ISharedPreferences prefs = Android.Preferences.PreferenceManager.GetDefaultSharedPreferences(this);
String defaultFileName = prefs.GetString(PasswordActivity.KeyDefaultFilename, "");
if (defaultFileName.Length > 0)
{
try
{
PasswordActivity.Launch(this, LoadIoc(defaultFileName), _appTask);
Finish();
return;
}
catch (Exception e)
{
Toast.MakeText(this, e.Message, ToastLength.Long);
// Ignore exception
}
}
}
else
{
PasswordActivity.Launch(this, App.Kp2a.GetDb().Ioc, _appTask);
Finish();
return;
}
Intent intent = new Intent(this, typeof(FileSelectActivity));
_appTask.ToIntent(intent); _appTask.ToIntent(intent);

View File

@ -291,6 +291,8 @@ namespace keepass2android
LoadDb task = new LoadDb(App.Kp2a, _ioConnection, _loadDbTask, pass, key, new AfterLoad(handler, this)); LoadDb task = new LoadDb(App.Kp2a, _ioConnection, _loadDbTask, pass, key, new AfterLoad(handler, this));
_loadDbTask = null; // prevent accidental re-use _loadDbTask = null; // prevent accidental re-use
SetNewDefaultFile();
new ProgressTask(App.Kp2a, this, task).Run(); new ProgressTask(App.Kp2a, this, task).Run();
}; };
@ -319,31 +321,7 @@ namespace keepass2android
} }
}; };
CheckBox defaultCheck = (CheckBox)FindViewById(Resource.Id.default_database);
//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))
{
defaultCheck.Enabled = false;
} else
{
defaultCheck.Enabled = true;
}
defaultCheck.CheckedChange += (sender, e) =>
{
String newDefaultFileName;
if (e.IsChecked)
{
newDefaultFileName = _ioConnection.Path;
} else
{
newDefaultFileName = "";
}
ISharedPreferencesEditor editor = _prefs.Edit();
editor.PutString(KeyDefaultFilename, newDefaultFileName);
EditorCompat.Apply(editor);
};
ImageButton browse = (ImageButton)FindViewById(Resource.Id.browse_button); ImageButton browse = (ImageButton)FindViewById(Resource.Id.browse_button);
browse.Click += (sender, evt) => browse.Click += (sender, evt) =>
@ -365,6 +343,34 @@ namespace keepass2android
RetrieveSettings(); RetrieveSettings();
} }
private void SetNewDefaultFile()
{
//Don't allow the current file to be the default if we don't have stored credentials
bool makeFileDefault;
if ((_ioConnection.IsLocalFile() == false) && (_ioConnection.CredSaveMode != IOCredSaveMode.SaveCred))
{
makeFileDefault = false;
}
else
{
makeFileDefault = true;
}
String newDefaultFileName;
if (makeFileDefault)
{
newDefaultFileName = _ioConnection.Path;
}
else
{
newDefaultFileName = "";
}
ISharedPreferencesEditor editor = _prefs.Edit();
editor.PutString(KeyDefaultFilename, newDefaultFileName);
EditorCompat.Apply(editor);
}
protected override void OnStart() protected override void OnStart()
{ {
base.OnStart(); base.OnStart();
@ -432,11 +438,6 @@ namespace keepass2android
} }
private void RetrieveSettings() { private void RetrieveSettings() {
String defaultFilename = _prefs.GetString(KeyDefaultFilename, "");
if (!String.IsNullOrEmpty(_ioConnection.Path) && _ioConnection.Path.Equals(defaultFilename)) {
CheckBox checkbox = (CheckBox) FindViewById(Resource.Id.default_database);
checkbox.Checked = true;
}
CheckBox cbQuickUnlock = (CheckBox)FindViewById(Resource.Id.enable_quickunlock); CheckBox cbQuickUnlock = (CheckBox)FindViewById(Resource.Id.enable_quickunlock);
cbQuickUnlock.Checked = _prefs.GetBoolean(GetString(Resource.String.QuickUnlockDefaultEnabled_key), true); cbQuickUnlock.Checked = _prefs.GetBoolean(GetString(Resource.String.QuickUnlockDefaultEnabled_key), true);
} }
@ -494,16 +495,27 @@ namespace keepass2android
public override bool OnOptionsItemSelected(IMenuItem item) { public override bool OnOptionsItemSelected(IMenuItem item) {
switch ( item.ItemId ) { switch ( item.ItemId ) {
case Resource.Id.menu_about: case Resource.Id.menu_about:
AboutDialog dialog = new AboutDialog(this); AboutDialog dialog = new AboutDialog(this);
dialog.Show(); dialog.Show();
return true; return true;
case Resource.Id.menu_app_settings:
AppSettingsActivity.Launch(this);
return true;
case Resource.Id.menu_change_db:
Intent intent = new Intent(this, typeof(FileSelectActivity));
AppTask.ToIntent(intent);
StartActivityForResult(intent, 0);
Finish();
return true;
case Resource.Id.menu_app_settings:
AppSettingsActivity.Launch(this);
return true;
} }
return base.OnOptionsItemSelected(item); return base.OnOptionsItemSelected(item);
} }

File diff suppressed because it is too large Load Diff

View File

@ -91,16 +91,10 @@
android:layout_width="fill_parent" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_below="@id/keyfileLine" /> android:layout_below="@id/keyfileLine" />
<CheckBox
android:id="@+id/default_database"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/pass_ok"
android:text="@string/default_checkbox" />
<CheckBox <CheckBox
android:id="@+id/enable_quickunlock" android:id="@+id/enable_quickunlock"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_below="@id/default_database" android:layout_below="@id/pass_ok"
android:text="@string/enable_quickunlock" /> android:text="@string/enable_quickunlock" />
</RelativeLayout> </RelativeLayout>

View File

@ -79,16 +79,10 @@
android:layout_width="fill_parent" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_below="@id/pass_keyfile" /> android:layout_below="@id/pass_keyfile" />
<CheckBox
android:id="@+id/default_database"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/pass_ok"
android:text="@string/default_checkbox" />
<CheckBox <CheckBox
android:id="@+id/enable_quickunlock" android:id="@+id/enable_quickunlock"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_below="@id/default_database" android:layout_below="@id/pass_ok"
android:text="@string/enable_quickunlock" /> android:text="@string/enable_quickunlock" />
</RelativeLayout> </RelativeLayout>

View File

@ -20,6 +20,11 @@
android:title="@string/menu_about" android:title="@string/menu_about"
android:icon="@android:drawable/ic_menu_help" android:icon="@android:drawable/ic_menu_help"
android:showAsAction="ifRoom" android:showAsAction="ifRoom"
/>
<item android:id="@+id/menu_change_db"
android:icon="@drawable/collections_collection"
android:title="@string/menu_change_db"
android:showAsAction="ifRoom"
/> />
<item android:id="@+id/menu_app_settings" <item android:id="@+id/menu_app_settings"
android:title="@string/menu_app_settings" android:title="@string/menu_app_settings"

View File

@ -19,6 +19,10 @@
<item android:id="@+id/menu_app_settings" <item android:id="@+id/menu_app_settings"
android:icon="@android:drawable/ic_menu_preferences" android:icon="@android:drawable/ic_menu_preferences"
android:title="@string/menu_app_settings" android:title="@string/menu_app_settings"
/>
<item android:id="@+id/menu_change_db"
android:icon="@drawable/collections_collection"
android:title="@string/menu_change_db"
/> />
<item android:id="@+id/menu_about" <item android:id="@+id/menu_about"
android:icon="@android:drawable/ic_menu_help" android:icon="@android:drawable/ic_menu_help"

View File

@ -131,6 +131,7 @@
<string name="menu_search">Search</string> <string name="menu_search">Search</string>
<string name="menu_search_advanced">Advanced Search</string> <string name="menu_search_advanced">Advanced Search</string>
<string name="menu_url">Go to URL</string> <string name="menu_url">Go to URL</string>
<string name="menu_change_db">Change database…</string>
<string name="minus">Minus</string> <string name="minus">Minus</string>
<string name="never">Never</string> <string name="never">Never</string>
<string name="yes">Yes</string> <string name="yes">Yes</string>

View File

@ -62,11 +62,6 @@ namespace keepass2android
internal AppTask AppTask; internal AppTask AppTask;
IOConnectionInfo LoadIoc(string defaultFileName)
{
return _DbHelper.CursorToIoc(_DbHelper.FetchFileByName(defaultFileName));
}
void ShowFilenameDialog(bool showOpenButton, bool showCreateButton, bool showBrowseButton, string defaultFilename, string detailsText, int requestCodeBrowse) void ShowFilenameDialog(bool showOpenButton, bool showCreateButton, bool showBrowseButton, string defaultFilename, string detailsText, int requestCodeBrowse)
{ {
AlertDialog.Builder builder = new AlertDialog.Builder(this); AlertDialog.Builder builder = new AlertDialog.Builder(this);
@ -376,6 +371,7 @@ namespace keepass2android
ioc.Password = password; ioc.Password = password;
ioc.CredSaveMode = (IOCredSaveMode)credentialRememberMode; ioc.CredSaveMode = (IOCredSaveMode)credentialRememberMode;
PasswordActivity.Launch(this, ioc, AppTask); PasswordActivity.Launch(this, ioc, AppTask);
Finish();
}); });
builder.SetView(LayoutInflater.Inflate(Resource.Layout.url_credentials, null)); builder.SetView(LayoutInflater.Inflate(Resource.Layout.url_credentials, null));
builder.SetNeutralButton(GetString(Android.Resource.String.Cancel), builder.SetNeutralButton(GetString(Android.Resource.String.Cancel),
@ -391,6 +387,7 @@ namespace keepass2android
try try
{ {
PasswordActivity.Launch(this, ioc, AppTask); PasswordActivity.Launch(this, ioc, AppTask);
Finish();
} catch (Java.IO.FileNotFoundException) } catch (Java.IO.FileNotFoundException)
{ {
Toast.MakeText(this, Resource.String.FileNotFound, ToastLength.Long).Show(); Toast.MakeText(this, Resource.String.FileNotFound, ToastLength.Long).Show();
@ -474,36 +471,6 @@ namespace keepass2android
_fileSelectButtons.UpdateExternalStorageWarning(); _fileSelectButtons.UpdateExternalStorageWarning();
if (!_createdWithActivityResult)
{
if ((Intent.Action == Intent.ActionSend) && (App.Kp2a.GetDb().Loaded))
{
PasswordActivity.Launch(this, App.Kp2a.GetDb().Ioc , AppTask);
} else
{
// Load default database
ISharedPreferences prefs = Android.Preferences.PreferenceManager.GetDefaultSharedPreferences(this);
String defaultFileName = prefs.GetString(PasswordActivity.KeyDefaultFilename, "");
if (defaultFileName.Length > 0)
{
Java.IO.File db = new Java.IO.File(defaultFileName);
if (db.Exists())
{
try
{
PasswordActivity.Launch(this, LoadIoc(defaultFileName), AppTask);
} catch (Exception e)
{
Toast.MakeText(this, e.Message, ToastLength.Long);
// Ignore exception
}
}
}
}
}
} }
@ -516,8 +483,10 @@ namespace keepass2android
if (db.Loaded) if (db.Loaded)
{ {
PasswordActivity.Launch(this, db.Ioc, AppTask); PasswordActivity.Launch(this, db.Ioc, AppTask);
Finish();
} }
} }
public override bool OnCreateOptionsMenu(IMenu menu) { public override bool OnCreateOptionsMenu(IMenu menu) {
base.OnCreateOptionsMenu(menu); base.OnCreateOptionsMenu(menu);