1 package de.dhbwloe.campusapp.fragments;
4 import android.graphics.Bitmap;
5 import android.graphics.BitmapFactory;
6 import android.graphics.Matrix;
7 import android.graphics.Point;
8 import android.graphics.drawable.Drawable;
9 import android.os.Bundle;
10 import android.os.Handler;
11 import android.support.v4.app.Fragment;
12 import android.view.Display;
13 import android.view.LayoutInflater;
14 import android.view.View;
15 import android.view.ViewGroup;
16 import android.view.ViewTreeObserver;
17 import android.widget.ImageView;
18 import android.widget.ProgressBar;
19 import android.widget.TextView;
21 import java.util.Date;
23 import de.dhbwloe.campusapp.CampusApp;
24 import de.dhbwloe.campusapp.CampusAppFragment;
25 import de.dhbwloe.campusapp.R;
26 import de.dhbwloe.campusapp.mensaplan.MensaplanManager;
27 import de.dhbwloe.campusapp.mensaplan.MensaplanManagerInterface;
28 import de.dhbwloe.campusapp.news.NewsManager;
29 import de.dhbwloe.campusapp.news.NewsManagerInterface;
30 import de.dhbwloe.campusapp.vorlesungen.VorlesungsplanManager;
31 import de.dhbwloe.campusapp.vorlesungen.VorlesungsplanManagerInterface;
34 * A simple {@link Fragment} subclass.
36 public class SplashScreen extends CampusAppFragment {
37 private ProgressBar splashProgress;
38 private int progressCounter;
39 private Handler timerHandler = new Handler();
40 private Runnable timerRunnable;
43 public void onCreate(Bundle savedInstanceState) {
44 super.onCreate(savedInstanceState);
50 public void onSaveInstanceState(Bundle savedInstanceState) {
51 timerHandler.removeCallbacksAndMessages(null);
52 super.onSaveInstanceState(savedInstanceState);
56 public View onCreateView(LayoutInflater inflater, ViewGroup container,
57 Bundle savedInstanceState) {
58 View view = inflater.inflate(R.layout.fragment_splashscreen, container, false);
60 splashProgress = (ProgressBar)view.findViewById(R.id.splashProgress);
61 splashProgress.setMax(20);
62 splashProgress.setProgress(0);
65 AppContext.setTitle("DHBW Lörrach");
67 ImageView splashImage = (ImageView)view.findViewById(R.id.splashImage);
68 BitmapFactory.Options dimensions = new BitmapFactory.Options();
69 Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.dhbw_campus_hd, dimensions);
70 int height = dimensions.outHeight;
71 int width = dimensions.outWidth;
72 Display display = AppContext.getMainActivity().getWindowManager().getDefaultDisplay();
73 Point size = new Point();
74 display.getSize(size);
75 float scaleX = (float)size.x / (float)width;
76 float scaleY = (float)size.y / (float)height;
77 float scale = Math.max(scaleX, scaleY);
78 int newWidth = (int)(width*scale);
79 int newHeight = (int)(height*scale);
80 Bitmap newBitmap = Bitmap.createScaledBitmap(mBitmap, newWidth, newHeight, true);
81 splashImage.setImageBitmap(newBitmap);
83 timerRunnable = new Runnable() {
87 splashProgress.setProgress(progressCounter);
88 long now = (new Date()).getTime()/1000;
89 switch(progressCounter) {
91 AppContext.getDatabaseManager().initializeDatabase();
94 AppContext.addDefaultSearchIndexes();
97 String startCounter = AppContext.getDatabaseManager().getRuntimeCache("AppStartCounter");
98 if(startCounter == null || Integer.parseInt(startCounter) == 0) {
99 AppContext.getNavigationManager().navigatePage("FirstRun", null, false);
102 AppContext.getDatabaseManager().setRuntimeCache("AppStartCounter", Integer.toString(Integer.parseInt(startCounter) + 1));
105 String lastVLMFullSyncStr = AppContext.getDatabaseManager().getRuntimeCache("LastVLMFullSync");
106 String lastVLMPartialSyncStr = AppContext.getDatabaseManager().getRuntimeCache("LastVLMPartialSync");
107 long lastVLMFullSync, lastVLMPartialSync;
108 if(lastVLMFullSyncStr != null)
109 lastVLMFullSync = Long.parseLong(lastVLMFullSyncStr);
112 if(lastVLMPartialSyncStr != null)
113 lastVLMPartialSync = Long.parseLong(lastVLMPartialSyncStr);
115 lastVLMPartialSync = 0;
117 VorlesungsplanManager vpm = new VorlesungsplanManager(AppContext, "tif13a");
119 if(lastVLMFullSync == 0 || now - lastVLMFullSync > (86400 * 14)) { // full sync every 14 days
120 vpm.performFullSynchronisation(new VorlesungsplanManagerInterface() {
122 public void onVorlesungsplanUpdateDone() {
123 long now = (new Date()).getTime() / 1000;
124 AppContext.getDatabaseManager().setRuntimeCache("LastVLMFullSync", Long.toString(now));
125 AppContext.getDatabaseManager().setRuntimeCache("LastVLMPartialSync", Long.toString(now));
126 timerHandler.postDelayed(timerRunnable, 100);
130 public void onVorlesungsplanUpdateFail(String errorMessage) {
131 timerHandler.postDelayed(timerRunnable, 100);
135 else if(lastVLMPartialSync == 0 || now - lastVLMPartialSync > (86400)) { // partial sync every day
136 vpm.performFastSynchronisation(new VorlesungsplanManagerInterface() {
138 public void onVorlesungsplanUpdateDone() {
139 long now = (new Date()).getTime()/1000;
140 AppContext.getDatabaseManager().setRuntimeCache("LastVLMPartialSync", Long.toString(now));
141 timerHandler.postDelayed(timerRunnable, 100);
144 public void onVorlesungsplanUpdateFail(String errorMessage) {
145 timerHandler.postDelayed(timerRunnable, 100);
153 String lastMPMSyncStr = AppContext.getDatabaseManager().getRuntimeCache("LastMPSync");
155 if(lastMPMSyncStr != null)
156 lastMPMSync = Long.parseLong(lastMPMSyncStr);
160 MensaplanManager mpm = new MensaplanManager(AppContext);
162 if(lastMPMSync == 0 || now - lastMPMSync > (86400 * 2)) { //sync every 2 days
163 mpm.performSynchronisation(new MensaplanManagerInterface() {
165 public void onMensaplanUpdateDone() {
166 long now = (new Date()).getTime() / 1000;
167 AppContext.getDatabaseManager().setRuntimeCache("LastMPSync", Long.toString(now));
168 timerHandler.postDelayed(timerRunnable, 100);
172 public void onMensaplanUpdateFail(String errorMessage) {
173 timerHandler.postDelayed(timerRunnable, 100);
182 final String syncSource = (progressCounter == 6 ? "DHBW" : "STUV");
183 String lastNewsSyncStr = AppContext.getDatabaseManager().getRuntimeCache("LastNewsSync_"+syncSource);
185 if(lastNewsSyncStr != null)
186 lastNewsSync = Long.parseLong(lastNewsSyncStr);
190 NewsManager nm = new NewsManager(AppContext, syncSource);
192 if(lastNewsSync == 0 || now - lastNewsSync > (86400 * 1)) { //sync every day
193 nm.performSynchronisation(new NewsManagerInterface() {
195 public void onNewsUpdateDone() {
196 long now = (new Date()).getTime() / 1000;
197 AppContext.getDatabaseManager().setRuntimeCache("LastNewsSync_"+syncSource, Long.toString(now));
198 timerHandler.postDelayed(timerRunnable, 100);
202 public void onNewsUpdateFail(String errorMessage) {
203 timerHandler.postDelayed(timerRunnable, 100);
211 String lastStuvSyncStr = AppContext.getDatabaseManager().getRuntimeCache("LastStuvSync");
213 if(lastStuvSyncStr != null)
214 lastStuvSync = Long.parseLong(lastStuvSyncStr);
218 VorlesungsplanManager stuvsyncmgr = new VorlesungsplanManager(AppContext, "STUV");
220 if(lastStuvSync == 0 || now - lastStuvSync > (86400 * 3)) { // full sync every 3 days
221 stuvsyncmgr.performFullSynchronisation(new VorlesungsplanManagerInterface() {
223 public void onVorlesungsplanUpdateDone() {
224 long now = (new Date()).getTime() / 1000;
225 AppContext.getDatabaseManager().setRuntimeCache("LastStuvSync", Long.toString(now));
226 timerHandler.postDelayed(timerRunnable, 100);
230 public void onVorlesungsplanUpdateFail(String errorMessage) {
231 timerHandler.postDelayed(timerRunnable, 100);
239 // some more tasks to do here?
242 ((CampusApp)AppContext.getMainActivity()).loadMainUi();
243 AppContext.getNavigationManager().navigatePage("Dashboard", null, false);
246 timerHandler.postDelayed(timerRunnable, 100);
249 timerHandler.postDelayed(timerRunnable, 500);