From 716dfa6577ab5d28690301d003d5e574476df681 Mon Sep 17 00:00:00 2001 From: Marcel Hibbe Date: Fri, 22 Nov 2024 17:23:05 +0100 Subject: [PATCH] Add permanent hint when battery optimization is not ignored Also add an option to not show the hint, when user doesn't want to ignore battery optimization. Only show these features when gplay services are available. Otherwise don't show them as it would pretend notifications would be possible without gplay. Signed-off-by: Marcel Hibbe --- .../ConversationsListActivity.kt | 67 ++++++++++++------- .../talk/settings/SettingsActivity.kt | 14 ++++ .../utils/preferences/AppPreferences.java | 3 + .../utils/preferences/AppPreferencesImpl.kt | 14 ++++ .../res/layout/activity_conversations.xml | 12 ++++ app/src/main/res/layout/activity_settings.xml | 35 ++++++++++ app/src/main/res/values/colors.xml | 1 + app/src/main/res/values/strings.xml | 2 + 8 files changed, 123 insertions(+), 25 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 68b0e14e1..46d52cbdc 100644 --- a/app/src/main/java/com/nextcloud/talk/conversationlist/ConversationsListActivity.kt +++ b/app/src/main/java/com/nextcloud/talk/conversationlist/ConversationsListActivity.kt @@ -273,6 +273,12 @@ class ConversationsListActivity : adapter!!.addListener(this) prepareViews() + if (shouldShowIgnoreBatteryOptimizationHint()) { + showIgnoreBatteryOptimizationHint() + } else { + binding.chatListBatteryOptimizationIgnoredHint.visibility = View.GONE + } + showShareToScreen = hasActivityActionSendIntent() if (!eventBus.isRegistered(this)) { @@ -1453,32 +1459,8 @@ class ConversationsListActivity : } REQUEST_POST_NOTIFICATIONS_PERMISSION -> { - // whenever user allowed notifications, also check to ignore battery optimization if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_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) - ) - } + Log.d(TAG, "Notification permission was granted") } else { Log.d( TAG, @@ -1490,6 +1472,41 @@ 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 shouldShowIgnoreBatteryOptimizationHint() : Boolean { + return !PowerManagerUtils().isIgnoringBatteryOptimizations() && + ClosedInterfaceImpl().isGooglePlayServicesAvailable && + appPreferences.getShowIgnoreBatteryOptimizationHint() + } + private fun openConversation(textToPaste: String? = "") { if (CallActivity.active && selectedConversation!!.token != ApplicationWideCurrentRoomHolder.getInstance().currentRoomToken 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 72a7e9795..493bcae75 100644 --- a/app/src/main/java/com/nextcloud/talk/settings/SettingsActivity.kt +++ b/app/src/main/java/com/nextcloud/talk/settings/SettingsActivity.kt @@ -662,6 +662,7 @@ class SettingsActivity : BaseActivity(), SetPhoneNumberDialogFragment.SetPhoneNu private fun themeSwitchPreferences() { binding.run { listOf( + settingsShowIgnoreBatteryOptimizationHintSwitch, settingsScreenLockSwitch, settingsScreenSecuritySwitch, settingsIncognitoKeyboardSwitch, @@ -857,6 +858,19 @@ class SettingsActivity : BaseActivity(), SetPhoneNumberDialogFragment.SetPhoneNu } private fun setupCheckables() { + binding.settingsShowIgnoreBatteryOptimizationHintSwitch.isChecked = + appPreferences.showIgnoreBatteryOptimizationHint + + if (ClosedInterfaceImpl().isGooglePlayServicesAvailable) { + binding.settingsShowIgnoreBatteryOptimizationHint.setOnClickListener { + val isChecked = binding.settingsShowIgnoreBatteryOptimizationHintSwitch.isChecked + binding.settingsShowIgnoreBatteryOptimizationHintSwitch.isChecked = !isChecked + appPreferences.setShowIgnoreBatteryOptimizationHint(!isChecked) + } + } else { + binding.settingsShowIgnoreBatteryOptimizationHint.visibility = View.GONE + } + binding.settingsScreenSecuritySwitch.isChecked = appPreferences.isScreenSecured binding.settingsIncognitoKeyboardSwitch.isChecked = appPreferences.isKeyboardIncognito 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 606614a72..b779337ed 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,6 +178,9 @@ public interface AppPreferences { void deleteAllMessageQueuesFor(String userId); + boolean getShowIgnoreBatteryOptimizationHint(); + + void setShowIgnoreBatteryOptimizationHint(boolean showIgnoreBatteryOptimizationHint); 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 c3e2fcc48..6a913ea19 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,6 +544,19 @@ class AppPreferencesImpl(val context: Context) : AppPreferences { } } + override fun getShowIgnoreBatteryOptimizationHint(): Boolean { + return runBlocking { async { + readBoolean(SHOW_IGNORE_BATTERY_OPTIMIZATION_HINT, true).first() + } }.getCompleted() + } + + override fun setShowIgnoreBatteryOptimizationHint(showIgnoreBatteryOptimizationHint: Boolean) = + runBlocking { + async { + writeBoolean(SHOW_IGNORE_BATTERY_OPTIMIZATION_HINT, showIgnoreBatteryOptimizationHint) + } + } + override fun clear() {} private suspend fun writeString(key: String, value: String) = @@ -628,6 +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" 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 bdf5880e1..0c71bb4ba 100644 --- a/app/src/main/res/layout/activity_conversations.xml +++ b/app/src/main/res/layout/activity_conversations.xml @@ -37,6 +37,18 @@ android:visibility="gone" tools:visibility="visible" /> + + + + + + + + + + + + + + #99000000 #EF3B02 #DBE2E9 + #FF9800 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index ca4c364cf..01f10ccdf 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -181,6 +181,8 @@ 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 Meta information Generation of system report