Skip to content

[Android] Split setup into registerPhoneAccount and registerAndroidEvents #242

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/
26 changes: 26 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,30 @@ 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._
_On iOS you still have to call `setup()`._

```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._
_On iOS you still have to call `setup()`._

```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