AppContext.setTitle(savedInstanceState.getString("activetitle"));
}
+
+
+ Bundle contextVars = savedInstanceState.getBundle("savedContextVariables");
+ if(contextVars != null) {
+ Bundle contextVariables = AppContext.getContextVariables();
+ contextVariables.putAll(contextVars);
+ }
}
prepareMainUi();
TextView titleView = (TextView) findViewById(R.id.title);
savedInstanceState.putString("activetitle", titleView.getText().toString());
+ Bundle contextVariables = AppContext.getContextVariables();
+ savedInstanceState.putBundle("savedContextVariables", contextVariables);
+
// Always call the superclass so it can save the view hierarchy state
super.onSaveInstanceState(savedInstanceState);
}
private NavigationManager oNavigationManager;
private DatabaseManager oDatabaseManager;
private NfcCardListener oNfcCardListener;
+ private Bundle oContextVariables;
public CampusAppContext(CampusApp mainActivity, int fragmentContainerId) {
final CampusAppContext AppContext = this;
instance = this;
oMainActivity = mainActivity;
+ oContextVariables = new Bundle();
oNavigationManager = new NavigationManager(this, fragmentContainerId);
oDatabaseManager = new DatabaseManager(this);
oNfcCardListener = new NfcCardListener(this);
private void onNfcCardDataReceived(NfcCardData data) {
Bundle bundle = new Bundle();
- bundle.putDouble("balance", data.getBalance() / 100.0);
+ double cardBalance = data.getBalance() / 100.0;
+ bundle.putDouble("balance", cardBalance);
bundle.putDouble("transaction", data.getLastTransaction() / 100.0);
+ oContextVariables.putInt("nfcCardUniqueId", data.getUniqueId());
+ oContextVariables.putDouble("nfcCardBalance", cardBalance);
+
String pagename = oNavigationManager.getCurrentPageName();
if(pagename != null && pagename.equalsIgnoreCase("MensaCard")) {
MensaCard fragment = (MensaCard) oNavigationManager.getCurrentFragment();
oNavigationManager.navigatePage("MensaCard", bundle);
}
+ public Bundle getContextVariables() {
+ return oContextVariables;
+ }
+
}
public boolean closeDialog() {
if(oCurrentPage != null && oCurrentPage.fragmentType == 3) {
PopupFragment fragment = (PopupFragment) oCurrentFragment;
+ boolean wasAdded = false;
if(oCurrentFragment.isAdded()) {
fragment.destroyView();
transaction.remove(oCurrentFragment);
transaction.commit();
+ wasAdded = true;
}
oCurrentPage = oParentPage;
oCurrentFragment = oParentFragment;
-
- return true;
+ return wasAdded;
}
return false;
}
return resultsArr;
}
+ public NfcCardData getLatestNfcCardData(int cardId) {
+ String value = null;
+ String[] whereArgs = {
+ Integer.toString(cardId)
+ };
+ Cursor resultSet = database.rawQuery("SELECT CardId,UpdateTime,CardBalance,CardLastTransaction FROM NfcCardStore WHERE CardId = ? ORDER BY UpdateTime DESC LIMIT 1", whereArgs);
+ NfcCardData nfcCardData = null;
+ if(resultSet.moveToFirst()) {
+ nfcCardData = new NfcCardData(resultSet.getInt(0), resultSet.getLong(1), resultSet.getInt(2), resultSet.getInt(3));
+ }
+ resultSet.close();
+ return nfcCardData;
+ }
+
public void updateCourseCalendar(CourseEvent event) {
if(vorlesungsplanDBHelper == null)
vorlesungsplanDBHelper = new VorlesungsplanDatabaseHelper(AppContext, database);
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
-import android.graphics.Matrix;
import android.graphics.Point;
-import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
-import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.ProgressBar;
-import android.widget.TextView;
import java.util.Date;
import de.dhbwloe.campusapp.mensaplan.MensaplanManagerInterface;
import de.dhbwloe.campusapp.news.NewsManager;
import de.dhbwloe.campusapp.news.NewsManagerInterface;
-import de.dhbwloe.campusapp.vorlesungen.VorlesungsplanManager;
-import de.dhbwloe.campusapp.vorlesungen.VorlesungsplanManagerInterface;
+import de.dhbwloe.campusapp.search.SearchIndices;
+import de.dhbwloe.campusapp.vorlesungen.CalendarManager;
+import de.dhbwloe.campusapp.vorlesungen.CalendarManagerInterface;
+import de.dhbwloe.campusapp.vorlesungen.CourseEvent;
+import de.dhbwloe.campusapp.vorlesungen.CourseGroup;
/**
* A simple {@link Fragment} subclass.
else
lastVLMPartialSync = 0;
- VorlesungsplanManager vpm = new VorlesungsplanManager(AppContext, "tif13a");
+ String kursTag = AppContext.getDatabaseManager().getRuntimeCache("CourseName");
+ if(kursTag == null || kursTag.isEmpty())
+ break;
+ CalendarManager vpm = new CalendarManager(AppContext, kursTag);
if(lastVLMFullSync == 0 || now - lastVLMFullSync > (86400 * 14)) { // full sync every 14 days
- vpm.performFullSynchronisation(new VorlesungsplanManagerInterface() {
+ vpm.performFullSynchronisation(new CalendarManagerInterface() {
@Override
- public void onVorlesungsplanUpdateDone() {
+ public void onCalendarUpdateDone() {
long now = (new Date()).getTime() / 1000;
AppContext.getDatabaseManager().setRuntimeCache("LastVLMFullSync", Long.toString(now));
AppContext.getDatabaseManager().setRuntimeCache("LastVLMPartialSync", Long.toString(now));
}
@Override
- public void onVorlesungsplanUpdateFail(String errorMessage) {
+ public void onCalendarUpdateFail(String errorMessage) {
timerHandler.postDelayed(timerRunnable, 100);
}
+ @Override
+ public SearchIndices onGenerateCalendarSearchIndices(CourseEvent event) {
+ CourseGroup group = event.getCourseGroup();
+ SearchIndices indices = new SearchIndices("Vorlesungsplan#Group"+group.getGroupId(), false);
+ indices.setUpdateTime(event.getEventFrom());
+ indices.setTarget("#Vorlesungsplan#groupid=" + group.getGroupId());
+ indices.setTitle("Vorlesungsplan " + event.getCourseName());
+ indices.setDescription("Vorlesung " + event.getEventTitle());
+ indices.addKeyWord(event.getGroupTitle());
+ indices.addKeyWord(event.getEventLocation());
+ return null;
+ }
});
}
else if(lastVLMPartialSync == 0 || now - lastVLMPartialSync > (86400)) { // partial sync every day
- vpm.performFastSynchronisation(new VorlesungsplanManagerInterface() {
+ vpm.performFastSynchronisation(new CalendarManagerInterface() {
@Override
- public void onVorlesungsplanUpdateDone() {
+ public void onCalendarUpdateDone() {
long now = (new Date()).getTime()/1000;
AppContext.getDatabaseManager().setRuntimeCache("LastVLMPartialSync", Long.toString(now));
timerHandler.postDelayed(timerRunnable, 100);
}
@Override
- public void onVorlesungsplanUpdateFail(String errorMessage) {
+ public void onCalendarUpdateFail(String errorMessage) {
timerHandler.postDelayed(timerRunnable, 100);
}
+ @Override
+ public SearchIndices onGenerateCalendarSearchIndices(CourseEvent event) {
+ CourseGroup group = event.getCourseGroup();
+ SearchIndices indices = new SearchIndices("Vorlesungsplan#Group"+group.getGroupId(), false);
+ indices.setUpdateTime(event.getEventFrom());
+ indices.setTarget("#Vorlesungsplan#groupid=" + group.getGroupId());
+ indices.setTitle("Vorlesungsplan " + event.getCourseName());
+ indices.setDescription("Vorlesung " + event.getEventTitle());
+ indices.addKeyWord(event.getGroupTitle());
+ indices.addKeyWord(event.getEventLocation());
+ return null;
+ }
});
}
else
else
lastStuvSync = 0;
- VorlesungsplanManager stuvsyncmgr = new VorlesungsplanManager(AppContext, "STUV");
+ final String calendarName = "STUV";
+ CalendarManager stuvsyncmgr = new CalendarManager(AppContext, calendarName);
if(lastStuvSync == 0 || now - lastStuvSync > (86400 * 3)) { // full sync every 3 days
- stuvsyncmgr.performFullSynchronisation(new VorlesungsplanManagerInterface() {
+ stuvsyncmgr.performFullSynchronisation(new CalendarManagerInterface() {
@Override
- public void onVorlesungsplanUpdateDone() {
+ public void onCalendarUpdateDone() {
long now = (new Date()).getTime() / 1000;
AppContext.getDatabaseManager().setRuntimeCache("LastStuvSync", Long.toString(now));
timerHandler.postDelayed(timerRunnable, 100);
}
@Override
- public void onVorlesungsplanUpdateFail(String errorMessage) {
+ public void onCalendarUpdateFail(String errorMessage) {
timerHandler.postDelayed(timerRunnable, 100);
}
+
+ @Override
+ public SearchIndices onGenerateCalendarSearchIndices(CourseEvent event) {
+ CourseGroup group = event.getCourseGroup();
+ SearchIndices indices = new SearchIndices("Vorlesungsplan#Group"+group.getGroupId(), false);
+ indices.setUpdateTime(event.getEventFrom());
+ indices.setTarget("#News#showevent=" + group.getGroupId() + "&course=" + calendarName);
+ indices.setTitle(calendarName+" Event: " + event.getCourseName());
+ indices.setDescription(event.getEventTitle());
+ indices.addKeyWord(event.getGroupTitle());
+ indices.addKeyWord(event.getEventLocation());
+
+ return null;
+ }
});
}
else
timerHandler.postDelayed(timerRunnable, 100);
}
};
- timerHandler.postDelayed(timerRunnable, 500);
return view;
}
+ @Override
+ public void onResume() {
+ super.onResume();
+ timerHandler.postDelayed(timerRunnable, 500);
+ }
+
}
public long calculateChkSum() {
CRC32 crc = new CRC32();
crc.update(sMenuName.getBytes());
- crc.update(sName.getBytes());
- crc.update(sNameHtml.getBytes());
+ if(sName != null)
+ crc.update(sName.getBytes());
+ if(sNameHtml != null)
+ crc.update(sNameHtml.getBytes());
if(sAdditional!= null)
crc.update(sAdditional.getBytes());
if(sNotes!= null)
import java.util.Date;
import java.util.List;
import java.util.Locale;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
import de.dhbwloe.campusapp.CampusAppContext;
import de.dhbwloe.campusapp.network.XmlEntry;
import de.dhbwloe.campusapp.network.XmlRequestHelper;
import de.dhbwloe.campusapp.search.SearchIndices;
-import de.dhbwloe.campusapp.vorlesungen.VorlesungsplanManagerInterface;
/**
* Created by pk910 on 22.01.2016.
}
private int getPriceFromString(String pricestr) {
- // 2,90€
+ // 2,90€( pro 100g)?
if(pricestr == null)
return 0;
- NumberFormat nf = NumberFormat.getInstance(Locale.GERMAN);
- try {
- Number result = nf.parse(pricestr.replace("€", ""));
- return (int)(result.doubleValue()*100);
- } catch (ParseException e) {
+ Pattern r = Pattern.compile("^([0-9,]+)€( pro [0-9]+[a-z]+)$");
+ Matcher m = r.matcher(pricestr);
+ if (m.find()) {
+ NumberFormat nf = NumberFormat.getInstance(Locale.GERMAN);
+ try {
+ Number result = nf.parse(m.group(1));
+ return (int)(result.doubleValue()*100);
+ } catch (ParseException e) {
+ return 0;
+ }
+ } else
return 0;
- }
}
@Override
parser.require(XmlPullParser.START_TAG, ns, rootElementName);
while (parser.next() != XmlPullParser.END_TAG) {
+
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
}
private void updateNfcState() {
- if(bNfcAdapterState != oAdapter.isEnabled()) {
- bNfcAdapterState = oAdapter.isEnabled();
+ boolean isEnabled = oAdapter.isEnabled();
+ if(bNfcAdapterState != isEnabled) {
+ bNfcAdapterState = isEnabled;
for(NfcCardInterface nfcCardInterface : lNfcCardInterfaces) {
nfcCardInterface.onNfcReaderStateChanged(bNfcAdapterState);
}
--- /dev/null
+package de.dhbwloe.campusapp.vorlesungen;
+
+
+import android.util.Log;
+
+import net.fortuna.ical4j.model.Calendar;
+import net.fortuna.ical4j.model.Component;
+import net.fortuna.ical4j.model.Property;
+
+import java.text.DateFormat;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.ListIterator;
+import java.util.Locale;
+
+import de.dhbwloe.campusapp.CampusAppContext;
+import de.dhbwloe.campusapp.network.IscRequestHelper;
+import de.dhbwloe.campusapp.search.SearchIndices;
+
+/**
+ * Created by pk910 on 19.01.2016.
+ */
+public class CalendarManager extends IscRequestHelper {
+ private static final String[][] PLAN_SOURCES = {
+ {"STUV", "https://www.google.com/calendar/ical/asta.dhbw.de_c0g35t6hrh16kr4ankrqg2rdm4%40group.calendar.google.com/public/basic.ics"},
+ };
+
+ private CampusAppContext AppContext;
+ private boolean bRequestRunning = false;
+ private boolean bFastSynchronisation = false;
+ private String sCourseName;
+ private String source[];
+ private ArrayList<CalendarManagerInterface> aCallbackInterfaces = new ArrayList<CalendarManagerInterface>();
+
+ public CalendarManager(CampusAppContext context, String courseName) {
+ AppContext = context;
+ sCourseName = courseName;
+
+ for(String src[] : PLAN_SOURCES) {
+ if(src[0].equalsIgnoreCase(courseName)) {
+ source = src;
+ break;
+ }
+ }
+ }
+
+ public void performFastSynchronisation(CalendarManagerInterface callback) {
+ performSynchronisation(callback, false);
+ }
+
+ public void performFullSynchronisation(CalendarManagerInterface callback) {
+ performSynchronisation(callback, true);
+ }
+
+ private void performSynchronisation(CalendarManagerInterface callback, boolean fullsync) {
+ if(sCourseName.length() == 0) {
+ callback.onCalendarUpdateFail("no course name");
+ return;
+ }
+
+ aCallbackInterfaces.add(callback);
+ if(bRequestRunning)
+ return;
+
+ bFastSynchronisation = !fullsync;
+ bRequestRunning = true;
+ String courseCalendarUrl;
+ if(source == null) {
+ courseCalendarUrl = "https://webmail.dhbw-loerrach.de/owa/calendar/kal-" + sCourseName + "@dhbw-loerrach.de/Kalender/calendar.ics";
+ } else {
+ courseCalendarUrl = source[1];
+ }
+ requestCalenderFromWeb(courseCalendarUrl);
+ }
+
+ @Override
+ protected void onCalendarReceived(Calendar calendar) {
+ long timeFrom, timeTo;
+ long now = (new Date()).getTime() / 1000;
+ if(bFastSynchronisation) {
+ timeFrom = now - (86400 * 5);
+ timeTo = now + (86400 * 7 * 4);
+ } else {
+ timeFrom = now - (86400 * 365 * 3);
+ timeTo = now + (86400 * 365 * 4);
+ }
+ CourseEvent[] events = AppContext.getDatabaseManager().getCourseCalendarEvents(sCourseName, timeFrom, timeTo);
+ Log.i("CMSync", "Event count: " + events.length);
+ ArrayList<CourseEvent> newEvents = new ArrayList<CourseEvent>();
+ ArrayList<SearchIndices> newIndices = new ArrayList<SearchIndices>();
+ int lastEventIndex = 0;
+ ListIterator cIterator = calendar.getComponents().listIterator();
+
+ while (cIterator.hasNext()) {
+ Component event = (Component)cIterator.next();
+ CourseEvent dbEvent;
+ Property prop;
+
+ prop = event.getProperty("UID");
+ String uid;
+ if(prop != null)
+ uid = prop.getValue();
+ else
+ continue;
+
+ prop = event.getProperty("SEQUENCE");
+ int sequence;
+ if(prop != null)
+ sequence = Integer.parseInt(prop.getValue());
+ else
+ sequence = 1;
+
+ long startTime = 0, endTime = 0;
+ prop = event.getProperty("DTSTART");
+ if(prop != null)
+ startTime = getTimeFromTimestamp(prop.getValue());
+ else
+ Log.i("CMSync", "Parse Event: DTSTART not found!");
+
+ prop = event.getProperty("DTEND");
+ if(prop != null)
+ endTime = getTimeFromTimestamp(prop.getValue());
+ else
+ Log.i("CMSync", "Parse Event: DTEND not found!");
+
+ if(!(endTime > timeFrom && startTime < timeTo)) {
+ Log.i("CMSync", "Skip Entry: ("+timeFrom+" - "+timeTo+") Filter: "+startTime+" - "+endTime);
+ continue;
+ }
+
+ dbEvent = null;
+ for(int i = lastEventIndex; i < events.length; i++) {
+ if(events[i].getUniqueId().equalsIgnoreCase(uid)) {
+ dbEvent = events[i];
+ break;
+ }
+ }
+ if(dbEvent == null && lastEventIndex > 0) {
+ for(int i = 0; i < lastEventIndex; i++) {
+ if(events[i].getUniqueId().equalsIgnoreCase(uid)) {
+ dbEvent = events[i];
+ break;
+ }
+ }
+ }
+ if(dbEvent == null) {
+ for(CourseEvent cevent : newEvents) {
+ if(cevent.getUniqueId().equalsIgnoreCase(uid)) {
+ dbEvent = cevent;
+ break;
+ }
+ }
+ }
+
+ if(dbEvent == null) {
+ // new event!
+ Log.i("CMSync", "New Event "+uid);
+ dbEvent = new CourseEvent(sCourseName, uid, sequence, true);
+ newEvents.add(dbEvent);
+ } else {
+ Log.i("CMSync", "Existing Event "+uid+" ("+dbEvent.getSequenceId()+" >= "+sequence+")");
+ if(dbEvent.getSequenceId() >= sequence) {
+ continue; // skip event
+ }
+ dbEvent.setSequenceId(sequence);
+ }
+ // perform update
+ prop = event.getProperty("SUMMARY");
+ if(prop != null)
+ dbEvent.setEventTitle(prop.getValue());
+ else
+ Log.i("CMSync", "Parse Event: SUMMARY not found!");
+
+ dbEvent.setEventFrom(startTime);
+ dbEvent.setEventTo(endTime);
+
+ prop = event.getProperty("LOCATION");
+ if(prop != null)
+ dbEvent.setEventLocation(prop.getValue());
+ else
+ Log.i("CMSync", "Parse Event: LOCATION not found!");
+
+ prop = event.getProperty("STATUS");
+ if(prop != null)
+ dbEvent.setEventStatus(prop.getValue());
+ else
+ Log.i("CMSync", "Parse Event: STATUS not found!");
+
+ prop = event.getProperty("RRULE");
+ if(prop != null)
+ dbEvent.setRecurRule(prop.getValue());
+ else
+ dbEvent.setRecurRule("");
+
+ prop = event.getProperty("EXDATE");
+ if(prop != null)
+ dbEvent.setExcludeDates(prop.getValue());
+ else
+ dbEvent.setExcludeDates("");
+
+ CourseGroup group = CourseGroup.GetCourseGroupByName(AppContext.getDatabaseManager(), sCourseName, dbEvent.getGroupTitle().trim());
+ if(group != null)
+ dbEvent.setCourseGroup(group);
+
+ if(group.isNewGroup(true)) {
+ for(CalendarManagerInterface callback : aCallbackInterfaces) {
+ SearchIndices indices = callback.onGenerateCalendarSearchIndices(dbEvent);
+ if(indices != null) {
+ newIndices.add(indices);
+ }
+ }
+ }
+
+ dbEvent.update(AppContext.getDatabaseManager());
+ Log.i("CMSync", "Update Event: "+dbEvent.getUniqueId());
+ }
+
+ SearchIndices[] newIndicesArr = new SearchIndices[newIndices.size()];
+ newIndicesArr = newIndices.toArray(newIndicesArr);
+ AppContext.addSearchIndices(newIndicesArr);
+
+ for(CalendarManagerInterface callback : aCallbackInterfaces) {
+ callback.onCalendarUpdateDone();
+ }
+ aCallbackInterfaces.clear();
+ bRequestRunning = false;
+ }
+
+ private long getTimeFromTimestamp(String timestamp) {
+ // 20160425T090000
+ // 20160321
+ DateFormat df = new SimpleDateFormat("yyyyMMdd'T'kkmmss", Locale.ENGLISH);
+ try {
+ Date result = df.parse(timestamp);
+ return result.getTime()/1000;
+ } catch (ParseException e) {
+ df = new SimpleDateFormat("yyyyMMdd", Locale.ENGLISH);
+ try {
+ Date result = df.parse(timestamp);
+ return result.getTime()/1000;
+ } catch (ParseException e2) {
+ Log.i("CMSync", "Failed parsing: "+timestamp);
+ return 0;
+ }
+ }
+ }
+
+ @Override
+ protected void onCalendarRequestFail(int statusCode, String errorMessage) {
+ Log.i("CMSync", "Calendar Error: "+statusCode);
+ for(CalendarManagerInterface callback : aCallbackInterfaces) {
+ callback.onCalendarUpdateFail("error " + statusCode + ": " + errorMessage);
+ }
+ aCallbackInterfaces.clear();
+ bRequestRunning = false;
+ }
+}
--- /dev/null
+package de.dhbwloe.campusapp.vorlesungen;
+
+import de.dhbwloe.campusapp.search.SearchIndices;
+
+/**
+ * Created by pk910 on 20.01.2016.
+ */
+public interface CalendarManagerInterface {
+
+ public void onCalendarUpdateDone();
+ public void onCalendarUpdateFail(String errorMessage);
+
+ public SearchIndices onGenerateCalendarSearchIndices(CourseEvent event);
+
+}
+++ /dev/null
-package de.dhbwloe.campusapp.vorlesungen;
-
-
-import android.util.Log;
-
-import net.fortuna.ical4j.model.Calendar;
-import net.fortuna.ical4j.model.Component;
-import net.fortuna.ical4j.model.Property;
-
-import java.text.DateFormat;
-import java.text.ParseException;
-import java.text.SimpleDateFormat;
-import java.util.ArrayList;
-import java.util.Date;
-import java.util.ListIterator;
-import java.util.Locale;
-
-import de.dhbwloe.campusapp.CampusAppContext;
-import de.dhbwloe.campusapp.network.IscRequestHelper;
-import de.dhbwloe.campusapp.search.SearchIndices;
-
-/**
- * Created by pk910 on 19.01.2016.
- */
-public class VorlesungsplanManager extends IscRequestHelper {
- private static final String[][] PLAN_SOURCES = {
- {"STUV", "https://www.google.com/calendar/ical/asta.dhbw.de_c0g35t6hrh16kr4ankrqg2rdm4%40group.calendar.google.com/public/basic.ics"},
- };
-
- private CampusAppContext AppContext;
- private boolean bRequestRunning = false;
- private boolean bFastSynchronisation = false;
- private String sCourseName;
- private String source[];
- private ArrayList<VorlesungsplanManagerInterface> aCallbackInterfaces = new ArrayList<VorlesungsplanManagerInterface>();
-
- public VorlesungsplanManager(CampusAppContext context, String courseName) {
- AppContext = context;
- sCourseName = courseName;
-
- for(String src[] : PLAN_SOURCES) {
- if(src[0].equalsIgnoreCase(courseName)) {
- source = src;
- break;
- }
- }
- }
-
- public void performFastSynchronisation(VorlesungsplanManagerInterface callback) {
- performSynchronisation(callback, false);
- }
-
- public void performFullSynchronisation(VorlesungsplanManagerInterface callback) {
- performSynchronisation(callback, true);
- }
-
- private void performSynchronisation(VorlesungsplanManagerInterface callback, boolean fullsync) {
- if(sCourseName.length() == 0) {
- callback.onVorlesungsplanUpdateFail("no course name");
- return;
- }
-
- aCallbackInterfaces.add(callback);
- if(bRequestRunning)
- return;
-
- bFastSynchronisation = !fullsync;
- bRequestRunning = true;
- String courseCalendarUrl;
- if(source == null) {
- courseCalendarUrl = "https://webmail.dhbw-loerrach.de/owa/calendar/kal-" + sCourseName + "@dhbw-loerrach.de/Kalender/calendar.ics";
- } else {
- courseCalendarUrl = source[1];
- }
- requestCalenderFromWeb(courseCalendarUrl);
- }
-
- @Override
- protected void onCalendarReceived(Calendar calendar) {
- long timeFrom, timeTo;
- long now = (new Date()).getTime() / 1000;
- if(bFastSynchronisation) {
- timeFrom = now - (86400 * 5);
- timeTo = now + (86400 * 7 * 4);
- } else {
- timeFrom = now - (86400 * 365 * 3);
- timeTo = now + (86400 * 365 * 4);
- }
- CourseEvent[] events = AppContext.getDatabaseManager().getCourseCalendarEvents(sCourseName, timeFrom, timeTo);
- Log.i("VPMSync", "Event count: " + events.length);
- ArrayList<CourseEvent> newEvents = new ArrayList<CourseEvent>();
- ArrayList<SearchIndices> newIndices = new ArrayList<SearchIndices>();
- int lastEventIndex = 0;
- ListIterator cIterator = calendar.getComponents().listIterator();
-
- while (cIterator.hasNext()) {
- Component event = (Component)cIterator.next();
- CourseEvent dbEvent;
- Property prop;
-
- prop = event.getProperty("UID");
- String uid;
- if(prop != null)
- uid = prop.getValue();
- else
- continue;
-
- prop = event.getProperty("SEQUENCE");
- int sequence;
- if(prop != null)
- sequence = Integer.parseInt(prop.getValue());
- else
- sequence = 1;
-
- long startTime = 0, endTime = 0;
- prop = event.getProperty("DTSTART");
- if(prop != null)
- startTime = getTimeFromTimestamp(prop.getValue());
- else
- Log.i("VPMSync", "Parse Event: DTSTART not found!");
-
- prop = event.getProperty("DTEND");
- if(prop != null)
- endTime = getTimeFromTimestamp(prop.getValue());
- else
- Log.i("VPMSync", "Parse Event: DTEND not found!");
-
- if(!(endTime > timeFrom && startTime < timeTo)) {
- Log.i("VPMSync", "Skip Entry: ("+timeFrom+" - "+timeTo+") Filter: "+startTime+" - "+endTime);
- continue;
- }
-
- dbEvent = null;
- for(int i = lastEventIndex; i < events.length; i++) {
- if(events[i].getUniqueId().equalsIgnoreCase(uid)) {
- dbEvent = events[i];
- break;
- }
- }
- if(dbEvent == null && lastEventIndex > 0) {
- for(int i = 0; i < lastEventIndex; i++) {
- if(events[i].getUniqueId().equalsIgnoreCase(uid)) {
- dbEvent = events[i];
- break;
- }
- }
- }
- if(dbEvent == null) {
- for(CourseEvent cevent : newEvents) {
- if(cevent.getUniqueId().equalsIgnoreCase(uid)) {
- dbEvent = cevent;
- break;
- }
- }
- }
-
- if(dbEvent == null) {
- // new event!
- Log.i("VLPSync", "New Event "+uid);
- dbEvent = new CourseEvent(sCourseName, uid, sequence, true);
- newEvents.add(dbEvent);
- } else {
- Log.i("VLPSync", "Existing Event "+uid+" ("+dbEvent.getSequenceId()+" >= "+sequence+")");
- if(dbEvent.getSequenceId() >= sequence) {
- continue; // skip event
- }
- dbEvent.setSequenceId(sequence);
- }
- // perform update
- prop = event.getProperty("SUMMARY");
- if(prop != null)
- dbEvent.setEventTitle(prop.getValue());
- else
- Log.i("VPMSync", "Parse Event: SUMMARY not found!");
-
- dbEvent.setEventFrom(startTime);
- dbEvent.setEventTo(endTime);
-
- prop = event.getProperty("LOCATION");
- if(prop != null)
- dbEvent.setEventLocation(prop.getValue());
- else
- Log.i("VPMSync", "Parse Event: LOCATION not found!");
-
- prop = event.getProperty("STATUS");
- if(prop != null)
- dbEvent.setEventStatus(prop.getValue());
- else
- Log.i("VPMSync", "Parse Event: STATUS not found!");
-
- prop = event.getProperty("RRULE");
- if(prop != null)
- dbEvent.setRecurRule(prop.getValue());
- else
- dbEvent.setRecurRule("");
-
- prop = event.getProperty("EXDATE");
- if(prop != null)
- dbEvent.setExcludeDates(prop.getValue());
- else
- dbEvent.setExcludeDates("");
-
- CourseGroup group = CourseGroup.GetCourseGroupByName(AppContext.getDatabaseManager(), sCourseName, dbEvent.getGroupTitle().trim());
- if(group != null)
- dbEvent.setCourseGroup(group);
-
- if(group.isNewGroup(true)) {
- SearchIndices indices = new SearchIndices("Vorlesungsplan#Group"+group.getGroupId(), false);
- indices.setUpdateTime(dbEvent.getEventFrom());
- indices.setTarget("#Vorlesungsplan#groupid=" + group.getGroupId());
- indices.setTitle("Vorlesungsplan " + dbEvent.getCourseName());
- indices.setDescription("Vorlesung " + dbEvent.getEventTitle());
- indices.addKeyWord(dbEvent.getGroupTitle());
- indices.addKeyWord(dbEvent.getEventLocation());
- newIndices.add(indices);
- }
-
- dbEvent.update(AppContext.getDatabaseManager());
- Log.i("VPMSync", "Update Event: "+dbEvent.getUniqueId());
- }
-
- SearchIndices[] newIndicesArr = new SearchIndices[newIndices.size()];
- newIndicesArr = newIndices.toArray(newIndicesArr);
- AppContext.addSearchIndices(newIndicesArr);
-
- for(VorlesungsplanManagerInterface callback : aCallbackInterfaces) {
- callback.onVorlesungsplanUpdateDone();
- }
- aCallbackInterfaces.clear();
- bRequestRunning = false;
- }
-
- private long getTimeFromTimestamp(String timestamp) {
- // 20160425T090000
- // 20160321
- DateFormat df = new SimpleDateFormat("yyyyMMdd'T'kkmmss", Locale.ENGLISH);
- try {
- Date result = df.parse(timestamp);
- return result.getTime()/1000;
- } catch (ParseException e) {
- df = new SimpleDateFormat("yyyyMMdd", Locale.ENGLISH);
- try {
- Date result = df.parse(timestamp);
- return result.getTime()/1000;
- } catch (ParseException e2) {
- Log.i("VPMSync", "Failed parsing: "+timestamp);
- return 0;
- }
- }
- }
-
- @Override
- protected void onCalendarRequestFail(int statusCode, String errorMessage) {
- Log.i("VPM", "Calendar Error: "+statusCode);
- for(VorlesungsplanManagerInterface callback : aCallbackInterfaces) {
- callback.onVorlesungsplanUpdateFail("error "+statusCode+": "+errorMessage);
- }
- aCallbackInterfaces.clear();
- bRequestRunning = false;
- }
-}
+++ /dev/null
-package de.dhbwloe.campusapp.vorlesungen;
-
-/**
- * Created by pk910 on 20.01.2016.
- */
-public interface VorlesungsplanManagerInterface {
-
- public void onVorlesungsplanUpdateDone();
- public void onVorlesungsplanUpdateFail(String errorMessage);
-
-}