Java Code Examples for android.app.AlarmManager#AlarmClockInfo

The following examples show how to use android.app.AlarmManager#AlarmClockInfo . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AlarmManagerService.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
private void updateNextAlarmInfoForUserLocked(int userId,
        AlarmManager.AlarmClockInfo alarmClock) {
    if (alarmClock != null) {
        if (DEBUG_ALARM_CLOCK) {
            Log.v(TAG, "Next AlarmClockInfoForUser(" + userId + "): " +
                    formatNextAlarm(getContext(), alarmClock, userId));
        }
        mNextAlarmClockForUser.put(userId, alarmClock);
    } else {
        if (DEBUG_ALARM_CLOCK) {
            Log.v(TAG, "Next AlarmClockInfoForUser(" + userId + "): None");
        }
        mNextAlarmClockForUser.remove(userId);
    }

    mPendingSendNextAlarmClockChangedForUser.put(userId, true);
    mHandler.removeMessages(AlarmHandler.SEND_NEXT_ALARM_CLOCK_CHANGED);
    mHandler.sendEmptyMessage(AlarmHandler.SEND_NEXT_ALARM_CLOCK_CHANGED);
}
 
Example 2
Source File: AlarmManagerService.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
public Alarm(int _type, long _when, long _whenElapsed, long _windowLength, long _maxWhen,
        long _interval, PendingIntent _op, IAlarmListener _rec, String _listenerTag,
        WorkSource _ws, int _flags, AlarmManager.AlarmClockInfo _info,
        int _uid, String _pkgName) {
    type = _type;
    origWhen = _when;
    wakeup = _type == AlarmManager.ELAPSED_REALTIME_WAKEUP
            || _type == AlarmManager.RTC_WAKEUP;
    when = _when;
    whenElapsed = _whenElapsed;
    expectedWhenElapsed = _whenElapsed;
    windowLength = _windowLength;
    maxWhenElapsed = expectedMaxWhenElapsed = clampPositive(_maxWhen);
    repeatInterval = _interval;
    operation = _op;
    listener = _rec;
    listenerTag = _listenerTag;
    statsTag = makeTag(_op, _listenerTag, _type);
    workSource = _ws;
    flags = _flags;
    alarmClock = _info;
    uid = _uid;
    packageName = _pkgName;
    sourcePackage = (operation != null) ? operation.getCreatorPackage() : packageName;
    creatorUid = (operation != null) ? operation.getCreatorUid() : uid;
}
 
Example 3
Source File: AlarmUtil.java    From android-DirectBoot with Apache License 2.0 6 votes vote down vote up
/**
 * Schedules an alarm using {@link AlarmManager}.
 *
 * @param alarm the alarm to be scheduled
 */
public void scheduleAlarm(Alarm alarm) {
    Intent intent = new Intent(mContext, AlarmIntentService.class);
    Bundle extras = writeAlarm(alarm);
    intent.putExtras(extras);

    PendingIntent pendingIntent = PendingIntent
        .getService(mContext, alarm.id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    Calendar alarmTime = Calendar.getInstance();
    alarmTime.set(Calendar.MONTH, alarm.month);
    alarmTime.set(Calendar.DATE, alarm.date);
    alarmTime.set(Calendar.HOUR_OF_DAY, alarm.hour);
    alarmTime.set(Calendar.MINUTE, alarm.minute);
    alarmTime.set(Calendar.SECOND, 0);

    AlarmManager.AlarmClockInfo alarmClockInfo = new AlarmManager.AlarmClockInfo(
        alarmTime.getTimeInMillis(),
        pendingIntent);
    mAlarmManager.setAlarmClock(alarmClockInfo, pendingIntent);
    Log.i(TAG,
        String.format("Alarm scheduled at (%2d:%02d) Date: %d, Month: %d",
            alarm.hour, alarm.minute,
            alarm.month, alarm.date));
}
 
Example 4
Source File: AlarmUtil.java    From security-samples with Apache License 2.0 6 votes vote down vote up
/**
 * Schedules an alarm using {@link AlarmManager}.
 *
 * @param alarm the alarm to be scheduled
 */
public void scheduleAlarm(Alarm alarm) {
    Intent intent = new Intent(mContext, AlarmIntentService.class);
    Bundle extras = writeAlarm(alarm);
    intent.putExtras(extras);

    PendingIntent pendingIntent = PendingIntent
        .getService(mContext, alarm.id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    Calendar alarmTime = Calendar.getInstance();
    alarmTime.set(Calendar.MONTH, alarm.month);
    alarmTime.set(Calendar.DATE, alarm.date);
    alarmTime.set(Calendar.HOUR_OF_DAY, alarm.hour);
    alarmTime.set(Calendar.MINUTE, alarm.minute);
    alarmTime.set(Calendar.SECOND, 0);

    AlarmManager.AlarmClockInfo alarmClockInfo = new AlarmManager.AlarmClockInfo(
        alarmTime.getTimeInMillis(),
        pendingIntent);
    mAlarmManager.setAlarmClock(alarmClockInfo, pendingIntent);
    Log.i(TAG,
        String.format("Alarm scheduled at (%2d:%02d) Date: %d, Month: %d",
            alarm.hour, alarm.minute,
            alarm.month, alarm.date));
}
 
Example 5
Source File: AlarmManagerService.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
/**
 * Formats an alarm like platform/packages/apps/DeskClock used to.
 */
private static String formatNextAlarm(final Context context, AlarmManager.AlarmClockInfo info,
        int userId) {
    String skeleton = DateFormat.is24HourFormat(context, userId) ? "EHm" : "Ehma";
    String pattern = DateFormat.getBestDateTimePattern(Locale.getDefault(), skeleton);
    return (info == null) ? "" :
            DateFormat.format(pattern, info.getTriggerTime()).toString();
}
 
Example 6
Source File: MainActivity.java    From Android-Developer-Fundamentals-Version-2 with GNU General Public License v3.0 5 votes vote down vote up
public void next_alarm_button(View view) {
    AlarmManager.AlarmClockInfo info = alarmManager.getNextAlarmClock();
    String message;
    if (info != null){
        Date nextAlarm = new Date(info.getTriggerTime());
        String newDate = DateFormat.getTimeInstance().format(nextAlarm);

        message = "The alarm is set for " + newDate + ".";
    }
    else{
        message = "There is no alarm scheduled.";
    }
    Toast.makeText(this, message, Toast.LENGTH_LONG).show();
}
 
Example 7
Source File: GeofencesScannerSwitchGPSBroadcastReceiver.java    From PhoneProfilesPlus with Apache License 2.0 4 votes vote down vote up
@SuppressLint({"SimpleDateFormat", "NewApi"})
static void setAlarm(Context context)
{
    int delay = 1; // one minute with GPS ON
    if (!GeofencesScanner.useGPS)
        delay = 30;  // 30 minutes with GPS OFF

    if (ApplicationPreferences.applicationUseAlarmClock) {
        //Intent intent = new Intent(_context, GeofencesScannerSwitchGPSBroadcastReceiver.class);
        Intent intent = new Intent();
        intent.setAction(PhoneProfilesService.ACTION_GEOFENCES_SCANNER_SWITCH_GPS_BROADCAST_RECEIVER);
        //intent.setClass(context, GeofencesScannerSwitchGPSBroadcastReceiver.class);

        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        if (alarmManager != null) {
            Calendar now = Calendar.getInstance();
            now.add(Calendar.MINUTE, delay);
            long alarmTime = now.getTimeInMillis();

            /*if (PPApplication.logEnabled()) {
                SimpleDateFormat sdf = new SimpleDateFormat("EE d.MM.yyyy HH:mm:ss:S");
                String result = sdf.format(alarmTime);
                PPApplication.logE("GeofencesScannerSwitchGPSBroadcastReceiver.setAlarm", "alarmTime=" + result);
            }*/

            Intent editorIntent = new Intent(context, EditorProfilesActivity.class);
            editorIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
            PendingIntent infoPendingIntent = PendingIntent.getActivity(context, 1000, editorIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            AlarmManager.AlarmClockInfo clockInfo = new AlarmManager.AlarmClockInfo(alarmTime, infoPendingIntent);
            alarmManager.setAlarmClock(clockInfo, pendingIntent);
        }
    }
    else {
        Data workData = new Data.Builder()
                .putString(PhoneProfilesService.EXTRA_ELAPSED_ALARMS_WORK, ElapsedAlarmsWorker.ELAPSED_ALARMS_GEOFENCE_SCANNER_SWITCH_GPS)
                .build();

        OneTimeWorkRequest worker =
                new OneTimeWorkRequest.Builder(ElapsedAlarmsWorker.class)
                        .addTag(ElapsedAlarmsWorker.ELAPSED_ALARMS_GEOFENCE_SCANNER_SWITCH_GPS_TAG_WORK)
                        .setInputData(workData)
                        .setInitialDelay(delay, TimeUnit.MINUTES)
                        .build();
        try {
            if (PPApplication.getApplicationStarted(true)) {
                WorkManager workManager = PPApplication.getWorkManagerInstance();
                if (workManager != null) {
                    //PPApplication.logE("[HANDLER] GeofencesScannerSwitchGPSBroadcastReceiver.setAlarm", "enqueueUniqueWork - delay="+delay);
                    workManager.enqueueUniqueWork(ElapsedAlarmsWorker.ELAPSED_ALARMS_GEOFENCE_SCANNER_SWITCH_GPS_TAG_WORK, ExistingWorkPolicy.KEEP, worker);
                }
            }
        } catch (Exception e) {
            PPApplication.recordException(e);
        }
    }

    /*
    //Intent intent = new Intent(_context, GeofencesScannerSwitchGPSBroadcastReceiver.class);
    Intent intent = new Intent();
    intent.setAction(PhoneProfilesService.ACTION_GEOFENCES_SCANNER_SWITCH_GPS_BROADCAST_RECEIVER);
    //intent.setClass(context, GeofencesScannerSwitchGPSBroadcastReceiver.class);

    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    if (alarmManager != null) {
        if (ApplicationPreferences.applicationUseAlarmClock(context)) {

            Calendar now = Calendar.getInstance();
            now.add(Calendar.MINUTE, delay);
            long alarmTime = now.getTimeInMillis();

            if (PPApplication.logEnabled()) {
                SimpleDateFormat sdf = new SimpleDateFormat("EE d.MM.yyyy HH:mm:ss:S");
                String result = sdf.format(alarmTime);
                PPApplication.logE("GeofencesScannerSwitchGPSBroadcastReceiver.setAlarm", "alarmTime=" + result);
            }

            Intent editorIntent = new Intent(context, EditorProfilesActivity.class);
            editorIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
            PendingIntent infoPendingIntent = PendingIntent.getActivity(context, 1000, editorIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            AlarmManager.AlarmClockInfo clockInfo = new AlarmManager.AlarmClockInfo(alarmTime, infoPendingIntent);
            alarmManager.setAlarmClock(clockInfo, pendingIntent);
        }
        else {
            long alarmTime = SystemClock.elapsedRealtime() + delay * 60 * 1000;

            if (android.os.Build.VERSION.SDK_INT >= 23)
                alarmManager.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, alarmTime, pendingIntent);
            else //if (android.os.Build.VERSION.SDK_INT >= 19)
                alarmManager.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, alarmTime, pendingIntent);
            //else
            //    alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, alarmTime, pendingIntent);
        }
    }
    */
}
 
Example 8
Source File: EventPreferencesAlarmClock.java    From PhoneProfilesPlus with Apache License 2.0 4 votes vote down vote up
@SuppressLint({"SimpleDateFormat", "NewApi"})
private void setAlarm(long alarmTime, Context context)
{
    if (!_permanentRun) {
        if (_startTime > 0) {
            /*if (PPApplication.logEnabled()) {
                SimpleDateFormat sdf = new SimpleDateFormat("EE d.MM.yyyy HH:mm:ss:S");
                String result = sdf.format(alarmTime);
                PPApplication.logE("EventPreferencesAlarmClock.setAlarm", "endTime=" + result);
            }*/

            /*if (ApplicationPreferences.applicationUseAlarmClock(context)) {
                Intent intent = new Intent();
                intent.setAction(PhoneProfilesService.ACTION_ALARM_CLOCK_EVENT_END_BROADCAST_RECEIVER);

                //intent.putExtra(PPApplication.EXTRA_EVENT_ID, _event._id);

                PendingIntent pendingIntent = PendingIntent.getBroadcast(context, (int) _event._id, intent, PendingIntent.FLAG_UPDATE_CURRENT);

                AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
                if (alarmManager != null) {
                    Intent editorIntent = new Intent(context, EditorProfilesActivity.class);
                    editorIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                    PendingIntent infoPendingIntent = PendingIntent.getActivity(context, 1000, editorIntent, PendingIntent.FLAG_UPDATE_CURRENT);
                    AlarmManager.AlarmClockInfo clockInfo = new AlarmManager.AlarmClockInfo(alarmTime + Event.EVENT_ALARM_TIME_SOFT_OFFSET, infoPendingIntent);
                    alarmManager.setAlarmClock(clockInfo, pendingIntent);
                }
            }
            else {
                Calendar now = Calendar.getInstance();
                long elapsedTime = (alarmTime + Event.EVENT_ALARM_TIME_OFFSET) - now.getTimeInMillis();

                if (PPApplication.logEnabled()) {
                    long allSeconds = elapsedTime / 1000;
                    long hours = allSeconds / 60 / 60;
                    long minutes = (allSeconds - (hours * 60 * 60)) / 60;
                    long seconds = allSeconds % 60;

                    PPApplication.logE("EventPreferencesAlarmClock.setAlarm", "elapsedTime=" + hours + ":" + minutes + ":" + seconds);
                }

                Data workData = new Data.Builder()
                        .putString(PhoneProfilesService.EXTRA_ELAPSED_ALARMS_WORK, ElapsedAlarmsWorker.ELAPSED_ALARMS_ALARM_CLOCK_EVENT_END_SENSOR)
                        .build();

                OneTimeWorkRequest worker =
                        new OneTimeWorkRequest.Builder(ElapsedAlarmsWorker.class)
                                .addTag("elapsedAlarmsAlarmClockSensorWork_"+(int)_event._id)
                                .setInputData(workData)
                                .setInitialDelay(elapsedTime, TimeUnit.MILLISECONDS)
                                .build();
                try {
                    WorkManager workManager = WorkManager.getInstance(context);
                    PPApplication.logE("[HANDLER] EventPreferencesAlarmClock.setAlarm", "enqueueUniqueWork - elapsedTime="+elapsedTime);
                    //workManager.enqueueUniqueWork("elapsedAlarmsAlarmClockSensorWork_"+(int)_event._id, ExistingWorkPolicy.KEEP, worker);
                    workManager.enqueue(worker);
                } catch (Exception ignored) {}
            }*/

            Intent intent = new Intent();
            intent.setAction(PhoneProfilesService.ACTION_ALARM_CLOCK_EVENT_END_BROADCAST_RECEIVER);

            //intent.putExtra(PPApplication.EXTRA_EVENT_ID, _event._id);

            PendingIntent pendingIntent = PendingIntent.getBroadcast(context, (int) _event._id, intent, PendingIntent.FLAG_UPDATE_CURRENT);

            AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
            if (alarmManager != null) {
                if (ApplicationPreferences.applicationUseAlarmClock) {
                    Intent editorIntent = new Intent(context, EditorProfilesActivity.class);
                    editorIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                    PendingIntent infoPendingIntent = PendingIntent.getActivity(context, 1000, editorIntent, PendingIntent.FLAG_UPDATE_CURRENT);
                    AlarmManager.AlarmClockInfo clockInfo = new AlarmManager.AlarmClockInfo(alarmTime + Event.EVENT_ALARM_TIME_SOFT_OFFSET, infoPendingIntent);
                    alarmManager.setAlarmClock(clockInfo, pendingIntent);
                }
                else {
                    //if (android.os.Build.VERSION.SDK_INT >= 23)
                        alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, alarmTime + Event.EVENT_ALARM_TIME_OFFSET, pendingIntent);
                    //else //if (android.os.Build.VERSION.SDK_INT >= 19)
                    //    alarmManager.setExact(AlarmManager.RTC_WAKEUP, alarmTime + Event.EVENT_ALARM_TIME_OFFSET, pendingIntent);
                    //else
                    //    alarmManager.set(AlarmManager.RTC_WAKEUP, alarmTime + Event.EVENT_ALARM_TIME_OFFSET, pendingIntent);
                }
            }
        }
    }
}
 
Example 9
Source File: NextAlarmClockBroadcastReceiver.java    From PhoneProfilesPlus with Apache License 2.0 4 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
    //PPApplication.logE("##### NextAlarmClockBroadcastReceiver.onReceive", "xxx");

    //CallsCounter.logCounter(context, "NextAlarmClockBroadcastReceiver.onReceive", "NextAlarmClockBroadcastReceiver_onReceive");

    if (intent == null)
        return;

    //if (!PPApplication.getApplicationStarted(context.getApplicationContext(), true))
    //    return;

    //if (android.os.Build.VERSION.SDK_INT >= 21) {
        String action = intent.getAction();
        if ((action != null) && action.equals(AlarmManager.ACTION_NEXT_ALARM_CLOCK_CHANGED)) {
            AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
            if (alarmManager != null) {
                AlarmManager.AlarmClockInfo alarmClockInfo = alarmManager.getNextAlarmClock();
                if (alarmClockInfo != null) {
                    long _time = alarmClockInfo.getTriggerTime();
                    /*if (PPApplication.logEnabled()) {
                        SimpleDateFormat sdf = new SimpleDateFormat("EE d.MM.yyyy HH:mm:ss:S");
                        String result = sdf.format(_time);
                        PPApplication.logE("NextAlarmClockBroadcastReceiver.onReceive", "_time=" + result);
                    }*/

                    PendingIntent infoPendingIntent = alarmClockInfo.getShowIntent();
                    // infoPendingIntent == null - Xiaomi Clock :-/
                    // infoPendingIntent == null - LG Clock :-/
                    // infoPendingIntent == null - Huawei Clock :-/

                    if (infoPendingIntent != null) {
                        String packageName = infoPendingIntent.getCreatorPackage();
                        //PPApplication.logE("NextAlarmClockBroadcastReceiver.onReceive", "packageName=" + packageName);
                        if (packageName != null) {
                            if (!packageName.equals(context.getPackageName())) {
                                //PPApplication.logE("NextAlarmClockBroadcastReceiver.onReceive", "packageName=" + packageName);

                                // com.google.android.deskclock - Google Clock
                                // com.sec.android.app.clockpackage - Samsung Clock
                                // com.sonyericsson.organizer - Sony Clock
                                // com.amdroidalarmclock.amdroid - AMdroid
                                // com.alarmclock.xtreme.free - Alarm Clock XTreme free
                                // com.alarmclock.xtreme - Alarm Clock XTreme
                                // droom.sleepIfUCan - Alarmy (Sleep if u can)
                                // com.funanduseful.earlybirdalarm - Early Bird Alarm Clock
                                // com.apalon.alarmclock.smart - Good Morning Alarm Clock
                                // com.kog.alarmclock - I Can't Wake Up! Alarm Clock
                                // com.urbandroid.sleep - Sleep as Android
                                // ch.bitspin.timely - Timely
                                // com.angrydoughnuts.android.alarmclock - Alarm Klock

                                /*if (packageName.equals("com.google.android.deskclock") ||
                                    packageName.equals("com.sec.android.app.clockpackage") ||
                                    packageName.equals("com.sonyericsson.organizer") ||
                                    packageName.equals("com.amdroidalarmclock.amdroid") ||
                                    packageName.equals("com.alarmclock.xtreme") ||
                                    packageName.equals("com.alarmclock.xtreme.free") ||
                                    packageName.equals("droom.sleepIfUCan") ||
                                    packageName.equals("com.funanduseful.earlybirdalarm") ||
                                    packageName.equals("com.apalon.alarmclock.smart") ||
                                    packageName.equals("com.kog.alarmclock") ||
                                    packageName.equals("com.urbandroid.sleep") ||
                                    packageName.equals("ch.bitspin.timely") ||
                                    packageName.equals("com.angrydoughnuts.android.alarmclock"))*/

                                    setAlarm(_time, packageName, alarmManager, context);
                            }
                        } /*else {
                            //PPApplication.logE("NextAlarmClockBroadcastReceiver.onReceive", "packageName == null");
                            setAlarm(_time, "", alarmManager, context);
                        }*/
                    } /*else {
                        //PPApplication.logE("NextAlarmClockBroadcastReceiver.onReceive", "infoPendingIntent == null");
                        setAlarm(_time, "", alarmManager, context);
                    }*/
                }
                //else {
                    //PPApplication.logE("NextAlarmClockBroadcastReceiver.onReceive", "alarmClockInfo == null");
                    //removeAlarm(alarmManager, context);
                //}
            }
        }
    //}
}
 
Example 10
Source File: EventPreferencesNFC.java    From PhoneProfilesPlus with Apache License 2.0 4 votes vote down vote up
@SuppressLint({"SimpleDateFormat", "NewApi"})
private void setAlarm(long alarmTime, Context context)
{
    if (!_permanentRun) {
        if (_startTime > 0) {
            /*if (PPApplication.logEnabled()) {
                SimpleDateFormat sdf = new SimpleDateFormat("EE d.MM.yyyy HH:mm:ss:S");
                String result = sdf.format(alarmTime);
                PPApplication.logE("EventPreferencesNFC.setAlarm", "endTime=" + result);
            }*/

            /*if (ApplicationPreferences.applicationUseAlarmClock(context)) {
                //Intent intent = new Intent(context, NFCEventEndBroadcastReceiver.class);
                Intent intent = new Intent();
                intent.setAction(PhoneProfilesService.ACTION_NFC_EVENT_END_BROADCAST_RECEIVER);
                //intent.setClass(context, NFCEventEndBroadcastReceiver.class);

                //intent.putExtra(PPApplication.EXTRA_EVENT_ID, _event._id);

                PendingIntent pendingIntent = PendingIntent.getBroadcast(context, (int) _event._id, intent, PendingIntent.FLAG_UPDATE_CURRENT);

                AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
                if (alarmManager != null) {
                    Intent editorIntent = new Intent(context, EditorProfilesActivity.class);
                    editorIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                    PendingIntent infoPendingIntent = PendingIntent.getActivity(context, 1000, editorIntent, PendingIntent.FLAG_UPDATE_CURRENT);
                    AlarmManager.AlarmClockInfo clockInfo = new AlarmManager.AlarmClockInfo(alarmTime + Event.EVENT_ALARM_TIME_SOFT_OFFSET, infoPendingIntent);
                    alarmManager.setAlarmClock(clockInfo, pendingIntent);
                }
            } else {
                Calendar now = Calendar.getInstance();
                long elapsedTime = (alarmTime + Event.EVENT_ALARM_TIME_OFFSET) - now.getTimeInMillis();

                if (PPApplication.logEnabled()) {
                    long allSeconds = elapsedTime / 1000;
                    long hours = allSeconds / 60 / 60;
                    long minutes = (allSeconds - (hours * 60 * 60)) / 60;
                    long seconds = allSeconds % 60;

                    PPApplication.logE("EventPreferencesNFC.setAlarm", "elapsedTime=" + hours + ":" + minutes + ":" + seconds);
                }

                Data workData = new Data.Builder()
                        .putString(PhoneProfilesService.EXTRA_ELAPSED_ALARMS_WORK, ElapsedAlarmsWorker.ELAPSED_ALARMS_NFC_EVENT_END_SENSOR)
                        .build();

                OneTimeWorkRequest worker =
                        new OneTimeWorkRequest.Builder(ElapsedAlarmsWorker.class)
                                .addTag("elapsedAlarmsNFCSensorWork_"+(int)_event._id)
                                .setInputData(workData)
                                .setInitialDelay(elapsedTime, TimeUnit.MILLISECONDS)
                                .build();
                try {
                    WorkManager workManager = WorkManager.getInstance(context);
                    PPApplication.logE("[HANDLER] EventPreferencesNFC.setAlarm", "enqueueUniqueWork - elapsedTime="+elapsedTime);
                    //workManager.enqueueUniqueWork("elapsedAlarmsNFCSensorWork_"+(int)_event._id, ExistingWorkPolicy.KEEP, worker);
                    workManager.enqueue(worker);
                } catch (Exception ignored) {}
            }*/

            //Intent intent = new Intent(context, NFCEventEndBroadcastReceiver.class);
            Intent intent = new Intent();
            intent.setAction(PhoneProfilesService.ACTION_NFC_EVENT_END_BROADCAST_RECEIVER);
            //intent.setClass(context, NFCEventEndBroadcastReceiver.class);

            //intent.putExtra(PPApplication.EXTRA_EVENT_ID, _event._id);

            PendingIntent pendingIntent = PendingIntent.getBroadcast(context, (int) _event._id, intent, PendingIntent.FLAG_UPDATE_CURRENT);

            AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
            if (alarmManager != null) {
                if (ApplicationPreferences.applicationUseAlarmClock) {
                    Intent editorIntent = new Intent(context, EditorProfilesActivity.class);
                    editorIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                    PendingIntent infoPendingIntent = PendingIntent.getActivity(context, 1000, editorIntent, PendingIntent.FLAG_UPDATE_CURRENT);
                    AlarmManager.AlarmClockInfo clockInfo = new AlarmManager.AlarmClockInfo(alarmTime + Event.EVENT_ALARM_TIME_SOFT_OFFSET, infoPendingIntent);
                    alarmManager.setAlarmClock(clockInfo, pendingIntent);
                }
                else {
                    //if (android.os.Build.VERSION.SDK_INT >= 23)
                        alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, alarmTime + Event.EVENT_ALARM_TIME_OFFSET, pendingIntent);
                    //else //if (android.os.Build.VERSION.SDK_INT >= 19)
                    //    alarmManager.setExact(AlarmManager.RTC_WAKEUP, alarmTime + Event.EVENT_ALARM_TIME_OFFSET, pendingIntent);
                    //else
                    //    alarmManager.set(AlarmManager.RTC_WAKEUP, alarmTime + Event.EVENT_ALARM_TIME_OFFSET, pendingIntent);
                }
            }
        }
    }
}
 
Example 11
Source File: AlarmController.java    From ClockPlus with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Schedules the alarm with the {@link AlarmManager}.
 * If {@code alarm.}{@link Alarm#isEnabled() isEnabled()}
 * returns false, this does nothing and returns immediately.
 * 
 * If there is already an alarm for this Intent scheduled (with the equality of two
 * intents being defined by filterEquals(Intent)), then it will be removed and replaced
 * by this one. For most of our uses, the relevant criteria for equality will be the
 * action, the data, and the class (component). Although not documented, the request code
 * of a PendingIntent is also considered to determine equality of two intents.
 */
public void scheduleAlarm(Alarm alarm, boolean showSnackbar) {
    if (!alarm.isEnabled()) {
        return;
    }
    // Does nothing if it's not posted. This is primarily here for when alarms
    // are updated, instead of newly created, so that we don't leave behind
    // stray upcoming alarm notifications. This occurs e.g. when a single-use
    // alarm is updated to recur on a weekday later than the current day.
    removeUpcomingAlarmNotification(alarm);

    AlarmManager am = (AlarmManager) mAppContext.getSystemService(Context.ALARM_SERVICE);

    final long ringAt = alarm.isSnoozed() ? alarm.snoozingUntil() : alarm.ringsAt();
    final PendingIntent alarmIntent = alarmIntent(alarm, false);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        PendingIntent showIntent = ContentIntentUtils.create(mAppContext, MainActivity.PAGE_ALARMS, alarm.getId());
        AlarmManager.AlarmClockInfo info = new AlarmManager.AlarmClockInfo(ringAt, showIntent);
        am.setAlarmClock(info, alarmIntent);
    } else {
        // WAKEUP alarm types wake the CPU up, but NOT the screen;
        // you would handle that yourself by using a wakelock, etc..
        am.setExact(AlarmManager.RTC_WAKEUP, ringAt, alarmIntent);
        // Show alarm in the status bar
        Intent alarmChanged = new Intent("android.intent.action.ALARM_CHANGED");
        alarmChanged.putExtra("alarmSet", true/*enabled*/);
        mAppContext.sendBroadcast(alarmChanged);
    }

    final int hoursToNotifyInAdvance = AlarmPreferences.hoursBeforeUpcoming(mAppContext);
    if (hoursToNotifyInAdvance > 0 || alarm.isSnoozed()) {
        // If snoozed, upcoming note posted immediately.
        long upcomingAt = ringAt - HOURS.toMillis(hoursToNotifyInAdvance);
        // We use a WAKEUP alarm to send the upcoming alarm notification so it goes off even if the
        // device is asleep. Otherwise, it will not go off until the device is turned back on.
        am.set(AlarmManager.RTC_WAKEUP, upcomingAt, notifyUpcomingAlarmIntent(alarm, false));
    }

    if (showSnackbar) {
        String message = mAppContext.getString(R.string.alarm_set_for,
                DurationUtils.toString(mAppContext, alarm.ringsIn(), false/*abbreviate*/));
        showSnackbar(message);
    }
}
 
Example 12
Source File: LockDeviceAfterScreenOffBroadcastReceiver.java    From PhoneProfilesPlus with Apache License 2.0 4 votes vote down vote up
@SuppressLint("NewApi")
static void setAlarm(int lockDelay, Context context)
{
    final Context appContext = context.getApplicationContext();

    if (ApplicationPreferences.applicationUseAlarmClock) {
        //Intent intent = new Intent(context, PostDelayedBroadcastReceiver.class);
        Intent intent = new Intent();
        intent.setAction(ACTION_LOCK_DEVICE_AFTER_SCREEN_OFF);
        //intent.setClass(context, PostDelayedBroadcastReceiver.class);

        PendingIntent pendingIntent = PendingIntent.getBroadcast(appContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        if (alarmManager != null) {
            Calendar now = Calendar.getInstance();
            now.add(Calendar.MILLISECOND, lockDelay);
            long alarmTime = now.getTimeInMillis();

            /*if (PPApplication.logEnabled()) {
                @SuppressLint("SimpleDateFormat")
                SimpleDateFormat sdf = new SimpleDateFormat("EE d.MM.yyyy HH:mm:ss:S");
                String result = sdf.format(alarmTime);
                PPApplication.logE("LockDeviceAfterScreenOffBroadcastReceiver.setAlarm", "alarmTime=" + result);
            }*/

            Intent editorIntent = new Intent(context, EditorProfilesActivity.class);
            editorIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
            PendingIntent infoPendingIntent = PendingIntent.getActivity(context, 1000, editorIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            AlarmManager.AlarmClockInfo clockInfo = new AlarmManager.AlarmClockInfo(alarmTime + Event.EVENT_ALARM_TIME_SOFT_OFFSET, infoPendingIntent);
            alarmManager.setAlarmClock(clockInfo, pendingIntent);
        }
    }
    else {
        Data workData = new Data.Builder()
                .putString(PhoneProfilesService.EXTRA_ELAPSED_ALARMS_WORK, ElapsedAlarmsWorker.ELAPSED_ALARMS_LOCK_DEVICE_AFTER_SCREEN_OFF)
                .build();

        OneTimeWorkRequest worker =
                new OneTimeWorkRequest.Builder(ElapsedAlarmsWorker.class)
                        .addTag(ElapsedAlarmsWorker.ELAPSED_ALARMS_LOCK_DEVICE_AFTER_SCREEN_OFF_TAG_WORK)
                        .setInputData(workData)
                        .setInitialDelay(lockDelay, TimeUnit.MILLISECONDS)
                        .build();
        try {
            if (PPApplication.getApplicationStarted(true)) {
                WorkManager workManager = PPApplication.getWorkManagerInstance();
                if (workManager != null) {
                    //PPApplication.logE("[HANDLER] LockDeviceAfterScreenOffBroadcastReceiver.setAlarm", "enqueueUniqueWork - lockDelay=" + lockDelay);
                    workManager.enqueueUniqueWork(ElapsedAlarmsWorker.ELAPSED_ALARMS_LOCK_DEVICE_AFTER_SCREEN_OFF_TAG_WORK, ExistingWorkPolicy.KEEP, worker);
                }
            }
        } catch (Exception e) {
            PPApplication.recordException(e);
        }
    }

    /*final Context appContext = context.getApplicationContext();

    //Intent intent = new Intent(context, PostDelayedBroadcastReceiver.class);
    Intent intent = new Intent();
    intent.setAction(ACTION_LOCK_DEVICE_AFTER_SCREEN_OFF);
    //intent.setClass(context, PostDelayedBroadcastReceiver.class);

    PendingIntent pendingIntent = PendingIntent.getBroadcast(appContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    AlarmManager alarmManager = (AlarmManager) appContext.getSystemService(Context.ALARM_SERVICE);
    if (alarmManager != null) {
        if (ApplicationPreferences.applicationUseAlarmClock(context)) {

            Calendar now = Calendar.getInstance();
            now.add(Calendar.MILLISECOND, lockDelay);
            long alarmTime = now.getTimeInMillis();

            if (PPApplication.logEnabled()) {
                @SuppressLint("SimpleDateFormat")
                SimpleDateFormat sdf = new SimpleDateFormat("EE d.MM.yyyy HH:mm:ss:S");
                String result = sdf.format(alarmTime);
                PPApplication.logE("LockDeviceAfterScreenOffBroadcastReceiver.setAlarm", "alarmTime=" + result);
            }

            Intent editorIntent = new Intent(context, EditorProfilesActivity.class);
            editorIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
            PendingIntent infoPendingIntent = PendingIntent.getActivity(context, 1000, editorIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            AlarmManager.AlarmClockInfo clockInfo = new AlarmManager.AlarmClockInfo(alarmTime + Event.EVENT_ALARM_TIME_SOFT_OFFSET, infoPendingIntent);
            alarmManager.setAlarmClock(clockInfo, pendingIntent);
        }
        else {
            long alarmTime = SystemClock.elapsedRealtime() + lockDelay;

            if (android.os.Build.VERSION.SDK_INT >= 23)
                alarmManager.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, alarmTime, pendingIntent);
            else //if (android.os.Build.VERSION.SDK_INT >= 19)
                alarmManager.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, alarmTime, pendingIntent);
            //else
            //    alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, delayTime, pendingIntent);
        }
    }
    */
}
 
Example 13
Source File: RNLocalNotificationsModule.java    From react-native-local-notifications with MIT License 4 votes vote down vote up
public void createAlarm(Integer id, String text, String datetime, String sound, boolean update, String hiddendata) {
    if(update){
        this.deleteAlarm(id);
    }

    final SimpleDateFormat desiredFormat = new SimpleDateFormat("yyyy-MM-dd kk:mm");
    Date dateToMillis = null;
    try {
        dateToMillis = desiredFormat.parse(datetime);
    } catch (ParseException e) {
        //TODO: if you want feedback...
        e.printStackTrace();
    }
    Long timeInMillis = dateToMillis.getTime();

    Intent intent = new Intent(reactContext, AlarmReceiver.class);
    intent.setAction("com.github.wumke.RNLocalNotifications.showAlarm");
    intent.putExtra("id", id);
    intent.putExtra("text", text);
    intent.putExtra("datetime", datetime);
    intent.putExtra("sound", sound);
    intent.putExtra("hiddendata", hiddendata);
    intent.putExtra("largeIconName", largeIconName);
    intent.putExtra("largeIconType", largeIconType);
    intent.putExtra("smallIconName", smallIconName);
    intent.putExtra("smallIconType", smallIconType);

    PendingIntent mAlarmSender = PendingIntent.getBroadcast(reactContext, id, intent, 0);

    Calendar date = Calendar.getInstance();
    if(timeInMillis > date.getTimeInMillis()) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { //API LVL 23, Android 6
            alarmManager.setExactAndAllowWhileIdle (AlarmManager.RTC_WAKEUP, timeInMillis, mAlarmSender);
        }
        else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { //API LVL 21, Android 5
            AlarmManager.AlarmClockInfo info = new AlarmManager.AlarmClockInfo(timeInMillis, mAlarmSender);
            alarmManager.setAlarmClock (info, mAlarmSender);

        }
        else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { //API LVL 19, Android 4.4
            alarmManager.setExact(AlarmManager.RTC_WAKEUP, timeInMillis, mAlarmSender);
        }
        else { //<19
            alarmManager.set(AlarmManager.RTC_WAKEUP, timeInMillis, mAlarmSender);
        }
    }
}
 
Example 14
Source File: AlarmManagerWork.java    From GPT with Apache License 2.0 4 votes vote down vote up
public void set(String callingPackage, int type, long triggerAtTime, long windowLength,
                long interval, int flags, PendingIntent operation, IAlarmListener listener,
                String listenerTag, WorkSource workSource, AlarmManager.AlarmClockInfo alarmClock) {
    mTarget.set(mHostContext.getPackageName(), type, triggerAtTime, windowLength,
            interval, flags, operation, listener, listenerTag, workSource, alarmClock);
}
 
Example 15
Source File: EventPreferencesCall.java    From PhoneProfilesPlus with Apache License 2.0 4 votes vote down vote up
@SuppressLint("NewApi")
private void setAlarm(long alarmTime, Context context) {
    if (!_permanentRun) {
        if (_startTime > 0) {
            /*if (PPApplication.logEnabled()) {
                @SuppressLint("SimpleDateFormat")
                SimpleDateFormat sdf = new SimpleDateFormat("EE d.MM.yyyy HH:mm:ss:S");
                String result = sdf.format(alarmTime);
                PPApplication.logE("EventPreferencesCall.setAlarm", "endTime=" + result);
            }*/

            /*if (ApplicationPreferences.applicationUseAlarmClock(context)) {
                //Intent intent = new Intent(context, MissedCallEventEndBroadcastReceiver.class);
                Intent intent = new Intent();
                intent.setAction(PhoneProfilesService.ACTION_MISSED_CALL_EVENT_END_BROADCAST_RECEIVER);
                //intent.setClass(context, MissedCallEventEndBroadcastReceiver.class);

                //intent.putExtra(PPApplication.EXTRA_EVENT_ID, _event._id);

                PendingIntent pendingIntent = PendingIntent.getBroadcast(context, (int) _event._id, intent, PendingIntent.FLAG_UPDATE_CURRENT);

                AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
                if (alarmManager != null) {
                    Intent editorIntent = new Intent(context, EditorProfilesActivity.class);
                    editorIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                    PendingIntent infoPendingIntent = PendingIntent.getActivity(context, 1000, editorIntent, PendingIntent.FLAG_UPDATE_CURRENT);
                    AlarmManager.AlarmClockInfo clockInfo = new AlarmManager.AlarmClockInfo(alarmTime + Event.EVENT_ALARM_TIME_SOFT_OFFSET, infoPendingIntent);
                    alarmManager.setAlarmClock(clockInfo, pendingIntent);
                }
            }
            else {
                Calendar now = Calendar.getInstance();
                long elapsedTime = (alarmTime + Event.EVENT_ALARM_TIME_OFFSET) - now.getTimeInMillis();

                if (PPApplication.logEnabled()) {
                    long allSeconds = elapsedTime / 1000;
                    long hours = allSeconds / 60 / 60;
                    long minutes = (allSeconds - (hours * 60 * 60)) / 60;
                    long seconds = allSeconds % 60;

                    PPApplication.logE("EventPreferencesCall.setAlarm", "elapsedTime=" + hours + ":" + minutes + ":" + seconds);
                }

                Data workData = new Data.Builder()
                        .putString(PhoneProfilesService.EXTRA_ELAPSED_ALARMS_WORK, ElapsedAlarmsWorker.ELAPSED_ALARMS_CALL_SENSOR)
                        .build();

                OneTimeWorkRequest worker =
                        new OneTimeWorkRequest.Builder(ElapsedAlarmsWorker.class)
                                .addTag("elapsedAlarmsCallSensorWork_"+(int)_event._id)
                                .setInputData(workData)
                                .setInitialDelay(elapsedTime, TimeUnit.MILLISECONDS)
                                .build();
                try {
                    WorkManager workManager = WorkManager.getInstance(context);
                    PPApplication.logE("[HANDLER] EventPreferencesCall.setAlarm", "enqueueUniqueWork - elapsedTime="+elapsedTime);
                    //workManager.enqueueUniqueWork("elapsedAlarmsCallSensorWork_"+(int)_event._id, ExistingWorkPolicy.KEEP, worker);
                    workManager.enqueue(worker);
                } catch (Exception ignored) {}
            }*/

            //Intent intent = new Intent(context, MissedCallEventEndBroadcastReceiver.class);
            Intent intent = new Intent();
            intent.setAction(PhoneProfilesService.ACTION_MISSED_CALL_EVENT_END_BROADCAST_RECEIVER);
            //intent.setClass(context, MissedCallEventEndBroadcastReceiver.class);

            //intent.putExtra(PPApplication.EXTRA_EVENT_ID, _event._id);

            PendingIntent pendingIntent = PendingIntent.getBroadcast(context, (int) _event._id, intent, PendingIntent.FLAG_UPDATE_CURRENT);

            AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
            if (alarmManager != null) {
                if (ApplicationPreferences.applicationUseAlarmClock) {
                    Intent editorIntent = new Intent(context, EditorProfilesActivity.class);
                    editorIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                    PendingIntent infoPendingIntent = PendingIntent.getActivity(context, 1000, editorIntent, PendingIntent.FLAG_UPDATE_CURRENT);
                    AlarmManager.AlarmClockInfo clockInfo = new AlarmManager.AlarmClockInfo(alarmTime + Event.EVENT_ALARM_TIME_SOFT_OFFSET, infoPendingIntent);
                    alarmManager.setAlarmClock(clockInfo, pendingIntent);
                }
                else {
                    //if (android.os.Build.VERSION.SDK_INT >= 23)
                        alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, alarmTime + Event.EVENT_ALARM_TIME_OFFSET, pendingIntent);
                    //else //if (android.os.Build.VERSION.SDK_INT >= 19)
                    //    alarmManager.setExact(AlarmManager.RTC_WAKEUP, alarmTime + Event.EVENT_ALARM_TIME_OFFSET, pendingIntent);
                    //else
                    //    alarmManager.set(AlarmManager.RTC_WAKEUP, alarmTime + Event.EVENT_ALARM_TIME_OFFSET, pendingIntent);
                }
            }
        }
    }
}
 
Example 16
Source File: ZenModeConfig.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
private static long getNextAlarm(Context context) {
    final AlarmManager alarms = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    final AlarmManager.AlarmClockInfo info = alarms.getNextAlarmClock(context.getUserId());
    return info != null ? info.getTriggerTime() : 0;
}
 
Example 17
Source File: EventPreferencesNotification.java    From PhoneProfilesPlus with Apache License 2.0 4 votes vote down vote up
@SuppressLint({"SimpleDateFormat", "NewApi"})
private void setAlarm(long alarmTime, Context context)
{
    if (alarmTime > 0) {
        /*if (PPApplication.logEnabled()) {
            SimpleDateFormat sdf = new SimpleDateFormat("EE d.MM.yyyy HH:mm:ss:S");
            String result = sdf.format(alarmTime);
            PPApplication.logE("EventPreferencesNotification.setAlarm", "endTime=" + result);
        }*/

        /*if (ApplicationPreferences.applicationUseAlarmClock(context)) {
            //Intent intent = new Intent(context, NotificationEventEndBroadcastReceiver.class);
            Intent intent = new Intent();
            intent.setAction(PhoneProfilesService.ACTION_NOTIFICATION_EVENT_END_BROADCAST_RECEIVER);
            //intent.setClass(context, NotificationEventEndBroadcastReceiver.class);

            //intent.putExtra(PPApplication.EXTRA_EVENT_ID, _event._id);

            PendingIntent pendingIntent = PendingIntent.getBroadcast(context, (int) _event._id, intent, PendingIntent.FLAG_UPDATE_CURRENT);

            AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
            if (alarmManager != null) {
                Intent editorIntent = new Intent(context, EditorProfilesActivity.class);
                editorIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                PendingIntent infoPendingIntent = PendingIntent.getActivity(context, 1000, editorIntent, PendingIntent.FLAG_UPDATE_CURRENT);
                AlarmManager.AlarmClockInfo clockInfo = new AlarmManager.AlarmClockInfo(alarmTime + Event.EVENT_ALARM_TIME_SOFT_OFFSET, infoPendingIntent);
                alarmManager.setAlarmClock(clockInfo, pendingIntent);

            }
        } else {
            Calendar now = Calendar.getInstance();
            long elapsedTime = (alarmTime + Event.EVENT_ALARM_TIME_OFFSET) - now.getTimeInMillis();

            if (PPApplication.logEnabled()) {
                long allSeconds = elapsedTime / 1000;
                long hours = allSeconds / 60 / 60;
                long minutes = (allSeconds - (hours * 60 * 60)) / 60;
                long seconds = allSeconds % 60;

                PPApplication.logE("EventPreferencesNotification.setAlarm", "elapsedTime=" + hours + ":" + minutes + ":" + seconds);
            }

            Data workData = new Data.Builder()
                    .putString(PhoneProfilesService.EXTRA_ELAPSED_ALARMS_WORK, ElapsedAlarmsWorker.ELAPSED_ALARMS_NOTIFICATION_EVENT_END_SENSOR)
                    .build();

            OneTimeWorkRequest worker =
                    new OneTimeWorkRequest.Builder(ElapsedAlarmsWorker.class)
                            .addTag("elapsedAlarmsNotificationSensorWork_"+(int)_event._id)
                            .setInputData(workData)
                            .setInitialDelay(elapsedTime, TimeUnit.MILLISECONDS)
                            .build();
            try {
                WorkManager workManager = WorkManager.getInstance(context);
                PPApplication.logE("[HANDLER] EventPreferencesNotification.setAlarm", "enqueueUniqueWork - elapsedTime="+elapsedTime);
                //workManager.enqueueUniqueWork("elapsedAlarmsNotificationSensorWork_"+(int)_event._id, ExistingWorkPolicy.KEEP, worker);
                workManager.enqueue(worker);
            } catch (Exception ignored) {}
        }*/

        //Intent intent = new Intent(context, NotificationEventEndBroadcastReceiver.class);
        Intent intent = new Intent();
        intent.setAction(PhoneProfilesService.ACTION_NOTIFICATION_EVENT_END_BROADCAST_RECEIVER);
        //intent.setClass(context, NotificationEventEndBroadcastReceiver.class);

        //intent.putExtra(PPApplication.EXTRA_EVENT_ID, _event._id);

        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, (int) _event._id, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        if (alarmManager != null) {
            if (ApplicationPreferences.applicationUseAlarmClock) {
                Intent editorIntent = new Intent(context, EditorProfilesActivity.class);
                editorIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                PendingIntent infoPendingIntent = PendingIntent.getActivity(context, 1000, editorIntent, PendingIntent.FLAG_UPDATE_CURRENT);
                AlarmManager.AlarmClockInfo clockInfo = new AlarmManager.AlarmClockInfo(alarmTime + Event.EVENT_ALARM_TIME_SOFT_OFFSET, infoPendingIntent);
                alarmManager.setAlarmClock(clockInfo, pendingIntent);
            }
            else {
                //if (android.os.Build.VERSION.SDK_INT >= 23)
                    alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, alarmTime + Event.EVENT_ALARM_TIME_OFFSET, pendingIntent);
                //else //if (android.os.Build.VERSION.SDK_INT >= 19)
                //    alarmManager.setExact(AlarmManager.RTC_WAKEUP, alarmTime + Event.EVENT_ALARM_TIME_OFFSET, pendingIntent);
                //else
                //    alarmManager.set(AlarmManager.RTC_WAKEUP, alarmTime + Event.EVENT_ALARM_TIME_OFFSET, pendingIntent);
            }
        }
    }
}
 
Example 18
Source File: AlarmManagerService.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
AlarmManager.AlarmClockInfo getNextAlarmClockImpl(int userId) {
    synchronized (mLock) {
        return mNextAlarmClockForUser.get(userId);
    }
}
 
Example 19
Source File: EventPreferencesDeviceBoot.java    From PhoneProfilesPlus with Apache License 2.0 4 votes vote down vote up
@SuppressLint({"SimpleDateFormat", "NewApi"})
private void setAlarm(long alarmTime, Context context)
{
    //PPApplication.logE("[BOOT] EventPreferencesDeviceBoot.setAlarm","_permanentRun="+_permanentRun);
    //PPApplication.logE("[BOOT] EventPreferencesDeviceBoot.setAlarm","_startTime="+_startTime);

    if (!_permanentRun) {
        if (_startTime > 0) {
            /*if (PPApplication.logEnabled()) {
                SimpleDateFormat sdf = new SimpleDateFormat("EE d.MM.yyyy HH:mm:ss:S");
                String result = sdf.format(alarmTime);
                PPApplication.logE("[BOOT] EventPreferencesDeviceBoot.setAlarm", "endTime=" + result);
            }*/

            /*if (ApplicationPreferences.applicationUseAlarmClock(context)) {
                Intent intent = new Intent();
                intent.setAction(PhoneProfilesService.ACTION_DEVICE_BOOT_EVENT_END_BROADCAST_RECEIVER);

                //intent.putExtra(PPApplication.EXTRA_EVENT_ID, _event._id);

                PendingIntent pendingIntent = PendingIntent.getBroadcast(context, (int) _event._id, intent, PendingIntent.FLAG_UPDATE_CURRENT);

                AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
                if (alarmManager != null) {
                    Intent editorIntent = new Intent(context, EditorProfilesActivity.class);
                    editorIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                    PendingIntent infoPendingIntent = PendingIntent.getActivity(context, 1000, editorIntent, PendingIntent.FLAG_UPDATE_CURRENT);
                    AlarmManager.AlarmClockInfo clockInfo = new AlarmManager.AlarmClockInfo(alarmTime + Event.EVENT_ALARM_TIME_SOFT_OFFSET, infoPendingIntent);
                    alarmManager.setAlarmClock(clockInfo, pendingIntent);
                }
            }
            else {
                Calendar now = Calendar.getInstance();
                long elapsedTime = (alarmTime + Event.EVENT_ALARM_TIME_OFFSET) - now.getTimeInMillis();

                if (PPApplication.logEnabled()) {
                    long allSeconds = elapsedTime / 1000;
                    long hours = allSeconds / 60 / 60;
                    long minutes = (allSeconds - (hours * 60 * 60)) / 60;
                    long seconds = allSeconds % 60;

                    PPApplication.logE("EventPreferencesDeviceBoot.setAlarm", "elapsedTime=" + hours + ":" + minutes + ":" + seconds);
                }

                Data workData = new Data.Builder()
                        .putString(PhoneProfilesService.EXTRA_ELAPSED_ALARMS_WORK, ElapsedAlarmsWorker.ELAPSED_ALARMS_DEVICE_BOOT_EVENT_END_SENSOR)
                        .build();

                OneTimeWorkRequest worker =
                        new OneTimeWorkRequest.Builder(ElapsedAlarmsWorker.class)
                                .addTag("elapsedAlarmsDeviceBootSensorWork_"+(int)_event._id)
                                .setInputData(workData)
                                .setInitialDelay(elapsedTime, TimeUnit.MILLISECONDS)
                                .build();
                try {
                    WorkManager workManager = WorkManager.getInstance(context);
                    PPApplication.logE("[HANDLER] EventPreferencesDeviceBoot.setAlarm", "enqueueUniqueWork - elapsedTime="+elapsedTime);
                    //workManager.enqueueUniqueWork("elapsedAlarmsDeviceBootSensorWork_"+(int)_event._id, ExistingWorkPolicy.KEEP, worker);
                    workManager.enqueue(worker);
                } catch (Exception ignored) {}
            }*/

            Intent intent = new Intent();
            intent.setAction(PhoneProfilesService.ACTION_DEVICE_BOOT_EVENT_END_BROADCAST_RECEIVER);

            //intent.putExtra(PPApplication.EXTRA_EVENT_ID, _event._id);

            PendingIntent pendingIntent = PendingIntent.getBroadcast(context, (int) _event._id, intent, PendingIntent.FLAG_UPDATE_CURRENT);

            AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
            if (alarmManager != null) {
                if (ApplicationPreferences.applicationUseAlarmClock) {
                    Intent editorIntent = new Intent(context, EditorProfilesActivity.class);
                    editorIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                    PendingIntent infoPendingIntent = PendingIntent.getActivity(context, 1000, editorIntent, PendingIntent.FLAG_UPDATE_CURRENT);
                    AlarmManager.AlarmClockInfo clockInfo = new AlarmManager.AlarmClockInfo(alarmTime + Event.EVENT_ALARM_TIME_SOFT_OFFSET, infoPendingIntent);
                    alarmManager.setAlarmClock(clockInfo, pendingIntent);
                }
                else {
                    //if (android.os.Build.VERSION.SDK_INT >= 23)
                        alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, alarmTime + Event.EVENT_ALARM_TIME_OFFSET, pendingIntent);
                    //else //if (android.os.Build.VERSION.SDK_INT >= 19)
                    //    alarmManager.setExact(AlarmManager.RTC_WAKEUP, alarmTime + Event.EVENT_ALARM_TIME_OFFSET, pendingIntent);
                    //else
                    //    alarmManager.set(AlarmManager.RTC_WAKEUP, alarmTime + Event.EVENT_ALARM_TIME_OFFSET, pendingIntent);
                }
            }
        }
    }
}
 
Example 20
Source File: LockDeviceActivityFinishBroadcastReceiver.java    From PhoneProfilesPlus with Apache License 2.0 4 votes vote down vote up
@SuppressLint({"SimpleDateFormat", "NewApi"})
static void setAlarm(Context context)
{
    removeAlarm(context);

    int delay = 20; // 20 seconds

    if (ApplicationPreferences.applicationUseAlarmClock) {
        //Intent intent = new Intent(_context, LockDeviceActivityFinishBroadcastReceiver.class);
        Intent intent = new Intent();
        intent.setAction(PhoneProfilesService.ACTION_LOCK_DEVICE_ACTIVITY_FINISH_BROADCAST_RECEIVER);
        //intent.setClass(context, LockDeviceActivityFinishBroadcastReceiver.class);

        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        if (alarmManager != null) {
            Calendar now = Calendar.getInstance();
            now.add(Calendar.SECOND, delay);
            long alarmTime = now.getTimeInMillis();

            /*if (PPApplication.logEnabled()) {
                SimpleDateFormat sdf = new SimpleDateFormat("EE d.MM.yyyy HH:mm:ss:S");
                String result = sdf.format(alarmTime);
                PPApplication.logE("[HANDLER] LockDeviceActivityFinishBroadcastReceiver.setAlarm", "alarmTime=" + result);
            }*/

            Intent editorIntent = new Intent(context, EditorProfilesActivity.class);
            editorIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
            PendingIntent infoPendingIntent = PendingIntent.getActivity(context, 1000, editorIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            AlarmManager.AlarmClockInfo clockInfo = new AlarmManager.AlarmClockInfo(alarmTime, infoPendingIntent);
            alarmManager.setAlarmClock(clockInfo, pendingIntent);
        }
    }
    else {
        Data workData = new Data.Builder()
                .putString(PhoneProfilesService.EXTRA_ELAPSED_ALARMS_WORK, ElapsedAlarmsWorker.ELAPSED_ALARMS_LOCK_DEVICE_FINISH_ACTIVITY)
                .build();

        OneTimeWorkRequest worker =
                new OneTimeWorkRequest.Builder(ElapsedAlarmsWorker.class)
                        .addTag(ElapsedAlarmsWorker.ELAPSED_ALARMS_LOCK_DEVICE_FINISH_ACTIVITY_TAG_WORK)
                        .setInputData(workData)
                        .setInitialDelay(delay, TimeUnit.SECONDS)
                        .build();
        try {
            if (PPApplication.getApplicationStarted(true)) {
                WorkManager workManager = PPApplication.getWorkManagerInstance();
                if (workManager != null) {
                    //PPApplication.logE("[HANDLER] LockDeviceActivityFinishBroadcastReceiver.setAlarm", "enqueueUniqueWork - alarmTime=" + delay);
                    workManager.enqueueUniqueWork(ElapsedAlarmsWorker.ELAPSED_ALARMS_LOCK_DEVICE_FINISH_ACTIVITY_TAG_WORK, ExistingWorkPolicy.KEEP, worker);
                }
            }
        } catch (Exception e) {
            PPApplication.recordException(e);
        }
    }

    /*//Intent intent = new Intent(_context, LockDeviceActivityFinishBroadcastReceiver.class);
    Intent intent = new Intent();
    intent.setAction(PhoneProfilesService.ACTION_LOCK_DEVICE_ACTIVITY_FINISH_BROADCAST_RECEIVER);
    //intent.setClass(context, LockDeviceActivityFinishBroadcastReceiver.class);

    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    if (alarmManager != null) {
        if (ApplicationPreferences.applicationUseAlarmClock(context)) {

            Calendar now = Calendar.getInstance();
            now.add(Calendar.SECOND, delay);
            long alarmTime = now.getTimeInMillis();

            if (PPApplication.logEnabled()) {
                SimpleDateFormat sdf = new SimpleDateFormat("EE d.MM.yyyy HH:mm:ss:S");
                String result = sdf.format(alarmTime);
                PPApplication.logE("LockDeviceActivityFinishBroadcastReceiver.setAlarm", "alarmTime=" + result);
            }

            Intent editorIntent = new Intent(context, EditorProfilesActivity.class);
            editorIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
            PendingIntent infoPendingIntent = PendingIntent.getActivity(context, 1000, editorIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            AlarmManager.AlarmClockInfo clockInfo = new AlarmManager.AlarmClockInfo(alarmTime, infoPendingIntent);
            alarmManager.setAlarmClock(clockInfo, pendingIntent);
        }
        else {
            long alarmTime = SystemClock.elapsedRealtime() + delay * 1000;

            if (android.os.Build.VERSION.SDK_INT >= 23)
                alarmManager.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, alarmTime, pendingIntent);
            else //if (android.os.Build.VERSION.SDK_INT >= 19)
                alarmManager.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, alarmTime, pendingIntent);
            //else
            //    alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, alarmTime, pendingIntent);
        }
    }
    */
}