Search in sources :

Example 21 with AlarmManager

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

the class BluetoothDiscoverableTimeoutReceiver method cancelDiscoverableAlarm.

public static void cancelDiscoverableAlarm(Context context) {
    Log.d(TAG, "cancelDiscoverableAlarm(): Enter");
    Intent intent = new Intent(INTENT_DISCOVERABLE_TIMEOUT);
    intent.setClass(context, BluetoothDiscoverableTimeoutReceiver.class);
    PendingIntent pending = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_NO_CREATE);
    if (pending != null) {
        // Cancel any previous alarms that do the same thing.
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        alarmManager.cancel(pending);
    }
}
Also used : AlarmManager(android.app.AlarmManager) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) PendingIntent(android.app.PendingIntent)

Example 22 with AlarmManager

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

the class NetworkStatsServiceTest method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    mServiceContext = new BroadcastInterceptingContext(getContext());
    mStatsDir = getContext().getFilesDir();
    if (mStatsDir.exists()) {
        IoUtils.deleteContents(mStatsDir);
    }
    mNetManager = createMock(INetworkManagementService.class);
    // TODO: Mock AlarmManager when migrating this test to Mockito.
    AlarmManager alarmManager = (AlarmManager) mServiceContext.getSystemService(Context.ALARM_SERVICE);
    mTime = createMock(TrustedTime.class);
    mSettings = createMock(NetworkStatsSettings.class);
    mConnManager = createMock(IConnectivityManager.class);
    PowerManager powerManager = (PowerManager) mServiceContext.getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
    mService = new NetworkStatsService(mServiceContext, mNetManager, alarmManager, wakeLock, mTime, TelephonyManager.getDefault(), mSettings, new NetworkStatsObservers(), mStatsDir, getBaseDir(mStatsDir));
    mHandlerThread = new IdleableHandlerThread("HandlerThread");
    mHandlerThread.start();
    Handler.Callback callback = new NetworkStatsService.HandlerCallback(mService);
    mHandler = new Handler(mHandlerThread.getLooper(), callback);
    mService.setHandler(mHandler, callback);
    mService.bindConnectivityManager(mConnManager);
    mElapsedRealtime = 0L;
    expectCurrentTime();
    expectDefaultSettings();
    expectNetworkStatsUidDetail(buildEmptyStats());
    expectSystemReady();
    // catch INetworkManagementEventObserver during systemReady()
    final Capture<INetworkManagementEventObserver> networkObserver = new Capture<INetworkManagementEventObserver>();
    mNetManager.registerObserver(capture(networkObserver));
    expectLastCall().atLeastOnce();
    replay();
    mService.systemReady();
    mSession = mService.openSession();
    verifyAndReset();
    mNetworkObserver = networkObserver.getValue();
}
Also used : TrustedTime(android.util.TrustedTime) INetworkManagementService(android.os.INetworkManagementService) BroadcastInterceptingContext(com.android.server.BroadcastInterceptingContext) Handler(android.os.Handler) IdleHandler(android.os.MessageQueue.IdleHandler) Capture(org.easymock.Capture) NetworkStatsSettings(com.android.server.net.NetworkStatsService.NetworkStatsSettings) PowerManager(android.os.PowerManager) NetworkStatsService(com.android.server.net.NetworkStatsService) IAlarmManager(android.app.IAlarmManager) AlarmManager(android.app.AlarmManager) INetworkManagementEventObserver(android.net.INetworkManagementEventObserver) IConnectivityManager(android.net.IConnectivityManager)

Example 23 with AlarmManager

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

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 24 with AlarmManager

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

the class CountdownConditionProvider method onSubscribe.

@Override
public void onSubscribe(Uri conditionId) {
    if (DEBUG)
        Slog.d(TAG, "onSubscribe " + conditionId);
    mTime = ZenModeConfig.tryParseCountdownConditionId(conditionId);
    final AlarmManager alarms = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
    final Intent intent = new Intent(ACTION).putExtra(EXTRA_CONDITION_ID, conditionId).setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
    final PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    alarms.cancel(pendingIntent);
    if (mTime > 0) {
        final long now = System.currentTimeMillis();
        final CharSequence span = DateUtils.getRelativeTimeSpanString(mTime, now, DateUtils.MINUTE_IN_MILLIS);
        if (mTime <= now) {
            // in the past, already false
            notifyCondition(newCondition(mTime, Condition.STATE_FALSE));
        } else {
            // in the future, set an alarm
            alarms.setExact(AlarmManager.RTC_WAKEUP, mTime, pendingIntent);
        }
        if (DEBUG)
            Slog.d(TAG, String.format("%s %s for %s, %s in the future (%s), now=%s", (mTime <= now ? "Not scheduling" : "Scheduling"), ACTION, ts(mTime), mTime - now, span, ts(now)));
    }
}
Also used : AlarmManager(android.app.AlarmManager) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) PendingIntent(android.app.PendingIntent)

Example 25 with AlarmManager

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

the class EventConditionProvider method rescheduleAlarm.

private void rescheduleAlarm(long now, long time) {
    mNextAlarmTime = time;
    final AlarmManager alarms = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
    final PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, REQUEST_CODE_EVALUATE, new Intent(ACTION_EVALUATE).addFlags(Intent.FLAG_RECEIVER_FOREGROUND).putExtra(EXTRA_TIME, time), PendingIntent.FLAG_UPDATE_CURRENT);
    alarms.cancel(pendingIntent);
    if (time == 0 || time < now) {
        if (DEBUG)
            Slog.d(TAG, "Not scheduling evaluate: " + (time == 0 ? "no time specified" : "specified time in the past"));
        return;
    }
    if (DEBUG)
        Slog.d(TAG, String.format("Scheduling evaluate for %s, in %s, now=%s", ts(time), formatDuration(time - now), ts(now)));
    alarms.setExact(AlarmManager.RTC_WAKEUP, time, pendingIntent);
}
Also used : AlarmManager(android.app.AlarmManager) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) PendingIntent(android.app.PendingIntent)

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