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 <dev@mhibbe.de>
This commit is contained in:
Marcel Hibbe 2024-11-22 17:23:05 +01:00
parent 996720bf13
commit 716dfa6577
No known key found for this signature in database
GPG key ID: C793F8B59F43CE7B
8 changed files with 123 additions and 25 deletions

View file

@ -273,6 +273,12 @@ class ConversationsListActivity :
adapter!!.addListener(this) adapter!!.addListener(this)
prepareViews() prepareViews()
if (shouldShowIgnoreBatteryOptimizationHint()) {
showIgnoreBatteryOptimizationHint()
} else {
binding.chatListBatteryOptimizationIgnoredHint.visibility = View.GONE
}
showShareToScreen = hasActivityActionSendIntent() showShareToScreen = hasActivityActionSendIntent()
if (!eventBus.isRegistered(this)) { if (!eventBus.isRegistered(this)) {
@ -1453,11 +1459,23 @@ class ConversationsListActivity :
} }
REQUEST_POST_NOTIFICATIONS_PERMISSION -> { REQUEST_POST_NOTIFICATIONS_PERMISSION -> {
// whenever user allowed notifications, also check to ignore battery optimization
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) { if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (!PowerManagerUtils().isIgnoringBatteryOptimizations() && Log.d(TAG, "Notification permission was granted")
ClosedInterfaceImpl().isGooglePlayServicesAvailable } else {
) { Log.d(
TAG,
"Notification permission is denied. Either because user denied it when being asked. " +
"Or permission is already denied and android decided to not offer the dialog."
)
}
}
}
}
@SuppressLint("StringFormatInvalid")
private fun showIgnoreBatteryOptimizationHint() {
binding.chatListBatteryOptimizationIgnoredHint.visibility = View.VISIBLE
val dialogText = String.format( val dialogText = String.format(
context.resources.getString(R.string.nc_ignore_battery_optimization_dialog_text), context.resources.getString(R.string.nc_ignore_battery_optimization_dialog_text),
context.resources.getString(R.string.nc_app_name) context.resources.getString(R.string.nc_app_name)
@ -1473,21 +1491,20 @@ class ConversationsListActivity :
} }
.setNegativeButton(R.string.nc_common_dismiss, null) .setNegativeButton(R.string.nc_common_dismiss, null)
viewThemeUtils.dialog.colorMaterialAlertDialogBackground(this, dialogBuilder) viewThemeUtils.dialog.colorMaterialAlertDialogBackground(this, dialogBuilder)
binding.chatListBatteryOptimizationIgnoredHint.setOnClickListener {
val dialog = dialogBuilder.show() val dialog = dialogBuilder.show()
viewThemeUtils.platform.colorTextButtons( viewThemeUtils.platform.colorTextButtons(
dialog.getButton(AlertDialog.BUTTON_POSITIVE), dialog.getButton(AlertDialog.BUTTON_POSITIVE),
dialog.getButton(AlertDialog.BUTTON_NEGATIVE) dialog.getButton(AlertDialog.BUTTON_NEGATIVE)
) )
} }
} else {
Log.d(
TAG,
"Notification permission is denied. Either because user denied it when being asked. " +
"Or permission is already denied and android decided to not offer the dialog."
)
}
}
} }
private fun shouldShowIgnoreBatteryOptimizationHint() : Boolean {
return !PowerManagerUtils().isIgnoringBatteryOptimizations() &&
ClosedInterfaceImpl().isGooglePlayServicesAvailable &&
appPreferences.getShowIgnoreBatteryOptimizationHint()
} }
private fun openConversation(textToPaste: String? = "") { private fun openConversation(textToPaste: String? = "") {

View file

@ -662,6 +662,7 @@ class SettingsActivity : BaseActivity(), SetPhoneNumberDialogFragment.SetPhoneNu
private fun themeSwitchPreferences() { private fun themeSwitchPreferences() {
binding.run { binding.run {
listOf( listOf(
settingsShowIgnoreBatteryOptimizationHintSwitch,
settingsScreenLockSwitch, settingsScreenLockSwitch,
settingsScreenSecuritySwitch, settingsScreenSecuritySwitch,
settingsIncognitoKeyboardSwitch, settingsIncognitoKeyboardSwitch,
@ -857,6 +858,19 @@ class SettingsActivity : BaseActivity(), SetPhoneNumberDialogFragment.SetPhoneNu
} }
private fun setupCheckables() { 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.settingsScreenSecuritySwitch.isChecked = appPreferences.isScreenSecured
binding.settingsIncognitoKeyboardSwitch.isChecked = appPreferences.isKeyboardIncognito binding.settingsIncognitoKeyboardSwitch.isChecked = appPreferences.isKeyboardIncognito

View file

@ -178,6 +178,9 @@ public interface AppPreferences {
void deleteAllMessageQueuesFor(String userId); void deleteAllMessageQueuesFor(String userId);
boolean getShowIgnoreBatteryOptimizationHint();
void setShowIgnoreBatteryOptimizationHint(boolean showIgnoreBatteryOptimizationHint);
void clear(); void clear();
} }

View file

@ -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<Unit> {
async {
writeBoolean(SHOW_IGNORE_BATTERY_OPTIMIZATION_HINT, showIgnoreBatteryOptimizationHint)
}
}
override fun clear() {} override fun clear() {}
private suspend fun writeString(key: String, value: String) = 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 PHONE_BOOK_INTEGRATION_LAST_RUN = "phone_book_integration_last_run"
const val TYPING_STATUS = "typing_status" const val TYPING_STATUS = "typing_status"
const val MESSAGE_QUEUE = "@message_queue" const val MESSAGE_QUEUE = "@message_queue"
const val SHOW_IGNORE_BATTERY_OPTIMIZATION_HINT = "show_ignore_battery_optimization_hint"
private fun String.convertStringToArray(): Array<Float> { private fun String.convertStringToArray(): Array<Float> {
var varString = this var varString = this
val floatList = mutableListOf<Float>() val floatList = mutableListOf<Float>()

View file

@ -37,6 +37,18 @@
android:visibility="gone" android:visibility="gone"
tools:visibility="visible" /> tools:visibility="visible" />
<com.google.android.material.textview.MaterialTextView
android:id="@+id/chat_list_battery_optimization_ignored_hint"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/nc_warning"
android:gravity="center"
android:minHeight="40dp"
android:text="@string/nc_diagnose_battery_optimization_not_ignored"
android:textColor="@color/white"
android:visibility="gone"
tools:visibility="visible" />
<com.google.android.material.card.MaterialCardView <com.google.android.material.card.MaterialCardView
android:id="@+id/search_toolbar" android:id="@+id/search_toolbar"
android:layout_width="match_parent" android:layout_width="match_parent"

View file

@ -251,6 +251,41 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
tools:text="@string/nc_diagnose_battery_optimization_ignored"/> tools:text="@string/nc_diagnose_battery_optimization_ignored"/>
</LinearLayout> </LinearLayout>
<LinearLayout
android:id="@+id/settings_show_ignore_battery_optimization_hint"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:orientation="horizontal"
android:padding="@dimen/standard_padding">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<com.google.android.material.textview.MaterialTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/nc_show_ignore_battery_optimization_hint_title"
android:textSize="@dimen/headline_text_size"/>
<com.google.android.material.textview.MaterialTextView
android:id="@+id/settings_show_ignore_battery_optimization_hint_summary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/nc_show_ignore_battery_optimization_hint_description"/>
</LinearLayout>
<com.google.android.material.materialswitch.MaterialSwitch
android:id="@+id/settings_show_ignore_battery_optimization_hint_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="false"
android:gravity="center_vertical"/>
</LinearLayout>
</LinearLayout> </LinearLayout>
<LinearLayout <LinearLayout

View file

@ -92,5 +92,6 @@
<color name="icon_on_bg_default">#99000000</color> <color name="icon_on_bg_default">#99000000</color>
<color name="badge_color">#EF3B02</color> <color name="badge_color">#EF3B02</color>
<color name="secondary_button_background">#DBE2E9</color> <color name="secondary_button_background">#DBE2E9</color>
<color name="nc_warning">#FF9800</color>
</resources> </resources>

View file

@ -181,6 +181,8 @@ How to translate with transifex:
<string name="nc_ignore_battery_optimization_dialog_title">Ignore battery optimization</string> <string name="nc_ignore_battery_optimization_dialog_title">Ignore battery optimization</string>
<string name="nc_ignore_battery_optimization_dialog_text">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</string> <string name="nc_ignore_battery_optimization_dialog_text">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</string>
<string name="nc_show_ignore_battery_optimization_hint_title">Show battery optimization hint</string>
<string name="nc_show_ignore_battery_optimization_hint_description">When the battery optimization is not ignored, show a hint</string>
<string name="nc_diagnose_meta_category_title">Meta information</string> <string name="nc_diagnose_meta_category_title">Meta information</string>
<string name="nc_diagnose_meta_system_report_date">Generation of system report</string> <string name="nc_diagnose_meta_system_report_date">Generation of system report</string>