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