Added README.txt and GPL Header to Source Files
[DHBWCampusApp.git] / app / src / main / java / de / dhbwloe / campusapp / fragments / News.java
1 /* News.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.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;
27
28 import java.util.Collections;
29 import java.util.Comparator;
30 import java.util.Date;
31 import java.util.ArrayList;
32
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;
38
39 /**
40  * A simple {@link Fragment} subclass.
41  */
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) {{
47                     setUpdateTime(1);
48                     setTarget("#News");
49                     setTitle(R.string.search_news_title);
50                     setDescription(R.string.search_news_description);
51                     addKeyWord(R.string.search_news_keywords);
52                 }},
53         };
54     }
55
56     private View view;
57     private NewsListAdapter listAdapter;
58     protected ArrayList<NewsListItem> newsListItems = new ArrayList<NewsListItem>();
59
60     @Override
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));
65
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);
69
70         CompoundButton.OnCheckedChangeListener changeListener = new CompoundButton.OnCheckedChangeListener() {
71             @Override
72             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
73                 refreshNewsItems();
74             }
75         };
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);
79
80         newsItemsList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
81             @Override
82             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
83                 NewsListItem item = newsListItems.get(position);
84                 if (item == null)
85                     return;
86
87                 String link = item.getLink();
88                 if(link != null) {
89                     Bundle args = new Bundle();
90                     args.putString("url", link);
91                     AppContext.getNavigationManager().navigatePage("WebBrowser", args, false);
92                 }
93             }
94         });
95
96         return view;
97     }
98
99     @Override
100     public void onResume() {
101         super.onResume();
102         if(view != null)
103             refreshNewsItems();
104     }
105
106     protected void refreshNewsItems() {
107         newsListItems.clear();
108
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);
112
113         if(showDhbwNewsChkbok.isChecked()) {
114             loadNewsSource("DHBW", true);
115         }
116         if(showStuvNewsChkbok.isChecked()) {
117             loadNewsSource("STUV", false);
118         }
119         if(showStuvEventsChkbok.isChecked()) {
120             loadEventsSource("STUV", false);
121         }
122
123         Collections.sort(newsListItems, new Comparator<NewsListItem>() {
124             @Override
125             public int compare(NewsListItem item2, NewsListItem item1) {
126                 long now = (new Date()).getTime() / 1000;
127                 return (int) (item2.getTimeDifference(now) - item1.getTimeDifference(now));
128             }
129         });
130         if(listAdapter != null)
131             listAdapter.notifyDataSetChanged();
132     }
133
134     protected void loadNewsSource(String sourceName, boolean isDhbw) {
135         long now = (new Date()).getTime() / 1000;
136
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);
141         }
142     }
143
144     protected void loadEventsSource(String sourceName, boolean isDhbw) {
145         long now = (new Date()).getTime() / 1000;
146
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);
151         }
152     }
153 }