package com.example.alarm04;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import java.util.Calendar;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
 

public class MainActivity extends Activity {
    Toast mToast;
      @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
 
            Button button = (Button)findViewById(R.id.one_shot);
            button.setOnClickListener(mOneShotListener);
            button = (Button)findViewById(R.id.start_repeating);
            button.setOnClickListener(mStartRepeatingListener);
            button = (Button)findViewById(R.id.stop_repeating);
            button.setOnClickListener(mStopRepeatingListener);
        }
 
        private OnClickListener mOneShotListener = new OnClickListener() {
            public void onClick(View v) {
 
                Intent intent = new Intent(MainActivity.this, RepeatingAlarm.class);
                PendingIntent sender = PendingIntent.getBroadcast(MainActivity.this,
                        0, intent, 0);
 
                // We want the alarm to go off 10 seconds from now.
                Calendar calendar = Calendar.getInstance();
                calendar.setTimeInMillis(System.currentTimeMillis());
                calendar.add(Calendar.SECOND, 10);
 
                // Schedule the alarm!
                AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
                am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);
 
                // Tell the user about what we did.
                if (mToast != null) {
                    mToast.cancel();
                }
                mToast = Toast.makeText(MainActivity.this,"Alarm Started",
                        Toast.LENGTH_LONG);
                mToast.show();
            }
        };
 
        private OnClickListener mStartRepeatingListener = new OnClickListener()
        {
            public void onClick(View v) 
            {
 
                Intent intent = new Intent(MainActivity.this, RepeatingAlarm.class);
                PendingIntent sender = PendingIntent.getBroadcast(MainActivity.this,
                        0, intent, 0);
 
                // We want the alarm to go off 5 seconds from now.
                long firstTime = SystemClock.elapsedRealtime();
                firstTime += 5*1000;
 
                // Schedule the alarm!
                AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
                am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                                firstTime, 5*1000, sender);
 
                // Tell the user about what we did.
                if (mToast != null) {
                    mToast.cancel();
                }
                mToast = Toast.makeText(MainActivity.this, "Rescheduled",
                        Toast.LENGTH_LONG);
                mToast.show();
            }
        };
 
        private OnClickListener mStopRepeatingListener = new OnClickListener() {
            public void onClick(View v) {
                // Create the same intent, and thus a matching IntentSender, for
                // the one that was scheduled.
                Intent intent = new Intent(MainActivity.this, RepeatingAlarm.class);
                PendingIntent sender = PendingIntent.getBroadcast(MainActivity.this,
                        0, intent, 0);
 
                // And cancel the alarm.
                AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
                am.cancel(sender);
 
                // Tell the user about what we did.
                if (mToast != null) {
                    mToast.cancel();
                }
                mToast = Toast.makeText(MainActivity.this,"Repeating Alarm Unscheduled.", Toast.LENGTH_LONG);
                mToast.show();
            }
        };
    }