-
Notifications
You must be signed in to change notification settings - Fork 207
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Alter In foreground calculation #466
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
cf7ac94
refactor: extract foreground anr detection logic to separate class
fractalwrench 5de93af
feat: update SessionTracker implementation to use new inForeground ca…
fractalwrench 260b670
docs: add changelog entry
fractalwrench 20f1761
Merge branch 'next' into in-foreground
fractalwrench a4a207a
docs: add additional docs for inforeground detection
fractalwrench File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 0 additions & 38 deletions
38
sdk/src/androidTest/java/com/bugsnag/android/BreadcrumbLifecycleCrashTest.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
sdk/src/main/java/com/bugsnag/android/ForegroundDetector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package com.bugsnag.android; | ||
|
||
import android.app.ActivityManager; | ||
import android.content.Context; | ||
import android.os.Build; | ||
import android.os.Process; | ||
import android.support.annotation.Nullable; | ||
|
||
import java.util.List; | ||
|
||
class ForegroundDetector { | ||
|
||
private final ActivityManager activityManager; | ||
|
||
ForegroundDetector(Context context) { | ||
this.activityManager = | ||
(ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); | ||
} | ||
|
||
/** | ||
* Determines whether or not the application is in the foreground, by using the process' | ||
* importance as a proxy. | ||
* <p/> | ||
* In the unlikely event that information about the process cannot be retrieved, this method | ||
* will return true. This is deemed preferable as ANRs are only reported when the application | ||
* is in the foreground, and we would rather deliver false-positives than miss true ANRs in | ||
* this case. We also need to report 'inForeground' as a boolean value in API calls, and | ||
* need to keep the definition of the value consistent throughout the application. | ||
* | ||
* @return whether the application is in the foreground or not | ||
*/ | ||
boolean isInForeground() { | ||
ActivityManager.RunningAppProcessInfo info = getProcessInfo(); | ||
|
||
if (info != null) { | ||
return info.importance <= ActivityManager.RunningAppProcessInfo.IMPORTANCE_VISIBLE; | ||
} else { // prefer a potential false positive if process info not available | ||
return true; | ||
} | ||
} | ||
|
||
private ActivityManager.RunningAppProcessInfo getProcessInfo() { | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { | ||
ActivityManager.RunningAppProcessInfo info = | ||
new ActivityManager.RunningAppProcessInfo(); | ||
ActivityManager.getMyMemoryState(info); | ||
return info; | ||
} else { | ||
return getProcessInfoPreApi16(); | ||
} | ||
} | ||
|
||
@Nullable | ||
private ActivityManager.RunningAppProcessInfo getProcessInfoPreApi16() { | ||
List<ActivityManager.RunningAppProcessInfo> appProcesses; | ||
|
||
try { | ||
appProcesses = activityManager.getRunningAppProcesses(); | ||
} catch (SecurityException exc) { | ||
return null; | ||
} | ||
|
||
if (appProcesses != null) { | ||
int pid = Process.myPid(); | ||
|
||
for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) { | ||
if (pid == appProcess.pid) { | ||
return appProcess; | ||
} | ||
} | ||
} | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why default rather than falling back to the old implementation?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We made the decision when implementing ANR detection that we should prefer a false positive when we're unable to determine process importance. This is because otherwise the notifier might discard an actual error, since the old implementation can be inaccurate if the main thread is blocked and lifecycle callbacks cannot be invoked.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. To rephrase, there are actually three states for whether the app is in the foreground,
yes
,no
, andunknown
. In theunknown
case, ANR errors should still be reported, so this usesyes
instead ofunknown
or an implementation based on main thread activity. Is that a fair assessment?It would be clearer (and delineate responsibilities better) to actually return a value which indicates that the state is currently unknown and let the ANR reporter handle that appropriately, but barring that, the comment should state for future travelers why a potential false positive is preferable and how it affects reporting. "If this was false, some ANRs might get ignored" was not what I expected, and I can see how a future PR to tweak to how this value is computed could unintentionally break this.