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