/*
 * Copyright (c) 2014 Sebastian Paarmann
 */

package paarmann.physikprofil;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.util.Log;

import java.util.Calendar;

public class SettingsActivity extends Activity {

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    getFragmentManager().beginTransaction()
      .replace(android.R.id.content, new SettingsFragment())
      .commit();
  }

  public class SettingsFragment extends PreferenceFragment
                implements SharedPreferences.OnSharedPreferenceChangeListener {
    @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      addPreferencesFromResource(R.xml.preferences);
    }

    public SettingsFragment() {
      super();
    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences preferences, String key) {
      updateSummary(preferences, key);

      if (key.equals(MainActivity.PREF_AUTOUPDATES)) {
        boolean autoUpdate = preferences.getBoolean(key, false);

        //Register/Unregister alarms for updates
        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        Uri uriAfterSchool = Uri.fromParts("homeworkUpdate", "afterSchool", "");
        Uri uriAfternoon = Uri.fromParts("homeworkUpdate", "afternoon", "");

        Intent intentAfterSchool = new Intent(MainActivity.ACTION_UPDATEHOMEWORK, uriAfterSchool,
           SettingsActivity.this, AutomaticUpdateService.class);
        Intent intentAfternoon = new Intent(MainActivity.ACTION_UPDATEHOMEWORK, uriAfternoon,
           SettingsActivity.this, AutomaticUpdateService.class); 

        PendingIntent piAfterSchool = PendingIntent.getService(SettingsActivity.this,
            1, intentAfterSchool, 0);
        PendingIntent piAfternoon = PendingIntent.getService(SettingsActivity.this,
            2, intentAfternoon, 0);

        if (autoUpdate) {
          Calendar afterSchool = Calendar.getInstance();
          afterSchool.set(Calendar.HOUR_OF_DAY, 14);
          afterSchool.set(Calendar.MINUTE, 15);
          Calendar afternoon = Calendar.getInstance();
          afternoon.set(Calendar.HOUR_OF_DAY, 17);
          afternoon.set(Calendar.MINUTE, 15);

          long oneDayMillis = 24 * 60 * 60 * 1000;

          alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, afterSchool.getTimeInMillis(),
              oneDayMillis, piAfterSchool);
          alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, afternoon.getTimeInMillis(),
              oneDayMillis, piAfternoon);
        } else {
          alarmManager.cancel(piAfterSchool);
          alarmManager.cancel(piAfternoon);
        }
      }
    }

    private void updateSummary(SharedPreferences preferences, String key) {
      if (key.equals(MainActivity.PREF_FILTERSUBJECTS)) {
        Preference filterPref = findPreference(key);
        boolean filter = preferences.getBoolean(key, false);
        if (filter) {
          filterPref.setSummary(getResources().getString(R.string.pref_filter_summary_true));
        } else {
          filterPref.setSummary(getResources().getString(R.string.pref_filter_summary));
        }
      } else if (key.equals(MainActivity.PREF_AUTOUPDATES)) {
        Preference updatePref = findPreference(key);
        boolean autoUpdate = preferences.getBoolean(key, false);
        if (autoUpdate) {
          updatePref.setSummary(getResources().getString(R.string.pref_autoupdates_summary_true));
        } else {
          updatePref.setSummary(getResources().getString(R.string.pref_autoupdates_summary));
        }
      }
    }

    @Override
    public void onResume() {
      super.onResume();
      SharedPreferences preferences = getPreferenceScreen().getSharedPreferences();

      preferences.registerOnSharedPreferenceChangeListener(this);

      updateSummary(preferences, MainActivity.PREF_FILTERSUBJECTS);
      updateSummary(preferences, MainActivity.PREF_AUTOUPDATES);
    }

    @Override
    public void onPause() {
      super.onPause();
      getPreferenceScreen().getSharedPreferences()
        .unregisterOnSharedPreferenceChangeListener(this);
    }
  }

}

@@ -626,107 +626,448 @@
     
-getFragmentManager().beginTransaction()%0A      .replace(android.R.id.content, new SettingsFragment()
+SettingsFragment fragment = new SettingsFragment();%0A    fragment.setContext(this);%0A%0A    getFragmentManager().beginTransaction()%0A      .replace(android.R.id.content, fragment)%0A      .commit();%0A  %7D%0A%0A  @Override%0A  public void onResume() %7B%0A    super.onResume();%0A    %0A    SettingsFragment fragment = new SettingsFragment();%0A    fragment.setContext(this);%0A%0A    getFragmentManager().beginTransaction()%0A      .replace(android.R.id.content, fragment
 )%0A  
@@ -1091,24 +1091,31 @@
 %7D%0A%0A  public 
+static 
 class Settin
@@ -1232,16 +1232,47 @@
 tener %7B%0A
+%0A    private Context context;%0A%0A
     @Ove
@@ -1479,32 +1479,115 @@
 super();%0A    %7D%0A%0A
+    public void setContext(Context context) %7B%0A      this.context = context;%0A    %7D%0A%0A
     @Override%0A  
@@ -1932,16 +1932,24 @@
 anager) 
+context.
 getSyste
@@ -2249,37 +2249,23 @@
         
-SettingsActivity.this
+context
 , Automa
@@ -2394,37 +2394,23 @@
         
-SettingsActivity.this
+context
 , Automa
@@ -2499,37 +2499,23 @@
 Service(
-SettingsActivity.this
+context
 ,%0A      
@@ -2611,29 +2611,15 @@
 ice(
-SettingsActivity.this
+context
 ,%0A  

Fix #16 - Crash in SettingsActivity