@@ -250,8 +250,8 @@ private void onNewAppLaunched(String packageName) {
250250 /// Return if no restriction applied
251251 AppRestrictions appRestrictions = mAppsRestrictions .get (packageName );
252252 if (appRestrictions == null ) return ;
253+
253254 PurgedReason timerReason = null ;
254- boolean isActivePeriodTimer = false ;
255255 long timerDelayMS = Long .MAX_VALUE ;
256256
257257 /// Check for app launch limit
@@ -265,26 +265,26 @@ private void onNewAppLaunched(String packageName) {
265265
266266 /// Check for app's active period
267267 if (appRestrictions .periodDurationInMins > 0 ) {
268- PurgedReason reason = new PurgedReason ( getString ( R . string . app_paused_dialog_info_for_active_period_over )) ;
268+ int periodEndTimeMinutes = appRestrictions . activePeriodStart + appRestrictions . periodDurationInMins ;
269269
270270 /// Outside active period
271- if (Utils .isTimeOutsideTODs (appRestrictions .activePeriodStart , appRestrictions . activePeriodEnd )) {
271+ if (Utils .isTimeOutsideTODs (appRestrictions .activePeriodStart , periodEndTimeMinutes )) {
272272 Log .d (TAG , "onNewAppLaunched: App's active period is over" );
273+ PurgedReason reason = new PurgedReason (getString (R .string .app_paused_dialog_info_for_active_period_over ));
273274 showOverlayDialog (packageName , reason );
274275 return ;
275276 }
276277
277- /// Between active period so update recall delay
278- long willOverInMs = Utils .todDifferenceFromNow (appRestrictions . activePeriodEnd );
278+ /// Launched between active period so set timer for period ending
279+ long willOverInMs = Utils .todDifferenceFromNow (periodEndTimeMinutes );
279280 if (willOverInMs < timerDelayMS ) {
280- timerReason = reason ;
281- isActivePeriodTimer = true ;
281+ timerReason = null ;
282282 timerDelayMS = willOverInMs ;
283283 }
284284 }
285285
286286 /// Fetch usage for all apps
287- HashMap <String , Long > allAppsScreenUsage = ScreenUsageHelper .fetchAppUsageTodayTillNow (mUsageStatsManager );
287+ HashMap <String , Long > allAppsScreenUsage = ScreenUsageHelper .fetchAppUsageTodayTillNow (mUsageStatsManager , packageName );
288288
289289 /// Check for app timer
290290 if (appRestrictions .timerSec > 0 ) {
@@ -303,7 +303,6 @@ private void onNewAppLaunched(String packageName) {
303303 long leftAppLimitMs = (appRestrictions .timerSec - appScreenTimeSec ) * 1000 ;
304304 if (leftAppLimitMs < timerDelayMS ) {
305305 timerReason = new PurgedReason (getString (R .string .app_paused_dialog_info_for_app_timer_left ), appRestrictions .timerSec , appScreenTimeSec );
306- isActivePeriodTimer = false ;
307306 timerDelayMS = leftAppLimitMs ;
308307 }
309308 }
@@ -315,29 +314,28 @@ private void onNewAppLaunched(String packageName) {
315314
316315 /// Check for group's active period
317316 if (associatedGroup .periodDurationInMins > 0 ) {
318- PurgedReason reason = new PurgedReason (getString (R .string .group_paused_dialog_info_for_active_period_over , associatedGroup .groupName ));
317+ int periodEndTimeMinutes = associatedGroup .activePeriodStart + associatedGroup .periodDurationInMins ;
318+
319319 /// Outside active period
320- if (Utils .isTimeOutsideTODs (associatedGroup .activePeriodStart , associatedGroup . activePeriodEnd )) {
320+ if (Utils .isTimeOutsideTODs (associatedGroup .activePeriodStart , periodEndTimeMinutes )) {
321321 Log .d (TAG , "onNewAppLaunched: App's associated group's active period is over" );
322+ PurgedReason reason = new PurgedReason (getString (R .string .group_paused_dialog_info_for_active_period_over , associatedGroup .groupName ));
322323 showOverlayDialog (packageName , reason );
323324 return ;
324325 }
325326
326- /// Between active period so update recall delay
327- long willOverInMs = Utils .todDifferenceFromNow (associatedGroup . activePeriodEnd );
327+ /// Launched between active period so set timer for period ending
328+ long willOverInMs = Utils .todDifferenceFromNow (periodEndTimeMinutes );
328329 if (willOverInMs < timerDelayMS ) {
329- timerReason = reason ;
330- isActivePeriodTimer = true ;
330+ timerReason = null ;
331331 timerDelayMS = willOverInMs ;
332332 }
333333 }
334334
335335
336336 /// Check for associated group's timer
337337 if (associatedGroup .timerSec > 0 ) {
338- long groupScreenTimeSec = associatedGroup .distractingApps .stream ()
339- .mapToLong (app -> ScreenUsageHelper .fetchAppUsageTodayTillNow (mUsageStatsManager ).getOrDefault (app , 0L ))
340- .sum ();
338+ long groupScreenTimeSec = associatedGroup .distractingApps .stream ().mapToLong (app -> allAppsScreenUsage .getOrDefault (app , 0L )).sum ();
341339
342340 /// Group timer ran out
343341 if (groupScreenTimeSec >= associatedGroup .timerSec ) {
@@ -352,7 +350,6 @@ private void onNewAppLaunched(String packageName) {
352350 long leftGroupLimitMs = (associatedGroup .timerSec - groupScreenTimeSec ) * 1000 ;
353351 if (leftGroupLimitMs < timerDelayMS ) {
354352 timerReason = new PurgedReason (getString (R .string .app_paused_dialog_info_for_group_timer_left , associatedGroup .groupName ), associatedGroup .timerSec , groupScreenTimeSec );
355- isActivePeriodTimer = false ;
356353 timerDelayMS = leftGroupLimitMs ;
357354 }
358355 }
@@ -368,7 +365,6 @@ private void onNewAppLaunched(String packageName) {
368365 timerReason ,
369366 appRestrictions .alertInterval ,
370367 appRestrictions .alertByDialog ,
371- isActivePeriodTimer ,
372368 timerDelayMS
373369 );
374370 }
@@ -400,19 +396,17 @@ private boolean isAppAlreadyPurged(String packageName) {
400396 * Schedules a countdown timer to alert the user of remaining time for a specific app.
401397 * Provides notifications or overlay dialogs based on specified alert intervals and thresholds.
402398 *
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.
399+ * @param packageName The package name of the app.
400+ * @param unfinishedReason The unfinishedReason for which to schedule timer.
401+ * @param alertIntervalSec The interval at which alerts should occur in SECONDS.
402+ * @param alertByDialog True if alerts should be shown as overlay dialogs, otherwise as notifications.
403+ * @param millisInFuture The time in Ms in future till the countdown timer will run.
409404 */
410405 private void scheduleUsageAlertCountDownTimer (
411406 String packageName ,
412- PurgedReason reason ,
407+ @ Nullable PurgedReason unfinishedReason ,
413408 int alertIntervalSec ,
414409 boolean alertByDialog ,
415- boolean isForActivePeriod ,
416410 long millisInFuture
417411 ) {
418412 cancelTimers ();
@@ -431,8 +425,8 @@ public void onTick(long millisUntilFinished) {
431425
432426 // Trigger alert if remaining time in minutes matches any alert time
433427 if (alertMinuteTicks .contains (minutesRemaining )) {
434- if (alertByDialog && ! isForActivePeriod ) {
435- showOverlayDialog (packageName , reason );
428+ if (unfinishedReason != null && alertByDialog ) {
429+ showOverlayDialog (packageName , unfinishedReason );
436430 } else {
437431 pushUsageAlertNotification (packageName , minutesRemaining );
438432 }
@@ -443,10 +437,8 @@ public void onTick(long millisUntilFinished) {
443437
444438 @ Override
445439 public void onFinish () {
446- onNewAppLaunched (packageName );
447- if (!isForActivePeriod ) mPurgedApps .put (packageName , reason );
448- showOverlayDialog (packageName , reason );
449440 Log .d (TAG , "scheduleUsageAlertCountDownTimer: Countdown finished for package: " + packageName );
441+ onNewAppLaunched (packageName );
450442 }
451443 };
452444 Log .d (TAG , "scheduleUsageAlertCountDownTimer: Timer scheduled for " + packageName + " ending at: " +
0 commit comments