private void SetDefaultIoc()
        {
            File directory = GetExternalFilesDir(null);

            if (directory == null)
            {
                directory = FilesDir;
            }

            string strDir = directory.CanonicalPath;

            if (!strDir.EndsWith(File.Separator))
            {
                strDir += File.Separator;
            }

            string filename = strDir + "keepass.kdbx";

            filename = FileSelectHelper.ConvertFilenameToIocPath(filename);
            int count = 2;

            while (new File(filename).Exists())
            {
                filename = FileSelectHelper.ConvertFilenameToIocPath(strDir + "keepass" + count + ".kdbx");
                count++;
            }

            _ioc = new IOConnectionInfo
            {
                Path = filename
            };
        }
        protected override void OnCreate(Bundle bundle)
        {
            _design.ApplyTheme();
            base.OnCreate(bundle);


            SupportActionBar.SetDisplayHomeAsUpEnabled(true);
            SupportActionBar.SetHomeButtonEnabled(true);

            SetContentView(Resource.Layout.create_database);
            _appTask = AppTask.GetTaskInOnCreate(bundle, Intent);

            SetDefaultIoc();

            FindViewById(Resource.Id.keyfile_filename).Visibility = ViewStates.Gone;


            var keyfileCheckbox = FindViewById <CheckBox>(Resource.Id.use_keyfile);

            if (bundle != null)
            {
                _keyfileFilename = bundle.GetString(KeyfilefilenameBundleKey, null);
                if (_keyfileFilename != null)
                {
                    FindViewById <TextView>(Resource.Id.keyfile_filename).Text = FileSelectHelper.ConvertFilenameToIocPath(_keyfileFilename);
                    FindViewById(Resource.Id.keyfile_filename).Visibility      = ViewStates.Visible;
                    keyfileCheckbox.Checked = true;
                }

                if (bundle.GetString(Util.KeyFilename, null) != null)
                {
                    _ioc = new IOConnectionInfo
                    {
                        Path         = bundle.GetString(Util.KeyFilename),
                        UserName     = bundle.GetString(Util.KeyServerusername),
                        Password     = bundle.GetString(Util.KeyServerpassword),
                        CredSaveMode = (IOCredSaveMode)bundle.GetInt(Util.KeyServercredmode),
                    };
                }
            }

            UpdateIocView();

            keyfileCheckbox.CheckedChange += (sender, args) =>
            {
                if (keyfileCheckbox.Checked)
                {
                    if (_restoringInstanceState)
                    {
                        return;
                    }

                    Util.ShowBrowseDialog(this, RequestCodeKeyFile, false, true);
                }
                else
                {
                    FindViewById(Resource.Id.keyfile_filename).Visibility = ViewStates.Gone;
                    _keyfileFilename = null;
                }
            };


            FindViewById(Resource.Id.btn_change_location).Click += (sender, args) =>
            {
                Intent intent = new Intent(this, typeof(FileStorageSelectionActivity));
                StartActivityForResult(intent, RequestCodeDbFilename);
            };

            Button generatePassword = (Button)FindViewById(Resource.Id.generate_button);

            generatePassword.Click += (sender, e) =>
            {
                GeneratePasswordActivity.LaunchWithoutLockCheck(this);
            };

            FindViewById(Resource.Id.btn_create).Click += (sender, evt) =>
            {
                CreateDatabase(Intent.GetBooleanExtra("MakeCurrent", true));
            };

            ImageButton btnTogglePassword = (ImageButton)FindViewById(Resource.Id.toggle_password);

            btnTogglePassword.Click += (sender, e) =>
            {
                _showPassword = !_showPassword;
                MakePasswordMaskedOrVisible();
            };
            Android.Graphics.PorterDuff.Mode mMode = Android.Graphics.PorterDuff.Mode.SrcAtop;
            Android.Graphics.Color           color = new Android.Graphics.Color(224, 224, 224);
            btnTogglePassword.SetColorFilter(color, mMode);
        }
        protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
        {
            base.OnActivityResult(requestCode, resultCode, data);

            if (resultCode == KeePass.ResultOkPasswordGenerator)
            {
                String generatedPassword = data.GetStringExtra("keepass2android.password.generated_password");
                FindViewById <TextView>(Resource.Id.entry_password).Text     = generatedPassword;
                FindViewById <TextView>(Resource.Id.entry_confpassword).Text = generatedPassword;
            }

            FileSelectHelper fileSelectHelper = new FileSelectHelper(this, true, true, RequestCodeDbFilename)
            {
                DefaultExtension = "kdbx"
            };

            fileSelectHelper.OnOpen += (sender, info) =>
            {
                _ioc = info;
                (sender as CreateDatabaseActivity ?? this).UpdateIocView();
            };

            if (fileSelectHelper.HandleActivityResult(this, requestCode, resultCode, data))
            {
                return;
            }


            if (resultCode == Result.Ok)
            {
                if (requestCode == RequestCodeKeyFile)
                {
                    if (data.Data.Scheme == "content")
                    {
                        if ((int)Build.VERSION.SdkInt >= 19)
                        {
                            //try to take persistable permissions
                            try
                            {
                                Kp2aLog.Log("TakePersistableUriPermission");
                                var takeFlags = data.Flags
                                                & (ActivityFlags.GrantReadUriPermission
                                                   | ActivityFlags.GrantWriteUriPermission);
                                this.ContentResolver.TakePersistableUriPermission(data.Data, takeFlags);
                            }
                            catch (Exception e)
                            {
                                Kp2aLog.Log(e.ToString());
                            }
                        }
                    }


                    string filename = Util.IntentToFilename(data, this);
                    if (filename == null)
                    {
                        filename = data.DataString;
                    }


                    _keyfileFilename = FileSelectHelper.ConvertFilenameToIocPath(filename);
                    FindViewById <TextView>(Resource.Id.keyfile_filename).Text = _keyfileFilename;
                    FindViewById(Resource.Id.keyfile_filename).Visibility      = ViewStates.Visible;
                }
            }
            if (resultCode == (Result)FileStorageResults.FileUsagePrepared)
            {
                _ioc = new IOConnectionInfo();
                Util.SetIoConnectionFromIntent(_ioc, data);
                UpdateIocView();
            }
            if (resultCode == (Result)FileStorageResults.FileChooserPrepared)
            {
                IOConnectionInfo ioc = new IOConnectionInfo();
                Util.SetIoConnectionFromIntent(ioc, data);

                new FileSelectHelper(this, true, true, RequestCodeDbFilename)
                {
                    DefaultExtension = "kdbx"
                }
                .StartFileChooser(ioc.Path);
            }
        }
        public bool OnActivityResult(int requestCode, Result resultCode, Intent data)
        {
            if (requestCode == _requestCode)
            {
                if (resultCode == KeePass.ExitFileStorageSelectionOk)
                {
                    string protocolId = data.GetStringExtra("protocolId");
                    if (protocolId == "content")
                    {
                        Util.ShowBrowseDialog(_activity, _requestCode, true, true);
                    }
                    else
                    {
                        FileSelectHelper fileSelectHelper = new FileSelectHelper(_activity, true, true, _requestCode);
                        fileSelectHelper.OnOpen += (sender, ioc) =>
                        {
                            SaveFile(ioc);
                        };
                        App.Kp2a.GetFileStorage(protocolId).StartSelectFile(
                            new FileStorageSetupInitiatorActivity(_activity, (i, result, arg3) => OnActivityResult(i, result, arg3), s => fileSelectHelper.PerformManualFileSelect(s)),
                            true,
                            _requestCode,
                            protocolId);
                    }
                    return(true);
                }

                if (resultCode == (Result)FileStorageResults.FileUsagePrepared)
                {
                    var ioc = new IOConnectionInfo();
                    Util.SetIoConnectionFromIntent(ioc, data);
                    SaveFile(ioc);
                    return(true);
                }
                if (resultCode == (Result)FileStorageResults.FileChooserPrepared)
                {
                    IOConnectionInfo ioc = new IOConnectionInfo();
                    Util.SetIoConnectionFromIntent(ioc, data);
                    new FileSelectHelper(_activity, true, true, _requestCode).StartFileChooser(ioc.Path);
                    return(true);
                }
                if (resultCode == Result.Ok)
                {
                    if (requestCode == _requestCode)
                    {
                        if (data.Data.Scheme == "content")
                        {
                            if ((int)Android.OS.Build.VERSION.SdkInt >= 19)
                            {
                                //try to take persistable permissions
                                try
                                {
                                    Kp2aLog.Log("TakePersistableUriPermission");
                                    var takeFlags = data.Flags
                                                    & (ActivityFlags.GrantReadUriPermission
                                                       | ActivityFlags.GrantWriteUriPermission);
                                    _activity.ContentResolver.TakePersistableUriPermission(data.Data, takeFlags);
                                }
                                catch (Exception e)
                                {
                                    Kp2aLog.Log(e.ToString());
                                }
                            }
                        }


                        string filename = Util.IntentToFilename(data, _activity);
                        if (filename == null)
                        {
                            filename = data.DataString;
                        }

                        bool fileExists = data.GetBooleanExtra("group.pals.android.lib.ui.filechooser.FileChooserActivity.result_file_exists", true);

                        if (fileExists)
                        {
                            SaveFile(new IOConnectionInfo {
                                Path = FileSelectHelper.ConvertFilenameToIocPath(filename)
                            });
                        }
                        else
                        {
                            var task = new CreateNewFilename(_activity, new ActionOnFinish(_activity, (success, messageOrFilename, activity) =>
                            {
                                if (!success)
                                {
                                    Toast.MakeText(activity, messageOrFilename, ToastLength.Long).Show();
                                    return;
                                }
                                SaveFile(new IOConnectionInfo {
                                    Path = FileSelectHelper.ConvertFilenameToIocPath(messageOrFilename)
                                });
                            }), filename);

                            new ProgressTask(App.Kp2a, _activity, task).Run();
                        }

                        return(true);
                    }
                }
                Clear();
                return(true);
            }



            return(false);
        }