Skip to content

Commit a232341

Browse files
committed
[Android] Split setup into registerPhoneAccount and registerAndroidEvents
1 parent df8b02c commit a232341

File tree

5 files changed

+90
-11
lines changed

5 files changed

+90
-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: 24 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

@@ -547,6 +549,28 @@ Allows to remove the listener on an event.
547549
RNCallKeep.removeEventListener('checkReachability');
548550
```
549551

552+
### registerPhoneAccount
553+
554+
Registers Android phone account manually, useful for custom permission prompts when you don't want to call `setup()`.
555+
This method is called by `setup`, if you already use setup you don't need it.
556+
557+
_This feature is available only on Android._
558+
559+
```js
560+
RNCallKeep.registerPhoneAccount();
561+
```
562+
563+
### registerAndroidEvents
564+
565+
Registers Android UI events, useful when you don't want to call `setup()`.
566+
This method is called by `setup`, if you already use setup you don't need it.
567+
568+
_This feature is available only on Android._
569+
570+
```js
571+
RNCallKeep.registerAndroidEvents();
572+
```
573+
550574
## Example
551575

552576
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
@@ -54,11 +54,19 @@ export default class RNCallKeep {
5454
static hasDefaultPhoneAccount(): boolean {
5555

5656
}
57-
57+
5858
static answerIncomingCall(uuid: string) {
5959

6060
}
6161

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

index.js

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

51+
registerPhoneAccount = () => {
52+
if (isIOS) {
53+
return;
54+
}
55+
RNCallKeepModule.registerPhoneAccount();
56+
};
57+
58+
59+
registerAndroidEvents = () => {
60+
if (isIOS) {
61+
return;
62+
}
63+
RNCallKeepModule.bindEvents();
64+
};
65+
5166
hasDefaultPhoneAccount = async (options) => {
5267
if (!isIOS) {
5368
return this._hasDefaultPhoneAccount(options);

0 commit comments

Comments
 (0)