Skip to content

Commit d68ac53

Browse files
committed
compatible with RN >= 29
1 parent 990ca7a commit d68ac53

File tree

2 files changed

+75
-38
lines changed

2 files changed

+75
-38
lines changed

android/src/main/java/com/zxcpoiu/incallmanager/InCallManagerModule.java

Lines changed: 73 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@
5252
public class InCallManagerModule extends ReactContextBaseJavaModule implements LifecycleEventListener {
5353
private static final String REACT_NATIVE_MODULE_NAME = "InCallManager";
5454
private static final String TAG = REACT_NATIVE_MODULE_NAME;
55-
private static ReactApplicationContext reactContext;
5655
private static SparseArray<Promise> mRequestPermissionCodePromises;
5756
private static SparseArray<String> mRequestPermissionCodeTargetPermission;
57+
private String mPackageName = "com.zxcpoiu.incallmanager";
5858

5959
// --- Screen Manager
6060
private PowerManager mPowerManager;
@@ -89,6 +89,7 @@ public class InCallManagerModule extends ReactContextBaseJavaModule implements L
8989
private SensorManager mSensorManager;
9090
private Sensor proximitySensor;
9191
private SensorEventListener proximitySensorEventListener;
92+
private OnFocusChangeListener mOnFocusChangeListener;
9293

9394
// --- same as: RingtoneManager.getActualDefaultRingtoneUri(reactContext, RingtoneManager.TYPE_RINGTONE);
9495
private Uri defaultRingtoneUri = Settings.System.DEFAULT_RINGTONE_URI;
@@ -117,9 +118,9 @@ public String getName() {
117118
return REACT_NATIVE_MODULE_NAME;
118119
}
119120

120-
public InCallManagerModule(ReactApplicationContext _reactContext) {
121-
super(_reactContext);
122-
reactContext = _reactContext;
121+
public InCallManagerModule(ReactApplicationContext reactContext) {
122+
super(reactContext);
123+
mPackageName = reactContext.getPackageName();
123124
reactContext.addLifecycleEventListener(this);
124125
mWindowManager = (WindowManager) reactContext.getSystemService(Context.WINDOW_SERVICE);
125126
mPowerManager = (PowerManager) reactContext.getSystemService(Context.POWER_SERVICE);
@@ -130,7 +131,7 @@ public InCallManagerModule(ReactApplicationContext _reactContext) {
130131
mPartialLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
131132
mPartialLock.setReferenceCounted(false);
132133
audioManager = ((AudioManager) reactContext.getSystemService(Context.AUDIO_SERVICE));
133-
mSensorManager = (SensorManager)reactContext.getSystemService(Context.SENSOR_SERVICE);
134+
mSensorManager = (SensorManager) reactContext.getSystemService(Context.SENSOR_SERVICE);
134135
proximitySensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
135136
checkProximitySupport();
136137
audioUriMap = new HashMap<String, Uri>();
@@ -142,6 +143,7 @@ public InCallManagerModule(ReactApplicationContext _reactContext) {
142143
audioUriMap.put("bundleBusytoneUri", bundleBusytoneUri);
143144
mRequestPermissionCodePromises = new SparseArray<Promise>();
144145
mRequestPermissionCodeTargetPermission = new SparseArray<String>();
146+
mOnFocusChangeListener = new OnFocusChangeListener();
145147
Log.d(TAG, "InCallManager initialized");
146148
}
147149

@@ -392,14 +394,24 @@ public void onReceive(Context context, Intent intent) {
392394
}
393395
}
394396
};
395-
reactContext.registerReceiver(wiredHeadsetReceiver, filter);
397+
ReactContext reactContext = getReactApplicationContext();
398+
if (reactContext != null) {
399+
reactContext.registerReceiver(wiredHeadsetReceiver, filter);
400+
} else {
401+
Log.d(TAG, "startWiredHeadsetEvent() reactContext is null");
402+
}
396403
}
397404
}
398405

399406
private void stopWiredHeadsetEvent() {
400407
if (wiredHeadsetReceiver != null) {
401408
Log.d(TAG, "stopWiredHeadsetEvent()");
402-
reactContext.unregisterReceiver(wiredHeadsetReceiver);
409+
ReactContext reactContext = getReactApplicationContext();
410+
if (reactContext != null) {
411+
reactContext.unregisterReceiver(wiredHeadsetReceiver);
412+
} else {
413+
Log.d(TAG, "stopWiredHeadsetEvent() reactContext is null");
414+
}
403415
wiredHeadsetReceiver = null;
404416
}
405417
}
@@ -419,14 +431,24 @@ public void onReceive(Context context, Intent intent) {
419431
}
420432
}
421433
};
422-
reactContext.registerReceiver(noisyAudioReceiver, filter);
434+
ReactContext reactContext = getReactApplicationContext();
435+
if (reactContext != null) {
436+
reactContext.registerReceiver(noisyAudioReceiver, filter);
437+
} else {
438+
Log.d(TAG, "startNoisyAudioEvent() reactContext is null");
439+
}
423440
}
424441
}
425442

426443
private void stopNoisyAudioEvent() {
427444
if (noisyAudioReceiver != null) {
428445
Log.d(TAG, "stopNoisyAudioEvent()");
429-
reactContext.unregisterReceiver(noisyAudioReceiver);
446+
ReactContext reactContext = getReactApplicationContext();
447+
if (reactContext != null) {
448+
reactContext.unregisterReceiver(noisyAudioReceiver);
449+
} else {
450+
Log.d(TAG, "stopNoisyAudioEvent() reactContext is null");
451+
}
430452
noisyAudioReceiver = null;
431453
}
432454
}
@@ -481,14 +503,24 @@ public void onReceive(Context context, Intent intent) {
481503
}
482504
}
483505
};
484-
reactContext.registerReceiver(mediaButtonReceiver, filter);
506+
ReactContext reactContext = getReactApplicationContext();
507+
if (reactContext != null) {
508+
reactContext.registerReceiver(mediaButtonReceiver, filter);
509+
} else {
510+
Log.d(TAG, "startMediaButtonEvent() reactContext is null");
511+
}
485512
}
486513
}
487514

488515
private void stopMediaButtonEvent() {
489516
if (mediaButtonReceiver != null) {
490517
Log.d(TAG, "stopMediaButtonEvent()");
491-
reactContext.unregisterReceiver(mediaButtonReceiver);
518+
ReactContext reactContext = getReactApplicationContext();
519+
if (reactContext != null) {
520+
reactContext.unregisterReceiver(mediaButtonReceiver);
521+
} else {
522+
Log.d(TAG, "stopMediaButtonEvent() reactContext is null");
523+
}
492524
mediaButtonReceiver = null;
493525
}
494526
}
@@ -557,16 +589,7 @@ private void stopProximitySensor() {
557589
}
558590
}
559591

560-
private static final class OnFocusChangeListener implements AudioManager.OnAudioFocusChangeListener {
561-
562-
private static OnFocusChangeListener instance;
563-
564-
protected static OnFocusChangeListener getInstance() {
565-
if (instance == null) {
566-
instance = new OnFocusChangeListener();
567-
}
568-
return instance;
569-
}
592+
private class OnFocusChangeListener implements AudioManager.OnAudioFocusChangeListener {
570593

571594
@Override
572595
public void onAudioFocusChange(final int focusChange) {
@@ -638,11 +661,16 @@ public void test_deviceCallback() {
638661
// -- TODO: bluetooth support
639662
*/
640663

641-
private static void sendEvent(final String eventName, @Nullable WritableMap params) {
664+
private void sendEvent(final String eventName, @Nullable WritableMap params) {
642665
try {
643-
reactContext
644-
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
645-
.emit(eventName, params);
666+
ReactContext reactContext = getReactApplicationContext();
667+
if (reactContext != null && reactContext.hasActiveCatalystInstance()) {
668+
reactContext
669+
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
670+
.emit(eventName, params);
671+
} else {
672+
Log.e(TAG, "sendEvent(): reactContext is null or not having CatalystInstance yet.");
673+
}
646674
} catch (RuntimeException e) {
647675
Log.e(TAG, "sendEvent(): java.lang.RuntimeException: Trying to invoke JS before CatalystInstance has been set!");
648676
}
@@ -734,7 +762,7 @@ private void stopEvents() {
734762

735763
private void requestAudioFocus() {
736764
if (!isAudioFocused) {
737-
int result = audioManager.requestAudioFocus(OnFocusChangeListener.getInstance(), AudioManager.STREAM_VOICE_CALL, AudioManager.AUDIOFOCUS_GAIN);
765+
int result = audioManager.requestAudioFocus(mOnFocusChangeListener, AudioManager.STREAM_VOICE_CALL, AudioManager.AUDIOFOCUS_GAIN);
738766
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
739767
Log.d(TAG, "AudioFocus granted");
740768
isAudioFocused = true;
@@ -775,7 +803,7 @@ private void debugScreenPowerState() {
775803

776804
if (android.os.Build.VERSION.SDK_INT >= 23) {
777805
isDeviceIdleMode = String.format("%s", mPowerManager.isDeviceIdleMode());
778-
isIgnoringBatteryOptimizations = String.format("%s", mPowerManager.isIgnoringBatteryOptimizations(reactContext.getPackageName()));
806+
isIgnoringBatteryOptimizations = String.format("%s", mPowerManager.isIgnoringBatteryOptimizations(mPackageName));
779807
}
780808
if (android.os.Build.VERSION.SDK_INT >= 21) {
781809
isPowerSaveMode = String.format("%s", mPowerManager.isPowerSaveMode());
@@ -1201,14 +1229,20 @@ private Uri getAudioUri(final String _type, final String fileBundle, final Strin
12011229
String type = _type;
12021230
if (type.equals("_BUNDLE_")) {
12031231
if (audioUriMap.get(uriBundle) == null) {
1204-
int res = reactContext.getResources().getIdentifier(fileBundle, "raw", reactContext.getPackageName());
1232+
int res = 0;
1233+
ReactContext reactContext = getReactApplicationContext();
1234+
if (reactContext != null) {
1235+
res = reactContext.getResources().getIdentifier(fileBundle, "raw", mPackageName);
1236+
} else {
1237+
Log.d(TAG, "getAudioUri() reactContext is null");
1238+
}
12051239
if (res <= 0) {
12061240
Log.d(TAG, String.format("getAudioUri() %s.%s not found in bundle.", fileBundle, fileBundleExt));
12071241
audioUriMap.put(uriBundle, null);
12081242
//type = fileSysWithExt;
12091243
return getDefaultUserUri(uriDefault); // --- if specified bundle but not found, use default directlly
12101244
} else {
1211-
audioUriMap.put(uriBundle, Uri.parse("android.resource://" + reactContext.getPackageName() + "/" + Integer.toString(res)));
1245+
audioUriMap.put(uriBundle, Uri.parse("android.resource://" + mPackageName + "/" + Integer.toString(res)));
12121246
//bundleRingtoneUri = Uri.parse("android.resource://" + reactContext.getPackageName() + "/" + R.raw.incallmanager_ringtone);
12131247
//bundleRingtoneUri = Uri.parse("android.resource://" + reactContext.getPackageName() + "/raw/incallmanager_ringtone");
12141248
Log.d(TAG, "getAudioUri() using: " + type);
@@ -1414,6 +1448,7 @@ public void startPlay(final Map data) {
14141448
int stream = (Integer) data.get("audioStream");
14151449
String name = (String) data.get("name");
14161450

1451+
ReactContext reactContext = getReactApplicationContext();
14171452
setDataSource(reactContext, sourceUri);
14181453
setLooping(setLooping);
14191454
setAudioStreamType(stream); // is better using STREAM_DTMF for ToneGenerator?
@@ -1484,9 +1519,15 @@ private void _checkCameraPermission() {
14841519
}
14851520

14861521
private String _checkPermission(String targetPermission) {
1487-
if (ContextCompat.checkSelfPermission(reactContext, targetPermission) == PackageManager.PERMISSION_GRANTED) {
1488-
return "granted";
1489-
} else {
1522+
try {
1523+
ReactContext reactContext = getReactApplicationContext();
1524+
if (ContextCompat.checkSelfPermission(reactContext, targetPermission) == PackageManager.PERMISSION_GRANTED) {
1525+
return "granted";
1526+
} else {
1527+
return "denied";
1528+
}
1529+
} catch (Exception e) {
1530+
Log.d(TAG, "_checkPermission() catch");
14901531
return "denied";
14911532
}
14921533
}

android/src/main/java/com/zxcpoiu/incallmanager/InCallManagerPackage.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,14 @@
66
import com.facebook.react.bridge.ReactApplicationContext;
77
import com.facebook.react.uimanager.ViewManager;
88

9-
import java.util.ArrayList;
10-
import java.util.Arrays;
119
import java.util.Collections;
1210
import java.util.List;
1311

1412
public class InCallManagerPackage implements ReactPackage {
1513

1614
@Override
1715
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
18-
List<NativeModule> modules = new ArrayList<>();
19-
modules.add(new InCallManagerModule(reactContext));
20-
return modules;
16+
return Collections.<NativeModule>singletonList(new InCallManagerModule(reactContext));
2117
}
2218

2319
@Override
@@ -27,7 +23,7 @@ public List<Class<? extends JavaScriptModule>> createJSModules() {
2723

2824
@Override
2925
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
30-
return Collections.emptyList();
26+
return Collections.emptyList();
3127
}
3228

3329
public static void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {

0 commit comments

Comments
 (0)