From 391735dc28a398312ecc201455efdb895970193c Mon Sep 17 00:00:00 2001 From: andrew Date: Wed, 31 May 2023 13:02:33 +0930 Subject: [PATCH] Convert CallToggleListener to Kotlin --- .../preferences/CallToggleListener.kt | 52 +++++++++++++++++++ .../PrivacySettingsPreferenceFragment.java | 50 ------------------ 2 files changed, 52 insertions(+), 50 deletions(-) create mode 100644 app/src/main/java/org/thoughtcrime/securesms/preferences/CallToggleListener.kt diff --git a/app/src/main/java/org/thoughtcrime/securesms/preferences/CallToggleListener.kt b/app/src/main/java/org/thoughtcrime/securesms/preferences/CallToggleListener.kt new file mode 100644 index 0000000000..de5f812fd0 --- /dev/null +++ b/app/src/main/java/org/thoughtcrime/securesms/preferences/CallToggleListener.kt @@ -0,0 +1,52 @@ +package org.thoughtcrime.securesms.preferences + +import android.Manifest +import android.app.AlertDialog +import android.content.DialogInterface +import androidx.appcompat.view.ContextThemeWrapper +import androidx.fragment.app.Fragment +import androidx.preference.Preference +import network.loki.messenger.R +import org.session.libsession.utilities.TextSecurePreferences +import org.session.libsession.utilities.TextSecurePreferences.Companion.setBooleanPreference +import org.thoughtcrime.securesms.permissions.Permissions + +internal class CallToggleListener( + private val context: Fragment, + private val setCallback: Function1 +) : Preference.OnPreferenceChangeListener { + + private fun requestMicrophonePermission() { + Permissions.with(context) + .request(Manifest.permission.RECORD_AUDIO) + .onAllGranted { + setBooleanPreference( + context.requireContext(), + TextSecurePreferences.CALL_NOTIFICATIONS_ENABLED, + true + ) + setCallback(true) + } + .onAnyDenied { setCallback(false) } + .execute() + } + + override fun onPreferenceChange(preference: Preference, newValue: Any): Boolean { + if (!(newValue as Boolean)) return true + + // check if we've shown the info dialog and check for microphone permissions + val dialog = AlertDialog.Builder( + ContextThemeWrapper( + context.requireContext(), R.style.ThemeOverlay_Session_AlertDialog + ) + ) + .setTitle(R.string.dialog_voice_video_title) + .setMessage(R.string.dialog_voice_video_message) + .setPositiveButton(R.string.dialog_link_preview_enable_button_title) { d: DialogInterface?, w: Int -> requestMicrophonePermission() } + .setNegativeButton(R.string.cancel) { d: DialogInterface?, w: Int -> } + .show() + val positiveButton = dialog.getButton(DialogInterface.BUTTON_POSITIVE) + positiveButton.contentDescription = "Enable" + return false + } +} \ No newline at end of file diff --git a/app/src/main/java/org/thoughtcrime/securesms/preferences/PrivacySettingsPreferenceFragment.java b/app/src/main/java/org/thoughtcrime/securesms/preferences/PrivacySettingsPreferenceFragment.java index ac03efa362..e7fbd14f95 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/preferences/PrivacySettingsPreferenceFragment.java +++ b/app/src/main/java/org/thoughtcrime/securesms/preferences/PrivacySettingsPreferenceFragment.java @@ -1,21 +1,17 @@ package org.thoughtcrime.securesms.preferences; -import android.Manifest; import android.app.Activity; import android.app.AlertDialog; import android.app.KeyguardManager; import android.content.Context; -import android.content.DialogInterface; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.provider.Settings; -import android.widget.Button; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.appcompat.view.ContextThemeWrapper; -import androidx.fragment.app.Fragment; import androidx.preference.Preference; import org.session.libsession.utilities.TextSecurePreferences; @@ -26,7 +22,6 @@ import org.thoughtcrime.securesms.service.KeyCachingService; import org.thoughtcrime.securesms.util.CallNotificationBuilder; import org.thoughtcrime.securesms.util.IntentUtils; -import kotlin.jvm.functions.Function1; import network.loki.messenger.BuildConfig; import network.loki.messenger.R; @@ -155,49 +150,4 @@ public class PrivacySettingsPreferenceFragment extends ListSummaryPreferenceFrag } } - private class CallToggleListener implements Preference.OnPreferenceChangeListener { - - private final Fragment context; - private final Function1 setCallback; - - private CallToggleListener(Fragment context, Function1 setCallback) { - this.context = context; - this.setCallback = setCallback; - } - - private void requestMicrophonePermission() { - Permissions.with(context) - .request(Manifest.permission.RECORD_AUDIO) - .onAllGranted(() -> { - TextSecurePreferences.setBooleanPreference(context.requireContext(), TextSecurePreferences.CALL_NOTIFICATIONS_ENABLED, true); - setCallback.invoke(true); - }) - .onAnyDenied(() -> setCallback.invoke(false)) - .execute(); - } - - @Override - public boolean onPreferenceChange(Preference preference, Object newValue) { - boolean val = (boolean) newValue; - if (val) { - // check if we've shown the info dialog and check for microphone permissions - AlertDialog dialog = new AlertDialog.Builder(new ContextThemeWrapper(context.requireContext(), R.style.ThemeOverlay_Session_AlertDialog)) - .setTitle(R.string.dialog_voice_video_title) - .setMessage(R.string.dialog_voice_video_message) - .setPositiveButton(R.string.dialog_link_preview_enable_button_title, (d, w) -> { - requestMicrophonePermission(); - }) - .setNegativeButton(R.string.cancel, (d, w) -> { - - }) - .show(); - Button positiveButton = dialog.getButton(DialogInterface.BUTTON_POSITIVE); - positiveButton.setContentDescription("Enable"); - return false; - } else { - return true; - } - } - } - }