Search in sources :

Example 76 with AlarmManager

use of android.app.AlarmManager in project android_frameworks_base by crdroidandroid.

the class WakeLoopService method onStartCommand.

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // get wakeup interval from intent
    long wakeupInterval = intent.getLongExtra(WAKEUP_INTERNAL, 0);
    long maxLoop = intent.getLongExtra(MAX_LOOP, 0);
    if (wakeupInterval == 0) {
        // stop and error
        Log.e(LOG_TAG, "No wakeup interval specified, not starting the service");
        stopSelf();
        return START_NOT_STICKY;
    }
    FileUtil.get().writeDateToFile(new File(Environment.getExternalStorageDirectory(), "wakeup-loop-start.txt"));
    Log.d(LOG_TAG, String.format("WakeLoop: STARTED interval = %d, total loop = %d", wakeupInterval, maxLoop));
    // calculate when device should be waken up
    long atTime = SystemClock.elapsedRealtime() + wakeupInterval;
    AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    Intent wakupIntent = new Intent(WakeUpCall.WAKEUP_CALL).putExtra(WAKEUP_INTERNAL, wakeupInterval).putExtra(MAX_LOOP, maxLoop).putExtra(THIS_LOOP, 0L).putExtra(STOP_CALLBACK, new Messenger(mHandler));
    PendingIntent pi = PendingIntent.getBroadcast(this, 0, wakupIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    // set alarm, which will be delivered in form of the wakeupIntent
    am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, atTime, pi);
    return START_NOT_STICKY;
}
Also used : AlarmManager(android.app.AlarmManager) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) Messenger(android.os.Messenger) PendingIntent(android.app.PendingIntent) File(java.io.File)

Example 77 with AlarmManager

use of android.app.AlarmManager in project android_frameworks_base by crdroidandroid.

the class WakeUpCall method onReceive.

@Override
public void onReceive(Context context, Intent intent) {
    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    boolean cancel = intent.hasExtra(CANCEL);
    if (!cancel) {
        long maxLoop = intent.getLongExtra(WakeLoopService.MAX_LOOP, 0);
        long wakeupInterval = intent.getLongExtra(WakeLoopService.WAKEUP_INTERNAL, 0);
        long thisLoop = intent.getLongExtra(WakeLoopService.THIS_LOOP, -1);
        Log.d(LOG_TAG, String.format("incoming: interval = %d, max loop = %d, this loop = %d", wakeupInterval, maxLoop, thisLoop));
        if (thisLoop == -1) {
            Log.e(LOG_TAG, "no valid loop count received, trying to stop service");
            stopService(intent);
            return;
        }
        if (wakeupInterval == 0) {
            Log.e(LOG_TAG, "no valid wakeup interval received, trying to stop service");
            stopService(intent);
            return;
        }
        thisLoop++;
        Log.d(LOG_TAG, String.format("WakeLoop - iteration %d of %d", thisLoop, maxLoop));
        if (thisLoop == maxLoop) {
            // when maxLoop is 0, we loop forever, so not checking that case
            // here
            Log.d(LOG_TAG, "reached max loop count, stopping service");
            stopService(intent);
            return;
        }
        screenOn(context);
        FileUtil.get().writeDateToFile(new File(Environment.getExternalStorageDirectory(), "wakeup-loop.txt"));
        // calculate when device should be waken up
        long atTime = SystemClock.elapsedRealtime() + wakeupInterval;
        intent.putExtra(WakeLoopService.THIS_LOOP, thisLoop);
        PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        // set alarm, which will be delivered in form of the wakeupIntent
        am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, atTime, pi);
    } else {
        // cancel alarms
        Log.d(LOG_TAG, "cancelling future alarms on request");
        am.cancel(PendingIntent.getBroadcast(context, 0, intent, 0));
    }
}
Also used : AlarmManager(android.app.AlarmManager) PendingIntent(android.app.PendingIntent) File(java.io.File)

Example 78 with AlarmManager

use of android.app.AlarmManager in project android_frameworks_base by crdroidandroid.

the class DaylightHeaderProvider method stopAlarm.

private void stopAlarm() {
    if (mAlarmIntent != null) {
        final AlarmManager alarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
        if (DEBUG)
            Log.i(TAG, "stop alarm");
        alarmManager.cancel(mAlarmIntent);
    }
    mAlarmIntent = null;
}
Also used : AlarmManager(android.app.AlarmManager)

Example 79 with AlarmManager

use of android.app.AlarmManager in project android_frameworks_base by crdroidandroid.

the class DaylightHeaderProvider method startAlarm.

private void startAlarm() {
    // TODO actually this should find out the next needed alarm
    // instead of forcing it every interval
    final Calendar c = Calendar.getInstance();
    final AlarmManager alarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
    if (mAlarmIntent != null) {
        alarmManager.cancel(mAlarmIntent);
    }
    Intent intent = new Intent(StatusBarHeaderMachine.STATUS_BAR_HEADER_UPDATE_ACTION);
    mAlarmIntent = PendingIntent.getBroadcast(mContext, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
    // make sure hourly alarm is aligned with hour
    if (mAlarmIntervalMinutes == 0) {
        c.add(Calendar.HOUR_OF_DAY, 1);
        c.set(Calendar.MINUTE, 0);
    }
    long alarmStart = c.getTimeInMillis();
    long interval = mAlarmIntervalMinutes == 0 ? AlarmManager.INTERVAL_HOUR : mAlarmIntervalMinutes * 60 * 1000;
    if (DEBUG)
        Log.i(TAG, "start alarm at " + new Date(alarmStart) + " with interval of " + interval);
    alarmManager.setInexactRepeating(AlarmManager.RTC, alarmStart, interval, mAlarmIntent);
}
Also used : Calendar(java.util.Calendar) AlarmManager(android.app.AlarmManager) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) Date(java.util.Date)

Example 80 with AlarmManager

use of android.app.AlarmManager in project android_frameworks_base by crdroidandroid.

the class NetworkStatsService method create.

public static NetworkStatsService create(Context context, INetworkManagementService networkManager) {
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
    NetworkStatsService service = new NetworkStatsService(context, networkManager, alarmManager, wakeLock, NtpTrustedTime.getInstance(context), TelephonyManager.getDefault(), new DefaultNetworkStatsSettings(context), new NetworkStatsObservers(), getDefaultSystemDir(), getDefaultBaseDir());
    HandlerThread handlerThread = new HandlerThread(TAG);
    Handler.Callback callback = new HandlerCallback(service);
    handlerThread.start();
    Handler handler = new Handler(handlerThread.getLooper(), callback);
    service.setHandler(handler, callback);
    HandlerThread mStatsThread = new HandlerThread("StatsObserver");
    mStatsThread.start();
    Handler mStatsHandler = new Handler(mStatsThread.getLooper());
    return service;
}
Also used : PowerManager(android.os.PowerManager) HandlerThread(android.os.HandlerThread) INetworkStatsService(android.net.INetworkStatsService) IAlarmManager(android.app.IAlarmManager) AlarmManager(android.app.AlarmManager) Handler(android.os.Handler)

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