Extra commit to complement: Fill in Users/Groups List of a 'share with' from server...
[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.net.Uri;
29 import android.os.Bundle;
30 import android.support.v4.app.Fragment;
31 import android.view.LayoutInflater;
32 import android.view.View;
33 import android.view.ViewGroup;
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
40 /**
41 * Fragment for Searching users and groups
42 *
43 * A simple {@link Fragment} subclass.
44 * Activities that contain this fragment must implement the
45 * {@link SearchFragment.OnSearchFragmentInteractionListener} interface
46 * to handle interaction events.
47 * Use the {@link SearchFragment#newInstance} factory method to
48 * create an instance of this fragment.
49 */
50 public class SearchFragment extends Fragment {
51 private static final String TAG = SearchFragment.class.getSimpleName();
52
53 // the fragment initialization parameters
54 private static final String ARG_FILE = "FILE";
55 private static final String ARG_ACCOUNT = "ACCOUNT";
56
57 // Parameters
58 private OCFile mFile;
59 private Account mAccount;
60
61 private OnSearchFragmentInteractionListener mListener;
62
63 /**
64 * Public factory method to create new SearchFragment instances.
65 *
66 * @param fileToShare An {@link OCFile} to show in the fragment
67 * @param account An ownCloud account
68 * @return A new instance of fragment SearchFragment.
69 */
70 // TODO: Rename and change types and number of parameters
71 public static SearchFragment newInstance(OCFile fileToShare, Account account) {
72 SearchFragment fragment = new SearchFragment();
73 Bundle args = new Bundle();
74 args.putParcelable(ARG_FILE, fileToShare);
75 args.putParcelable(ARG_ACCOUNT, account);
76 fragment.setArguments(args);
77 return fragment;
78 }
79
80 public SearchFragment() {
81 // Required empty public constructor
82 }
83
84 @Override
85 public void onCreate(Bundle savedInstanceState) {
86 super.onCreate(savedInstanceState);
87 if (getArguments() != null) {
88 mFile = getArguments().getParcelable(ARG_FILE);
89 mAccount = getArguments().getParcelable(ARG_ACCOUNT);
90 }
91
92 }
93
94 @Override
95 public View onCreateView(LayoutInflater inflater, ViewGroup container,
96 Bundle savedInstanceState) {
97 // Inflate the layout for this fragment
98 View view = inflater.inflate(R.layout.search_users_groups_layout, container, false);
99
100 // Get the SearchView and set the searchable configuration
101 SearchView searchView = (SearchView) view.findViewById(R.id.searchView);
102 SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
103 searchView.setSearchableInfo(searchManager.getSearchableInfo(
104 getActivity().getComponentName()) // assumes parent activity is the searchable activity
105 );
106 searchView.setIconifiedByDefault(false); // do not iconify the widget; expand it by default
107
108 //searchView.setImeOptions(EditorInfo.IME_ACTION_NEXT | EditorInfo.IME_FLAG_NO_FULLSCREEN);
109
110 searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
111 @Override
112 public boolean onQueryTextSubmit(String query) {
113 Log_OC.v(TAG, "onQueryTextSubmit intercepted, query: " + query);
114 return true; // return true to prevent the query is processed to be queried;
115 // a user / group will be picked only if selected in the list of suggestions
116 }
117
118 @Override
119 public boolean onQueryTextChange(String newText) {
120 return false; // let it for the parent listener in the hierarchy / default behaviour
121 }
122 });
123
124 return view;
125 }
126
127 // TODO: Rename method, update argument and hook method into UI event
128 public void onButtonPressed(Uri uri) {
129 if (mListener != null) {
130 mListener.onSearchFragmentInteraction(uri);
131 }
132 }
133
134 @Override
135 public void onAttach(Activity activity) {
136 super.onAttach(activity);
137 try {
138 mListener = (OnSearchFragmentInteractionListener) activity;
139 } catch (ClassCastException e) {
140 throw new ClassCastException(activity.toString()
141 + " must implement OnFragmentInteractionListener");
142 }
143 }
144
145 @Override
146 public void onDetach() {
147 super.onDetach();
148 mListener = null;
149 }
150
151 // TODO: review if it is necessary
152 /**
153 * This interface must be implemented by activities that contain this
154 * fragment to allow an interaction in this fragment to be communicated
155 * to the activity and potentially other fragments contained in that
156 * activity.
157 * <p/>
158 * See the Android Training lesson <a href=
159 * "http://developer.android.com/training/basics/fragments/communicating.html"
160 * >Communicating with Other Fragments</a> for more information.
161 */
162 public interface OnSearchFragmentInteractionListener {
163 // TODO: Update argument type and name
164 public void onSearchFragmentInteraction(Uri uri);
165 }
166
167 }