From 4336266b7f5f0244a56a05111570f7ea1521f7e3 Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Thu, 28 Sep 2023 12:26:01 +0200 Subject: [PATCH] Disable account creation if push notifications aren't available --- .../ui/assistant/fragment/RegisterFragment.kt | 10 +++++ .../viewmodel/AccountCreationViewModel.kt | 5 +++ .../main/history/model/CallLogHistoryModel.kt | 2 +- .../ui/main/history/model/CallLogModel.kt | 2 +- .../java/org/linphone/utils/LinphoneUtils.kt | 42 ++++++++++++++++++- .../layout/assistant_register_fragment.xml | 6 ++- app/src/main/res/values/strings.xml | 1 + 7 files changed, 64 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/org/linphone/ui/assistant/fragment/RegisterFragment.kt b/app/src/main/java/org/linphone/ui/assistant/fragment/RegisterFragment.kt index b713412f3..5950616ef 100644 --- a/app/src/main/java/org/linphone/ui/assistant/fragment/RegisterFragment.kt +++ b/app/src/main/java/org/linphone/ui/assistant/fragment/RegisterFragment.kt @@ -126,6 +126,16 @@ class RegisterFragment : Fragment() { override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {} }) + viewModel.pushNotificationsAvailable.observe(viewLifecycleOwner) { available -> + if (!available) { + val text = getString(R.string.assistant_account_register_unavailable_no_push_toast) + (requireActivity() as AssistantActivity).showRedToast( + text, + R.drawable.warning_circle + ) + } + } + viewModel.normalizedPhoneNumberEvent.observe(viewLifecycleOwner) { it.consume { number -> showPhoneNumberConfirmationDialog(number) diff --git a/app/src/main/java/org/linphone/ui/assistant/viewmodel/AccountCreationViewModel.kt b/app/src/main/java/org/linphone/ui/assistant/viewmodel/AccountCreationViewModel.kt index 4891be12a..9848bb8e2 100644 --- a/app/src/main/java/org/linphone/ui/assistant/viewmodel/AccountCreationViewModel.kt +++ b/app/src/main/java/org/linphone/ui/assistant/viewmodel/AccountCreationViewModel.kt @@ -46,6 +46,7 @@ import org.linphone.core.Factory import org.linphone.core.tools.Log import org.linphone.utils.AppUtils import org.linphone.utils.Event +import org.linphone.utils.LinphoneUtils class AccountCreationViewModel @UiThread constructor() : ViewModel() { companion object { @@ -74,6 +75,8 @@ class AccountCreationViewModel @UiThread constructor() : ViewModel() { val createEnabled = MediatorLiveData() + val pushNotificationsAvailable = MutableLiveData() + val confirmationMessage = MutableLiveData() val smsCodeFirstDigit = MutableLiveData() @@ -274,6 +277,8 @@ class AccountCreationViewModel @UiThread constructor() : ViewModel() { operationInProgress.value = false coreContext.postOnCoreThread { core -> + pushNotificationsAvailable.postValue(LinphoneUtils.arePushNotificationsAvailable(core)) + val dialPlans = Factory.instance().dialPlans.toList() for (dialPlan in dialPlans) { dialPlansList.add(dialPlan) diff --git a/app/src/main/java/org/linphone/ui/main/history/model/CallLogHistoryModel.kt b/app/src/main/java/org/linphone/ui/main/history/model/CallLogHistoryModel.kt index fa0cbb0ff..e0a30232a 100644 --- a/app/src/main/java/org/linphone/ui/main/history/model/CallLogHistoryModel.kt +++ b/app/src/main/java/org/linphone/ui/main/history/model/CallLogHistoryModel.kt @@ -42,6 +42,6 @@ class CallLogHistoryModel @WorkerThread constructor(val callLog: CallLog) { ) isSuccessful.postValue(callLog.status == Call.Status.Success) - iconResId.postValue(LinphoneUtils.getIconResId(callLog.status, callLog.dir)) + iconResId.postValue(LinphoneUtils.getCallIconResId(callLog.status, callLog.dir)) } } diff --git a/app/src/main/java/org/linphone/ui/main/history/model/CallLogModel.kt b/app/src/main/java/org/linphone/ui/main/history/model/CallLogModel.kt index 62f870bfb..bb4a27cd5 100644 --- a/app/src/main/java/org/linphone/ui/main/history/model/CallLogModel.kt +++ b/app/src/main/java/org/linphone/ui/main/history/model/CallLogModel.kt @@ -55,7 +55,7 @@ class CallLogModel @WorkerThread constructor(private val callLog: CallLog) { friendExists = false } - iconResId.postValue(LinphoneUtils.getIconResId(callLog.status, callLog.dir)) + iconResId.postValue(LinphoneUtils.getCallIconResId(callLog.status, callLog.dir)) } @UiThread diff --git a/app/src/main/java/org/linphone/utils/LinphoneUtils.kt b/app/src/main/java/org/linphone/utils/LinphoneUtils.kt index 341775236..606d5d8a9 100644 --- a/app/src/main/java/org/linphone/utils/LinphoneUtils.kt +++ b/app/src/main/java/org/linphone/utils/LinphoneUtils.kt @@ -160,9 +160,49 @@ class LinphoneUtils { return core.defaultAccount?.params?.audioVideoConferenceFactoryAddress != null } + @WorkerThread + fun arePushNotificationsAvailable(core: Core): Boolean { + if (!core.isPushNotificationAvailable) { + Log.w( + "$TAG Push notifications aren't available in the Core, disable account creation" + ) + return false + } + + val pushConfig = core.pushNotificationConfig + if (pushConfig == null) { + Log.w( + "$TAG Core's push notifications configuration is null, disable account creation" + ) + return false + } + + if (pushConfig.provider.isNullOrEmpty()) { + Log.w( + "$TAG Core's push notifications configuration provider is null or empty, disable account creation" + ) + return false + } + if (pushConfig.param.isNullOrEmpty()) { + Log.w( + "$TAG Core's push notifications configuration param is null or empty, disable account creation" + ) + return false + } + if (pushConfig.prid.isNullOrEmpty()) { + Log.w( + "$TAG Core's push notifications configuration prid is null or empty, disable account creation" + ) + return false + } + + Log.i("$TAG Push notifications seems to be available") + return true + } + @AnyThread @IntegerRes - fun getIconResId(callStatus: Status, callDir: Dir): Int { + fun getCallIconResId(callStatus: Status, callDir: Dir): Int { return when (callStatus) { Status.Missed -> { if (callDir == Dir.Outgoing) { diff --git a/app/src/main/res/layout/assistant_register_fragment.xml b/app/src/main/res/layout/assistant_register_fragment.xml index 3753cd582..ad00a082a 100644 --- a/app/src/main/res/layout/assistant_register_fragment.xml +++ b/app/src/main/res/layout/assistant_register_fragment.xml @@ -87,6 +87,7 @@ Default Interoperable Continue + Push notifications not available, account creation disabled This account already exists Invalid characters: capital letters and special characters are not allowed Username is too short