If you think the Android project Tacere listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
/*
* Copyright (c) 2014 Jonathan Nelson//fromwww.java2s.com
* Released under the BSD license. For details see the COPYING file.
*/package org.ciasaboark.tacere.manager;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import org.ciasaboark.tacere.service.EventSilencerService;
import org.ciasaboark.tacere.service.RequestTypes;
publicclass AlarmManagerWrapper {
privatestaticfinal String TAG = "AlarmManagerWrapper";
// requestCodes for the different pending intents
privatestaticfinalint RC_EVENT = 1;
privatestaticfinalint RC_QUICKSILENT = 2;
privatestaticfinalint RC_NOTIFICATION = 3;
privatefinal Context context;
public AlarmManagerWrapper(Context ctx) {
if (ctx == null) {
thrownew IllegalArgumentException("context can not be null");
}
this.context = ctx.getApplicationContext();
}
publicvoid scheduleNormalWakeAt(long time) {
scheduleAlarmAt(time, RequestTypes.NORMAL);
}
privatevoid scheduleAlarmAt(long time, RequestTypes type) {
scheduleAlarmAt(time, type, null);
}
privatevoid scheduleAlarmAt(long time, RequestTypes type, Bundle additionalArgs) {
Log.d(TAG, "alarm scheduled at " + time + " for " + type);
if (null == type) {
thrownew IllegalArgumentException("unknown type: " + type);
}
Intent i = new Intent(context, EventSilencerService.class);
Bundle bundle;
if (additionalArgs != null) {
bundle = new Bundle(additionalArgs);
} else {
bundle = new Bundle();
}
bundle.putSerializable(EventSilencerService.WAKE_REASON, type);
i.putExtras(bundle);
// note that the android alarm manager allows multiple pending intents to be scheduled per
// app but only if each intent has a unique request code. Since we want to schedule wakeups
// for ending quicksilent durations as well as starting events we check the type and assign
// a different requestCode
// default to 0
int requestCode = 0;
if (type.equals(RequestTypes.CANCEL_QUICKSILENCE)) {
requestCode = RC_QUICKSILENT;
} elseif (type.equals(RequestTypes.NORMAL)) {
requestCode = RC_EVENT;
}
PendingIntent pintent = PendingIntent.getService(context, requestCode, i,
PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarm.set(AlarmManager.RTC_WAKEUP, time, pintent);
}
publicvoid scheduleImmediateQuicksilenceForDuration(int duration) {
if (duration <= 0) {
thrownew IllegalArgumentException("duration must be positive int >= 1");
}
Bundle args = new Bundle();
args.putInt(EventSilencerService.QUICKSILENCE_DURATION, duration);
scheduleAlarmAt(System.currentTimeMillis(), RequestTypes.QUICKSILENCE, args);
}
publicvoid scheduleImmediateAlarm(RequestTypes type) {
if (type == null) {
thrownew IllegalArgumentException("request type can not be null");
}
scheduleAlarmAt(System.currentTimeMillis(), type);
}
publicvoid scheduleCancelQuickSilenceAlarmAt(long time) {
scheduleAlarmAt(time, RequestTypes.CANCEL_QUICKSILENCE);
}
publicvoid scheduleActivityRestartWakeAt(long time) {
scheduleAlarmAt(time, RequestTypes.NORMAL);
}
publicvoid cancelAllAlarms() {
// there could be multiple alarms scheduled, we have to cancel all of them
for (int requestCode = 0; requestCode <= 4; requestCode++) {
Intent i = new Intent(context, EventSilencerService.class);
PendingIntent pintent = PendingIntent.getService(context, requestCode, i, 0);
AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarm.cancel(pintent);
}
}
}