SearchShareesFragment.java 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /**
  2. * ownCloud Android client application
  3. *
  4. * @author masensio
  5. * @author David A. Velasco
  6. * Copyright (C) 2015 ownCloud Inc.
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. package com.owncloud.android.ui.fragment;
  22. import android.accounts.Account;
  23. import android.app.Activity;
  24. import android.app.SearchManager;
  25. import android.content.Context;
  26. import android.os.Bundle;
  27. import android.support.v4.app.Fragment;
  28. import android.support.v7.widget.SearchView;
  29. import android.view.LayoutInflater;
  30. import android.view.View;
  31. import android.view.ViewGroup;
  32. import android.view.inputmethod.EditorInfo;
  33. import android.view.inputmethod.InputMethodManager;
  34. import android.widget.ListView;
  35. import com.owncloud.android.R;
  36. import com.owncloud.android.datamodel.OCFile;
  37. import com.owncloud.android.lib.common.utils.Log_OC;
  38. import com.owncloud.android.lib.resources.shares.OCShare;
  39. import com.owncloud.android.ui.activity.FileActivity;
  40. import com.owncloud.android.ui.adapter.ShareUserListAdapter;
  41. import java.util.ArrayList;
  42. /**
  43. * Fragment for Searching sharees (users and groups)
  44. *
  45. * A simple {@link Fragment} subclass.
  46. *
  47. * Activities that contain this fragment must implement the
  48. * {@link ShareFragmentListener} interface
  49. * to handle interaction events.
  50. *
  51. * Use the {@link SearchShareesFragment#newInstance} factory method to
  52. * create an instance of this fragment.
  53. */
  54. public class SearchShareesFragment extends Fragment implements ShareUserListAdapter.ShareUserAdapterListener {
  55. private static final String TAG = SearchShareesFragment.class.getSimpleName();
  56. // the fragment initialization parameters
  57. private static final String ARG_FILE = "FILE";
  58. private static final String ARG_ACCOUNT = "ACCOUNT";
  59. // Parameters
  60. private OCFile mFile;
  61. private Account mAccount;
  62. // other members
  63. private ArrayList<OCShare> mShares;
  64. private ShareUserListAdapter mUserGroupsAdapter = null;
  65. private ShareFragmentListener mListener;
  66. /**
  67. * Public factory method to create new SearchShareesFragment instances.
  68. *
  69. * @param fileToShare An {@link OCFile} to be shared
  70. * @param account The ownCloud account containing fileToShare
  71. * @return A new instance of fragment SearchShareesFragment.
  72. */
  73. public static SearchShareesFragment newInstance(OCFile fileToShare, Account account) {
  74. SearchShareesFragment fragment = new SearchShareesFragment();
  75. Bundle args = new Bundle();
  76. args.putParcelable(ARG_FILE, fileToShare);
  77. args.putParcelable(ARG_ACCOUNT, account);
  78. fragment.setArguments(args);
  79. return fragment;
  80. }
  81. /**
  82. * {@inheritDoc}
  83. */
  84. @Override
  85. public void onCreate(Bundle savedInstanceState) {
  86. super.onCreate(savedInstanceState);
  87. if (getArguments() != null) {
  88. mFile = getArguments().getParcelable(ARG_FILE);
  89. mAccount = getArguments().getParcelable(ARG_ACCOUNT);
  90. }
  91. }
  92. /**
  93. * {@inheritDoc}
  94. */
  95. @Override
  96. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  97. Bundle savedInstanceState) {
  98. // Inflate the layout for this fragment
  99. View view = inflater.inflate(R.layout.search_users_groups_layout, container, false);
  100. // Get the SearchView and set the searchable configuration
  101. SearchView searchView = (SearchView) view.findViewById(R.id.searchView);
  102. SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
  103. searchView.setSearchableInfo(searchManager.getSearchableInfo(
  104. getActivity().getComponentName()) // assumes parent activity is the searchable activity
  105. );
  106. searchView.setIconifiedByDefault(false); // do not iconify the widget; expand it by default
  107. searchView.setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI); // avoid fullscreen with softkeyboard
  108. searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
  109. @Override
  110. public boolean onQueryTextSubmit(String query) {
  111. Log_OC.v(TAG, "onQueryTextSubmit intercepted, query: " + query);
  112. return true; // return true to prevent the query is processed to be queried;
  113. // a user / group will be picked only if selected in the list of suggestions
  114. }
  115. @Override
  116. public boolean onQueryTextChange(String newText) {
  117. return false; // let it for the parent listener in the hierarchy / default behaviour
  118. }
  119. });
  120. return view;
  121. }
  122. @Override
  123. public void onActivityCreated(Bundle savedInstanceState) {
  124. super.onActivityCreated(savedInstanceState);
  125. getActivity().setTitle(R.string.share_with_title);
  126. // Load data into the list
  127. refreshUsersOrGroupsListFromDB();
  128. }
  129. /**
  130. * Get users and groups from the DB to fill in the "share with" list
  131. *
  132. * Depends on the parent Activity provides a {@link com.owncloud.android.datamodel.FileDataStorageManager}
  133. * instance ready to use. If not ready, does nothing.
  134. */
  135. public void refreshUsersOrGroupsListFromDB (){
  136. // Get Users and Groups
  137. if (((FileActivity) mListener).getStorageManager() != null) {
  138. mShares = ((FileActivity) mListener).getStorageManager().getSharesWithForAFile(
  139. mFile.getRemotePath(),
  140. mAccount.name
  141. );
  142. // Update list of users/groups
  143. updateListOfUserGroups();
  144. }
  145. }
  146. private void updateListOfUserGroups() {
  147. // Update list of users/groups
  148. // TODO Refactoring: create a new {@link ShareUserListAdapter} instance with every call should not be needed
  149. mUserGroupsAdapter = new ShareUserListAdapter(
  150. getActivity().getApplicationContext(),
  151. R.layout.share_user_item, mShares, this
  152. );
  153. // Show data
  154. ListView usersList = (ListView) getView().findViewById(R.id.searchUsersListView);
  155. if (mShares.size() > 0) {
  156. usersList.setVisibility(View.VISIBLE);
  157. usersList.setAdapter(mUserGroupsAdapter);
  158. } else {
  159. usersList.setVisibility(View.GONE);
  160. }
  161. }
  162. @Override
  163. public void onAttach(Activity activity) {
  164. super.onAttach(activity);
  165. try {
  166. mListener = (ShareFragmentListener) activity;
  167. } catch (ClassCastException e) {
  168. throw new ClassCastException(activity.toString()
  169. + " must implement OnFragmentInteractionListener");
  170. }
  171. }
  172. @Override
  173. public void onStart() {
  174. super.onStart();
  175. // focus the search view and request the software keyboard be shown
  176. View searchView = getView().findViewById(R.id.searchView);
  177. if (searchView.requestFocus()) {
  178. InputMethodManager imm = (InputMethodManager)
  179. getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
  180. if (imm != null) {
  181. imm.showSoftInput(searchView.findFocus(), InputMethodManager.SHOW_IMPLICIT);
  182. }
  183. }
  184. }
  185. @Override
  186. public void onDetach() {
  187. super.onDetach();
  188. mListener = null;
  189. }
  190. @Override
  191. public void unshareButtonPressed(OCShare share) {
  192. // Unshare
  193. mListener.unshareWith(share);
  194. Log_OC.d(TAG, "Unshare - " + share.getSharedWithDisplayName());
  195. }
  196. @Override
  197. public void editShare(OCShare share) {
  198. // move to fragment to edit share
  199. Log_OC.d(TAG, "Editing " + share.getSharedWithDisplayName());
  200. mListener.showEditShare(share);
  201. }
  202. }