Code cleanups
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / fragment / SearchShareesFragment.java
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
22 package com.owncloud.android.ui.fragment;
23
24 import android.accounts.Account;
25 import android.app.Activity;
26 import android.app.SearchManager;
27 import android.content.Context;
28 import android.os.Bundle;
29 import android.support.v4.app.Fragment;
30 import android.view.LayoutInflater;
31 import android.view.View;
32 import android.view.ViewGroup;
33 import android.view.inputmethod.EditorInfo;
34 import android.view.inputmethod.InputMethodManager;
35 import android.widget.ListView;
36 import android.widget.SearchView;
37
38 import com.owncloud.android.R;
39 import com.owncloud.android.datamodel.OCFile;
40 import com.owncloud.android.lib.common.utils.Log_OC;
41 import com.owncloud.android.lib.resources.shares.OCShare;
42 import com.owncloud.android.ui.activity.FileActivity;
43 import com.owncloud.android.ui.activity.ShareActivity;
44 import com.owncloud.android.ui.adapter.ShareUserListAdapter;
45
46 import java.util.ArrayList;
47
48 /**
49 * Fragment for Searching sharees (users and groups)
50 *
51 * A simple {@link Fragment} subclass.
52 *
53 * Activities that contain this fragment must implement the
54 * {@link SearchShareesFragment.OnSearchFragmentInteractionListener} interface
55 * to handle interaction events.
56 *
57 * Use the {@link SearchShareesFragment#newInstance} factory method to
58 * create an instance of this fragment.
59 */
60 public class SearchShareesFragment extends Fragment implements ShareUserListAdapter.ShareUserAdapterListener {
61 private static final String TAG = SearchShareesFragment.class.getSimpleName();
62
63 // the fragment initialization parameters
64 private static final String ARG_FILE = "FILE";
65 private static final String ARG_ACCOUNT = "ACCOUNT";
66
67 // Parameters
68 private OCFile mFile;
69 private Account mAccount;
70
71 // other members
72 private ArrayList<OCShare> mShares;
73 private ShareUserListAdapter mUserGroupsAdapter = null;
74 private OnSearchFragmentInteractionListener mListener;
75
76
77 /**
78 * Public factory method to create new SearchShareesFragment instances.
79 *
80 * @param fileToShare An {@link OCFile} to be shared
81 * @param account The ownCloud account containing fileToShare
82 * @return A new instance of fragment SearchShareesFragment.
83 */
84 public static SearchShareesFragment newInstance(OCFile fileToShare, Account account) {
85 SearchShareesFragment fragment = new SearchShareesFragment();
86 Bundle args = new Bundle();
87 args.putParcelable(ARG_FILE, fileToShare);
88 args.putParcelable(ARG_ACCOUNT, account);
89 fragment.setArguments(args);
90 return fragment;
91 }
92
93 public SearchShareesFragment() {
94 // Required empty public constructor
95 }
96
97 /**
98 * {@inheritDoc}
99 */
100 @Override
101 public void onCreate(Bundle savedInstanceState) {
102 super.onCreate(savedInstanceState);
103 if (getArguments() != null) {
104 mFile = getArguments().getParcelable(ARG_FILE);
105 mAccount = getArguments().getParcelable(ARG_ACCOUNT);
106 }
107
108 }
109
110 /**
111 * {@inheritDoc}
112 */
113 @Override
114 public View onCreateView(LayoutInflater inflater, ViewGroup container,
115 Bundle savedInstanceState) {
116 // Inflate the layout for this fragment
117 View view = inflater.inflate(R.layout.search_users_groups_layout, container, false);
118
119 // Get the SearchView and set the searchable configuration
120 SearchView searchView = (SearchView) view.findViewById(R.id.searchView);
121 SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
122 searchView.setSearchableInfo(searchManager.getSearchableInfo(
123 getActivity().getComponentName()) // assumes parent activity is the searchable activity
124 );
125 searchView.setIconifiedByDefault(false); // do not iconify the widget; expand it by default
126
127 searchView.setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI); // avoid fullscreen with softkeyboard
128
129 searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
130 @Override
131 public boolean onQueryTextSubmit(String query) {
132 Log_OC.v(TAG, "onQueryTextSubmit intercepted, query: " + query);
133 return true; // return true to prevent the query is processed to be queried;
134 // a user / group will be picked only if selected in the list of suggestions
135 }
136
137 @Override
138 public boolean onQueryTextChange(String newText) {
139 return false; // let it for the parent listener in the hierarchy / default behaviour
140 }
141 });
142
143 return view;
144 }
145
146
147 @Override
148 public void onActivityCreated(Bundle savedInstanceState) {
149 super.onActivityCreated(savedInstanceState);
150
151 // Load data into the list
152 refreshUsersOrGroupsListFromDB();
153 }
154
155
156 /**
157 * Get users and groups from the DB to fill in the "share with" list
158 *
159 * Depends on the parent Activity provides a {@link com.owncloud.android.datamodel.FileDataStorageManager}
160 * instance ready to use. If not ready, does nothing.
161 */
162 public void refreshUsersOrGroupsListFromDB (){
163 // Get Users and Groups
164 if (((FileActivity) mListener).getStorageManager() != null) {
165 mShares = ((FileActivity) mListener).getStorageManager().getSharesWithForAFile(
166 mFile.getRemotePath(),
167 mAccount.name
168 );
169
170 // Update list of users/groups
171 updateListOfUserGroups();
172 }
173 }
174
175 private void updateListOfUserGroups() {
176 // Update list of users/groups
177 // TODO Refactoring: create a new {@link ShareUserListAdapter} instance with every call should not be needed
178 mUserGroupsAdapter = new ShareUserListAdapter(
179 getActivity().getApplicationContext(),
180 R.layout.share_user_item, mShares, this
181 );
182
183 // Show data
184 ListView usersList = (ListView) getView().findViewById(R.id.searchUsersListView);
185
186 if (mShares.size() > 0) {
187 usersList.setVisibility(View.VISIBLE);
188 usersList.setAdapter(mUserGroupsAdapter);
189
190 } else {
191 usersList.setVisibility(View.GONE);
192 }
193 }
194
195 @Override
196 public void onAttach(Activity activity) {
197 super.onAttach(activity);
198 try {
199 mListener = (OnSearchFragmentInteractionListener) activity;
200 } catch (ClassCastException e) {
201 throw new ClassCastException(activity.toString()
202 + " must implement OnFragmentInteractionListener");
203 }
204 }
205
206 @Override
207 public void onStart() {
208 super.onStart();
209 // focus the search view and request the software keyboard be shown
210 View searchView = getView().findViewById(R.id.searchView);
211 if (searchView.requestFocus()) {
212 InputMethodManager imm = (InputMethodManager)
213 getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
214 if (imm != null) {
215 imm.showSoftInput(searchView.findFocus(), InputMethodManager.SHOW_IMPLICIT);
216 }
217 }
218 }
219
220 @Override
221 public void onDetach() {
222 super.onDetach();
223 mListener = null;
224 }
225
226 @Override
227 public void unshareButtonPressed(OCShare share) {
228 // Unshare
229 mListener.unshareWith(share);
230 Log_OC.d(TAG, "Unshare - " + share.getSharedWithDisplayName());
231 }
232
233 /**
234 * This interface must be implemented by activities that contain this
235 * fragment to allow an interaction in this fragment to be communicated
236 * to the activity and potentially other fragments contained in that
237 * activity.
238 * <p/>
239 * See the Android Training lesson <a href=
240 * "http://developer.android.com/training/basics/fragments/communicating.html"
241 * >Communicating with Other Fragments</a> for more information.
242 */
243 public interface OnSearchFragmentInteractionListener {
244 void unshareWith(OCShare share);
245 }
246
247 }