@@ -250,12 +250,13 @@ private void onNewAppLaunched(String packageName) {
250250 /// Return if no restriction applied
251251 AppRestrictions appRestrictions = mAppsRestrictions .get (packageName );
252252 if (appRestrictions == null ) return ;
253- long recallDelayMS = Long . MAX_VALUE ;
254- PurgedReason recallReason = null ;
255- boolean isPeriodRecall = false ;
253+ PurgedReason timerReason = null ;
254+ boolean isActivePeriodTimer = false ;
255+ long timerDelayMS = Long . MAX_VALUE ;
256256
257257 /// Check for app launch limit
258258 if (appRestrictions .launchLimit > 0 && launchCount > appRestrictions .launchLimit ) {
259+ Log .d (TAG , "onNewAppLaunched: App's launch limit ran out" );
259260 PurgedReason reason = new PurgedReason (getString (R .string .app_paused_dialog_info_for_launch_count_out ));
260261 mPurgedApps .put (packageName , reason );
261262 showOverlayDialog (packageName , reason );
@@ -268,17 +269,17 @@ private void onNewAppLaunched(String packageName) {
268269
269270 /// Outside active period
270271 if (Utils .isTimeOutsideTODs (appRestrictions .activePeriodStart , appRestrictions .activePeriodEnd )) {
271- mPurgedApps . put ( packageName , reason );
272+ Log . d ( TAG , "onNewAppLaunched: App's active period is over" );
272273 showOverlayDialog (packageName , reason );
273274 return ;
274275 }
275276
276277 /// Between active period so update recall delay
277278 long willOverInMs = Utils .todDifferenceFromNow (appRestrictions .activePeriodEnd );
278- if (willOverInMs < recallDelayMS ) {
279- recallDelayMS = willOverInMs ;
280- isPeriodRecall = true ;
281- recallReason = reason ;
279+ if (willOverInMs < timerDelayMS ) {
280+ timerReason = reason ;
281+ isActivePeriodTimer = true ;
282+ timerDelayMS = willOverInMs ;
282283 }
283284 }
284285
@@ -291,6 +292,7 @@ private void onNewAppLaunched(String packageName) {
291292
292293 /// App timer ran out
293294 if (appScreenTimeSec >= appRestrictions .timerSec ) {
295+ Log .d (TAG , "onNewAppLaunched: App's timer is over" );
294296 PurgedReason reason = new PurgedReason (getString (R .string .app_paused_dialog_info_for_app_timer_out ), appRestrictions .timerSec , appScreenTimeSec );
295297 mPurgedApps .put (packageName , reason );
296298 showOverlayDialog (packageName , reason );
@@ -299,10 +301,10 @@ private void onNewAppLaunched(String packageName) {
299301
300302 /// App timer left so update recall delay
301303 long leftAppLimitMs = (appRestrictions .timerSec - appScreenTimeSec ) * 1000 ;
302- if (leftAppLimitMs < recallDelayMS ) {
303- recallDelayMS = leftAppLimitMs ;
304- recallReason = new PurgedReason ( getString ( R . string . app_paused_dialog_info_for_app_timer_left ), appRestrictions . timerSec , appScreenTimeSec ) ;
305- isPeriodRecall = false ;
304+ if (leftAppLimitMs < timerDelayMS ) {
305+ timerReason = new PurgedReason ( getString ( R . string . app_paused_dialog_info_for_app_timer_left ), appRestrictions . timerSec , appScreenTimeSec ) ;
306+ isActivePeriodTimer = false ;
307+ timerDelayMS = leftAppLimitMs ;
306308 }
307309 }
308310
@@ -316,17 +318,17 @@ private void onNewAppLaunched(String packageName) {
316318 PurgedReason reason = new PurgedReason (getString (R .string .group_paused_dialog_info_for_active_period_over , associatedGroup .groupName ));
317319 /// Outside active period
318320 if (Utils .isTimeOutsideTODs (associatedGroup .activePeriodStart , associatedGroup .activePeriodEnd )) {
319- mPurgedApps . put ( packageName , reason );
321+ Log . d ( TAG , "onNewAppLaunched: App's associated group's active period is over" );
320322 showOverlayDialog (packageName , reason );
321323 return ;
322324 }
323325
324326 /// Between active period so update recall delay
325327 long willOverInMs = Utils .todDifferenceFromNow (associatedGroup .activePeriodEnd );
326- if (willOverInMs < recallDelayMS ) {
327- recallDelayMS = willOverInMs ;
328- isPeriodRecall = true ;
329- recallReason = reason ;
328+ if (willOverInMs < timerDelayMS ) {
329+ timerReason = reason ;
330+ isActivePeriodTimer = true ;
331+ timerDelayMS = willOverInMs ;
330332 }
331333 }
332334
@@ -339,6 +341,7 @@ private void onNewAppLaunched(String packageName) {
339341
340342 /// Group timer ran out
341343 if (groupScreenTimeSec >= associatedGroup .timerSec ) {
344+ Log .d (TAG , "onNewAppLaunched: App's associated group's timer is over" );
342345 PurgedReason reason = new PurgedReason (getString (R .string .app_paused_dialog_info_for_group_timer_out , associatedGroup .groupName ), associatedGroup .timerSec , groupScreenTimeSec );
343346 mPurgedApps .put (packageName , reason );
344347 showOverlayDialog (packageName , reason );
@@ -347,25 +350,26 @@ private void onNewAppLaunched(String packageName) {
347350
348351 /// Group timer left so update recall delay
349352 long leftGroupLimitMs = (associatedGroup .timerSec - groupScreenTimeSec ) * 1000 ;
350- if (leftGroupLimitMs < recallDelayMS ) {
351- recallDelayMS = leftGroupLimitMs ;
352- recallReason = new PurgedReason ( getString ( R . string . app_paused_dialog_info_for_group_timer_left , associatedGroup . groupName ), associatedGroup . timerSec , groupScreenTimeSec ) ;
353- isPeriodRecall = false ;
353+ if (leftGroupLimitMs < timerDelayMS ) {
354+ timerReason = new PurgedReason ( getString ( R . string . app_paused_dialog_info_for_group_timer_left , associatedGroup . groupName ), associatedGroup . timerSec , groupScreenTimeSec ) ;
355+ isActivePeriodTimer = false ;
356+ timerDelayMS = leftGroupLimitMs ;
354357 }
355358 }
356359 }
357360
358361
359362 // Return if delay doesn't changed
360- if (recallDelayMS == Long .MAX_VALUE ) return ;
363+ if (timerDelayMS == Long .MAX_VALUE ) return ;
361364
362365 // schedule timer for lowest time to recall for usage recheck
363366 scheduleUsageAlertCountDownTimer (
364367 packageName ,
365- recallReason ,
368+ timerReason ,
366369 appRestrictions .alertInterval ,
367- appRestrictions .alertByDialog && !isPeriodRecall ,
368- recallDelayMS
370+ appRestrictions .alertByDialog ,
371+ isActivePeriodTimer ,
372+ timerDelayMS
369373 );
370374 }
371375
@@ -396,36 +400,38 @@ private boolean isAppAlreadyPurged(String packageName) {
396400 * Schedules a countdown timer to alert the user of remaining time for a specific app.
397401 * Provides notifications or overlay dialogs based on specified alert intervals and thresholds.
398402 *
399- * @param packageName The package name of the app.
400- * @param reason The reason for which to schedule timer.
401- * @param alertByDialog True if alerts should be shown as overlay dialogs, otherwise as notifications.
402- * @param alertIntervalSec The interval at which alerts should occur in SECONDS.
403- * @param millisInFuture The time in Ms in future till the countdown timer will run.
403+ * @param packageName The package name of the app.
404+ * @param reason The reason for which to schedule timer.
405+ * @param alertIntervalSec The interval at which alerts should occur in SECONDS.
406+ * @param alertByDialog True if alerts should be shown as overlay dialogs, otherwise as notifications.
407+ * @param isForActivePeriod Does the timer scheduling is for app's or group's active period or not.
408+ * @param millisInFuture The time in Ms in future till the countdown timer will run.
404409 */
405410 private void scheduleUsageAlertCountDownTimer (
406411 String packageName ,
407412 PurgedReason reason ,
408413 int alertIntervalSec ,
409414 boolean alertByDialog ,
415+ boolean isForActivePeriod ,
410416 long millisInFuture
411417 ) {
412418 cancelTimers ();
413419 final Set <Integer > alertMinuteTicks = getAlertTickFromDuration (millisInFuture , alertIntervalSec );
414420
415- // Schedule the countdown timer on the main thread
421+ // This method is called on a background thread so we have to schedule the timer from the main thread.
422+ // We can't schedule timer(background task) from another background thread. It will throw exception
416423 new Handler (Looper .getMainLooper ()).post (new Runnable () {
417424 @ Override
418425 public void run () {
426+ // Ticks every minute
419427 mOngoingAppTimer = new CountDownTimer (millisInFuture , 60 * 1000 ) {
420- // Ticks every minute
421428 @ Override
422429 public void onTick (long millisUntilFinished ) {
423- // Convert to minutes
424430 int minutesRemaining = (int ) (millisUntilFinished / 60000 );
425431
426432 // Trigger alert if remaining time in minutes matches any alert time
427433 if (alertMinuteTicks .contains (minutesRemaining )) {
428- if (alertByDialog ) {
434+ if (alertByDialog && ! isForActivePeriod ) {
429435 showOverlayDialog (packageName , reason );
430436 } else {
431437 pushUsageAlertNotification (packageName , minutesRemaining );
@@ -437,14 +443,15 @@ public void onTick(long millisUntilFinished) {
437443
438444 @ Override
439445 public void onFinish () {
440- mPurgedApps .put (packageName , reason );
446+ onNewAppLaunched (packageName );
447+ if (!isForActivePeriod ) mPurgedApps .put (packageName , reason );
441448 showOverlayDialog (packageName , reason );
442449 Log .d (TAG , "scheduleUsageAlertCountDownTimer: Countdown finished for package: " + packageName );
443450 }
444451 };
445- mOngoingAppTimer .start ();
446452 Log .d (TAG , "scheduleUsageAlertCountDownTimer: Timer scheduled for " + packageName + " ending at: " +
447453 new Date (millisInFuture + System .currentTimeMillis ()));
454+ mOngoingAppTimer .start ();
448455 }
449456 });
450457 }
0 commit comments