diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 2ddbcd935..b1f29e2b3 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -93,8 +93,6 @@ - - diff --git a/app/src/main/java/com/keepassdroid/GroupActivity.java b/app/src/main/java/com/keepassdroid/GroupActivity.java index 1a1a7632b..4c88ecee1 100644 --- a/app/src/main/java/com/keepassdroid/GroupActivity.java +++ b/app/src/main/java/com/keepassdroid/GroupActivity.java @@ -51,10 +51,11 @@ import com.keepassdroid.view.GroupAddEntryView; import com.keepassdroid.view.GroupRootView; import com.keepassdroid.view.GroupViewOnlyView; -public abstract class GroupActivity extends GroupBaseActivity { - - public static final int UNINIT = -1; - +public abstract class GroupActivity extends GroupBaseActivity + implements GroupEditFragment.CreateGroupListener, IconPickerFragment.IconPickerListener { + + private static final String TAG_CREATE_GROUP = "TAG_CREATE_GROUP"; + protected boolean addGroupEnabled = false; protected boolean addEntryEnabled = false; protected boolean isRoot = false; @@ -158,7 +159,8 @@ public abstract class GroupActivity extends GroupBaseActivity { addGroup.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { - GroupEditActivity.Launch(GroupActivity.this); + GroupEditFragment groupEditFragment = new GroupEditFragment(); + groupEditFragment.show(getSupportFragmentManager(), TAG_CREATE_GROUP); } }); } @@ -195,8 +197,6 @@ public abstract class GroupActivity extends GroupBaseActivity { cv.onCreateMenu(menu, menuInfo); } - - @Override public boolean onContextItemSelected(MenuItem item) { AdapterContextMenuInfo acmi = (AdapterContextMenuInfo) item.getMenuInfo(); @@ -204,27 +204,31 @@ public abstract class GroupActivity extends GroupBaseActivity { return cv.onContextItemSelected(item); } - - @Override - protected void onActivityResult(int requestCode, int resultCode, Intent data) - { - switch (resultCode) - { - case Activity.RESULT_OK: - String GroupName = data.getExtras().getString(GroupEditActivity.KEY_NAME); - int GroupIconID = data.getExtras().getInt(GroupEditActivity.KEY_ICON_ID); - GroupActivity act = GroupActivity.this; - Handler handler = new Handler(); - AddGroup task = AddGroup.getInstance(this, App.getDB(), GroupName, GroupIconID, mGroup, act.new RefreshTask(handler), false); - ProgressTask pt = new ProgressTask(act, task, R.string.saving_database); - pt.run(); - break; - case Activity.RESULT_CANCELED: - default: - break; - } - } + @Override + public void approveCreateGroup(Bundle bundle) { + String GroupName = bundle.getString(GroupEditFragment.KEY_NAME); + int GroupIconID = bundle.getInt(GroupEditFragment.KEY_ICON_ID); + GroupActivity act = GroupActivity.this; + Handler handler = new Handler(); + AddGroup task = AddGroup.getInstance(this, App.getDB(), GroupName, GroupIconID, mGroup, act.new RefreshTask(handler), false); + ProgressTask pt = new ProgressTask(act, task, R.string.saving_database); + pt.run(); + } + + @Override + public void cancelCreateGroup(Bundle bundle) { + // Do nothing here + } + + @Override + // For icon in create group dialog + public void iconPicked(Bundle bundle) { + GroupEditFragment groupEditFragment = (GroupEditFragment) getSupportFragmentManager().findFragmentByTag(TAG_CREATE_GROUP); + if (groupEditFragment != null) { + groupEditFragment.iconPicked(bundle); + } + } protected void showWarnings() { if (App.getDB().readOnly) { @@ -236,6 +240,4 @@ public abstract class GroupActivity extends GroupBaseActivity { } } } - - } diff --git a/app/src/main/java/com/keepassdroid/GroupEditActivity.java b/app/src/main/java/com/keepassdroid/GroupEditActivity.java deleted file mode 100644 index e720b676e..000000000 --- a/app/src/main/java/com/keepassdroid/GroupEditActivity.java +++ /dev/null @@ -1,118 +0,0 @@ -/* - * Copyright 2017 Brian Pellin, Jeremy Jamet / Kunzisoft. - * - * This file is part of KeePass DX. - * - * KeePass DX is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 2 of the License, or - * (at your option) any later version. - * - * KeePass DX is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with KeePass DX. If not, see . - * - */ -package com.keepassdroid; - -import android.app.Activity; -import android.content.Intent; -import android.os.Bundle; -import android.support.v7.app.AppCompatActivity; -import android.view.View; -import android.widget.Button; -import android.widget.ImageButton; -import android.widget.TextView; -import android.widget.Toast; - -import com.android.keepass.R; -import com.keepassdroid.icons.Icons; - -public class GroupEditActivity extends AppCompatActivity - implements IconPickerFragment.IconPickerListener { - - public static final String KEY_NAME = "name"; - public static final String KEY_ICON_ID = "icon_id"; - - private int mSelectedIconID; - - public static void Launch(Activity act) { - Intent i = new Intent(act, GroupEditActivity.class); - act.startActivityForResult(i, 0); - } - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.group_edit); - setTitle(R.string.add_group_title); - - ImageButton iconButton = (ImageButton) findViewById(R.id.icon_button); - iconButton.setOnClickListener(new View.OnClickListener() - { - public void onClick(View v) - { - IconPickerFragment.Launch(GroupEditActivity.this); - } - }); - - Button okButton = (Button) findViewById(R.id.ok); - okButton.setOnClickListener(new View.OnClickListener() - { - public void onClick(View v) - { - TextView nameField = (TextView) findViewById(R.id.group_name); - String name = nameField.getText().toString(); - - if ( name.length() > 0 ) - { - final Intent intent = new Intent(); - - intent.putExtra(KEY_NAME, name); - intent.putExtra(KEY_ICON_ID, mSelectedIconID); - setResult(Activity.RESULT_OK, intent); - - finish(); - } - else - { - Toast.makeText(GroupEditActivity.this, R.string.error_no_name, Toast.LENGTH_LONG).show(); - } - } - }); - - Button cancel = (Button) findViewById(R.id.cancel); - cancel.setOnClickListener(new View.OnClickListener() - { - public void onClick(View v) - { - final Intent intent = new Intent(); - setResult(Activity.RESULT_CANCELED, intent); - - finish(); - } - }); - } - - @Override - protected void onActivityResult(int requestCode, int resultCode, Intent data) - { - switch (resultCode) - { - case Activity.RESULT_CANCELED: - default: - break; - } - } - - @Override - public void iconPicked(Bundle bundle) { - mSelectedIconID = bundle.getInt(IconPickerFragment.KEY_ICON_ID); - ImageButton currIconButton = (ImageButton) findViewById(R.id.icon_button); - currIconButton.setImageResource(Icons.iconToResId(mSelectedIconID)); - } -} diff --git a/app/src/main/java/com/keepassdroid/GroupEditFragment.java b/app/src/main/java/com/keepassdroid/GroupEditFragment.java new file mode 100644 index 000000000..c4b2812d4 --- /dev/null +++ b/app/src/main/java/com/keepassdroid/GroupEditFragment.java @@ -0,0 +1,122 @@ +/* + * Copyright 2017 Brian Pellin, Jeremy Jamet / Kunzisoft. + * + * This file is part of KeePass DX. + * + * KeePass DX is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * KeePass DX is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with KeePass DX. If not, see . + * + */ +package com.keepassdroid; + +import android.app.Dialog; +import android.content.Context; +import android.content.DialogInterface; +import android.os.Bundle; +import android.support.annotation.NonNull; +import android.support.v4.app.DialogFragment; +import android.support.v7.app.AlertDialog; +import android.view.LayoutInflater; +import android.view.View; +import android.widget.ImageButton; +import android.widget.TextView; +import android.widget.Toast; + +import com.android.keepass.R; +import com.keepassdroid.icons.Icons; + +public class GroupEditFragment extends DialogFragment + implements IconPickerFragment.IconPickerListener { + + public static final String KEY_NAME = "name"; + public static final String KEY_ICON_ID = "icon_id"; + + private CreateGroupListener createGroupListener; + + private int mSelectedIconID; + private View root; + + @Override + public void onAttach(Context context) { + super.onAttach(context); + // Verify that the host activity implements the callback interface + try { + // Instantiate the NoticeDialogListener so we can send events to the host + createGroupListener = (CreateGroupListener) context; + createGroupListener = (CreateGroupListener) context; + } catch (ClassCastException e) { + // The activity doesn't implement the interface, throw exception + throw new ClassCastException(context.toString() + + " must implement " + GroupEditFragment.class.getName()); + } + } + + @NonNull + @Override + public Dialog onCreateDialog(Bundle savedInstanceState) { + AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); + LayoutInflater inflater = getActivity().getLayoutInflater(); + + root = inflater.inflate(R.layout.group_edit, null); + builder.setView(root) + .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int id) { + TextView nameField = (TextView) root.findViewById(R.id.group_name); + String name = nameField.getText().toString(); + + if ( name.length() > 0 ) { + Bundle bundle = new Bundle(); + bundle.putString(KEY_NAME, name); + bundle.putInt(KEY_ICON_ID, mSelectedIconID); + createGroupListener.approveCreateGroup(bundle); + + GroupEditFragment.this.getDialog().cancel(); + } + else { + Toast.makeText(getContext(), R.string.error_no_name, Toast.LENGTH_LONG).show(); + } + } + }) + .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + Bundle bundle = new Bundle(); + createGroupListener.cancelCreateGroup(bundle); + + GroupEditFragment.this.getDialog().cancel(); + } + }); + + final ImageButton iconButton = (ImageButton) root.findViewById(R.id.icon_button); + iconButton.setOnClickListener(new View.OnClickListener() { + public void onClick(View v) { + IconPickerFragment iconPickerFragment = new IconPickerFragment(); + iconPickerFragment.show(getFragmentManager(), "IconPickerFragment"); + } + }); + + return builder.create(); + } + + @Override + public void iconPicked(Bundle bundle) { + mSelectedIconID = bundle.getInt(IconPickerFragment.KEY_ICON_ID); + ImageButton currIconButton = (ImageButton) root.findViewById(R.id.icon_button); + currIconButton.setImageResource(Icons.iconToResId(mSelectedIconID)); + } + + public interface CreateGroupListener { + void approveCreateGroup(Bundle bundle); + void cancelCreateGroup(Bundle bundle); + } +} diff --git a/app/src/main/java/com/keepassdroid/IconPickerFragment.java b/app/src/main/java/com/keepassdroid/IconPickerFragment.java index 5c39c34c6..d95cc4342 100644 --- a/app/src/main/java/com/keepassdroid/IconPickerFragment.java +++ b/app/src/main/java/com/keepassdroid/IconPickerFragment.java @@ -82,7 +82,6 @@ public class IconPickerFragment extends DialogFragment { Bundle bundle = new Bundle(); bundle.putInt(KEY_ICON_ID, position); iconPickerListener.iconPicked(bundle); - // TODO setResult(EntryEditActivity.RESULT_OK_ICON_PICKER, intent); dismiss(); } diff --git a/app/src/main/res/layout/group_edit.xml b/app/src/main/res/layout/group_edit.xml index bc0879e8f..2f49cecb4 100644 --- a/app/src/main/res/layout/group_edit.xml +++ b/app/src/main/res/layout/group_edit.xml @@ -18,7 +18,7 @@ along with KeePass DX. If not, see . --> -