Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Licensing UI #2076

Merged
merged 7 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion play-services-base/core/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,17 @@
-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<permission android:name="${applicationId}.permission.READ_SETTINGS"
android:protectionLevel="signature" />
<permission android:name="${applicationId}.permission.WRITE_SETTINGS"
android:protectionLevel="signature" />

<application>
<provider
android:name="org.microg.gms.settings.SettingsProvider"
android:authorities="${applicationId}.microg.settings"
android:exported="false" />
android:exported="true"
android:readPermission="${applicationId}.permission.READ_SETTINGS"
android:writePermission="${applicationId}.permission.WRITE_SETTINGS" />
</application>
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,18 @@ object SettingsContract {
)
}

object Vending {
private const val id = "vending"
fun getContentUri(context: Context) = Uri.withAppendedPath(getAuthorityUri(context), id)
fun getContentType(context: Context) = "vnd.android.cursor.item/vnd.${getAuthority(context)}.$id"

const val LICENSING = "vending_licensing"

val PROJECTION = arrayOf(
LICENSING
)
}

private fun <T> withoutCallingIdentity(f: () -> T): T {
val identity = Binder.clearCallingIdentity()
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import org.microg.gms.settings.SettingsContract.DroidGuard
import org.microg.gms.settings.SettingsContract.Exposure
import org.microg.gms.settings.SettingsContract.Gcm
import org.microg.gms.settings.SettingsContract.Location
import org.microg.gms.settings.SettingsContract.Vending
import org.microg.gms.settings.SettingsContract.Profile
import org.microg.gms.settings.SettingsContract.SafetyNet
import org.microg.gms.settings.SettingsContract.getAuthority
Expand Down Expand Up @@ -78,6 +79,7 @@ class SettingsProvider : ContentProvider() {
DroidGuard.getContentUri(context!!) -> queryDroidGuard(projection ?: DroidGuard.PROJECTION)
Profile.getContentUri(context!!) -> queryProfile(projection ?: Profile.PROJECTION)
Location.getContentUri(context!!) -> queryLocation(projection ?: Location.PROJECTION)
Vending.getContentUri(context!!) -> queryVending(projection ?: Vending.PROJECTION)
else -> null
}

Expand All @@ -98,6 +100,7 @@ class SettingsProvider : ContentProvider() {
DroidGuard.getContentUri(context!!) -> updateDroidGuard(values)
Profile.getContentUri(context!!) -> updateProfile(values)
Location.getContentUri(context!!) -> updateLocation(values)
Vending.getContentUri(context!!) -> updateVending(values)
else -> return 0
}
return 1
Expand Down Expand Up @@ -335,6 +338,25 @@ class SettingsProvider : ContentProvider() {
editor.apply()
}

private fun queryVending(p: Array<out String>): Cursor = MatrixCursor(p).addRow(p) { key ->
when (key) {
Vending.LICENSING -> getSettingsBoolean(key, false)
else -> throw IllegalArgumentException("Unknown key: $key")
}
}

private fun updateVending(values: ContentValues) {
if (values.size() == 0) return
val editor = preferences.edit()
values.valueSet().forEach { (key, value) ->
when (key) {
Vending.LICENSING -> editor.putBoolean(key, value as Boolean)
else -> throw IllegalArgumentException("Unknown key: $key")
}
}
editor.apply()
}

private fun MatrixCursor.addRow(
p: Array<out String>,
valueGetter: (String) -> Any?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import com.google.android.gms.R
import org.microg.gms.checkin.CheckinPreferences
import org.microg.gms.gcm.GcmDatabase
import org.microg.gms.gcm.GcmPrefs
import org.microg.gms.vending.VendingPreferences
import org.microg.gms.safetynet.SafetyNetPreferences
import org.microg.gms.ui.settings.SettingsProvider
import org.microg.gms.ui.settings.getAllSettingsProviders
Expand Down Expand Up @@ -42,11 +43,18 @@ class SettingsFragment : ResourceSettingsFragment() {
findNavController().navigate(requireContext(), R.id.openLocationSettings)
true
}
findPreference<Preference>(PREF_ABOUT)!!.onPreferenceClickListener = Preference.OnPreferenceClickListener {
findNavController().navigate(requireContext(), R.id.openAbout)
findPreference<Preference>(PREF_VENDING)!!.onPreferenceClickListener = Preference.OnPreferenceClickListener {
findNavController().navigate(requireContext(), R.id.openVendingSettings)
true
}
findPreference<Preference>(PREF_ABOUT)!!.summary = getString(org.microg.tools.ui.R.string.about_version_str, AboutFragment.getSelfVersion(context))

findPreference<Preference>(PREF_ABOUT)!!.apply {
onPreferenceClickListener = Preference.OnPreferenceClickListener {
findNavController().navigate(requireContext(), R.id.openAbout)
true
}
summary = getString(org.microg.tools.ui.R.string.about_version_str, AboutFragment.getSelfVersion(context))
}

for (entry in getAllSettingsProviders(requireContext()).flatMap { it.getEntriesStatic(requireContext()) }) {
entry.createPreference()
Expand Down Expand Up @@ -101,6 +109,7 @@ class SettingsFragment : ResourceSettingsFragment() {

findPreference<Preference>(PREF_CHECKIN)!!.setSummary(if (CheckinPreferences.isEnabled(requireContext())) org.microg.gms.base.core.R.string.service_status_enabled_short else org.microg.gms.base.core.R.string.service_status_disabled_short)
findPreference<Preference>(PREF_SNET)!!.setSummary(if (SafetyNetPreferences.isEnabled(requireContext())) org.microg.gms.base.core.R.string.service_status_enabled_short else org.microg.gms.base.core.R.string.service_status_disabled_short)
findPreference<Preference>(PREF_VENDING)!!.setSummary(if (VendingPreferences.isLicensingEnabled(requireContext())) R.string.pref_vending_summary_licensing_on else R.string.pref_vending_summary_licensing_off)

lifecycleScope.launchWhenResumed {
val entries = getAllSettingsProviders(requireContext()).flatMap { it.getEntriesDynamic(requireContext()) }
Expand All @@ -121,6 +130,7 @@ class SettingsFragment : ResourceSettingsFragment() {
const val PREF_SNET = "pref_snet"
const val PREF_LOCATION = "pref_location"
const val PREF_CHECKIN = "pref_checkin"
const val PREF_VENDING = "pref_vending"
}

init {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* SPDX-FileCopyrightText: 2023, e Foundation
* SPDX-License-Identifier: Apache-2.0
*/

package org.microg.gms.ui

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.lifecycle.lifecycleScope
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import androidx.preference.TwoStatePreference
import com.google.android.gms.R
import org.microg.gms.vending.VendingPreferences

class VendingFragment : PreferenceFragmentCompat() {
private lateinit var licensingEnabled: TwoStatePreference

override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
addPreferencesFromResource(R.xml.preferences_vending)
}

@SuppressLint("RestrictedApi")
override fun onBindPreferences() {
licensingEnabled = preferenceScreen.findPreference(PREF_LICENSING_ENABLED) ?: licensingEnabled
licensingEnabled.onPreferenceChangeListener = Preference.OnPreferenceChangeListener { _, newValue ->
val appContext = requireContext().applicationContext
lifecycleScope.launchWhenResumed {
if (newValue is Boolean) {
VendingPreferences.setLicensingEnabled(appContext, newValue)
}
updateContent()
}
true
}
}

override fun onResume() {
super.onResume()
updateContent()
}

private fun updateContent() {
val appContext = requireContext().applicationContext
lifecycleScope.launchWhenResumed {
licensingEnabled.isChecked = VendingPreferences.isLicensingEnabled(appContext)
}
}

companion object {
const val PREF_LICENSING_ENABLED = "vending_licensing"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* SPDX-FileCopyrightText: 2023, e Foundation
* SPDX-License-Identifier: Apache-2.0
*/

package org.microg.gms.vending

import android.content.Context
import org.microg.gms.settings.SettingsContract

object VendingPreferences {
@JvmStatic
fun isLicensingEnabled(context: Context): Boolean {
val projection = arrayOf(SettingsContract.Vending.LICENSING)
return SettingsContract.getSettings(context, SettingsContract.Vending.getContentUri(context), projection) { c ->
c.getInt(0) != 0
}
}

@JvmStatic
fun setLicensingEnabled(context: Context, enabled: Boolean) {
SettingsContract.setSettings(context, SettingsContract.Vending.getContentUri(context)) {
put(SettingsContract.Vending.LICENSING, enabled)
}
}
}
16 changes: 16 additions & 0 deletions play-services-core/src/main/res/drawable/ic_shop.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ SPDX-FileCopyrightText: 2020, Google Inc.
~ SPDX-FileCopyrightText: 2023, e Foundation
~ SPDX-License-Identifier: Apache-2.0
-->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorAccent">
<path
android:fillColor="@android:color/white"
android:pathData="M16,6L16,4c0,-1.11 -0.89,-2 -2,-2h-4c-1.11,0 -2,0.89 -2,2v2L2,6v13c0,1.11 0.89,2 2,2h16c1.11,0 2,-0.89 2,-2L22,6h-6zM10,4h4v2h-4L10,4zM9,18L9,9l7.5,4L9,18z"/>
</vector>
10 changes: 10 additions & 0 deletions play-services-core/src/main/res/navigation/nav_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
<action
android:id="@+id/openSafetyNetSettings"
app:destination="@id/safetyNetFragment" />
<action
android:id="@+id/openVendingSettings"
app:destination="@id/vendingFragment" />
<action
android:id="@+id/openMoreGoogleSettings"
app:destination="@id/googleMoreFragment" />
Expand Down Expand Up @@ -134,6 +137,13 @@
app:argType="string" />
</fragment>

<!-- Play Store services -->

<fragment
android:id="@+id/vendingFragment"
android:name="org.microg.gms.ui.VendingFragment"
android:label="@string/service_name_vending" />

<!-- More -->

<fragment
Expand Down
6 changes: 6 additions & 0 deletions play-services-core/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ This can take a couple of minutes."</string>
<string name="service_name_checkin">Google device registration</string>
<string name="service_name_mcs">Cloud Messaging</string>
<string name="service_name_snet">Google SafetyNet</string>
<string name="service_name_vending">Play Store services</string>

<string name="games_title">Google Play Games</string>
<string name="games_info_title"><xliff:g example="F-Droid">%1$s</xliff:g> would like to use Play Games</string>
Expand Down Expand Up @@ -231,4 +232,9 @@ This can take a couple of minutes."</string>
<string name="push_notifications_summary_manual">ON / Manual: %s</string>
<string name="push_notifications_summary_values_seconds">%s seconds</string>
<string name="push_notifications_summary_values_minutes">%s minutes</string>

<string name="pref_vending_summary_licensing_off">Licensing off</string>
<string name="pref_vending_summary_licensing_on">Licensing on</string>
<string name="pref_vending_licensing_enable_switch">Answer license verification requests</string>
<string name="pref_vending_license_enable_summary">Some apps require verification that you have purchased them on Google Play. When requested by an app, microG can download a proof of purchase from Google. If disabled, or if no Google account is added, requests for license verification are ignored.</string>
</resources>
4 changes: 4 additions & 0 deletions play-services-core/src/main/res/xml/preferences_start.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@
android:icon="@drawable/ic_certificate"
android:key="pref_snet"
android:title="@string/service_name_snet" />
<Preference
android:icon="@drawable/ic_shop"
android:key="pref_vending"
android:title="@string/service_name_vending" />
<Preference
android:icon="@drawable/dots_horizontal"
android:key="pref_google_more"
Expand Down
20 changes: 20 additions & 0 deletions play-services-core/src/main/res/xml/preferences_vending.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ SPDX-FileCopyrightText: 2023, e Foundation
~ SPDX-License-Identifier: Apache-2.0
-->

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

<SwitchPreference
android:title="@string/pref_vending_licensing_enable_switch"
android:key="vending_licensing"
android:persistent="false" />

<PreferenceCategory android:layout="@layout/preference_category_no_label">
<org.microg.gms.ui.TextPreference
android:icon="@drawable/ic_info_outline"
android:selectable="false"
android:summary="@string/pref_vending_license_enable_summary" />
</PreferenceCategory>
</PreferenceScreen>
Loading