/*
 * Copyright (c) 2015  Sebastian Paarmann
 * Licensed under the MIT license, see the LICENSE file
 */

package paarmann.physikprofil;

import android.app.IntentService;
import android.content.Intent;

import java.util.List;

/**
 * Service that updates homework data in the background.
 */
public class AutomaticUpdateService extends IntentService
    implements HomeworkUpdater.OnHomeworkLoadedListener {

  public static final String TAG = "AutomaticUpdateService";

  private boolean done;

  public AutomaticUpdateService() {
    super("AutomaticUpdateService");
  }

  @Override
  protected void onHandleIntent(Intent intent) {
    Log.i(TAG, "Starting update of homework data");

    done = false;
    HomeworkUpdater updater = new HomeworkUpdater(this);
    updater.setOnHomeworkLoadedListener(this);
    updater.downloadHomework();

    while (!done) {
      try {
        Thread.sleep(200);
      } catch (InterruptedException e) {
        // Just keep waiting
      }
    }

    Log.i(TAG, "Finished update of homework data");
  }

  @Override
  public void setData(List<HAElement> data) {
    done = true;
  }
}

@@ -214,20 +214,125 @@
 va.util.
-List
+Calendar;%0Aimport java.util.Date;%0Aimport java.util.List;%0A%0Aimport paarmann.physikprofil.network.HomeworkManager
 ;%0A%0A/**%0A 
@@ -452,64 +452,8 @@
 vice
-%0A    implements HomeworkUpdater.OnHomeworkLoadedListener
  %7B%0A%0A
@@ -753,141 +753,286 @@
 se;%0A
+%0A
     
-HomeworkUpdater updater = new HomeworkUpdater(this);%0A    updater.setOnHomeworkLoadedListener(this);%0A    updater.downloadHomework(
+Calendar cal = Calendar.getInstance();%0A    Date startDate = cal.getTime();%0A    cal.add(Calendar.DAY_OF_MONTH, 64); // Server limits to 64 days%0A    Date endDate = cal.getTime();%0A    HomeworkManager.getHomework(this, startDate, endDate, hw -%3E %7B%0A      done = true;%0A    %7D, true
 );%0A%0A
@@ -1236,85 +1236,6 @@
  %7D%0A%0A
-  @Override%0A  public void setData(List%3CHAElement%3E data) %7B%0A    done = true;%0A  %7D%0A
 %7D%0A

Update AutomaticUpdateService to new server system