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 2 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="org.microg.gms.permission.READ_SETTINGS"
mar-v-in marked this conversation as resolved.
Show resolved Hide resolved
android:protectionLevel="signature" />
<permission android:name="org.microg.gms.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="org.microg.gms.permission.READ_SETTINGS"
mar-v-in marked this conversation as resolved.
Show resolved Hide resolved
android:writePermission="org.microg.gms.permission.WRITE_SETTINGS" />
</application>
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,18 @@ object SettingsContract {
)
}

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

const val LICENSING = "play_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.Play
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)
Play.getContentUri(context!!) -> queryPlay(projection ?: Play.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)
Play.getContentUri(context!!) -> updatePlay(values)
else -> return 0
}
return 1
Expand Down Expand Up @@ -335,6 +338,25 @@ class SettingsProvider : ContentProvider() {
editor.apply()
}

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

private fun updatePlay(values: ContentValues) {
if (values.size() == 0) return
val editor = preferences.edit()
values.valueSet().forEach { (key, value) ->
when (key) {
Play.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
@@ -0,0 +1,21 @@
package org.microg.gms.play

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

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

@JvmStatic
fun setLicensingEnabled(context: Context, enabled: Boolean) {
SettingsContract.setSettings(context, SettingsContract.Play.getContentUri(context)) {
put(SettingsContract.Play.LICENSING, enabled)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
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.play.PlayPreferences

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

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

@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) {
PlayPreferences.setLicensingEnabled(appContext, newValue)
}
updateContent()
}
true
}
}

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

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

companion object {
const val PREF_LICENSING_ENABLED = "play_licensing"
}
}
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.play.PlayPreferences
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_PLAY)!!.onPreferenceClickListener = Preference.OnPreferenceClickListener {
findNavController().navigate(requireContext(), R.id.openPlaySettings)
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_PLAY)!!.setSummary(if (PlayPreferences.isLicensingEnabled(requireContext())) R.string.pref_play_summary_licensing_on else R.string.pref_play_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_PLAY = "pref_play"
}

init {
Expand Down
10 changes: 10 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,10 @@
<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/openPlaySettings"
app:destination="@id/playFragment" />
<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/playFragment"
android:name="org.microg.gms.ui.PlayFragment"
android:label="@string/service_name_play" />

<!-- 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_play">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_play_summary_licensing_off">Licensing off</string>
<string name="pref_play_summary_licensing_on">Licensing on</string>
<string name="pref_play_licensing_enable_switch">Answer license verification requests</string>
<string name="pref_play_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>
31 changes: 31 additions & 0 deletions play-services-core/src/main/res/xml/preferences_play.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (C) 2013-2017 microG Project Team
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

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

<org.microg.gms.ui.SwitchBarPreference
android:title="@string/pref_play_licensing_enable_switch"
android:key="play_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_play_license_enable_summary" />
</PreferenceCategory>
</PreferenceScreen>
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_play"
android:title="@string/service_name_play" />
<Preference
android:icon="@drawable/dots_horizontal"
android:key="pref_google_more"
Expand Down
Loading