Skip to content

Commit

Permalink
Remove alternate method for app engine
Browse files Browse the repository at this point in the history
Restore original functionality for better stability.
  • Loading branch information
pranavpandey committed Jul 21, 2024
1 parent 4da53f6 commit 135a4d3
Showing 1 changed file with 2 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017-2022 Pranav Pandey
* Copyright 2017-2024 Pranav Pandey
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,7 +19,6 @@
import android.annotation.TargetApi;
import android.app.ActivityManager;
import android.app.usage.UsageEvents;
import android.app.usage.UsageStats;
import android.app.usage.UsageStatsManager;
import android.content.ComponentName;
import android.content.Context;
Expand All @@ -36,8 +35,6 @@
import com.pranavpandey.android.dynamic.engine.model.DynamicAppInfo;
import com.pranavpandey.android.dynamic.util.DynamicSdkUtils;

import java.util.List;

/**
* Helper class used for the {@link DynamicEngine}.
*/
Expand Down Expand Up @@ -218,30 +215,10 @@ public static int getForegroundEventType() {
if (event.getEventType() == getForegroundEventType()
&& !PACKAGE_ANDROID.equals(event.getPackageName())) {
packageName = event.getPackageName();
}
}
}

// Alternate method
if (DynamicSdkUtils.is29() && packageName == null) {
List<UsageStats> usageStats = usageStatsManager.queryUsageStats(
UsageStatsManager.INTERVAL_BEST, time - interval, time);
UsageStats usageStat = null;

for (UsageStats usageStatsEntry : usageStats) {
if (usageStatsEntry.getTotalTimeVisible() > 0
&& usageStatsEntry.getTotalTimeInForeground() > 0
&& !PACKAGE_ANDROID.equals(usageStatsEntry.getPackageName())) {
if (usageStat == null || usageStatsEntry.getLastTimeUsed()
> usageStat.getLastTimeUsed()) {
usageStat = usageStatsEntry;
}
break;
}
}

if (usageStat != null) {
packageName = usageStat.getPackageName();
}
}
} catch (Exception e) {
e.printStackTrace();
Expand Down

0 comments on commit 135a4d3

Please sign in to comment.