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