Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
feat(app, android): support list of Activities to ignore when detecti…
…ng AppState (#5235)

* Check json config for background activities
* Add json config key to test app to exercise code

Co-authored-by: Mike Hardy <github@mikehardy.net>
Co-authored-by: Dan White <40389335+danwhite-ipc@users.noreply.github.com>
  • Loading branch information
mikehardy and danwhite-ipc committed Apr 29, 2021
1 parent 6a30d4b commit 50a384f
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
Expand Up @@ -24,6 +24,8 @@
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import io.invertase.firebase.BuildConfig;

public class ReactNativeFirebaseJSON {
Expand Down Expand Up @@ -68,6 +70,25 @@ public String getStringValue(String key, String defaultValue) {
return jsonObject.optString(key, defaultValue);
}

public ArrayList<String> getArrayValue(String key) {
ArrayList<String> result = new ArrayList<String>();
if (jsonObject == null) return result;

try {
JSONArray array = jsonObject.optJSONArray(key);
if (array != null) {
for (int i = 0; i < array.length(); i++) {
result.add(array.getString(i));
}
}
}
catch (JSONException e) {
// do nothing
}

return result;
}

public String getRawJSON() {
return BuildConfig.FIREBASE_JSON_RAW;
}
Expand Down
Expand Up @@ -22,6 +22,7 @@
import android.graphics.Point;
import android.graphics.Rect;
import android.net.Uri;
import android.os.Build;
import android.util.Log;
import com.facebook.react.bridge.*;
import com.facebook.react.common.LifecycleState;
Expand Down Expand Up @@ -130,6 +131,38 @@ public static boolean isAppInForeground(Context context) {
List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
if (appProcesses == null) return false;

// Check if current activity is a background activity
ReactNativeFirebaseJSON json = ReactNativeFirebaseJSON.getSharedInstance();
if (json.contains("android_background_activity_names")) {
ArrayList<String> backgroundActivities = json.getArrayValue("android_background_activity_names");

if (backgroundActivities.size() != 0) {
String currentActivity = "";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
List<ActivityManager.AppTask> taskInfo = activityManager.getAppTasks();
if (taskInfo.size() > 0) {
ActivityManager.RecentTaskInfo task = taskInfo.get(0).getTaskInfo();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
currentActivity = task.baseActivity.getShortClassName();
} else {
currentActivity = task.origActivity != null ?
task.origActivity.getShortClassName() :
task.baseIntent.getComponent().getShortClassName();
}
}
} else {
List<ActivityManager.RunningTaskInfo> taskInfo = activityManager.getRunningTasks(1);
if (taskInfo.size() > 0) {
currentActivity = taskInfo.get(0).topActivity.getShortClassName();
}
}

if (!"".equals(currentActivity) && backgroundActivities.contains(currentActivity)) {
return false;
}
}
}

final String packageName = context.getPackageName();
for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
if (
Expand Down
4 changes: 4 additions & 0 deletions packages/app/firebase-schema.json
Expand Up @@ -81,6 +81,10 @@
"android_task_executor_keep_alive_seconds": {
"description": "Keep-alive time of ThreadPoolExecutor used by RNFirebase for Android, in seconds. Defaults to `3`.\n Excess threads in the pool executor will be terminated if they have been idle for more than the keep-alive time.",
"type": "number"
},
"android_background_activity_names": {
"description": "The names (as returned by `getShortClassName()` of Activities used outside the context of react native.\nThese are ignored when determining if the app is in foreground for purposes of calling javascript background handlers",
"type": "array"
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions tests/firebase.json
@@ -1,5 +1,7 @@
{
"react-native": {
"android_background_activity_names": "NotActuallyAnActivity",

"admob_delay_app_measurement_init": true,
"admob_ios_app_id": "ca-app-pub-4406399463942824~1625911479",
"admob_android_app_id": "ca-app-pub-4406399463942824~1625911479",
Expand Down

1 comment on commit 50a384f

@vercel
Copy link

@vercel vercel bot commented on 50a384f Apr 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.