Skip to content

Commit

Permalink
add GmsCore background data usage restriction notification
Browse files Browse the repository at this point in the history
  • Loading branch information
muhomorr authored and thestinger committed Nov 27, 2024
1 parent 268bd7c commit 05fc6f0
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 1 deletion.
5 changes: 4 additions & 1 deletion res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,10 @@ To receive them quicker, set “Battery usage” to “Unrestricted” in Play s
<string name="notif_gms_crash_report">Report to developers</string>

<string name="notif_gmscore_power_exemption">
"Push notifications will be delayed, because Play services app is not allowed to always run in the background. Tap to resolve."
"Push notifications will be delayed because Play services app is not allowed to always run in the background. Tap to resolve."
</string>
<string name="notif_gmscore_background_data_exemption">
"Push notifications will be delayed because Play services app is not allowed to use mobile data from the background. Tap to open Play services settings, which contain mobile data usage settings."
</string>
<string name="dont_show_again">Don\'t show again</string>

Expand Down
1 change: 1 addition & 0 deletions src/app/grapheneos/gmscompat/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public interface MainProcessPrefs {
String LOCATION_REQUEST_REDIRECTION_ENABLED = "enabled_redirections"; // historical name

String GmsCore_POWER_EXEMPTION_PROMPT_DISMISSED = "GmsCore_power_exemption_prompt_dismissed_2";
String GmsCore_BACKGROUND_DATA_EXEMPTION_PROMPT_DISMISSED = "GmsCore_background_data_exemption_prompt_dismissed";
String NOTIFICATION_DO_NOT_SHOW_AGAIN_PREFIX = "do_not_show_notification_";

// set of package names of core GMS components that Play Store to allowed to update to unknown versions
Expand Down
4 changes: 4 additions & 0 deletions src/app/grapheneos/gmscompat/BinderGms2Gca.kt
Original file line number Diff line number Diff line change
Expand Up @@ -534,4 +534,8 @@ object BinderGms2Gca : IGms2Gca.Stub() {
show(notifId)
}
}

override fun maybeShowGmsCoreRestrictedBackgroundDataNotif() {
Notifications.handleGmsCoreRestrictedBackgroundDataNotif()
}
}
43 changes: 43 additions & 0 deletions src/app/grapheneos/gmscompat/Notifications.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ object Notifications {
const val ID_CONTACTS_SYNC_PROMPT = 8
const val ID_MISSING_POST_NOTIFICATIONS_PERM = 9;
const val ID_ANDROID_AUTO_NEEDS_BASELINE_PERMS = 10
const val ID_GmsCore_BACKGROUND_DATA_EXEMPTION_PROMPT = 11

private val uniqueNotificationId = AtomicInteger(10_000)
fun generateUniqueNotificationId() = uniqueNotificationId.getAndIncrement()
Expand Down Expand Up @@ -141,6 +142,48 @@ object Notifications {
}
}

private var handledGmsCoreBgDataExemption = false

fun handleGmsCoreRestrictedBackgroundDataNotif() {
if (handledGmsCoreBgDataExemption) {
return
}
handledGmsCoreBgDataExemption = true

val ctx = App.ctx()

if (ctx.packageManager.checkPermission(android.Manifest.permission.INTERNET,
PackageId.GMS_CORE_NAME) != PackageManager.PERMISSION_GRANTED) {
return
}

if (App.preferences().getBoolean(MainProcessPrefs.GmsCore_BACKGROUND_DATA_EXEMPTION_PROMPT_DISMISSED, false)) {
return
}

val dontShowAgainPa = PendingAction.addOneShot {
App.preferences().edit()
.putBoolean(MainProcessPrefs.GmsCore_BACKGROUND_DATA_EXEMPTION_PROMPT_DISMISSED, true)
.apply()

cancel(ID_GmsCore_BACKGROUND_DATA_EXEMPTION_PROMPT)
}

val dontShowAgainAction = Notification.Action.Builder(null,
ctx.getText(R.string.dont_show_again), dontShowAgainPa.pendingIntent).build()

builder(CH_MISSING_OPTIONAL_PERMISSION).apply {
setSmallIcon(R.drawable.ic_configuration_required)
setContentTitle(R.string.missing_optional_permission)
setContentText(R.string.notif_gmscore_background_data_exemption)
setStyle(Notification.BigTextStyle())
setContentIntent(activityPendingIntent(gmsCoreSettings()))
setAutoCancel(true)
addAction(dontShowAgainAction)
show(ID_GmsCore_BACKGROUND_DATA_EXEMPTION_PROMPT)
}
}

fun handleMissingApp(channel: String, prompt: CharSequence, appPkg: String) {
if (isPkgInstalled(appPkg)) {
return
Expand Down

0 comments on commit 05fc6f0

Please sign in to comment.