2 * ownCloud Android client application
5 * @author David A. Velasco
6 * Copyright (C) 2015 ownCloud Inc.
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.
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.
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/>.
22 package com
.owncloud
.android
.ui
.fragment
;
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
.InputMethodManager
;
34 import android
.widget
.ListView
;
35 import android
.widget
.SearchView
;
37 import com
.owncloud
.android
.R
;
38 import com
.owncloud
.android
.datamodel
.OCFile
;
39 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
40 import com
.owncloud
.android
.lib
.resources
.shares
.OCShare
;
41 import com
.owncloud
.android
.ui
.activity
.ShareActivity
;
42 import com
.owncloud
.android
.ui
.adapter
.ShareUserListAdapter
;
44 import java
.util
.ArrayList
;
47 * Fragment for Searching users and groups
49 * A simple {@link Fragment} subclass.
50 * Activities that contain this fragment must implement the
51 * {@link SearchFragment.OnSearchFragmentInteractionListener} interface
52 * to handle interaction events.
53 * Use the {@link SearchFragment#newInstance} factory method to
54 * create an instance of this fragment.
56 public class SearchFragment
extends Fragment
implements ShareUserListAdapter
.ShareUserAdapterListener
{
57 private static final String TAG
= SearchFragment
.class.getSimpleName();
59 // the fragment initialization parameters
60 private static final String ARG_FILE
= "FILE";
61 private static final String ARG_ACCOUNT
= "ACCOUNT";
62 private static final String ARG_SHARES
= "SHARES";
66 private Account mAccount
;
67 private ArrayList
<OCShare
> mShares
;
68 private ShareUserListAdapter mUserGroupsAdapter
= null
;
70 private OnSearchFragmentInteractionListener mListener
;
73 * Public factory method to create new SearchFragment instances.
75 * @param fileToShare An {@link OCFile} to show in the fragment
76 * @param account An ownCloud account
78 * @return A new instance of fragment SearchFragment.
80 // TODO: Rename and change types and number of parameters
81 public static SearchFragment
newInstance(OCFile fileToShare
, Account account
, ArrayList
<OCShare
> shares
) {
82 SearchFragment fragment
= new SearchFragment();
83 Bundle args
= new Bundle();
84 args
.putParcelable(ARG_FILE
, fileToShare
);
85 args
.putParcelable(ARG_ACCOUNT
, account
);
86 args
.putParcelableArrayList(ARG_SHARES
, shares
);
87 fragment
.setArguments(args
);
91 public SearchFragment() {
92 // Required empty public constructor
96 public void onCreate(Bundle savedInstanceState
) {
97 super.onCreate(savedInstanceState
);
98 if (getArguments() != null
) {
99 mFile
= getArguments().getParcelable(ARG_FILE
);
100 mAccount
= getArguments().getParcelable(ARG_ACCOUNT
);
101 mShares
= getArguments().getParcelableArrayList(ARG_SHARES
);
107 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
,
108 Bundle savedInstanceState
) {
109 // Inflate the layout for this fragment
110 View view
= inflater
.inflate(R
.layout
.search_users_groups_layout
, container
, false
);
112 // Get the SearchView and set the searchable configuration
113 SearchView searchView
= (SearchView
) view
.findViewById(R
.id
.searchView
);
114 SearchManager searchManager
= (SearchManager
) getActivity().getSystemService(Context
.SEARCH_SERVICE
);
115 searchView
.setSearchableInfo(searchManager
.getSearchableInfo(
116 getActivity().getComponentName()) // assumes parent activity is the searchable activity
118 searchView
.setIconifiedByDefault(false
); // do not iconify the widget; expand it by default
120 //searchView.setImeOptions(EditorInfo.IME_ACTION_NEXT | EditorInfo.IME_FLAG_NO_FULLSCREEN);
122 searchView
.setOnQueryTextListener(new SearchView
.OnQueryTextListener() {
124 public boolean onQueryTextSubmit(String query
) {
125 Log_OC
.v(TAG
, "onQueryTextSubmit intercepted, query: " + query
);
126 return true
; // return true to prevent the query is processed to be queried;
127 // a user / group will be picked only if selected in the list of suggestions
131 public boolean onQueryTextChange(String newText
) {
132 return false
; // let it for the parent listener in the hierarchy / default behaviour
136 // Show data: Fill in list of users and groups
137 ListView usersList
= (ListView
) view
.findViewById(R
.id
.searchUsersListView
);
138 mUserGroupsAdapter
= new ShareUserListAdapter(getActivity().getApplicationContext(),
139 R
.layout
.share_user_item
, mShares
, this);
140 if (mShares
.size() > 0) {
141 usersList
.setVisibility(View
.VISIBLE
);
142 usersList
.setAdapter(mUserGroupsAdapter
);
149 * Get users and groups fromn the DB to fill in the "share with" list
151 public void refreshUsersOrGroupsListFromDB (){
152 // Get Users and Groups
153 mShares
= ((ShareActivity
) mListener
).getStorageManager().getSharesWithForAFile(mFile
.getRemotePath(),
156 // Update list of users/groups
157 updateListOfUserGroups();
160 private void updateListOfUserGroups() {
161 // Update list of users/groups
162 mUserGroupsAdapter
= new ShareUserListAdapter(getActivity().getApplicationContext(),
163 R
.layout
.share_user_item
, mShares
, this);
166 ListView usersList
= (ListView
) getView().findViewById(R
.id
.searchUsersListView
);
168 if (mShares
.size() > 0) {
169 usersList
.setVisibility(View
.VISIBLE
);
170 usersList
.setAdapter(mUserGroupsAdapter
);
173 usersList
.setVisibility(View
.GONE
);
178 public void onAttach(Activity activity
) {
179 super.onAttach(activity
);
181 mListener
= (OnSearchFragmentInteractionListener
) activity
;
182 } catch (ClassCastException e
) {
183 throw new ClassCastException(activity
.toString()
184 + " must implement OnFragmentInteractionListener");
189 public void onStart() {
191 // focus the search view and request the software keyboard be shown
192 View searchView
= getView().findViewById(R
.id
.searchView
);
193 if (searchView
.requestFocus()) {
194 InputMethodManager imm
= (InputMethodManager
)
195 getActivity().getSystemService(Context
.INPUT_METHOD_SERVICE
);
197 imm
.showSoftInput(searchView
.findFocus(), InputMethodManager
.SHOW_IMPLICIT
);
203 public void onDetach() {
209 public void unshareButtonPressed(OCShare share
) {
211 mListener
.unshareWith(share
);
212 Log_OC
.d(TAG
, "Unshare - " + share
.getSharedWithDisplayName());
216 * This interface must be implemented by activities that contain this
217 * fragment to allow an interaction in this fragment to be communicated
218 * to the activity and potentially other fragments contained in that
221 * See the Android Training lesson <a href=
222 * "http://developer.android.com/training/basics/fragments/communicating.html"
223 * >Communicating with Other Fragments</a> for more information.
225 public interface OnSearchFragmentInteractionListener
{
226 void unshareWith(OCShare share
);