Added README.txt and GPL Header to Source Files
[DHBWCampusApp.git] / app / src / main / java / de / dhbwloe / campusapp / fragments / AppSearchInternal.java
1 /* AppSearchInternal.java
2  *
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.
7  *
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.
12  *
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/>.
15  */
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;
26 import android.widget.TextView;
27
28 import java.util.ArrayList;
29 import java.util.Date;
30
31 import de.dhbwloe.campusapp.CampusAppContext;
32 import de.dhbwloe.campusapp.CampusAppFragment;
33 import de.dhbwloe.campusapp.R;
34 import de.dhbwloe.campusapp.search.SearchIndices;
35 import de.dhbwloe.campusapp.search.SearchTarget;
36
37 /**
38  * A simple {@link Fragment} subclass.
39  */
40 public class AppSearchInternal extends CampusAppFragment implements AppSearchProvider {
41     private String sSearchQuery;
42     private boolean bQueryExecuted = false;
43     private AppSearchListAdapter appSearchAdapter;
44     private ArrayList<AppSearchListItem> searchResultItems = new ArrayList<AppSearchListItem>();
45
46     @Override
47     public void onCreate(Bundle savedInstanceState) {
48         Log.i("AppSearchInternal", "Event: onCreate");
49         super.onCreate(savedInstanceState);
50         Bundle savedState;
51         if((savedState = getArguments()) != null)
52             sSearchQuery = savedState.getString("query");
53         else if(savedInstanceState != null && (savedState = savedInstanceState.getBundle("SearchInternal")) != null) {
54             sSearchQuery = savedState.getString("query");
55             if(savedState.getBoolean("executed")) {
56                 executeSearch(false);
57             }
58         }
59     }
60
61     @Override
62     public void onSaveInstanceState(Bundle savedInstanceState) {
63         Log.i("AppSearchInternal", "Event: onSaveInstanceState");
64         Bundle bundle = new Bundle();
65         bundle.putString("query", sSearchQuery);
66         bundle.putBoolean("executed", bQueryExecuted);
67         savedInstanceState.putBundle("SearchInternal", bundle);
68         super.onSaveInstanceState(savedInstanceState);
69     }
70
71     private void parseIstanceState(Bundle bundle) {
72         if(sSearchQuery != null)
73             return;
74         Bundle args = getArguments();
75         if(args != null) {
76             String queryString = args.getString("query");
77             if(queryString != null)
78                 sSearchQuery = queryString;
79         }
80         if(sSearchQuery == null && bundle != null) {
81             String queryString = bundle.getString("query");
82             if(queryString != null)
83                 sSearchQuery = queryString;
84         }
85     }
86
87     @Override
88     public View onCreateView(LayoutInflater inflater, ViewGroup container,
89                              Bundle savedInstanceState) {
90         Log.i("AppSearchInternal", "Event: onCreateView");
91         if(AppContext == null)
92             AppContext = CampusAppContext.getInstance();
93         oFragmentView = inflater.inflate(R.layout.fragment_appsearch_list, container, false);
94
95         parseIstanceState(savedInstanceState);
96
97         ListView searchResultList = (ListView) oFragmentView.findViewById(R.id.searchResultItems);
98         appSearchAdapter = new AppSearchListAdapter(oFragmentView.getContext(), R.layout.fragment_appsearch_listitem, searchResultItems);
99         searchResultList.setAdapter(appSearchAdapter);
100
101         searchResultList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
102             @Override
103             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
104                 AppSearchListItem item = searchResultItems.get(position);
105                 if (item == null)
106                     return;
107                 navigateToResult(item);
108             }
109         });
110
111         return oFragmentView;
112     }
113
114     public void executeSearch(boolean reset) {
115         if(sSearchQuery == null)
116             parseIstanceState(null);
117         Log.i("AppSearchInternal", "Action: executeSearch "+sSearchQuery);
118         if(AppContext == null)
119             AppContext = CampusAppContext.getInstance();
120         if(bQueryExecuted && !reset)
121             return;
122         bQueryExecuted = true;
123
124         searchResultItems.clear();
125
126         SearchIndices[] indices = AppContext.getDatabaseManager().performSearchRequest(sSearchQuery, 40);
127         for(int i = 0; i < indices.length; i++) {
128             SearchTarget target = new SearchTarget(indices[i].getTarget());
129             AppSearchListItem item = new AppSearchListItem(indices[i].getTitle(), indices[i].getDescription(), target);
130             searchResultItems.add(item);
131         }
132         if(appSearchAdapter != null)
133             appSearchAdapter.notifyDataSetChanged();
134
135     }
136
137     private void navigateToResult(AppSearchListItem result) {
138         SearchTarget target = result.getTarget();
139
140         target.navigate(AppContext.getNavigationManager());
141     }
142
143 }