package com.jservoire.hellomiss;

import java.util.Calendar;

import android.os.Bundle;
import android.preference.PreferenceManager;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;

public class SettingActivity extends Activity 
{
	private PendingIntent pendingIntentNotifMme;
	private PendingIntent pendingIntentNotifMiss;
	private AlarmManager alarmManagerMme;
	private AlarmManager alarmManagerMiss;
	private CheckBox chkMmeNotif;
	private CheckBox chkMissNotif;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_setting);

		Intent intentNotifMme = new Intent(SettingActivity.this, NotificationReceiver.class);
		pendingIntentNotifMme = PendingIntent.getBroadcast(SettingActivity.this, 0, intentNotifMme, 0);
		
		Intent intentNotifMiss = new Intent(SettingActivity.this, NotificationReceiver.class);
		pendingIntentNotifMiss = PendingIntent.getBroadcast(SettingActivity.this, 0, intentNotifMiss, 0);
		
		alarmManagerMme = (AlarmManager)this.getSystemService(Context.ALARM_SERVICE);
		alarmManagerMiss = (AlarmManager)this.getSystemService(Context.ALARM_SERVICE);
		
		chkMmeNotif = (CheckBox)findViewById(R.id.NotifMmeCheckBox);
		chkMmeNotif.setOnCheckedChangeListener(new OnCheckedChangeListener()
		{
		    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
		    {
		        if ( isChecked )
		        {
		        	alarmManagerMme.cancel(pendingIntentNotifMme);
		        	startMmeNotification();
		        	setMmePrefNotif(true);
		        }
		        else 
		        {
		        	alarmManagerMme.cancel(pendingIntentNotifMme);
		        	setMmePrefNotif(false);
		        }
		    }
		});
		
		chkMissNotif = (CheckBox)findViewById(R.id.NotifMissCheckBox);
		chkMissNotif.setOnCheckedChangeListener(new OnCheckedChangeListener()
		{
		    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
		    {
		        if ( isChecked )
		        {
		        	alarmManagerMiss.cancel(pendingIntentNotifMiss);
		        	startMissNotification();
		        	setMissPrefNotif(true);
		        }
		        else 
		        {
		        	alarmManagerMiss.cancel(pendingIntentNotifMiss);
		        	setMissPrefNotif(false);
		        }
		    }
		});
		
		this.loadNotificationPref();
	}
	
	public void startMmeNotification()
	{
		Calendar calendar = Calendar.getInstance();	
		calendar.set(Calendar.HOUR_OF_DAY, 10);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		alarmManagerMme.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntentNotifMme);	
	} 
	
	public void setMmePrefNotif(boolean isNotif)
	{
		SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
		SharedPreferences.Editor editor = preferences.edit();
		editor.putBoolean("MmeNotifications", isNotif);
		editor.commit();
	}
	
	public void startMissNotification()
	{
		Calendar calendar = Calendar.getInstance();	
		calendar.set(Calendar.HOUR_OF_DAY, 00);
		calendar.set(Calendar.MINUTE, 30);
		calendar.set(Calendar.SECOND, 0);
		alarmManagerMiss.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntentNotifMiss);		
	}
	
	public void setMissPrefNotif(boolean isNotif)
	{
		SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
		SharedPreferences.Editor editor = preferences.edit();
		editor.putBoolean("MissNotifications", isNotif);
		editor.commit();
	}
	
	private void loadNotificationPref()
	{
		SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
		boolean isNotifMme = preferences.getBoolean("MmeNotifications", false);
		boolean isNotifMiss = preferences.getBoolean("MissNotifications", false);
		chkMmeNotif.setChecked(isNotifMme);
		chkMissNotif.setChecked(isNotifMiss);
	}
}

@@ -640,53 +640,10 @@
 ager
-Mme;%0A%09private AlarmManager alarmManagerMiss
 ;%0A
+
 %09pri
@@ -874,19 +874,16 @@
 entNotif
-Mme
  = new I
@@ -1032,109 +1032,14 @@
 otif
-Mme
 , 0);%0A
-%09%09%0A%09%09Intent intentNotifMiss = new Intent(SettingActivity.this, NotificationReceiver.class);%0A
 %09%09pe
@@ -1102,33 +1102,33 @@
 gActivity.this, 
-0
+1
 , intentNotifMis
@@ -1128,18 +1128,13 @@
 otif
-Miss
 , 0);
-%0A
 %09%09%0A%09
@@ -1150,92 +1150,8 @@
 ager
-Mme = (AlarmManager)this.getSystemService(Context.ALARM_SERVICE);%0A%09%09alarmManagerMiss
  = (
@@ -1493,35 +1493,32 @@
    %09alarmManager
-Mme
 .cancel(pendingI
@@ -1665,19 +1665,16 @@
 mManager
-Mme
 .cancel(
@@ -2045,36 +2045,32 @@
    %09alarmManager
-Miss
 .cancel(pendingI
@@ -2220,20 +2220,16 @@
 mManager
-Miss
 .cancel(
@@ -2568,19 +2568,16 @@
 mManager
-Mme
 .setRepe
@@ -3146,32 +3146,32 @@
 dar.SECOND, 0);%0A
+
 %09%09alarmManagerMi
@@ -3168,20 +3168,16 @@
 mManager
-Miss
 .setRepe

Fix bug on notification.