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.support.v4.app.FragmentActivity;
20 import android.view.LayoutInflater;
21 import android.view.View;
22 import android.view.ViewGroup;
23 import android.widget.AdapterView;
24 import android.widget.CheckBox;
25 import android.widget.CompoundButton;
26 import android.widget.ListView;
28 import java.util.Collections;
29 import java.util.Comparator;
30 import java.util.Date;
31 import java.util.ArrayList;
33 import de.dhbwloe.campusapp.CampusAppFragment;
34 import de.dhbwloe.campusapp.R;
35 import de.dhbwloe.campusapp.news.NewsItem;
36 import de.dhbwloe.campusapp.search.SearchIndices;
37 import de.dhbwloe.campusapp.vorlesungen.CourseEvent;
40 * A simple {@link Fragment} subclass.
42 public class News extends CampusAppFragment {
43 /* implement this for search results ;) */
44 public static SearchIndices[] GetSearchIndices() {
45 return new SearchIndices[] {
46 new SearchIndices("News", true) {{
49 setTitle(R.string.search_news_title);
50 setDescription(R.string.search_news_description);
51 addKeyWord(R.string.search_news_keywords);
57 private NewsListAdapter listAdapter;
58 protected ArrayList<NewsListItem> newsListItems = new ArrayList<NewsListItem>();
61 public View onCreateView(LayoutInflater inflater, ViewGroup container,
62 Bundle savedInstanceState) {
63 view = inflater.inflate(R.layout.fragment_news, container, false);
64 AppContext.setTitle(AppContext.getResString(R.string.news_title));
66 ListView newsItemsList = (ListView) view.findViewById(R.id.newsListView);
67 listAdapter = new NewsListAdapter(view.getContext(), R.layout.fragment_news_item, newsListItems);
68 newsItemsList.setAdapter(listAdapter);
70 CompoundButton.OnCheckedChangeListener changeListener = new CompoundButton.OnCheckedChangeListener() {
72 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
76 ((CheckBox) view.findViewById(R.id.showDhbwNews)).setOnCheckedChangeListener(changeListener);
77 ((CheckBox) view.findViewById(R.id.showStuvNews)).setOnCheckedChangeListener(changeListener);
78 ((CheckBox) view.findViewById(R.id.showStuvEvents)).setOnCheckedChangeListener(changeListener);
80 newsItemsList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
82 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
83 NewsListItem item = newsListItems.get(position);
87 String link = item.getLink();
89 Bundle args = new Bundle();
90 args.putString("url", link);
91 AppContext.getNavigationManager().navigatePage("WebBrowser", args, false);
100 public void onResume() {
106 protected void refreshNewsItems() {
107 newsListItems.clear();
109 CheckBox showDhbwNewsChkbok = (CheckBox) view.findViewById(R.id.showDhbwNews);
110 CheckBox showStuvNewsChkbok = (CheckBox) view.findViewById(R.id.showStuvNews);
111 CheckBox showStuvEventsChkbok = (CheckBox) view.findViewById(R.id.showStuvEvents);
113 if(showDhbwNewsChkbok.isChecked()) {
114 loadNewsSource("DHBW", true);
116 if(showStuvNewsChkbok.isChecked()) {
117 loadNewsSource("STUV", false);
119 if(showStuvEventsChkbok.isChecked()) {
120 loadEventsSource("STUV", false);
123 Collections.sort(newsListItems, new Comparator<NewsListItem>() {
125 public int compare(NewsListItem item2, NewsListItem item1) {
126 long now = (new Date()).getTime() / 1000;
127 return (int) (item2.getTimeDifference(now) - item1.getTimeDifference(now));
130 if(listAdapter != null)
131 listAdapter.notifyDataSetChanged();
134 protected void loadNewsSource(String sourceName, boolean isDhbw) {
135 long now = (new Date()).getTime() / 1000;
137 NewsItem[] news = AppContext.getDatabaseManager().getNewsItems(sourceName, now, now - (86400 * 30 * 6)); // load 6 month
138 for(NewsItem cnews : news) {
139 NewsListItem listItem = new NewsListItem(cnews, isDhbw);
140 newsListItems.add(listItem);
144 protected void loadEventsSource(String sourceName, boolean isDhbw) {
145 long now = (new Date()).getTime() / 1000;
147 CourseEvent[] events = AppContext.getDatabaseManager().getCourseCalendarTimetable(sourceName, now, 30 * 6); // load 6 month
148 for(CourseEvent event : events) {
149 NewsListItem listItem = new NewsListItem(event, isDhbw);
150 newsListItems.add(listItem);