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