3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 package de.dhbwloe.campusapp.fragments;
17 import android.os.Bundle;
18 import android.support.v4.app.Fragment;
19 import android.util.Log;
20 import android.view.LayoutInflater;
21 import android.view.View;
22 import android.view.ViewGroup;
23 import android.widget.AdapterView;
24 import android.widget.ListView;
25 import android.widget.ProgressBar;
27 import java.util.ArrayList;
28 import java.util.List;
30 import de.dhbwloe.campusapp.CampusAppContext;
31 import de.dhbwloe.campusapp.CampusAppFragment;
32 import de.dhbwloe.campusapp.R;
33 import de.dhbwloe.campusapp.search.DhbwSearchHelper;
34 import de.dhbwloe.campusapp.search.SearchIndices;
35 import de.dhbwloe.campusapp.search.SearchResultListener;
36 import de.dhbwloe.campusapp.search.SearchTarget;
39 * A simple {@link Fragment} subclass.
41 public class AppSearchDhbw extends CampusAppFragment implements AppSearchProvider {
42 private String sSearchQuery;
43 private boolean bQueryExecuted = false;
44 private AppSearchListAdapter appSearchAdapter;
45 private DhbwSearchHelper seachHelper;
46 private ArrayList<AppSearchListItem> searchResultItems = new ArrayList<AppSearchListItem>();
47 private ProgressBar progressBar;
50 public void onCreate(Bundle savedInstanceState) {
51 Log.i("AppSearchDhbw", "Event: onCreate");
52 super.onCreate(savedInstanceState);
56 public void onSaveInstanceState(Bundle savedInstanceState) {
57 Log.i("AppSearchDhbw", "Event: onSaveInstanceState");
58 Bundle bundle = new Bundle();
59 bundle.putString("query", sSearchQuery);
60 bundle.putBoolean("executed", bQueryExecuted);
61 savedInstanceState.putBundle("SearchDhbw", bundle);
62 super.onSaveInstanceState(savedInstanceState);
66 public View onCreateView(LayoutInflater inflater, ViewGroup container,
67 Bundle savedInstanceState) {
68 Log.i("AppSearchDhbw", "Event: onCreateView");
70 if((savedState = getArguments()) != null)
71 sSearchQuery = savedState.getString("query");
72 else if(savedInstanceState != null && (savedState = savedInstanceState.getBundle("SearchDhbw")) != null) {
73 Log.i("AppSearchDhbw", "Parsed instance state");
74 sSearchQuery = savedState.getString("query");
75 if(savedState.getBoolean("executed")) {
76 bQueryExecuted = true;
79 if(AppContext == null)
80 AppContext = CampusAppContext.getInstance();
81 oFragmentView = inflater.inflate(R.layout.fragment_appsearch_list, container, false);
83 Bundle args = getArguments();
85 String queryString = args.getString("query");
86 if(queryString != null)
87 sSearchQuery = queryString;
90 ListView searchResultList = (ListView) oFragmentView.findViewById(R.id.searchResultItems);
91 appSearchAdapter = new AppSearchListAdapter(oFragmentView.getContext(), R.layout.fragment_appsearch_listitem, searchResultItems);
92 searchResultList.setAdapter(appSearchAdapter);
94 searchResultList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
96 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
97 AppSearchListItem item = searchResultItems.get(position);
100 navigateToResult(item);
104 return oFragmentView;
107 public void executeSearch(boolean reset) {
108 Log.i("AppSearchDhbw", "Action: executeSearch");
109 if(AppContext == null)
110 AppContext = CampusAppContext.getInstance();
111 if(bQueryExecuted && !reset)
113 bQueryExecuted = true;
114 progressBar = (ProgressBar) oFragmentView.findViewById(R.id.searchProgress);
115 progressBar.setVisibility(View.VISIBLE);
117 searchResultItems.clear();
119 if(seachHelper == null)
120 seachHelper = new DhbwSearchHelper();
121 seachHelper.search(sSearchQuery, new SearchResultListener() {
123 public void onSearchResultsReceived(List<AppSearchListItem> results) {
124 Log.i("SearchDhbw", "Success: "+results.size());
125 for(AppSearchListItem result : results)
126 searchResultItems.add(result);
127 progressBar.setVisibility(View.GONE);
128 if(appSearchAdapter != null)
129 appSearchAdapter.notifyDataSetChanged();
131 Log.i("SearchDhbw", "appSearchAdapter is null in executeSearch");
135 public void onSearchFailed(String error) {
136 Log.i("SearchDhbw", "Failed: " + error);
137 progressBar.setVisibility(View.GONE);
142 private void navigateToResult(AppSearchListItem result) {
143 SearchTarget target = result.getTarget();
145 target.navigate(AppContext.getNavigationManager());