use of android.app.AlarmManager in project android_frameworks_base by DirtyUnicorns.
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 = mInjector.getAlarmManager();
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 OneSignal-Android-SDK by OneSignal.
the class SyncService method scheduleServiceSyncTask.
static void scheduleServiceSyncTask(Context context, long atTime) {
OneSignal.Log(OneSignal.LOG_LEVEL.VERBOSE, "scheduleServiceSyncTask:atTime: " + atTime);
Intent intent = new Intent(context, SyncService.class);
intent.putExtra("task", TASK_SYNC);
// KEEP - PendingIntent.FLAG_UPDATE_CURRENT
// Some Samsung devices will throw the below exception otherwise.
// "java.lang.SecurityException: !@Too many alarms (500) registered"
PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarm.set(AlarmManager.RTC_WAKEUP, atTime, pendingIntent);
}
use of android.app.AlarmManager in project OneSignal-Android-SDK by OneSignal.
the class MainOneSignalClassRunner method testLocationFromSyncAlarm.
@Test
@Config(shadows = { ShadowGoogleApiClientBuilder.class, ShadowGoogleApiClientCompatProxy.class, ShadowFusedLocationApiWrapper.class })
public void testLocationFromSyncAlarm() throws Exception {
ShadowFusedLocationApiWrapper.lat = 1.0d;
ShadowFusedLocationApiWrapper.log = 2.0d;
ShadowFusedLocationApiWrapper.accuracy = 3.0f;
ShadowFusedLocationApiWrapper.time = 12345L;
OneSignalInit();
threadAndTaskWait();
StaticResetHelper.restSetStaticFields();
AlarmManager alarmManager = (AlarmManager) RuntimeEnvironment.application.getSystemService(Context.ALARM_SERVICE);
shadowOf(alarmManager).getScheduledAlarms().clear();
ShadowOneSignalRestClient.lastPost = null;
ShadowApplication.getInstance().grantPermissions("android.permission.ACCESS_COARSE_LOCATION");
Intent intent = new Intent();
// Sync
intent.putExtra("task", 1);
Robolectric.buildService(SyncService.class, intent).startCommand(0, 0);
threadAndTaskWait();
Assert.assertEquals(1.0, ShadowOneSignalRestClient.lastPost.optDouble("lat"));
Assert.assertEquals(2.0, ShadowOneSignalRestClient.lastPost.optDouble("long"));
Assert.assertEquals(3.0, ShadowOneSignalRestClient.lastPost.optDouble("loc_acc"));
Assert.assertEquals(0, ShadowOneSignalRestClient.lastPost.optInt("loc_type"));
Assert.assertEquals(true, ShadowOneSignalRestClient.lastPost.opt("loc_bg"));
// Checking make sure an update is scheduled.
alarmManager = (AlarmManager) RuntimeEnvironment.application.getSystemService(Context.ALARM_SERVICE);
Assert.assertEquals(1, shadowOf(alarmManager).getScheduledAlarms().size());
intent = shadowOf(shadowOf(alarmManager).getNextScheduledAlarm().operation).getSavedIntent();
Assert.assertEquals(SyncService.class, shadowOf(intent).getIntentClass());
shadowOf(alarmManager).getScheduledAlarms().clear();
}
use of android.app.AlarmManager in project android_frameworks_base by DirtyUnicorns.
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();
}
use of android.app.AlarmManager in project android_frameworks_base by DirtyUnicorns.
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));
}
}
Aggregations