Added README.txt and GPL Header to Source Files
[DHBWCampusApp.git] / app / src / main / java / de / dhbwloe / campusapp / fragments / SplashScreen.java
1 /* SplashScreen.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.graphics.Bitmap;
18 import android.graphics.BitmapFactory;
19 import android.graphics.Point;
20 import android.os.Bundle;
21 import android.os.Handler;
22 import android.support.v4.app.Fragment;
23 import android.view.Display;
24 import android.view.LayoutInflater;
25 import android.view.View;
26 import android.view.ViewGroup;
27 import android.widget.ImageView;
28 import android.widget.ProgressBar;
29 import android.widget.TextView;
30
31 import java.util.Date;
32
33 import de.dhbwloe.campusapp.CampusApp;
34 import de.dhbwloe.campusapp.CampusAppContext;
35 import de.dhbwloe.campusapp.CampusAppFragment;
36 import de.dhbwloe.campusapp.R;
37 import de.dhbwloe.campusapp.mensaplan.MensaplanManager;
38 import de.dhbwloe.campusapp.mensaplan.MensaplanManagerInterface;
39 import de.dhbwloe.campusapp.news.NewsManager;
40 import de.dhbwloe.campusapp.news.NewsManagerInterface;
41 import de.dhbwloe.campusapp.search.SearchIndices;
42 import de.dhbwloe.campusapp.vorlesungen.CalendarManager;
43 import de.dhbwloe.campusapp.vorlesungen.CalendarManagerInterface;
44 import de.dhbwloe.campusapp.vorlesungen.CourseEvent;
45 import de.dhbwloe.campusapp.vorlesungen.CourseGroup;
46
47 /**
48  * A simple {@link Fragment} subclass.
49  */
50 public class SplashScreen extends CampusAppFragment {
51     private ProgressBar splashProgress;
52     private int progressCounter;
53     private Handler timerHandler = new Handler();
54     private Runnable timerRunnable;
55
56     @Override
57     public void onCreate(Bundle savedInstanceState) {
58         super.onCreate(savedInstanceState);
59
60
61     }
62
63     @Override
64     public void onSaveInstanceState(Bundle savedInstanceState) {
65         timerHandler.removeCallbacksAndMessages(null);
66         super.onSaveInstanceState(savedInstanceState);
67     }
68
69     @Override
70     public View onCreateView(LayoutInflater inflater, ViewGroup container,
71                              Bundle savedInstanceState) {
72         View view = inflater.inflate(R.layout.fragment_splashscreen, container, false);
73
74         TextView versionView = (TextView) view.findViewById(R.id.version);
75         versionView.setText(CampusAppContext.APPVERSION);
76
77         splashProgress = (ProgressBar)view.findViewById(R.id.splashProgress);
78         splashProgress.setMax(20);
79         splashProgress.setProgress(0);
80         progressCounter = 0;
81
82         AppContext.setTitle("DHBW Lörrach");
83
84         ImageView splashImage = (ImageView)view.findViewById(R.id.splashImage);
85         BitmapFactory.Options dimensions = new BitmapFactory.Options();
86         Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.dhbw_campus_hd, dimensions);
87         int height = dimensions.outHeight;
88         int width =  dimensions.outWidth;
89         Display display = AppContext.getMainActivity().getWindowManager().getDefaultDisplay();
90         Point size = new Point();
91         display.getSize(size);
92         float scaleX = (float)size.x / (float)width;
93         float scaleY = (float)size.y / (float)height;
94         float scale = Math.max(scaleX, scaleY);
95         int newWidth = (int)(width*scale);
96         int newHeight = (int)(height*scale);
97         Bitmap newBitmap = Bitmap.createScaledBitmap(mBitmap, newWidth, newHeight, true);
98         splashImage.setImageBitmap(newBitmap);
99
100         timerRunnable = new Runnable() {
101             @Override
102             public void run() {
103                 progressCounter++;
104                 splashProgress.setProgress(progressCounter);
105                 long now = (new Date()).getTime()/1000;
106                 switch(progressCounter) {
107                     case 1:
108                         AppContext.getDatabaseManager().initializeDatabase();
109                         break;
110                     case 2:
111                         AppContext.addDefaultSearchIndexes();
112                         break;
113                     case 3:
114                         String startCounter = AppContext.getDatabaseManager().getRuntimeCache("AppStartCounter");
115                         if(startCounter == null || Integer.parseInt(startCounter) == 0) {
116                             AppContext.getNavigationManager().navigatePage("FirstRun", null, false);
117                             return;
118                         }
119                         AppContext.getDatabaseManager().setRuntimeCache("AppStartCounter", Integer.toString(Integer.parseInt(startCounter) + 1));
120                         break;
121                     case 4:
122                         String lastVLMFullSyncStr = AppContext.getDatabaseManager().getRuntimeCache("LastVLMFullSync");
123                         String lastVLMPartialSyncStr = AppContext.getDatabaseManager().getRuntimeCache("LastVLMPartialSync");
124                         long lastVLMFullSync, lastVLMPartialSync;
125                         if(lastVLMFullSyncStr != null)
126                             lastVLMFullSync = Long.parseLong(lastVLMFullSyncStr);
127                         else
128                             lastVLMFullSync = 0;
129                         if(lastVLMPartialSyncStr != null)
130                             lastVLMPartialSync = Long.parseLong(lastVLMPartialSyncStr);
131                         else
132                             lastVLMPartialSync = 0;
133
134                         String kursTag = AppContext.getDatabaseManager().getRuntimeCache("CourseName");
135                         if(kursTag == null || kursTag.isEmpty())
136                             break;
137                         CalendarManager vpm = new CalendarManager(AppContext, kursTag);
138
139                         if(lastVLMFullSync == 0 || now - lastVLMFullSync > (86400 * 14)) { // full sync every 14 days
140                             vpm.performFullSynchronisation(new CalendarManagerInterface() {
141                                 @Override
142                                 public void onCalendarUpdateDone() {
143                                     long now = (new Date()).getTime() / 1000;
144                                     AppContext.getDatabaseManager().setRuntimeCache("LastVLMFullSync", Long.toString(now));
145                                     AppContext.getDatabaseManager().setRuntimeCache("LastVLMPartialSync", Long.toString(now));
146                                     timerHandler.postDelayed(timerRunnable, 100);
147                                 }
148
149                                 @Override
150                                 public void onCalendarUpdateFail(String errorMessage) {
151                                     timerHandler.postDelayed(timerRunnable, 100);
152                                 }
153                                 @Override
154                                 public SearchIndices onGenerateCalendarSearchIndices(CourseEvent event) {
155                                     CourseGroup group = event.getCourseGroup();
156                                     SearchIndices indices = new SearchIndices("Vorlesungsplan#Group"+group.getGroupId(), false);
157                                     indices.setUpdateTime(event.getEventFrom());
158                                     indices.setTarget("#Vorlesungsplan#groupid=" + group.getGroupId());
159                                     indices.setTitle("Vorlesungsplan " + event.getCourseName());
160                                     indices.setDescription("Vorlesung " + event.getEventTitle());
161                                     indices.addKeyWord(event.getGroupTitle());
162                                     indices.addKeyWord(event.getEventLocation());
163                                     return indices;
164                                 }
165                             });
166                         }
167                         else if(lastVLMPartialSync == 0 || now - lastVLMPartialSync > (86400)) { // partial sync every day
168                             vpm.performFastSynchronisation(new CalendarManagerInterface() {
169                                 @Override
170                                 public void onCalendarUpdateDone() {
171                                     long now = (new Date()).getTime()/1000;
172                                     AppContext.getDatabaseManager().setRuntimeCache("LastVLMPartialSync", Long.toString(now));
173                                     timerHandler.postDelayed(timerRunnable, 100);
174                                 }
175                                 @Override
176                                 public void onCalendarUpdateFail(String errorMessage) {
177                                     timerHandler.postDelayed(timerRunnable, 100);
178                                 }
179                                 @Override
180                                 public SearchIndices onGenerateCalendarSearchIndices(CourseEvent event) {
181                                     CourseGroup group = event.getCourseGroup();
182                                     SearchIndices indices = new SearchIndices("Vorlesungsplan#Group"+group.getGroupId(), false);
183                                     indices.setUpdateTime(event.getEventFrom());
184                                     indices.setTarget("#Vorlesungsplan#groupid=" + group.getGroupId());
185                                     indices.setTitle(AppContext.getResString(R.string.search_vorlesungsplan_group_title, event.getCourseName()));
186                                     indices.setDescription(event.getEventTitle());
187                                     indices.addKeyWord(event.getGroupTitle());
188                                     indices.addKeyWord(event.getEventLocation());
189                                     return indices;
190                                 }
191                             });
192                         }
193                         else
194                             break;
195                         return;
196                     case 5:
197                         String lastMPMSyncStr = AppContext.getDatabaseManager().getRuntimeCache("LastMPSync");
198                         long lastMPMSync;
199                         if(lastMPMSyncStr != null)
200                             lastMPMSync = Long.parseLong(lastMPMSyncStr);
201                         else
202                             lastMPMSync = 0;
203
204                         MensaplanManager mpm = new MensaplanManager(AppContext);
205
206                         if(lastMPMSync == 0 || now - lastMPMSync > (86400 * 2)) { //sync every 2 days
207                             mpm.performSynchronisation(new MensaplanManagerInterface() {
208                                 @Override
209                                 public void onMensaplanUpdateDone() {
210                                     long now = (new Date()).getTime() / 1000;
211                                     AppContext.getDatabaseManager().setRuntimeCache("LastMPSync", Long.toString(now));
212                                     timerHandler.postDelayed(timerRunnable, 100);
213                                 }
214
215                                 @Override
216                                 public void onMensaplanUpdateFail(String errorMessage) {
217                                     timerHandler.postDelayed(timerRunnable, 100);
218                                 }
219                             });
220                         }
221                         else
222                             break;
223                         return;
224                     case 6:
225                     case 7:
226                         final String syncSource = (progressCounter == 6 ? "DHBW" : "STUV");
227                         String lastNewsSyncStr = AppContext.getDatabaseManager().getRuntimeCache("LastNewsSync_"+syncSource);
228                         long lastNewsSync;
229                         if(lastNewsSyncStr != null)
230                             lastNewsSync = Long.parseLong(lastNewsSyncStr);
231                         else
232                             lastNewsSync = 0;
233
234                         NewsManager nm = new NewsManager(AppContext, syncSource);
235
236                         if(lastNewsSync == 0 || now - lastNewsSync > (86400 * 1)) { //sync every day
237                             nm.performSynchronisation(new NewsManagerInterface() {
238                                 @Override
239                                 public void onNewsUpdateDone() {
240                                     long now = (new Date()).getTime() / 1000;
241                                     AppContext.getDatabaseManager().setRuntimeCache("LastNewsSync_"+syncSource, Long.toString(now));
242                                     timerHandler.postDelayed(timerRunnable, 100);
243                                 }
244
245                                 @Override
246                                 public void onNewsUpdateFail(String errorMessage) {
247                                     timerHandler.postDelayed(timerRunnable, 100);
248                                 }
249                             });
250                         }
251                         else
252                             break;
253                         return;
254                     case 8:
255                         String lastStuvSyncStr = AppContext.getDatabaseManager().getRuntimeCache("LastStuvSync");
256                         long lastStuvSync;
257                         if(lastStuvSyncStr != null)
258                             lastStuvSync = Long.parseLong(lastStuvSyncStr);
259                         else
260                             lastStuvSync = 0;
261
262                         final String calendarName = "STUV";
263                         CalendarManager stuvsyncmgr = new CalendarManager(AppContext, calendarName);
264
265                         if(lastStuvSync == 0 || now - lastStuvSync > (86400 * 3)) { // full sync every 3 days
266                             stuvsyncmgr.performFullSynchronisation(new CalendarManagerInterface() {
267                                 @Override
268                                 public void onCalendarUpdateDone() {
269                                     long now = (new Date()).getTime() / 1000;
270                                     AppContext.getDatabaseManager().setRuntimeCache("LastStuvSync", Long.toString(now));
271                                     timerHandler.postDelayed(timerRunnable, 100);
272                                 }
273
274                                 @Override
275                                 public void onCalendarUpdateFail(String errorMessage) {
276                                     timerHandler.postDelayed(timerRunnable, 100);
277                                 }
278
279                                 @Override
280                                 public SearchIndices onGenerateCalendarSearchIndices(CourseEvent event) {
281                                     CourseGroup group = event.getCourseGroup();
282                                     SearchIndices indices = new SearchIndices("Vorlesungsplan#Group"+group.getGroupId(), false);
283                                     indices.setUpdateTime(event.getEventFrom());
284                                     indices.setTarget("#News#showevent=" + group.getGroupId() + "&course=" + calendarName);
285                                     indices.setTitle(calendarName+" Event: " + event.getCourseName());
286                                     indices.setDescription(event.getEventTitle());
287                                     indices.addKeyWord(event.getGroupTitle());
288                                     indices.addKeyWord(event.getEventLocation());
289
290                                     return null;
291                                 }
292                             });
293                         }
294                         else
295                             break;
296                         return;
297
298                     // some more tasks to do here?
299
300                     case 20:
301                         ((CampusApp)AppContext.getMainActivity()).loadMainUi();
302                         AppContext.getNavigationManager().navigatePage("Dashboard", null, false);
303                         return;
304                 }
305                 timerHandler.postDelayed(timerRunnable, 100);
306             }
307         };
308
309         return view;
310     }
311
312     @Override
313     public void onResume() {
314         super.onResume();
315         timerHandler.postDelayed(timerRunnable, 500);
316     }
317
318 }