Skip to content

Commit

Permalink
[Android] Split setup into registerPhoneAccount and registerAndroidEv…
Browse files Browse the repository at this point in the history
…ents
  • Loading branch information
manuquentin committed Jul 16, 2020
1 parent df8b02c commit 7c53ef2
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 11 deletions.
3 changes: 3 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
example/
node_modules/
.idea/
.github/
docs/
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ RNCallKeep.setup(options).then(accepted => {});
- `additionalPermissions`: [PermissionsAndroid] (optional)
Any additional permissions you'd like your app to have at first launch. Can be used to simplify permission flows and avoid
multiple popups to the user at different times.

`setup` calls internally `registerPhoneAccount` and `registerEvents`.

## Constants

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

### registerPhoneAccount

Registers Android phone account manually, useful for custom permission prompts when you don't want to call `setup()`.
This method is called by `setup`, if you already use setup you don't need it.

_This feature is available only on Android._

```js
RNCallKeep.registerPhoneAccount();
```

### registerAndroidEvents

Registers Android UI events, useful when you don't want to call `setup()`.
This method is called by `setup`, if you already use setup you don't need it.

_This feature is available only on Android._

```js
RNCallKeep.registerAndroidEvents();
```

## Example

A full example is available in the [example](https://github.com/react-native-webrtc/react-native-callkeep/tree/master/example) folder.
Expand Down
49 changes: 39 additions & 10 deletions android/src/main/java/io/wazo/callkeep/RNCallKeepModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,32 @@ public void setup(ReadableMap options) {
this._settings = options;

if (isConnectionServiceAvailable()) {
this.registerPhoneAccount(this.getAppContext());
voiceBroadcastReceiver = new VoiceBroadcastReceiver();
registerReceiver();
VoiceConnectionService.setPhoneAccountHandle(handle);
this.registerPhoneAccount();
this.registerEvents();
VoiceConnectionService.setAvailable(true);
}
}

@ReactMethod
public void registerPhoneAccount() {
if (!isConnectionServiceAvailable()) {
return;
}

this.registerPhoneAccount(this.getAppContext());
}

@ReactMethod
public void registerEvents() {
if (!isConnectionServiceAvailable()) {
return;
}

voiceBroadcastReceiver = new VoiceBroadcastReceiver();
registerReceiver();
VoiceConnectionService.setPhoneAccountHandle(handle);
}

@ReactMethod
public void displayIncomingCall(String uuid, String number, String callerName) {
if (!isConnectionServiceAvailable() || !hasPhoneAccount()) {
Expand Down Expand Up @@ -346,6 +364,10 @@ public void updateDisplay(String uuid, String displayName, String uri) {

@ReactMethod
public void hasPhoneAccount(Promise promise) {
if (telecomManager == null) {
this.initializeTelecomManager();
}

promise.resolve(hasPhoneAccount());
}

Expand Down Expand Up @@ -439,15 +461,22 @@ public void backToForeground() {
}
}

private void initializeTelecomManager() {
Context context = this.getAppContext();
ComponentName cName = new ComponentName(context, VoiceConnectionService.class);
String appName = this.getApplicationName(context);

handle = new PhoneAccountHandle(cName, appName);
telecomManager = (TelecomManager) context.getSystemService(Context.TELECOM_SERVICE);
}

private void registerPhoneAccount(Context appContext) {
if (!isConnectionServiceAvailable()) {
return;
}

ComponentName cName = new ComponentName(this.getAppContext(), VoiceConnectionService.class);
String appName = this.getApplicationName(appContext);

handle = new PhoneAccountHandle(cName, appName);
this.initializeTelecomManager();
String appName = this.getApplicationName(this.getAppContext());

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

telephonyManager = (TelephonyManager) this.getAppContext().getSystemService(Context.TELEPHONY_SERVICE);
telecomManager = (TelecomManager) this.getAppContext().getSystemService(Context.TELECOM_SERVICE);

telecomManager.registerPhoneAccount(account);
}
Expand Down Expand Up @@ -492,7 +520,8 @@ private Boolean hasPermissions() {
}

private static boolean hasPhoneAccount() {
return isConnectionServiceAvailable() && telecomManager != null && telecomManager.getPhoneAccount(handle).isEnabled();
return isConnectionServiceAvailable() && telecomManager != null
&& telecomManager.getPhoneAccount(handle) != null && telecomManager.getPhoneAccount(handle).isEnabled();
}

private void registerReceiver() {
Expand Down
10 changes: 9 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,19 @@ export default class RNCallKeep {
static hasDefaultPhoneAccount(): boolean {

}

static answerIncomingCall(uuid: string) {

}

static registerPhoneAccount(): void {

}

static registerAndroidEvents(): void {

}

static displayIncomingCall(
uuid: string,
handle: string,
Expand Down
15 changes: 15 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,21 @@ class RNCallKeep {
return this._setupIOS(options.ios);
};

registerPhoneAccount = () => {
if (isIOS) {
return;
}
RNCallKeepModule.registerPhoneAccount();
};


registerAndroidEvents = () => {
if (isIOS) {
return;
}
RNCallKeepModule.registerEvents();
};

hasDefaultPhoneAccount = async (options) => {
if (!isIOS) {
return this._hasDefaultPhoneAccount(options);
Expand Down

0 comments on commit 7c53ef2

Please sign in to comment.