Skip to content

Commit 05e69b1

Browse files
committed
Add checkDefault method for addressing #33
1 parent 5722bd5 commit 05e69b1

File tree

3 files changed

+68
-24
lines changed

3 files changed

+68
-24
lines changed

android/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="io.wazo.callkeep">
33

44
<uses-permission android:name="android.permission.CALL_PHONE" />
5+
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
56
</manifest>

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,21 @@ public void checkPhoneAccountPermission(Promise promise) {
176176
promise.resolve(hasPhoneAccount());
177177
}
178178

179+
@ReactMethod
180+
public void checkDefaultPhoneAccount(Promise promise) {
181+
if (!isConnectionServiceAvailable() || !hasPhoneAccount()) {
182+
promise.resolve(true);
183+
return;
184+
}
185+
186+
if (!Build.MANUFACTURER.equalsIgnoreCase("Samsung")) {
187+
promise.resolve(true);
188+
return;
189+
}
190+
191+
promise.resolve(telecomManager.getDefaultOutgoingPhoneAccount("tel") != null);
192+
}
193+
179194
@ReactMethod
180195
public void hasPhoneAccount(Promise promise) {
181196
promise.resolve(hasPhoneAccount());
@@ -217,6 +232,15 @@ public void openPhoneAccounts() {
217232
return;
218233
}
219234

235+
openPhoneAccountSettings();
236+
}
237+
238+
@ReactMethod
239+
public void openPhoneAccountSettings() {
240+
if (!isConnectionServiceAvailable()) {
241+
return;
242+
}
243+
220244
Intent intent = new Intent(TelecomManager.ACTION_CHANGE_PHONE_ACCOUNTS);
221245
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
222246
this.getAppContext().startActivity(intent);

index.js

Lines changed: 43 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ class RNCallKeep {
3737
return this._setupIOS(options.ios);
3838
};
3939

40+
checkDefault = async (options) => {
41+
if (!isIOS) {
42+
return this._checkDefault(options);
43+
}
44+
45+
return;
46+
};
47+
4048
displayIncomingCall = (uuid, handle, localizedCallerName, handleType = 'number', hasVideo = false) => {
4149
if (!isIOS) {
4250
RNCallKeepModule.displayIncomingCall(handle, localizedCallerName);
@@ -121,33 +129,44 @@ class RNCallKeep {
121129

122130
_setupAndroid = async (options) => {
123131
const hasAccount = await RNCallKeepModule.checkPhoneAccountPermission();
132+
const shouldOpenAccounts = await this._alert(options, hasAccount);
133+
134+
if (shouldOpenAccounts) {
135+
RNCallKeepModule.openPhoneAccounts();
136+
}
137+
};
138+
139+
_checkDefault = async (options) => {
140+
const hasDefault = await RNCallKeepModule.checkDefaultPhoneAccount();
141+
const shouldOpenAccounts = await this._alert(options, hasDefault);
124142

125-
return new Promise((resolve, reject) => {
126-
if (hasAccount) {
127-
return resolve();
128-
}
129-
130-
Alert.alert(
131-
options.alertTitle,
132-
options.alertDescription,
133-
[
134-
{
135-
text: options.cancelButton,
136-
onPress: reject,
137-
style: 'cancel',
138-
},
139-
{ text: options.okButton,
140-
onPress: () => {
141-
RNCallKeepModule.openPhoneAccounts();
142-
resolve();
143-
}
144-
},
145-
],
146-
{ cancelable: true },
147-
);
148-
});
143+
if (shouldOpenAccounts) {
144+
RNCallKeepModule.openPhoneAccountSettings();
145+
}
149146
};
150147

148+
_alert = async (options, condition) => new Promise((resolve, reject) => {
149+
if (condition) {
150+
return resolve(false);
151+
}
152+
153+
Alert.alert(
154+
options.alertTitle,
155+
options.alertDescription,
156+
[
157+
{
158+
text: options.cancelButton,
159+
onPress: reject,
160+
style: 'cancel',
161+
},
162+
{ text: options.okButton,
163+
onPress: () => resolve(true)
164+
},
165+
],
166+
{ cancelable: true },
167+
);
168+
});
169+
151170
backToForeground() {
152171
if (isIOS) {
153172
return;

0 commit comments

Comments
 (0)