示例#1
0
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            if (LastNonConfigurationInstance != null)
            {
                //bug in Mono for Android or whatever: after config change the extra fields are wrong
                // -> reload:
                Reload();
                return;
            }

            _appTask = AppTask.GetTaskInOnCreate(savedInstanceState, Intent);

            SetContentView(Resource.Layout.entry_edit);
            _closeForReload = false;

            // Likely the app has been killed exit the activity
            if (!App.Kp2a.DatabaseIsUnlocked)
            {
                Finish();
                return;
            }


            if (Intent.GetBooleanExtra(IntentContinueWithEditing, false))
            {
                //property "State" will return the state
            }
            else
            {
                Database db = App.Kp2a.GetDb();

                App.Kp2a.EntryEditActivityState = new EntryEditActivityState();
                ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(this);
                State.ShowPassword = !prefs.GetBoolean(GetString(Resource.String.maskpass_key), Resources.GetBoolean(Resource.Boolean.maskpass_default));

                Intent i         = Intent;
                String uuidBytes = i.GetStringExtra(KeyEntry);

                PwUuid entryId = PwUuid.Zero;
                if (uuidBytes != null)
                {
                    entryId = new PwUuid(MemUtil.HexStringToByteArray(uuidBytes));
                }

                State.ParentGroup = null;
                if (entryId.Equals(PwUuid.Zero))
                {
                    //creating new entry
                    String groupId = i.GetStringExtra(KeyParent);
                    State.ParentGroup = db.KpDatabase.RootGroup.FindGroup(new PwUuid(MemUtil.HexStringToByteArray(groupId)), true);

                    PwUuid  templateId    = new PwUuid(MemUtil.HexStringToByteArray(i.GetStringExtra(KeyTemplateUuid)));
                    PwEntry templateEntry = null;
                    if (!PwUuid.Zero.Equals(templateId))
                    {
                        templateEntry = db.Entries[templateId];
                    }

                    if (KpEntryTemplatedEdit.IsTemplate(templateEntry))
                    {
                        CreateNewFromKpEntryTemplate(db, templateEntry);
                    }
                    else if (templateEntry != null)
                    {
                        CreateNewFromStandardTemplate(templateEntry);
                    }
                    else
                    {
                        CreateNewWithoutTemplate(db);
                    }

                    _appTask.PrepareNewEntry(State.EntryInDatabase);
                    State.IsNew         = true;
                    State.EntryModified = true;
                }
                else
                {
                    Debug.Assert(entryId != null);

                    State.EntryInDatabase = db.Entries [entryId];
                    State.IsNew           = false;
                }

                State.Entry = State.EntryInDatabase.CloneDeep();
                if (KpEntryTemplatedEdit.IsTemplated(db, State.Entry))
                {
                    State.EditMode = new KpEntryTemplatedEdit(db, State.Entry);
                }
                else
                {
                    State.EditMode = new DefaultEdit();
                }
            }

            if (!State.EntryModified)
            {
                SetResult(KeePass.ExitNormal);
            }
            else
            {
                SetResult(KeePass.ExitRefreshTitle);
            }



            FillData();
            View scrollView = FindViewById(Resource.Id.entry_scroll);

            scrollView.ScrollBarStyle = ScrollbarStyles.InsideInset;

            ImageButton iconButton = (ImageButton)FindViewById(Resource.Id.icon_button);

            if (State.SelectedIcon)
            {
                App.Kp2a.GetDb().DrawableFactory.AssignDrawableTo(iconButton, this, App.Kp2a.GetDb().KpDatabase, (PwIcon)State.SelectedIconId, State.SelectedCustomIconId, false);
            }
            iconButton.Click += (sender, evt) => {
                UpdateEntryFromUi(State.Entry);
                IconPickerActivity.Launch(this);
            };


            // Generate password button
            FindViewById(Resource.Id.generate_button).Click += (sender, e) =>
            {
                UpdateEntryFromUi(State.Entry);
                GeneratePasswordActivity.Launch(this);
            };



            // Save button
            //SupportActionBar.SetCustomView(Resource.Layout.SaveButton);

            if (State.IsNew)
            {
                SupportActionBar.Title = GetString(Resource.String.add_entry);
            }
            else
            {
                SupportActionBar.Title = GetString(Resource.String.edit_entry);
            }

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

            // Respect mask password setting
            MakePasswordVisibleOrHidden();

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

            btnTogglePassword.Click += (sender, e) =>
            {
                State.ShowPassword = !State.ShowPassword;
                MakePasswordVisibleOrHidden();
            };
            PorterDuff.Mode mMode = PorterDuff.Mode.SrcAtop;
            Color           color = new Color(189, 189, 189);

            btnTogglePassword.SetColorFilter(color, mMode);


            Button addButton = (Button)FindViewById(Resource.Id.add_advanced);

            addButton.Visibility = ViewStates.Visible;
            addButton.Click     += (sender, e) =>
            {
                LinearLayout container = (LinearLayout)FindViewById(Resource.Id.advanced_container);

                KeyValuePair <string, ProtectedString> pair = new KeyValuePair <string, ProtectedString>("", new ProtectedString(true, ""));
                View ees = CreateExtraStringView(pair);
                container.AddView(ees);

                State.EntryModified = true;

                /*TextView keyView = (TextView) ees.FindViewById(Resource.Id.title);
                 * keyView.RequestFocus();*/
                EditAdvancedString(ees.FindViewById(Resource.Id.edit_extra));
            };
            SetAddExtraStringEnabled();

            ((CheckBox)FindViewById(Resource.Id.entry_expires_checkbox)).CheckedChange += (sender, e) =>
            {
                State.Entry.Expires = e.IsChecked;
                if (e.IsChecked)
                {
                    if (State.Entry.ExpiryTime < DateTime.Now)
                    {
                        State.Entry.ExpiryTime = DateTime.Now;
                    }
                }
                UpdateExpires();
                State.EntryModified = true;
            };
        }
示例#2
0
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            if (LastNonConfigurationInstance != null)
            {
                //bug in Mono for Android or whatever: after config change the extra fields are wrong
                // -> reload:
                Reload();
                return;
            }

            _appTask = AppTask.GetTaskInOnCreate(savedInstanceState, Intent);

            SetContentView(Resource.Layout.entry_edit);
            _closeForReload = false;

            // Likely the app has been killed exit the activity
            if (!App.Kp2a.DatabaseIsUnlocked)
            {
                Finish();
                return;
            }

            if (Intent.GetBooleanExtra(IntentContinueWithEditing, false))
            {
                //property "State" will return the state

            } else
            {
                Database db = App.Kp2a.GetDb();

                App.Kp2a.EntryEditActivityState = new EntryEditActivityState();
                ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(this);
                State.ShowPassword = ! prefs.GetBoolean(GetString(Resource.String.maskpass_key), Resources.GetBoolean(Resource.Boolean.maskpass_default));

                Intent i = Intent;
                String uuidBytes = i.GetStringExtra(KeyEntry);

                PwUuid entryId = PwUuid.Zero;
                if (uuidBytes != null)
                    entryId = new PwUuid(MemUtil.HexStringToByteArray(uuidBytes));

                State.ParentGroup = null;
                if (entryId.Equals(PwUuid.Zero))
                {
                    String groupId = i.GetStringExtra(KeyParent);
                    State.ParentGroup = db.KpDatabase.RootGroup.FindGroup(new PwUuid(MemUtil.HexStringToByteArray(groupId)), true);

                    State.EntryInDatabase = new PwEntry(true, true);
                    State.EntryInDatabase.Strings.Set(PwDefs.UserNameField, new ProtectedString(
                        db.KpDatabase.MemoryProtection.ProtectUserName, db.KpDatabase.DefaultUserName));

                    /*KPDesktop
                 * ProtectedString psAutoGen;
            PwGenerator.Generate(out psAutoGen, Program.Config.PasswordGenerator.AutoGeneratedPasswordsProfile,
                null, Program.PwGeneratorPool);
            psAutoGen = psAutoGen.WithProtection(pwDb.MemoryProtection.ProtectPassword);
            pwe.Strings.Set(PwDefs.PasswordField, psAutoGen);

            int nExpireDays = Program.Config.Defaults.NewEntryExpiresInDays;
            if(nExpireDays >= 0)
            {
                pwe.Expires = true;
                pwe.ExpiryTime = DateTime.Now.AddDays(nExpireDays);
            }*/

                    if ((State.ParentGroup.IconId != PwIcon.Folder) && (State.ParentGroup.IconId != PwIcon.FolderOpen) &&
                        (State.ParentGroup.IconId != PwIcon.FolderPackage))
                    {
                        State.EntryInDatabase.IconId = State.ParentGroup.IconId; // Inherit icon from group
                    }
                    State.EntryInDatabase.CustomIconUuid = State.ParentGroup.CustomIconUuid;

                    /*
                 * KPDesktop
            if(strDefaultSeq.Length == 0)
            {
                PwGroup pg = m_pwEntry.ParentGroup;
                if(pg != null)
                {
                    strDefaultSeq = pg.GetAutoTypeSequenceInherited();

                    if(strDefaultSeq.Length == 0)
                    {
                        if(PwDefs.IsTanEntry(m_pwEntry))
                            strDefaultSeq = PwDefs.DefaultAutoTypeSequenceTan;
                        else
                            strDefaultSeq = PwDefs.DefaultAutoTypeSequence;
                    }
                }
            }*/
                    _appTask.PrepareNewEntry(State.EntryInDatabase);
                    State.IsNew = true;
                    State.EntryModified = true;

                } else
                {

                    System.Diagnostics.Debug.Assert(entryId != null);

                    State.EntryInDatabase = db.Entries [entryId];
                    State.IsNew = false;

                }

                State.Entry = State.EntryInDatabase.CloneDeep();

            }

            if (!State.EntryModified)
                SetResult(KeePass.ExitNormal);
            else
                SetResult(KeePass.ExitRefreshTitle);

            FillData();
            View scrollView = FindViewById(Resource.Id.entry_scroll);
            scrollView.ScrollBarStyle = ScrollbarStyles.InsideInset;

            ImageButton iconButton = (ImageButton)FindViewById(Resource.Id.icon_button);

            if (State.SelectedIcon)
            {
                //TODO: custom image
                iconButton.SetImageResource(Icons.IconToResId(State.SelectedIconId));
            }
            iconButton.Click += (sender, evt) => {
                UpdateEntryFromUi(State.Entry);
                IconPickerActivity.Launch(this);
            };

            // Generate password button
            Button generatePassword = (Button)FindViewById(Resource.Id.generate_button);
            generatePassword.Click += (sender, e) => {
                UpdateEntryFromUi(State.Entry);
                GeneratePasswordActivity.Launch(this);
            };

            // Save button
            ActionBar.SetCustomView(Resource.Layout.SaveButton);
            ActionBar.SetDisplayShowCustomEnabled(true);
            ActionBar.SetDisplayShowTitleEnabled(false);
            ActionBar.SetDisplayUseLogoEnabled(false);
            ActionBar.SetDisplayShowHomeEnabled(false);
            ActionBar.SetDisplayOptions(ActionBarDisplayOptions.ShowCustom,
                                        ActionBarDisplayOptions.ShowCustom);
            var save = FindViewById(Resource.Id.entry_save);
            save.Click += (sender, e) =>
            {
                SaveEntry();
            };

            FindViewById(Resource.Id.entry_save_cancel).Click += (sender, args) => Finish();

            // Respect mask password setting
            MakePasswordVisibleOrHidden();

            ImageButton btnTogglePassword = (ImageButton)FindViewById(Resource.Id.toggle_password);
            btnTogglePassword.Click += (sender, e) =>
            {
                State.ShowPassword = !State.ShowPassword;
                MakePasswordVisibleOrHidden();
            };

            Button addButton = (Button) FindViewById(Resource.Id.add_advanced);
            addButton.Visibility = ViewStates.Visible;
            addButton.Click += (sender, e) =>
            {
                LinearLayout container = (LinearLayout) FindViewById(Resource.Id.advanced_container);

                KeyValuePair<string, ProtectedString> pair = new KeyValuePair<string, ProtectedString>("" , new ProtectedString(true, ""));
                LinearLayout ees = CreateExtraStringView(pair);
                container.AddView(ees);

                State.EntryModified = true;

                TextView keyView = (TextView) ees.FindViewById(Resource.Id.title);
                keyView.RequestFocus();
            };
            SetAddExtraStringEnabled();

            ((CheckBox)FindViewById(Resource.Id.entry_expires_checkbox)).CheckedChange += (sender, e) =>
            {
                State.Entry.Expires = e.IsChecked;
                if (e.IsChecked)
                {
                    if (State.Entry.ExpiryTime < DateTime.Now)
                        State.Entry.ExpiryTime = DateTime.Now;
                }
                UpdateExpires();
                State.EntryModified = true;
            };
        }