Thursday, 11 June 2015

Boot receiver class

package com.siliconicpro.admin.ialarm;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;

import java.text.DateFormat;
import java.text.SimpleDateFormat;

public class BootReceiver extends BroadcastReceiver {
    private DateFormat timeFormat = new SimpleDateFormat("hh:mm aa");
    private DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
            Cursor cursor = AlarmsTable.getAllOnAlarms(context);
            while (cursor.moveToNext()) {
                // Getting Alarm Data
                int id = cursor.getInt(cursor.getColumnIndex(AlarmsTable._id));
                String time = cursor.getString(cursor.getColumnIndex(AlarmsTable.TIME));
                String date = cursor.getString(cursor.getColumnIndex(AlarmsTable.DATE));
                int interval = cursor.getInt(cursor.getColumnIndex(AlarmsTable.INTERVAL));
                int repeat = cursor.getInt(cursor.getColumnIndex(AlarmsTable.REPEAT));
                String alert1 = cursor.getString(cursor.getColumnIndex(AlarmsTable.ALERT_1));
                String alert2 = cursor.getString(cursor.getColumnIndex(AlarmsTable.ALERT_2));
                long alarmTime = Home.getRingTime(time, date, interval); // Alarm Time
                long currentTime = System.currentTimeMillis(); // System Time

                // For Repeat and No Repeat
                if (alarmTime > currentTime) {
                    // Turn On (Only in system not database)
                    Home.setNewAlarm(context, time, date, interval, repeat, (long) id, alert1, alert2);
                }
                // For Repeat and No Repeat
                else if(alarmTime == currentTime) {
                    // Turn On (Only in system not database)
                    Home.setNewAlarm(context, time, date, interval, repeat, (long) id, alert1, alert2);
                }
                // Only For Repeat
                else if (alarmTime < currentTime && repeat == 1) {
                    // Turn On (Only in system not database)
                    Boolean flag = false;
                    while(!flag){
                        alarmTime = alarmTime + Home.minuteToMillis(interval);
                        if(alarmTime > currentTime) {
                            flag = true;
                        }
                    }
                    // Setting starting time and date
                    String newTime = timeFormat.format(alarmTime - Home.minuteToMillis(interval));
                    String newDate = dateFormat.format(alarmTime - Home.minuteToMillis(interval));
                    AlarmsTable.updateRepeatTime(String.valueOf(id), newTime, newDate, Home.getDop());
                    Home.setNewAlarm(context, newTime, newDate, interval, repeat, (long) id, alert1, alert2);
                }
            }
        }
    }
}

No comments:

Post a Comment