SearchShareesFragment.java 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. private static final String SCREEN_NAME = "Sharee search";
  60. // Parameters
  61. private OCFile mFile;
  62. private Account mAccount;
  63. // other members
  64. private ArrayList<OCShare> mShares;
  65. private ShareUserListAdapter mUserGroupsAdapter = null;
  66. private ShareFragmentListener mListener;
  67. /**
  68. * Public factory method to create new SearchShareesFragment instances.
  69. *
  70. * @param fileToShare An {@link OCFile} to be shared
  71. * @param account The ownCloud account containing fileToShare
  72. * @return A new instance of fragment SearchShareesFragment.
  73. */
  74. public static SearchShareesFragment newInstance(OCFile fileToShare, Account account) {
  75. SearchShareesFragment fragment = new SearchShareesFragment();
  76. Bundle args = new Bundle();
  77. args.putParcelable(ARG_FILE, fileToShare);
  78. args.putParcelable(ARG_ACCOUNT, account);
  79. fragment.setArguments(args);
  80. return fragment;
  81. }
  82. /**
  83. * {@inheritDoc}
  84. */
  85. @Override
  86. public void onCreate(Bundle savedInstanceState) {
  87. super.onCreate(savedInstanceState);
  88. if (getArguments() != null) {
  89. mFile = getArguments().getParcelable(ARG_FILE);
  90. mAccount = getArguments().getParcelable(ARG_ACCOUNT);
  91. }
  92. }
  93. @Override
  94. public void onResume() {
  95. super.onResume();
  96. if (getActivity() != null) {
  97. AnalyticsUtils.setCurrentScreenName(getActivity(), SCREEN_NAME, TAG);
  98. }
  99. }
  100. /**
  101. * {@inheritDoc}
  102. */
  103. @Override
  104. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  105. Bundle savedInstanceState) {
  106. // Inflate the layout for this fragment
  107. View view = inflater.inflate(R.layout.search_users_groups_layout, container, false);
  108. // Get the SearchView and set the searchable configuration
  109. SearchView searchView = (SearchView) view.findViewById(R.id.searchView);
  110. SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
  111. searchView.setSearchableInfo(searchManager.getSearchableInfo(
  112. getActivity().getComponentName()) // assumes parent activity is the searchable activity
  113. );
  114. searchView.setIconifiedByDefault(false); // do not iconify the widget; expand it by default
  115. searchView.setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI); // avoid fullscreen with softkeyboard
  116. searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
  117. @Override
  118. public boolean onQueryTextSubmit(String query) {
  119. Log_OC.v(TAG, "onQueryTextSubmit intercepted, query: " + query);
  120. return true; // return true to prevent the query is processed to be queried;
  121. // a user / group will be picked only if selected in the list of suggestions
  122. }
  123. @Override
  124. public boolean onQueryTextChange(String newText) {
  125. return false; // let it for the parent listener in the hierarchy / default behaviour
  126. }
  127. });
  128. return view;
  129. }
  130. @Override
  131. public void onActivityCreated(Bundle savedInstanceState) {
  132. super.onActivityCreated(savedInstanceState);
  133. getActivity().setTitle(R.string.share_with_title);
  134. // Load data into the list
  135. refreshUsersOrGroupsListFromDB();
  136. }
  137. /**
  138. * Get users and groups from the DB to fill in the "share with" list
  139. *
  140. * Depends on the parent Activity provides a {@link com.owncloud.android.datamodel.FileDataStorageManager}
  141. * instance ready to use. If not ready, does nothing.
  142. */
  143. public void refreshUsersOrGroupsListFromDB (){
  144. // Get Users and Groups
  145. if (((FileActivity) mListener).getStorageManager() != null) {
  146. mShares = ((FileActivity) mListener).getStorageManager().getSharesWithForAFile(
  147. mFile.getRemotePath(),
  148. mAccount.name
  149. );
  150. // Update list of users/groups
  151. updateListOfUserGroups();
  152. }
  153. }
  154. private void updateListOfUserGroups() {
  155. // Update list of users/groups
  156. // TODO Refactoring: create a new {@link ShareUserListAdapter} instance with every call should not be needed
  157. mUserGroupsAdapter = new ShareUserListAdapter(
  158. getActivity().getApplicationContext(),
  159. R.layout.share_user_item, mShares, this
  160. );
  161. // Show data
  162. ListView usersList = (ListView) getView().findViewById(R.id.searchUsersListView);
  163. if (mShares.size() > 0) {
  164. usersList.setVisibility(View.VISIBLE);
  165. usersList.setAdapter(mUserGroupsAdapter);
  166. } else {
  167. usersList.setVisibility(View.GONE);
  168. }
  169. }
  170. @Override
  171. public void onAttach(Activity activity) {
  172. super.onAttach(activity);
  173. try {
  174. mListener = (ShareFragmentListener) activity;
  175. } catch (ClassCastException e) {
  176. throw new ClassCastException(activity.toString()
  177. + " must implement OnFragmentInteractionListener");
  178. }
  179. }
  180. @Override
  181. public void onStart() {
  182. super.onStart();
  183. // focus the search view and request the software keyboard be shown
  184. View searchView = getView().findViewById(R.id.searchView);
  185. if (searchView.requestFocus()) {
  186. InputMethodManager imm = (InputMethodManager)
  187. getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
  188. if (imm != null) {
  189. imm.showSoftInput(searchView.findFocus(), InputMethodManager.SHOW_IMPLICIT);
  190. }
  191. }
  192. }
  193. @Override
  194. public void onDetach() {
  195. super.onDetach();
  196. mListener = null;
  197. }
  198. @Override
  199. public void unshareButtonPressed(OCShare share) {
  200. // Unshare
  201. mListener.unshareWith(share);
  202. Log_OC.d(TAG, "Unshare - " + share.getSharedWithDisplayName());
  203. }
  204. @Override
  205. public void editShare(OCShare share) {
  206. // move to fragment to edit share
  207. Log_OC.d(TAG, "Editing " + share.getSharedWithDisplayName());
  208. mListener.showEditShare(share);
  209. }
  210. }