Skip to content

Commit bbf091c

Browse files
committed
add isAppOnForeground
1 parent fd5c82e commit bbf091c

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

android/src/main/java/com/incomingcall/UnlockScreenActivity.java

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import android.provider.Settings;
1515

1616
import androidx.appcompat.app.AppCompatActivity;
17+
import android.app.ActivityManager;
1718

1819
import com.facebook.react.bridge.Arguments;
1920
import com.facebook.react.bridge.ReactContext;
@@ -118,11 +119,11 @@ private void acceptDialing() {
118119
params.putBoolean("done", true);
119120
params.putString("uuid", uuid);
120121

121-
if (IncomingCallModule.reactContext.hasCurrentActivity()) {
122-
// App in foreground or background, send event for app to listen
122+
if (isAppOnForeground()) {
123+
// App in foreground, send event for app to listen
123124
sendEvent("answerCall", params);
124125
} else {
125-
// App killed, start app and add launch params
126+
// App in background or killed, start app and add launch params
126127
String packageNames = IncomingCallModule.reactContext.getPackageName();
127128
Intent launchIntent = IncomingCallModule.reactContext.getPackageManager().getLaunchIntentForPackage(packageNames);
128129
String className = launchIntent.getComponent().getClassName();
@@ -137,7 +138,7 @@ private void acceptDialing() {
137138
} catch(Exception e) {
138139
Log.e("RNIncomingCall", "Class not found", e);
139140
return;
140-
}
141+
}
141142
}
142143

143144
finish();
@@ -191,4 +192,24 @@ private void sendEvent(String eventName, WritableMap params) {
191192
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
192193
.emit(eventName, params);
193194
}
195+
196+
private boolean isAppOnForeground() {
197+
/**
198+
* We need to check if app is in foreground otherwise the app will crash.
199+
* http://stackoverflow.com/questions/8489993/check-android-application-is-in-foreground-or-not
200+
**/
201+
ActivityManager activityManager = (ActivityManager) IncomingCallModule.reactContext.getSystemService(Context.ACTIVITY_SERVICE);
202+
List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
203+
if (appProcesses == null) {
204+
return false;
205+
}
206+
final String packageName = IncomingCallModule.reactContext.getPackageName();
207+
for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
208+
if (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND
209+
&& appProcess.processName.equals(packageName)) {
210+
return true;
211+
}
212+
}
213+
return false;
214+
}
194215
}

0 commit comments

Comments
 (0)