From b1e550b049a28dcf8b174828da75f2211ee428bc Mon Sep 17 00:00:00 2001 From: Marcel Hibbe Date: Mon, 25 Nov 2024 13:18:51 +0100 Subject: [PATCH] Show a warning if notifications are not allowed or battery optimization is enabled Open notification settings when click on warning Also, still offer to enable notifications and disable battery optimization on first install Signed-off-by: Marcel Hibbe --- .../ConversationsListActivity.kt | 89 +++++++++++-------- .../talk/settings/SettingsActivity.kt | 42 ++++++--- .../nextcloud/talk/utils/bundle/BundleKeys.kt | 1 + .../utils/preferences/AppPreferences.java | 4 +- .../utils/preferences/AppPreferencesImpl.kt | 10 +-- .../res/layout/activity_conversations.xml | 4 +- app/src/main/res/layout/activity_settings.xml | 74 +++++++-------- app/src/main/res/values/strings.xml | 6 +- 8 files changed, 135 insertions(+), 95 deletions(-) diff --git a/app/src/main/java/com/nextcloud/talk/conversationlist/ConversationsListActivity.kt b/app/src/main/java/com/nextcloud/talk/conversationlist/ConversationsListActivity.kt index 46d52cbdc..f9807199e 100644 --- a/app/src/main/java/com/nextcloud/talk/conversationlist/ConversationsListActivity.kt +++ b/app/src/main/java/com/nextcloud/talk/conversationlist/ConversationsListActivity.kt @@ -121,6 +121,7 @@ import com.nextcloud.talk.utils.bundle.BundleKeys.KEY_FORWARD_MSG_TEXT import com.nextcloud.talk.utils.bundle.BundleKeys.KEY_INTERNAL_USER_ID import com.nextcloud.talk.utils.bundle.BundleKeys.KEY_NEW_CONVERSATION import com.nextcloud.talk.utils.bundle.BundleKeys.KEY_ROOM_TOKEN +import com.nextcloud.talk.utils.bundle.BundleKeys.KEY_SCROLL_TO_NOTIFICATION_CATEGORY import com.nextcloud.talk.utils.bundle.BundleKeys.KEY_SHARED_TEXT import com.nextcloud.talk.utils.permissions.PlatformPermissionUtil import com.nextcloud.talk.utils.power.PowerManagerUtils @@ -273,11 +274,7 @@ class ConversationsListActivity : adapter!!.addListener(this) prepareViews() - if (shouldShowIgnoreBatteryOptimizationHint()) { - showIgnoreBatteryOptimizationHint() - } else { - binding.chatListBatteryOptimizationIgnoredHint.visibility = View.GONE - } + updateNotificationWarning() showShareToScreen = hasActivityActionSendIntent() @@ -309,6 +306,14 @@ class ConversationsListActivity : showSearchOrToolbar() } + private fun updateNotificationWarning() { + if (shouldShowNotificationWarning()) { + showNotificationWarning() + } else { + binding.chatListNotificationWarning.visibility = View.GONE + } + } + private fun initObservers() { this.lifecycleScope.launch { networkMonitor.isOnline.onEach { isOnline -> @@ -1461,6 +1466,31 @@ class ConversationsListActivity : REQUEST_POST_NOTIFICATIONS_PERMISSION -> { if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) { Log.d(TAG, "Notification permission was granted") + + if (!PowerManagerUtils().isIgnoringBatteryOptimizations() && + ClosedInterfaceImpl().isGooglePlayServicesAvailable + ) { + val dialogText = String.format( + context.resources.getString(R.string.nc_ignore_battery_optimization_dialog_text), + context.resources.getString(R.string.nc_app_name) + ) + + val dialogBuilder = MaterialAlertDialogBuilder(this) + .setTitle(R.string.nc_ignore_battery_optimization_dialog_title) + .setMessage(dialogText) + .setPositiveButton(R.string.nc_ok) { _, _ -> + startActivity( + Intent(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS) + ) + } + .setNegativeButton(R.string.nc_common_dismiss, null) + viewThemeUtils.dialog.colorMaterialAlertDialogBackground(this, dialogBuilder) + val dialog = dialogBuilder.show() + viewThemeUtils.platform.colorTextButtons( + dialog.getButton(AlertDialog.BUTTON_POSITIVE), + dialog.getButton(AlertDialog.BUTTON_NEGATIVE) + ) + } } else { Log.d( TAG, @@ -1472,39 +1502,28 @@ class ConversationsListActivity : } } - @SuppressLint("StringFormatInvalid") - private fun showIgnoreBatteryOptimizationHint() { - binding.chatListBatteryOptimizationIgnoredHint.visibility = View.VISIBLE - - val dialogText = String.format( - context.resources.getString(R.string.nc_ignore_battery_optimization_dialog_text), - context.resources.getString(R.string.nc_app_name) - ) - - val dialogBuilder = MaterialAlertDialogBuilder(this) - .setTitle(R.string.nc_ignore_battery_optimization_dialog_title) - .setMessage(dialogText) - .setPositiveButton(R.string.nc_ok) { _, _ -> - startActivity( - Intent(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS) - ) - } - .setNegativeButton(R.string.nc_common_dismiss, null) - viewThemeUtils.dialog.colorMaterialAlertDialogBackground(this, dialogBuilder) - - binding.chatListBatteryOptimizationIgnoredHint.setOnClickListener { - val dialog = dialogBuilder.show() - viewThemeUtils.platform.colorTextButtons( - dialog.getButton(AlertDialog.BUTTON_POSITIVE), - dialog.getButton(AlertDialog.BUTTON_NEGATIVE) - ) + private fun showNotificationWarning() { + binding.chatListNotificationWarning.visibility = View.VISIBLE + binding.chatListNotificationWarning.setOnClickListener { + val bundle = Bundle() + bundle.putBoolean(KEY_SCROLL_TO_NOTIFICATION_CATEGORY, true) + val settingsIntent = Intent(context, SettingsActivity::class.java) + settingsIntent.putExtras(bundle) + startActivity(settingsIntent) } } - private fun shouldShowIgnoreBatteryOptimizationHint() : Boolean { - return !PowerManagerUtils().isIgnoringBatteryOptimizations() && - ClosedInterfaceImpl().isGooglePlayServicesAvailable && - appPreferences.getShowIgnoreBatteryOptimizationHint() + private fun shouldShowNotificationWarning() : Boolean { + val notificationPermissionNotGranted = Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU && + !platformPermissionUtil.isPostNotificationsPermissionGranted() + val batteryOptimizationNotIgnored = !PowerManagerUtils().isIgnoringBatteryOptimizations() + + val settingsOfUserAreWrong = notificationPermissionNotGranted || batteryOptimizationNotIgnored + val userWantsToBeNotifiedAboutWrongSettings = appPreferences.getShowNotificationWarning() + + return settingsOfUserAreWrong && + userWantsToBeNotifiedAboutWrongSettings && + ClosedInterfaceImpl().isGooglePlayServicesAvailable } private fun openConversation(textToPaste: String? = "") { diff --git a/app/src/main/java/com/nextcloud/talk/settings/SettingsActivity.kt b/app/src/main/java/com/nextcloud/talk/settings/SettingsActivity.kt index 493bcae75..da04c9305 100644 --- a/app/src/main/java/com/nextcloud/talk/settings/SettingsActivity.kt +++ b/app/src/main/java/com/nextcloud/talk/settings/SettingsActivity.kt @@ -77,6 +77,7 @@ import com.nextcloud.talk.utils.NotificationUtils.getCallRingtoneUri import com.nextcloud.talk.utils.NotificationUtils.getMessageRingtoneUri import com.nextcloud.talk.utils.SecurityUtils import com.nextcloud.talk.utils.SpreedFeatures +import com.nextcloud.talk.utils.bundle.BundleKeys.KEY_SCROLL_TO_NOTIFICATION_CATEGORY import com.nextcloud.talk.utils.permissions.PlatformPermissionUtil import com.nextcloud.talk.utils.power.PowerManagerUtils import com.nextcloud.talk.utils.preferences.AppPreferencesImpl @@ -129,6 +130,7 @@ class SettingsActivity : BaseActivity(), SetPhoneNumberDialogFragment.SetPhoneNu private lateinit var phoneBookIntegrationFlow: Flow private var profileQueryDisposable: Disposable? = null private var dbQueryDisposable: Disposable? = null + private var scrollToNotificationCategory: Boolean? = false @SuppressLint("StringFormatInvalid") override fun onCreate(savedInstanceState: Bundle?) { @@ -143,6 +145,7 @@ class SettingsActivity : BaseActivity(), SetPhoneNumberDialogFragment.SetPhoneNu binding.avatarImage.let { ViewCompat.setTransitionName(it, "userAvatar.transitionTag") } getCurrentUser() + handleIntent(intent) setupLicenceSetting() @@ -162,6 +165,11 @@ class SettingsActivity : BaseActivity(), SetPhoneNumberDialogFragment.SetPhoneNu setupClientCertView() } + private fun handleIntent(intent: Intent) { + val extras: Bundle? = intent.extras + scrollToNotificationCategory = extras?.getBoolean(KEY_SCROLL_TO_NOTIFICATION_CATEGORY) + } + override fun onResume() { super.onResume() supportActionBar?.show() @@ -210,6 +218,21 @@ class SettingsActivity : BaseActivity(), SetPhoneNumberDialogFragment.SetPhoneNu themeTitles() themeSwitchPreferences() + + if (scrollToNotificationCategory == true) { + scrollToNotificationCategory() + } + } + + private fun scrollToNotificationCategory() { + binding.scrollView.post { + val scrollViewLocation = IntArray(2) + val targetLocation = IntArray(2) + binding.scrollView.getLocationOnScreen(scrollViewLocation) + binding.settingsNotificationsCategory.getLocationOnScreen(targetLocation) + val offset = targetLocation[1] - scrollViewLocation[1] + binding.scrollView.smoothScrollBy(0, offset) + } } private fun loadCapabilitiesAndUpdateSettings() { @@ -255,9 +278,6 @@ class SettingsActivity : BaseActivity(), SetPhoneNumberDialogFragment.SetPhoneNu } private fun setupNotificationSettings() { - binding.settingsNotificationsTitle.text = resources!!.getString( - R.string.nc_settings_notification_sounds_post_oreo - ) setupNotificationSoundsSettings() setupNotificationPermissionSettings() } @@ -662,7 +682,7 @@ class SettingsActivity : BaseActivity(), SetPhoneNumberDialogFragment.SetPhoneNu private fun themeSwitchPreferences() { binding.run { listOf( - settingsShowIgnoreBatteryOptimizationHintSwitch, + settingsShowNotificationWarningSwitch, settingsScreenLockSwitch, settingsScreenSecuritySwitch, settingsIncognitoKeyboardSwitch, @@ -858,17 +878,17 @@ class SettingsActivity : BaseActivity(), SetPhoneNumberDialogFragment.SetPhoneNu } private fun setupCheckables() { - binding.settingsShowIgnoreBatteryOptimizationHintSwitch.isChecked = - appPreferences.showIgnoreBatteryOptimizationHint + binding.settingsShowNotificationWarningSwitch.isChecked = + appPreferences.showNotificationWarning if (ClosedInterfaceImpl().isGooglePlayServicesAvailable) { - binding.settingsShowIgnoreBatteryOptimizationHint.setOnClickListener { - val isChecked = binding.settingsShowIgnoreBatteryOptimizationHintSwitch.isChecked - binding.settingsShowIgnoreBatteryOptimizationHintSwitch.isChecked = !isChecked - appPreferences.setShowIgnoreBatteryOptimizationHint(!isChecked) + binding.settingsShowNotificationWarning.setOnClickListener { + val isChecked = binding.settingsShowNotificationWarningSwitch.isChecked + binding.settingsShowNotificationWarningSwitch.isChecked = !isChecked + appPreferences.setShowNotificationWarning(!isChecked) } } else { - binding.settingsShowIgnoreBatteryOptimizationHint.visibility = View.GONE + binding.settingsShowNotificationWarning.visibility = View.GONE } binding.settingsScreenSecuritySwitch.isChecked = appPreferences.isScreenSecured diff --git a/app/src/main/java/com/nextcloud/talk/utils/bundle/BundleKeys.kt b/app/src/main/java/com/nextcloud/talk/utils/bundle/BundleKeys.kt index 53de01b27..4080f315a 100644 --- a/app/src/main/java/com/nextcloud/talk/utils/bundle/BundleKeys.kt +++ b/app/src/main/java/com/nextcloud/talk/utils/bundle/BundleKeys.kt @@ -80,4 +80,5 @@ object BundleKeys { const val KEY_CREDENTIALS: String = "KEY_CREDENTIALS" const val KEY_FIELD_MAP: String = "KEY_FIELD_MAP" const val KEY_CHAT_URL: String = "KEY_CHAT_URL" + const val KEY_SCROLL_TO_NOTIFICATION_CATEGORY: String = "KEY_SCROLL_TO_NOTIFICATION_CATEGORY" } diff --git a/app/src/main/java/com/nextcloud/talk/utils/preferences/AppPreferences.java b/app/src/main/java/com/nextcloud/talk/utils/preferences/AppPreferences.java index b779337ed..ffe3e063b 100644 --- a/app/src/main/java/com/nextcloud/talk/utils/preferences/AppPreferences.java +++ b/app/src/main/java/com/nextcloud/talk/utils/preferences/AppPreferences.java @@ -178,9 +178,9 @@ public interface AppPreferences { void deleteAllMessageQueuesFor(String userId); - boolean getShowIgnoreBatteryOptimizationHint(); + boolean getShowNotificationWarning(); - void setShowIgnoreBatteryOptimizationHint(boolean showIgnoreBatteryOptimizationHint); + void setShowNotificationWarning(boolean showNotificationWarning); void clear(); } diff --git a/app/src/main/java/com/nextcloud/talk/utils/preferences/AppPreferencesImpl.kt b/app/src/main/java/com/nextcloud/talk/utils/preferences/AppPreferencesImpl.kt index 6a913ea19..a576fe9a0 100644 --- a/app/src/main/java/com/nextcloud/talk/utils/preferences/AppPreferencesImpl.kt +++ b/app/src/main/java/com/nextcloud/talk/utils/preferences/AppPreferencesImpl.kt @@ -544,16 +544,16 @@ class AppPreferencesImpl(val context: Context) : AppPreferences { } } - override fun getShowIgnoreBatteryOptimizationHint(): Boolean { + override fun getShowNotificationWarning(): Boolean { return runBlocking { async { - readBoolean(SHOW_IGNORE_BATTERY_OPTIMIZATION_HINT, true).first() + readBoolean(SHOW_NOTIFICATION_WARNING, true).first() } }.getCompleted() } - override fun setShowIgnoreBatteryOptimizationHint(showIgnoreBatteryOptimizationHint: Boolean) = + override fun setShowNotificationWarning(showNotificationWarning: Boolean) = runBlocking { async { - writeBoolean(SHOW_IGNORE_BATTERY_OPTIMIZATION_HINT, showIgnoreBatteryOptimizationHint) + writeBoolean(SHOW_NOTIFICATION_WARNING, showNotificationWarning) } } @@ -641,7 +641,7 @@ class AppPreferencesImpl(val context: Context) : AppPreferences { const val PHONE_BOOK_INTEGRATION_LAST_RUN = "phone_book_integration_last_run" const val TYPING_STATUS = "typing_status" const val MESSAGE_QUEUE = "@message_queue" - const val SHOW_IGNORE_BATTERY_OPTIMIZATION_HINT = "show_ignore_battery_optimization_hint" + const val SHOW_NOTIFICATION_WARNING = "show_notification_warning" private fun String.convertStringToArray(): Array { var varString = this val floatList = mutableListOf() diff --git a/app/src/main/res/layout/activity_conversations.xml b/app/src/main/res/layout/activity_conversations.xml index 0c71bb4ba..7518b482d 100644 --- a/app/src/main/res/layout/activity_conversations.xml +++ b/app/src/main/res/layout/activity_conversations.xml @@ -38,13 +38,13 @@ tools:visibility="visible" /> diff --git a/app/src/main/res/layout/activity_settings.xml b/app/src/main/res/layout/activity_settings.xml index c69b5b8c7..4011b26a9 100644 --- a/app/src/main/res/layout/activity_settings.xml +++ b/app/src/main/res/layout/activity_settings.xml @@ -201,7 +201,7 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="@dimen/standard_margin" - android:text="@string/nc_settings_notification_sounds" + android:text="@string/nc_settings_notification_sounds_post_oreo" android:textSize="@dimen/headline_text_size" android:textStyle="bold"/> @@ -211,6 +211,42 @@ android:layout_height="wrap_content" android:orientation="vertical"> + + + + + + + + + + + + + - - - - - - - - - - - - - Talk app is not installed on the server you tried to authenticate against Your already existing account was updated, instead of adding a new one The account is scheduled for deletion, and cannot be changed - Notification sounds Notifications Calls call_ringtone @@ -181,8 +180,9 @@ How to translate with transifex: Ignore battery optimization Battery optimization is not ignored. This should be changed to make sure that notifications work in the background! Please click OK and select \"All apps\" -> %1$s -> Do not optimize - Show battery optimization hint - When the battery optimization is not ignored, show a hint + Show notification warning + When notifications are not set up correctly, show a warning + Notifications are not set up correctly Meta information Generation of system report