예제 #1
0
        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
            };
        }
예제 #2
0
            public override View NewView(Context context, ICursor cursor, ViewGroup parent)
            {
                if (cursorInflater == null)
                {
                    cursorInflater = (LayoutInflater)context.GetSystemService(Context.LayoutInflaterService);
                }
                View view = cursorInflater.Inflate(Resource.Layout.file_row, parent, false);

                view.FindViewById(Resource.Id.group_name_vdots).Click += (sender, args) =>
                {
                    Handler handler = new Handler(Looper.MainLooper);
                    handler.Post(() =>
                    {
                        PopupMenu popupMenu = new PopupMenu(context, view.FindViewById(Resource.Id.group_name_vdots));

                        AccessManager.PreparePopup(popupMenu);
                        int remove = 0;
                        int edit   = 1;
                        popupMenu.Menu.Add(0, remove, 0, context.GetString(Resource.String.remove_from_filelist)).SetIcon(Resource.Drawable.ic_menu_delete_grey);

                        TextView textView = view.FindViewById <TextView>(Resource.Id.file_filename);

                        String filename      = (string)textView.Tag;
                        IOConnectionInfo ioc = new IOConnectionInfo {
                            Path = filename
                        };
                        if (FileSelectHelper.CanEditIoc(ioc))
                        {
                            popupMenu.Menu.Add(0, edit, 0, context.GetString(Resource.String.edit)).SetIcon(Resource.Drawable.ic_menu_edit_grey);
                        }


                        popupMenu.MenuItemClick += delegate(object sender2, PopupMenu.MenuItemClickEventArgs args2)
                        {
                            if (args2.Item.ItemId == remove)
                            {
                                App.Kp2a.FileDbHelper.DeleteFile(filename);

                                cursor.Requery();
                            }
                            if (args2.Item.ItemId == edit)
                            {
                                var fsh     = new FileSelectHelper(_activity, false, RequestCodeEditIoc);
                                fsh.OnOpen += (o, newConnectionInfo) =>
                                {
                                    _activity.EditFileEntry(filename, newConnectionInfo);
                                };
                                fsh.PerformManualFileSelect(filename);
                            }
                        };
                        popupMenu.Show();
                    });
                };

                return(view);
            }
        protected override void StartSelectFile(bool isForSave, int browseRequestCode, string protocolId)
        {
            FileSelectHelper fileSelectHelper = new FileSelectHelper(this, isForSave, browseRequestCode);

            fileSelectHelper.OnOpen += (sender, ioc) =>
            {
                IocSelected(ioc, browseRequestCode);
            };
            fileSelectHelper.OnCancel += (sender, args) =>
            {
                ReturnCancel();
            };

            App.Kp2a.GetFileStorage(protocolId).StartSelectFile(new FileStorageSetupInitiatorActivity(this,
                                                                                                      OnActivityResult,
                                                                                                      s => fileSelectHelper.PerformManualFileSelect(s)
                                                                                                      ), isForSave, browseRequestCode, protocolId);
        }
예제 #4
0
        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);
        }
예제 #5
0
        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, _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, _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 = 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 = ConvertFilenameToIocPath(messageOrFilename)
                                });
                            }), filename);

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

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



            return(false);
        }
        protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
        {
            base.OnActivityResult(requestCode, resultCode, data);

            if (resultCode == KeePass.ExitFileStorageSelectionOk)
            {
                string protocolId = data.GetStringExtra("protocolId");
                if (protocolId == "content")
                {
                    Util.ShowBrowseDialog(this, RequestCodeDbFilename, true, true);
                }
                else
                {
                    FileSelectHelper fileSelectHelper = new FileSelectHelper(this, true, RequestCodeDbFilename)
                    {
                        DefaultExtension = _ffp[_fileFormatIndex].DefaultExtension
                    };
                    fileSelectHelper.OnOpen += (sender, ioc) =>
                    {
                        ExportTo(ioc);
                    };
                    App.Kp2a.GetFileStorage(protocolId).StartSelectFile(
                        new FileStorageSetupInitiatorActivity(this, OnActivityResult, s => fileSelectHelper.PerformManualFileSelect(s)),
                        true,
                        RequestCodeDbFilename,
                        protocolId);
                }
                return;
            }

            if (resultCode == Result.Ok)
            {
                if (requestCode == RequestCodeDbFilename)
                {
                    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);
                                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;
                    }

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

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

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

                    return;
                }
            }
            if (resultCode == (Result)FileStorageResults.FileUsagePrepared)
            {
                var ioc = new IOConnectionInfo();
                PasswordActivity.SetIoConnectionFromIntent(ioc, data);
                ExportTo(ioc);
                return;
            }
            if (resultCode == (Result)FileStorageResults.FileChooserPrepared)
            {
                IOConnectionInfo ioc = new IOConnectionInfo();
                PasswordActivity.SetIoConnectionFromIntent(ioc, data);
                new FileSelectHelper(this, true, RequestCodeDbFilename)
                {
                    DefaultExtension = _ffp[_fileFormatIndex].DefaultExtension
                }
                .StartFileChooser(ioc.Path);
                return;
            }
            Finish();
        }
예제 #8
0
        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;
            }

            if (resultCode == KeePass.ExitFileStorageSelectionOk)
            {
                string protocolId = data.GetStringExtra("protocolId");
                if (protocolId == "content")
                {
                    Util.ShowBrowseDialog(this, RequestCodeDbFilename, true, true);
                }
                else
                {
                    FileSelectHelper fileSelectHelper = new FileSelectHelper(this, true, RequestCodeDbFilename)
                    {
                        DefaultExtension = "kdbx"
                    };
                    fileSelectHelper.OnOpen += (sender, info) =>
                    {
                        _ioc = info;
                        UpdateIocView();
                    };
                    App.Kp2a.GetFileStorage(protocolId).StartSelectFile(
                        new FileStorageSetupInitiatorActivity(this, OnActivityResult, s => fileSelectHelper.PerformManualFileSelect(s)),
                        true,
                        RequestCodeDbFilename,
                        protocolId);
                }
            }

            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 = ConvertFilenameToIocPath(filename);
                    FindViewById <TextView>(Resource.Id.keyfile_filename).Text = _keyfileFilename;
                    FindViewById(Resource.Id.keyfile_filename).Visibility      = ViewStates.Visible;
                }
                if (requestCode == RequestCodeDbFilename)
                {
                    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;
                    }

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

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

                        new ProgressTask(App.Kp2a, this, task).Run();
                    }
                }
            }
            if (resultCode == (Result)FileStorageResults.FileUsagePrepared)
            {
                _ioc = new IOConnectionInfo();
                PasswordActivity.SetIoConnectionFromIntent(_ioc, data);
                UpdateIocView();
            }
            if (resultCode == (Result)FileStorageResults.FileChooserPrepared)
            {
                IOConnectionInfo ioc = new IOConnectionInfo();
                PasswordActivity.SetIoConnectionFromIntent(ioc, data);

                new FileSelectHelper(this, true, RequestCodeDbFilename)
                {
                    DefaultExtension = "kdbx"
                }
                .StartFileChooser(ioc.Path);
            }
        }