AccountActionsDialogFragment.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package com.owncloud.android.ui.dialog;
  2. import android.os.Bundle;
  3. import android.support.v4.app.DialogFragment;
  4. import android.view.LayoutInflater;
  5. import android.view.Menu;
  6. import android.view.MenuItem;
  7. import android.view.View;
  8. import android.view.ViewGroup;
  9. import android.view.Window;
  10. import android.widget.AdapterView;
  11. import android.widget.AdapterView.OnItemClickListener;
  12. import android.widget.ArrayAdapter;
  13. import android.widget.ListView;
  14. import com.owncloud.android.R;
  15. import com.owncloud.android.ui.dialog.parcel.MenuItemParcelable;
  16. import com.owncloud.android.ui.dialog.parcel.MenuParcelable;
  17. import java.util.ArrayList;
  18. import java.util.List;
  19. /**
  20. * Dialog for choosing a file action.
  21. */
  22. public class AccountActionsDialogFragment extends DialogFragment implements
  23. OnItemClickListener {
  24. private static final String ARG_ITEM_LIST = "ITEM_LIST";
  25. private static final String ARG_FILE_POSITION = "FILE_POSITION";
  26. public static final String FTAG_FILE_ACTIONS = "FILE_ACTIONS_FRAGMENT";
  27. private List<MenuItemParcelable> mMenuItems;
  28. private int mFilePosition;
  29. private ListView mListView;
  30. /**
  31. * Listener interface for the file action fragment.
  32. */
  33. public interface FileActionsDialogFragmentListener {
  34. // TODO Tobi change to int array?
  35. public boolean onFileActionChosen(int menuId, int filePosition);
  36. }
  37. /**
  38. * Public factory method to create new FileActionsDialogFragment instances.
  39. *
  40. * @param menu menu to be display.
  41. * @return Dialog ready to show.
  42. */
  43. public static AccountActionsDialogFragment newInstance(Menu menu, int filePosition) {
  44. AccountActionsDialogFragment fragment = new AccountActionsDialogFragment();
  45. Bundle args = new Bundle();
  46. MenuParcelable menuParcelable = new MenuParcelable();
  47. menuParcelable.setMenuItems(calculateMenuParcel(menu));
  48. args.putParcelable(ARG_ITEM_LIST, menuParcelable);
  49. args.putInt(ARG_FILE_POSITION, filePosition);
  50. fragment.setArguments(args);
  51. return fragment;
  52. }
  53. /**
  54. * {@inheritDoc}
  55. */
  56. @Override
  57. public void onCreate(Bundle savedInstanceState) {
  58. super.onCreate(savedInstanceState);
  59. setStyle(DialogFragment.STYLE_NORMAL, R.style.ownCloud_Dialog);
  60. }
  61. /**
  62. * {@inheritDoc}
  63. */
  64. @Override
  65. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  66. Bundle savedInstanceState) {
  67. View view = inflater.inflate(R.layout.file_actions, null, false);
  68. mListView = (ListView) view.findViewById(R.id.file_actions_list);
  69. getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
  70. return view;
  71. }
  72. /**
  73. * {@inheritDoc}
  74. */
  75. @Override
  76. public void onActivityCreated(Bundle savedInstanceState) {
  77. super.onActivityCreated(savedInstanceState);
  78. mFilePosition = getArguments().getInt(ARG_FILE_POSITION);
  79. MenuParcelable menu = getArguments().getParcelable(ARG_ITEM_LIST);
  80. mMenuItems = menu.getMenuItems();
  81. List<String> stringList = new ArrayList<String>();
  82. for (int i = 0; i < mMenuItems.size(); i++) {
  83. stringList.add(mMenuItems.get(i).getMenuText());
  84. }
  85. ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
  86. R.layout.simple_dialog_list_item, stringList);
  87. mListView.setAdapter(adapter);
  88. mListView.setOnItemClickListener(this);
  89. }
  90. /**
  91. * {@inheritDoc}
  92. */
  93. @Override
  94. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  95. dismiss();
  96. ((FileActionsDialogFragmentListener) getTargetFragment()).onFileActionChosen(mMenuItems.get(position).getMenuItemId(), mFilePosition);
  97. }
  98. /**
  99. * calculates a parcelable list of MenuItemParcelable based on the given menu and the visibility of the menu items.
  100. *
  101. * @param menu the menu to be displayed
  102. * @return a filtered List of parcelables based on the menu
  103. */
  104. private static List<MenuItemParcelable> calculateMenuParcel(Menu menu) {
  105. int index = 0;
  106. boolean hasNext = true;
  107. List<MenuItemParcelable> itemList = new ArrayList<MenuItemParcelable>();
  108. MenuParcelable menuParcelable = new MenuParcelable();
  109. try {
  110. while (hasNext) {
  111. MenuItem item = menu.getItem(index);
  112. if (item.isVisible()) {
  113. itemList.add(new MenuItemParcelable(item));
  114. }
  115. index++;
  116. }
  117. } catch (IndexOutOfBoundsException iobe) {
  118. // reach the end of the item list
  119. }
  120. return itemList;
  121. }
  122. }