Skip to content

Commit db4b2e4

Browse files
authored
Merge pull request #242 from react-native-webrtc/split_setup
[Android] Split setup into registerPhoneAccount and registerAndroidEvents
2 parents 5111fe2 + e7cb4ed commit db4b2e4

File tree

5 files changed

+92
-11
lines changed

5 files changed

+92
-11
lines changed

.npmignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
example/
22
node_modules/
3+
.idea/
4+
.github/
5+
docs/

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ RNCallKeep.setup(options).then(accepted => {});
8686
- `additionalPermissions`: [PermissionsAndroid] (optional)
8787
Any additional permissions you'd like your app to have at first launch. Can be used to simplify permission flows and avoid
8888
multiple popups to the user at different times.
89+
90+
`setup` calls internally `registerPhoneAccount` and `registerEvents`.
8991

9092
## Constants
9193

@@ -567,6 +569,30 @@ Allows to remove the listener on an event.
567569
RNCallKeep.removeEventListener('checkReachability');
568570
```
569571

572+
### registerPhoneAccount
573+
574+
Registers Android phone account manually, useful for custom permission prompts when you don't want to call `setup()`.
575+
This method is called by `setup`, if you already use setup you don't need it.
576+
577+
_This feature is available only on Android._
578+
_On iOS you still have to call `setup()`._
579+
580+
```js
581+
RNCallKeep.registerPhoneAccount();
582+
```
583+
584+
### registerAndroidEvents
585+
586+
Registers Android UI events, useful when you don't want to call `setup()`.
587+
This method is called by `setup`, if you already use setup you don't need it.
588+
589+
_This feature is available only on Android._
590+
_On iOS you still have to call `setup()`._
591+
592+
```js
593+
RNCallKeep.registerAndroidEvents();
594+
```
595+
570596
## Example
571597

572598
A full example is available in the [example](https://github.com/react-native-webrtc/react-native-callkeep/tree/master/example) folder.

android/src/main/java/io/wazo/callkeep/RNCallKeepModule.java

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -121,14 +121,32 @@ public void setup(ReadableMap options) {
121121
this._settings = options;
122122

123123
if (isConnectionServiceAvailable()) {
124-
this.registerPhoneAccount(this.getAppContext());
125-
voiceBroadcastReceiver = new VoiceBroadcastReceiver();
126-
registerReceiver();
127-
VoiceConnectionService.setPhoneAccountHandle(handle);
124+
this.registerPhoneAccount();
125+
this.registerEvents();
128126
VoiceConnectionService.setAvailable(true);
129127
}
130128
}
131129

130+
@ReactMethod
131+
public void registerPhoneAccount() {
132+
if (!isConnectionServiceAvailable()) {
133+
return;
134+
}
135+
136+
this.registerPhoneAccount(this.getAppContext());
137+
}
138+
139+
@ReactMethod
140+
public void registerEvents() {
141+
if (!isConnectionServiceAvailable()) {
142+
return;
143+
}
144+
145+
voiceBroadcastReceiver = new VoiceBroadcastReceiver();
146+
registerReceiver();
147+
VoiceConnectionService.setPhoneAccountHandle(handle);
148+
}
149+
132150
@ReactMethod
133151
public void displayIncomingCall(String uuid, String number, String callerName) {
134152
if (!isConnectionServiceAvailable() || !hasPhoneAccount()) {
@@ -346,6 +364,10 @@ public void updateDisplay(String uuid, String displayName, String uri) {
346364

347365
@ReactMethod
348366
public void hasPhoneAccount(Promise promise) {
367+
if (telecomManager == null) {
368+
this.initializeTelecomManager();
369+
}
370+
349371
promise.resolve(hasPhoneAccount());
350372
}
351373

@@ -439,15 +461,22 @@ public void backToForeground() {
439461
}
440462
}
441463

464+
private void initializeTelecomManager() {
465+
Context context = this.getAppContext();
466+
ComponentName cName = new ComponentName(context, VoiceConnectionService.class);
467+
String appName = this.getApplicationName(context);
468+
469+
handle = new PhoneAccountHandle(cName, appName);
470+
telecomManager = (TelecomManager) context.getSystemService(Context.TELECOM_SERVICE);
471+
}
472+
442473
private void registerPhoneAccount(Context appContext) {
443474
if (!isConnectionServiceAvailable()) {
444475
return;
445476
}
446477

447-
ComponentName cName = new ComponentName(this.getAppContext(), VoiceConnectionService.class);
448-
String appName = this.getApplicationName(appContext);
449-
450-
handle = new PhoneAccountHandle(cName, appName);
478+
this.initializeTelecomManager();
479+
String appName = this.getApplicationName(this.getAppContext());
451480

452481
PhoneAccount.Builder builder = new PhoneAccount.Builder(handle, appName)
453482
.setCapabilities(PhoneAccount.CAPABILITY_CALL_PROVIDER);
@@ -461,7 +490,6 @@ private void registerPhoneAccount(Context appContext) {
461490
PhoneAccount account = builder.build();
462491

463492
telephonyManager = (TelephonyManager) this.getAppContext().getSystemService(Context.TELEPHONY_SERVICE);
464-
telecomManager = (TelecomManager) this.getAppContext().getSystemService(Context.TELECOM_SERVICE);
465493

466494
telecomManager.registerPhoneAccount(account);
467495
}
@@ -492,7 +520,8 @@ private Boolean hasPermissions() {
492520
}
493521

494522
private static boolean hasPhoneAccount() {
495-
return isConnectionServiceAvailable() && telecomManager != null && telecomManager.getPhoneAccount(handle).isEnabled();
523+
return isConnectionServiceAvailable() && telecomManager != null
524+
&& telecomManager.getPhoneAccount(handle) != null && telecomManager.getPhoneAccount(handle).isEnabled();
496525
}
497526

498527
private void registerReceiver() {

index.d.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,19 @@ export default class RNCallKeep {
5555
static hasDefaultPhoneAccount(): boolean {
5656

5757
}
58-
58+
5959
static answerIncomingCall(uuid: string) {
6060

6161
}
6262

63+
static registerPhoneAccount(): void {
64+
65+
}
66+
67+
static registerAndroidEvents(): void {
68+
69+
}
70+
6371
static displayIncomingCall(
6472
uuid: string,
6573
handle: string,

index.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,21 @@ class RNCallKeep {
5454
return this._setupIOS(options.ios);
5555
};
5656

57+
registerPhoneAccount = () => {
58+
if (isIOS) {
59+
return;
60+
}
61+
RNCallKeepModule.registerPhoneAccount();
62+
};
63+
64+
65+
registerAndroidEvents = () => {
66+
if (isIOS) {
67+
return;
68+
}
69+
RNCallKeepModule.registerEvents();
70+
};
71+
5772
hasDefaultPhoneAccount = async (options) => {
5873
if (!isIOS) {
5974
return this._hasDefaultPhoneAccount(options);

0 commit comments

Comments
 (0)