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)
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

View file

@ -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

View file

@ -178,6 +178,9 @@ public interface AppPreferences {
void deleteAllMessageQueuesFor(String userId);
boolean getShowIgnoreBatteryOptimizationHint();
void setShowIgnoreBatteryOptimizationHint(boolean showIgnoreBatteryOptimizationHint);
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() {}
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<Float> {
var varString = this
val floatList = mutableListOf<Float>()

View file

@ -37,6 +37,18 @@
android:visibility="gone"
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
android:id="@+id/search_toolbar"
android:layout_width="match_parent"

View file

@ -251,6 +251,41 @@
android:layout_height="wrap_content"
tools:text="@string/nc_diagnose_battery_optimization_ignored"/>
</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

View file

@ -92,5 +92,6 @@
<color name="icon_on_bg_default">#99000000</color>
<color name="badge_color">#EF3B02</color>
<color name="secondary_button_background">#DBE2E9</color>
<color name="nc_warning">#FF9800</color>
</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_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_system_report_date">Generation of system report</string>