com.appsaur.tarucassist.AutomuteAlarmReceiver.java Source code

Java tutorial

Introduction

Here is the source code for com.appsaur.tarucassist.AutomuteAlarmReceiver.java

Source

/*
 * Copyright 2015 Blanyal D'Souza.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.appsaur.tarucassist;

import android.app.AlarmManager;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.BitmapFactory;
import android.media.AudioManager;
import android.media.RingtoneManager;
import android.os.SystemClock;
import android.support.v4.app.NotificationCompat;
import android.support.v4.content.WakefulBroadcastReceiver;
import android.util.Log;

import com.appsaur.tarucassist.Activities.BaseActivity;
import com.appsaur.tarucassist.Activities.ViewScheduleActivity;
import com.appsaur.tarucassist.Databases.DataSources.ScheduleAutomuteDataSource;
import com.appsaur.tarucassist.Databases.DataSources.ScheduleReminderDataSource;
import com.appsaur.tarucassist.Databases.DataSources.StudentScheduleDataSource;
import com.appsaur.tarucassist.Domains.ScheduleAutomute;
import com.appsaur.tarucassist.Domains.ScheduleReminder;

import java.util.Calendar;

public class AutomuteAlarmReceiver extends WakefulBroadcastReceiver {
    AlarmManager mAlarmManager;
    PendingIntent mPendingIntent;
    Calendar mCalendar;
    AudioManager myAudioManager;

    @Override
    public void onReceive(Context context, Intent intent) {
        int mReceivedID = Integer.parseInt(intent.getStringExtra(BaseActivity.EXTRA_REMINDER_ID));

        // Get notification title from Reminder Database
        ScheduleAutomuteDataSource scheduleAutomuteDataSource = new ScheduleAutomuteDataSource(context);
        scheduleAutomuteDataSource.open();
        ScheduleAutomute scheduleAutomute = scheduleAutomuteDataSource.getScheduleAutomute(mReceivedID);
        mCalendar = Calendar.getInstance();

        myAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);

        if (scheduleAutomute.getAutomuteMode().equals("Vibrate")) {
            myAudioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
        } else if (scheduleAutomute.getAutomuteMode().equals("Silent")) {
            myAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
        } else if (scheduleAutomute.getAutomuteMode().equals("Normal"))
            myAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);

    }

    public void setAlarm(Context context, Calendar calendar, int ID) {
        mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

        // Put Reminder ID in Intent Extra
        Intent intent = new Intent(context, AutomuteAlarmReceiver.class);
        intent.putExtra(BaseActivity.EXTRA_REMINDER_ID, Integer.toString(ID));
        mPendingIntent = PendingIntent.getBroadcast(context, ID, intent, PendingIntent.FLAG_CANCEL_CURRENT);

        // Calculate notification time
        Calendar c = Calendar.getInstance();
        long currentTime = c.getTimeInMillis();
        long diffTime = calendar.getTimeInMillis() - currentTime;

        // Start alarm using notification time
        mAlarmManager.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + diffTime, mPendingIntent);

        // Restart alarm if device is rebooted
        ComponentName receiver = new ComponentName(context, BootReceiver.class);
        PackageManager pm = context.getPackageManager();
        pm.setComponentEnabledSetting(receiver, PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                PackageManager.DONT_KILL_APP);
    }

    public void setRepeatAlarm(Context context, Calendar calendar, int ID, long RepeatTime) {
        mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

        // Put Reminder ID in Intent Extra
        Intent intent = new Intent(context, AutomuteAlarmReceiver.class);
        intent.putExtra(BaseActivity.EXTRA_REMINDER_ID, Integer.toString(ID));
        mPendingIntent = PendingIntent.getBroadcast(context, ID, intent, PendingIntent.FLAG_CANCEL_CURRENT);

        // Calculate notification timein
        Calendar c = Calendar.getInstance();
        long currentTime = c.getTimeInMillis();
        long diffTime = calendar.getTimeInMillis() - currentTime;

        // Start alarm using initial notification time and repeat interval time
        mAlarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + diffTime,
                RepeatTime, mPendingIntent);

        // Restart alarm if device is rebooted
        ComponentName receiver = new ComponentName(context, BootReceiver.class);
        PackageManager pm = context.getPackageManager();
        pm.setComponentEnabledSetting(receiver, PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                PackageManager.DONT_KILL_APP);
    }

    public void cancelAlarm(Context context, int ID) {
        mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

        // Cancel Alarm using Reminder ID
        mPendingIntent = PendingIntent.getBroadcast(context, ID, new Intent(context, AutomuteAlarmReceiver.class),
                0);
        mAlarmManager.cancel(mPendingIntent);

        // Disable alarm
        ComponentName receiver = new ComponentName(context, BootReceiver.class);
        PackageManager pm = context.getPackageManager();
        pm.setComponentEnabledSetting(receiver, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                PackageManager.DONT_KILL_APP);
    }

}