use of android.app.AlarmManager in project platform_frameworks_base by android.
the class DevicePolicyManagerService method setExpirationAlarmCheckLocked.
/**
* Set an alarm for an upcoming event - expiration warning, expiration, or post-expiration
* reminders. Clears alarm if no expirations are configured.
*/
private void setExpirationAlarmCheckLocked(Context context, int userHandle, boolean parent) {
final long expiration = getPasswordExpirationLocked(null, userHandle, parent);
final long now = System.currentTimeMillis();
final long timeToExpire = expiration - now;
final long alarmTime;
if (expiration == 0) {
// No expirations are currently configured: Cancel alarm.
alarmTime = 0;
} else if (timeToExpire <= 0) {
// The password has already expired: Repeat every 24 hours.
alarmTime = now + MS_PER_DAY;
} else {
// Selecting the next alarm time: Roll forward to the next 24 hour multiple before
// the expiration time.
long alarmInterval = timeToExpire % MS_PER_DAY;
if (alarmInterval == 0) {
alarmInterval = MS_PER_DAY;
}
alarmTime = now + alarmInterval;
}
long token = mInjector.binderClearCallingIdentity();
try {
int affectedUserHandle = parent ? getProfileParentId(userHandle) : userHandle;
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
PendingIntent pi = PendingIntent.getBroadcastAsUser(context, REQUEST_EXPIRE_PASSWORD, new Intent(ACTION_EXPIRED_PASSWORD_NOTIFICATION), PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_UPDATE_CURRENT, UserHandle.of(affectedUserHandle));
am.cancel(pi);
if (alarmTime != 0) {
am.set(AlarmManager.RTC, alarmTime, pi);
}
} finally {
mInjector.binderRestoreCallingIdentity(token);
}
}
use of android.app.AlarmManager in project benCoding.AlarmManager by benbahrenburg.
the class AlarmManagerProxy method addAlarmService.
@Kroll.method
public void addAlarmService(@SuppressWarnings("rawtypes") HashMap hm) {
@SuppressWarnings("unchecked") KrollDict args = new KrollDict(hm);
if (!args.containsKeyAndNotNull("service")) {
throw new IllegalArgumentException("Service name (service) is required");
}
if (!args.containsKeyAndNotNull("minute") && !args.containsKeyAndNotNull("second")) {
throw new IllegalArgumentException("The minute or second field is required");
}
Calendar calendar = null;
boolean isRepeating = hasRepeating(args);
long repeatingFrequency = 0;
if (isRepeating) {
repeatingFrequency = repeatingFrequency(args);
}
//If seconds are provided but not years, we just take the seconds to mean to add seconds until fire
boolean secondBased = (args.containsKeyAndNotNull("second") && !args.containsKeyAndNotNull("year"));
//If minutes are provided but not years, we just take the minutes to mean to add minutes until fire
boolean minuteBased = (args.containsKeyAndNotNull("minute") && !args.containsKeyAndNotNull("year"));
//Based on what kind of duration we build our calendar
if (secondBased) {
calendar = getSecondBasedCalendar(args);
} else if (minuteBased) {
calendar = getMinuteBasedCalendar(args);
} else {
calendar = getFullCalendar(args);
}
//Get the requestCode if provided, if none provided
//we use 192837 for backwards compatibility
int requestCode = args.optInt("requestCode", AlarmmanagerModule.DEFAULT_REQUEST_CODE);
String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
utils.debugLog("Creating Alarm Notification for: " + sdf.format(calendar.getTime()));
AlarmManager am = (AlarmManager) TiApplication.getInstance().getApplicationContext().getSystemService(TiApplication.ALARM_SERVICE);
Intent intent = createAlarmServiceIntent(args);
if (isRepeating) {
utils.debugLog("Setting Alarm to repeat at frequency " + repeatingFrequency);
PendingIntent pendingIntent = PendingIntent.getBroadcast(TiApplication.getInstance().getApplicationContext(), requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), repeatingFrequency, pendingIntent);
} else {
PendingIntent sender = PendingIntent.getBroadcast(TiApplication.getInstance().getApplicationContext(), requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
utils.debugLog("Setting Alarm for a single run");
am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);
}
utils.infoLog("Alarm Service Request Created");
}
use of android.app.AlarmManager in project benCoding.AlarmManager by benbahrenburg.
the class AlarmManagerProxy method cancelAlarmNotification.
@Kroll.method
public void cancelAlarmNotification(@Kroll.argument(optional = true) Object requestCode) {
// To cancel an alarm the signature needs to be the same as the submitting one.
utils.debugLog("Cancelling Alarm Notification");
//Set the default request code
int intentRequestCode = AlarmmanagerModule.DEFAULT_REQUEST_CODE;
//If the optional code was provided, cast accordingly
if (requestCode != null) {
if (requestCode instanceof Number) {
intentRequestCode = ((Number) requestCode).intValue();
}
}
utils.debugLog(String.format("Cancelling requestCode = {%d}", intentRequestCode));
//Create a placeholder for the args value
HashMap<String, Object> placeholder = new HashMap<String, Object>(0);
KrollDict args = new KrollDict(placeholder);
//Create the Alarm Manager
AlarmManager am = (AlarmManager) TiApplication.getInstance().getApplicationContext().getSystemService(TiApplication.ALARM_SERVICE);
Intent intent = createAlarmNotifyIntent(args, intentRequestCode);
PendingIntent sender = PendingIntent.getBroadcast(TiApplication.getInstance().getApplicationContext(), intentRequestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
am.cancel(sender);
utils.debugLog("Alarm Notification Canceled");
}
use of android.app.AlarmManager in project XobotOS by xamarin.
the class CdmaServiceStateTracker method setAndBroadcastNetworkSetTimeZone.
/**
* Set the timezone and send out a sticky broadcast so the system can
* determine if the timezone was set by the carrier.
*
* @param zoneId timezone set by carrier
*/
private void setAndBroadcastNetworkSetTimeZone(String zoneId) {
AlarmManager alarm = (AlarmManager) phone.getContext().getSystemService(Context.ALARM_SERVICE);
alarm.setTimeZone(zoneId);
Intent intent = new Intent(TelephonyIntents.ACTION_NETWORK_SET_TIMEZONE);
intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
intent.putExtra("time-zone", zoneId);
phone.getContext().sendStickyBroadcast(intent);
}
use of android.app.AlarmManager in project apps-android-wikipedia by wikimedia.
the class NotificationPollBroadcastReceiver method startPollTask.
public void startPollTask(@NonNull Context context) {
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), TimeUnit.MINUTES.toMillis(context.getResources().getInteger(R.integer.notification_poll_interval_minutes)), getAlarmPendingIntent(context));
}
Aggregations