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