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
.widget
.ListView
;
34 import android
.widget
.SearchView
;
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
;
43 import java
.util
.ArrayList
;
46 * Fragment for Searching users and groups
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.
55 public class SearchFragment
extends Fragment
implements ShareUserListAdapter
.ShareUserAdapterListener
{
56 private static final String TAG
= SearchFragment
.class.getSimpleName();
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";
65 private Account mAccount
;
66 private ArrayList
<OCShare
> mShares
;
67 private ShareUserListAdapter mUserGroupsAdapter
= null
;
69 private OnSearchFragmentInteractionListener mListener
;
72 * Public factory method to create new SearchFragment instances.
74 * @param fileToShare An {@link OCFile} to show in the fragment
75 * @param account An ownCloud account
77 * @return A new instance of fragment SearchFragment.
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
);
90 public SearchFragment() {
91 // Required empty public constructor
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
);
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
);
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
117 searchView
.setIconifiedByDefault(false
); // do not iconify the widget; expand it by default
119 //searchView.setImeOptions(EditorInfo.IME_ACTION_NEXT | EditorInfo.IME_FLAG_NO_FULLSCREEN);
121 searchView
.setOnQueryTextListener(new SearchView
.OnQueryTextListener() {
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
130 public boolean onQueryTextChange(String newText
) {
131 return false
; // let it for the parent listener in the hierarchy / default behaviour
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
);
148 * Get users and groups fromn the DB to fill in the "share with" list
150 public void refreshUsersOrGroupsListFromDB (){
151 // Get Users and Groups
152 mShares
= ((ShareActivity
) mListener
).getStorageManager().getSharesWithForAFile(mFile
.getRemotePath(),
155 // Update list of users/groups
156 updateListOfUserGroups();
159 private void updateListOfUserGroups() {
160 // Update list of users/groups
161 mUserGroupsAdapter
= new ShareUserListAdapter(getActivity().getApplicationContext(),
162 R
.layout
.share_user_item
, mShares
, this);
165 ListView usersList
= (ListView
) getView().findViewById(R
.id
.searchUsersListView
);
167 if (mShares
.size() > 0) {
168 usersList
.setVisibility(View
.VISIBLE
);
169 usersList
.setAdapter(mUserGroupsAdapter
);
172 usersList
.setVisibility(View
.GONE
);
177 public void onAttach(Activity activity
) {
178 super.onAttach(activity
);
180 mListener
= (OnSearchFragmentInteractionListener
) activity
;
181 } catch (ClassCastException e
) {
182 throw new ClassCastException(activity
.toString()
183 + " must implement OnFragmentInteractionListener");
188 public void onDetach() {
194 public void unshareButtonPressed(OCShare share
) {
196 mListener
.unshareWith(share
);
197 Log_OC
.d(TAG
, "Unshare - " + share
.getSharedWithDisplayName());
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
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.
210 public interface OnSearchFragmentInteractionListener
{
211 void unshareWith(OCShare share
);