-
Notifications
You must be signed in to change notification settings - Fork 205
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #466 from bugsnag/in-foreground
Alter In foreground calculation
- Loading branch information
Showing
8 changed files
with
101 additions
and
111 deletions.
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