Ejemplo n.º 1
0
        void PopulateBinaries()
        {
            ViewGroup binariesGroup = (ViewGroup)FindViewById(Resource.Id.binaries);

            binariesGroup.RemoveAllViews();
            RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.FillParent, ViewGroup.LayoutParams.WrapContent);
            foreach (KeyValuePair <string, ProtectedBinary> pair in State.Entry.Binaries.OrderBy(p => p.Key))
            {
                String key   = pair.Key;
                String label = key;
                if ((String.IsNullOrEmpty(label) || (!App.Kp2a.GetDb().DatabaseFormat.SupportsAttachmentKeys)))
                {
                    label = "<attachment>";
                }
                //Button binaryButton = new Button(this, null, Resource.Style.EditEntryButton) {Text = label};
                Button binaryButton = (Button)LayoutInflater.Inflate(Resource.Layout.EntryEditButtonDelete, null);
                binaryButton.Text = label;

                //binaryButton.SetCompoundDrawablesWithIntrinsicBounds( Resources.GetDrawable(Android.Resource.Drawable.IcMenuDelete),null, null, null);
                binaryButton.Click += (sender, e) =>
                {
                    State.EntryModified = true;
                    State.Entry.Binaries.Remove(key);
                    PopulateBinaries();
                };
                binariesGroup.AddView(binaryButton, layoutParams);
            }

            //Button addBinaryButton = new Button(this, null, Resource.Style.EditEntryButton ) {Text = GetString(Resource.String.add_binary)};
            //addBinaryButton.SetCompoundDrawablesWithIntrinsicBounds( Resources.GetDrawable(Android.Resource.Drawable.IcMenuAdd) , null, null, null);
            Button addBinaryButton = (Button)LayoutInflater.Inflate(Resource.Layout.EntryEditButtonAdd, null);

            addBinaryButton.Text = GetString(Resource.String.add_binary);

            addBinaryButton.Enabled = true;

            if (!App.Kp2a.GetDb().DatabaseFormat.CanHaveMultipleAttachments)
            {
                addBinaryButton.Enabled = !State.Entry.Binaries.Any();
            }
            addBinaryButton.Click += (sender, e) =>
            {
                Util.ShowBrowseDialog(this, Intents.RequestCodeFileBrowseForBinary, false, true /*force OpenDocument if available, GetContent is not well support starting with Android 7 */);
            };
            binariesGroup.AddView(addBinaryButton, layoutParams);

            var binariesLabel = FindViewById(Resource.Id.entry_binaries_label);

            if (binariesLabel != null)
            {
                binariesLabel.Visibility = State.Entry.Binaries.UCount > 0 ? ViewStates.Visible : ViewStates.Gone;
            }
        }
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
0
        public static void ShowFilenameDialog(Activity activity, Func <string, Dialog, bool> onOpen, Func <string, Dialog, bool> onCreate, Action onCancel, bool showBrowseButton, string defaultFilename, string detailsText, int requestCodeBrowse)
        {
            AlertDialog.Builder builder = new AlertDialog.Builder(activity);
            builder.SetView(activity.LayoutInflater.Inflate(Resource.Layout.file_selection_filename, null));

            if (onCancel != null)
            {
                builder.SetOnCancelListener(new CancelListener(onCancel));
            }
            Dialog dialog = builder.Create();

            dialog.Show();

            Button openButton   = (Button)dialog.FindViewById(Resource.Id.open);
            Button createButton = (Button)dialog.FindViewById(Resource.Id.create);

            TextView enterFilenameDetails = (TextView)dialog.FindViewById(Resource.Id.label_open_by_filename_details);

            openButton.Visibility   = onOpen != null ? ViewStates.Visible : ViewStates.Gone;
            createButton.Visibility = onCreate != null? ViewStates.Visible : ViewStates.Gone;
            // Set the initial value of the filename
            EditText editFilename = (EditText)dialog.FindViewById(Resource.Id.file_filename);

            editFilename.Text               = defaultFilename;
            enterFilenameDetails.Text       = detailsText;
            enterFilenameDetails.Visibility = enterFilenameDetails.Text == "" ? ViewStates.Gone : ViewStates.Visible;

            // Open button
            if (onOpen != null)
            {
                openButton.Click += (sender, args) =>
                {
                    String fileName = ((EditText)dialog.FindViewById(Resource.Id.file_filename)).Text;
                    if (onOpen(fileName, dialog))
                    {
                        dialog.Dismiss();
                    }
                }
            }
            ;

            // Create button
            if (onCreate != null)
            {
                createButton.Click += (sender, args) =>
                {
                    String fileName = ((EditText)dialog.FindViewById(Resource.Id.file_filename)).Text;
                    if (onCreate(fileName, dialog))
                    {
                        dialog.Dismiss();
                    }
                }
            }
            ;

            Button cancelButton = (Button)dialog.FindViewById(Resource.Id.fnv_cancel);

            cancelButton.Click += delegate
            {
                dialog.Dismiss();
                if (onCancel != null)
                {
                    onCancel();
                }
            };

            ImageButton browseButton = (ImageButton)dialog.FindViewById(Resource.Id.browse_button);

            if (!showBrowseButton)
            {
                browseButton.Visibility = ViewStates.Invisible;
            }
            browseButton.Click += (sender, evt) =>
            {
                string filename = ((EditText)dialog.FindViewById(Resource.Id.file_filename)).Text;

                Util.ShowBrowseDialog(activity, requestCodeBrowse, onCreate != null, /*TODO should we prefer ActionOpenDocument here?*/ false);
            };
        }
 protected override void ShowAndroidBrowseDialog(int requestCode, bool isForSave, bool tryGetPermanentAccess)
 {
     Util.ShowBrowseDialog(this, requestCode, isForSave, tryGetPermanentAccess);
 }
Ejemplo n.º 5
0
        public bool HandleActivityResult(Activity activity, int requestCode, Result resultCode, Intent data)
        {
            if (requestCode != _requestCode)
            {
                return(false);
            }

            if (resultCode == KeePass.ExitFileStorageSelectionOk)
            {
                string protocolId = data.GetStringExtra("protocolId");
                if (protocolId == "content")
                {
                    Util.ShowBrowseDialog(activity, _requestCode, _isForSave, _tryGetPermanentAccess);
                }
                else
                {
                    App.Kp2a.GetFileStorage(protocolId).StartSelectFile(
                        new FileStorageSetupInitiatorActivity(activity, (i, result, arg3) => HandleActivityResult(activity, i, result, arg3), s => PerformManualFileSelect(s)),
                        _isForSave,
                        _requestCode,
                        protocolId);
                }
            }

            if (resultCode == Result.Ok)
            {
                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);
                            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)
                {
                    var ioc = new IOConnectionInfo {
                        Path = ConvertFilenameToIocPath(filename)
                    };
                    IocSelected(activity, ioc);
                }
                else
                {
                    var task = new CreateNewFilename(activity, new ActionOnFinish(activity, (success, messageOrFilename, newActivity) =>
                    {
                        if (!success)
                        {
                            Toast.MakeText(newActivity, messageOrFilename, ToastLength.Long).Show();
                            return;
                        }
                        var ioc = new IOConnectionInfo {
                            Path = ConvertFilenameToIocPath(messageOrFilename)
                        };
                        IocSelected(newActivity, ioc);
                    }), filename);

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


            if (resultCode == (Result)FileStorageResults.FileUsagePrepared)
            {
                var ioc = new IOConnectionInfo();
                Util.SetIoConnectionFromIntent(ioc, data);
                IocSelected(null, ioc);
            }
            if (resultCode == (Result)FileStorageResults.FileChooserPrepared)
            {
                IOConnectionInfo ioc = new IOConnectionInfo();
                Util.SetIoConnectionFromIntent(ioc, data);
                StartFileChooser(ioc.Path);
            }
            return(true);
        }
        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();
        }
Ejemplo n.º 7
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);
            }
        }