Skip to content

Commit 50a384f

Browse files
feat(app, android): support list of Activities to ignore when detecting 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>
1 parent 6a30d4b commit 50a384f

4 files changed

Lines changed: 60 additions & 0 deletions

File tree

packages/app/android/src/reactnative/java/io/invertase/firebase/common/ReactNativeFirebaseJSON.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import org.json.JSONException;
2525
import org.json.JSONObject;
2626

27+
import java.util.ArrayList;
28+
2729
import io.invertase.firebase.BuildConfig;
2830

2931
public class ReactNativeFirebaseJSON {
@@ -68,6 +70,25 @@ public String getStringValue(String key, String defaultValue) {
6870
return jsonObject.optString(key, defaultValue);
6971
}
7072

73+
public ArrayList<String> getArrayValue(String key) {
74+
ArrayList<String> result = new ArrayList<String>();
75+
if (jsonObject == null) return result;
76+
77+
try {
78+
JSONArray array = jsonObject.optJSONArray(key);
79+
if (array != null) {
80+
for (int i = 0; i < array.length(); i++) {
81+
result.add(array.getString(i));
82+
}
83+
}
84+
}
85+
catch (JSONException e) {
86+
// do nothing
87+
}
88+
89+
return result;
90+
}
91+
7192
public String getRawJSON() {
7293
return BuildConfig.FIREBASE_JSON_RAW;
7394
}

packages/app/android/src/reactnative/java/io/invertase/firebase/common/SharedUtils.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import android.graphics.Point;
2323
import android.graphics.Rect;
2424
import android.net.Uri;
25+
import android.os.Build;
2526
import android.util.Log;
2627
import com.facebook.react.bridge.*;
2728
import com.facebook.react.common.LifecycleState;
@@ -130,6 +131,38 @@ public static boolean isAppInForeground(Context context) {
130131
List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
131132
if (appProcesses == null) return false;
132133

134+
// Check if current activity is a background activity
135+
ReactNativeFirebaseJSON json = ReactNativeFirebaseJSON.getSharedInstance();
136+
if (json.contains("android_background_activity_names")) {
137+
ArrayList<String> backgroundActivities = json.getArrayValue("android_background_activity_names");
138+
139+
if (backgroundActivities.size() != 0) {
140+
String currentActivity = "";
141+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
142+
List<ActivityManager.AppTask> taskInfo = activityManager.getAppTasks();
143+
if (taskInfo.size() > 0) {
144+
ActivityManager.RecentTaskInfo task = taskInfo.get(0).getTaskInfo();
145+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
146+
currentActivity = task.baseActivity.getShortClassName();
147+
} else {
148+
currentActivity = task.origActivity != null ?
149+
task.origActivity.getShortClassName() :
150+
task.baseIntent.getComponent().getShortClassName();
151+
}
152+
}
153+
} else {
154+
List<ActivityManager.RunningTaskInfo> taskInfo = activityManager.getRunningTasks(1);
155+
if (taskInfo.size() > 0) {
156+
currentActivity = taskInfo.get(0).topActivity.getShortClassName();
157+
}
158+
}
159+
160+
if (!"".equals(currentActivity) && backgroundActivities.contains(currentActivity)) {
161+
return false;
162+
}
163+
}
164+
}
165+
133166
final String packageName = context.getPackageName();
134167
for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
135168
if (

packages/app/firebase-schema.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@
8181
"android_task_executor_keep_alive_seconds": {
8282
"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.",
8383
"type": "number"
84+
},
85+
"android_background_activity_names": {
86+
"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",
87+
"type": "array"
8488
}
8589
}
8690
}

tests/firebase.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
{
22
"react-native": {
3+
"android_background_activity_names": "NotActuallyAnActivity",
4+
35
"admob_delay_app_measurement_init": true,
46
"admob_ios_app_id": "ca-app-pub-4406399463942824~1625911479",
57
"admob_android_app_id": "ca-app-pub-4406399463942824~1625911479",

0 commit comments

Comments
 (0)