Put focus in search field and show keyboard automatically
[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.view.inputmethod.InputMethodManager;
34 import android.widget.ListView;
35 import android.widget.SearchView;
36
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;
43
44 import java.util.ArrayList;
45
46 /**
47 * Fragment for Searching users and groups
48 *
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.
55 */
56 public class SearchFragment extends Fragment implements ShareUserListAdapter.ShareUserAdapterListener {
57 private static final String TAG = SearchFragment.class.getSimpleName();
58
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";
63
64 // Parameters
65 private OCFile mFile;
66 private Account mAccount;
67 private ArrayList<OCShare> mShares;
68 private ShareUserListAdapter mUserGroupsAdapter = null;
69
70 private OnSearchFragmentInteractionListener mListener;
71
72 /**
73 * Public factory method to create new SearchFragment instances.
74 *
75 * @param fileToShare An {@link OCFile} to show in the fragment
76 * @param account An ownCloud account
77 * @param
78 * @return A new instance of fragment SearchFragment.
79 */
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);
88 return fragment;
89 }
90
91 public SearchFragment() {
92 // Required empty public constructor
93 }
94
95 @Override
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);
102 }
103
104 }
105
106 @Override
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);
111
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
117 );
118 searchView.setIconifiedByDefault(false); // do not iconify the widget; expand it by default
119
120 //searchView.setImeOptions(EditorInfo.IME_ACTION_NEXT | EditorInfo.IME_FLAG_NO_FULLSCREEN);
121
122 searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
123 @Override
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
128 }
129
130 @Override
131 public boolean onQueryTextChange(String newText) {
132 return false; // let it for the parent listener in the hierarchy / default behaviour
133 }
134 });
135
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);
143 }
144
145 return view;
146 }
147
148 /**
149 * Get users and groups fromn the DB to fill in the "share with" list
150 */
151 public void refreshUsersOrGroupsListFromDB (){
152 // Get Users and Groups
153 mShares = ((ShareActivity) mListener).getStorageManager().getSharesWithForAFile(mFile.getRemotePath(),
154 mAccount.name);
155
156 // Update list of users/groups
157 updateListOfUserGroups();
158 }
159
160 private void updateListOfUserGroups() {
161 // Update list of users/groups
162 mUserGroupsAdapter = new ShareUserListAdapter(getActivity().getApplicationContext(),
163 R.layout.share_user_item, mShares, this);
164
165 // Show data
166 ListView usersList = (ListView) getView().findViewById(R.id.searchUsersListView);
167
168 if (mShares.size() > 0) {
169 usersList.setVisibility(View.VISIBLE);
170 usersList.setAdapter(mUserGroupsAdapter);
171
172 } else {
173 usersList.setVisibility(View.GONE);
174 }
175 }
176
177 @Override
178 public void onAttach(Activity activity) {
179 super.onAttach(activity);
180 try {
181 mListener = (OnSearchFragmentInteractionListener) activity;
182 } catch (ClassCastException e) {
183 throw new ClassCastException(activity.toString()
184 + " must implement OnFragmentInteractionListener");
185 }
186 }
187
188 @Override
189 public void onStart() {
190 super.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);
196 if (imm != null) {
197 imm.showSoftInput(searchView.findFocus(), InputMethodManager.SHOW_IMPLICIT);
198 }
199 }
200 }
201
202 @Override
203 public void onDetach() {
204 super.onDetach();
205 mListener = null;
206 }
207
208 @Override
209 public void unshareButtonPressed(OCShare share) {
210 // Unshare
211 mListener.unshareWith(share);
212 Log_OC.d(TAG, "Unshare - " + share.getSharedWithDisplayName());
213 }
214
215 /**
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
219 * activity.
220 * <p/>
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.
224 */
225 public interface OnSearchFragmentInteractionListener {
226 void unshareWith(OCShare share);
227 }
228
229 }