Search in sources :

Example 66 with AlarmManager

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);
    }
}
Also used : AlarmManager(android.app.AlarmManager) PendingIntent(android.app.PendingIntent) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent)

Example 67 with AlarmManager

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");
}
Also used : GregorianCalendar(java.util.GregorianCalendar) Calendar(java.util.Calendar) AlarmManager(android.app.AlarmManager) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) PendingIntent(android.app.PendingIntent) KrollDict(org.appcelerator.kroll.KrollDict) SimpleDateFormat(java.text.SimpleDateFormat)

Example 68 with AlarmManager

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");
}
Also used : HashMap(java.util.HashMap) AlarmManager(android.app.AlarmManager) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) PendingIntent(android.app.PendingIntent) KrollDict(org.appcelerator.kroll.KrollDict)

Example 69 with AlarmManager

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);
}
Also used : AlarmManager(android.app.AlarmManager) Intent(android.content.Intent)

Example 70 with AlarmManager

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));
}
Also used : AlarmManager(android.app.AlarmManager)

Aggregations

AlarmManager (android.app.AlarmManager)471 PendingIntent (android.app.PendingIntent)349 Intent (android.content.Intent)323 Calendar (java.util.Calendar)75 SuppressLint (android.annotation.SuppressLint)24 Date (java.util.Date)24 Context (android.content.Context)22 SharedPreferences (android.content.SharedPreferences)20 SimpleDateFormat (java.text.SimpleDateFormat)18 Test (org.junit.Test)17 VisibleForTesting (com.android.internal.annotations.VisibleForTesting)13 Config (org.robolectric.annotation.Config)13 Handler (android.os.Handler)11 HashMap (java.util.HashMap)11 PowerManager (android.os.PowerManager)10 File (java.io.File)10 Map (java.util.Map)10 Activity (android.app.Activity)9 ShadowAlarmManager (org.robolectric.shadows.ShadowAlarmManager)9 TargetApi (android.annotation.TargetApi)8