Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f67856d4 authored by Fynn Godau's avatar Fynn Godau
Browse files

Add preference for play licensing

parent 94e9d701
Loading
Loading
Loading
Loading
+12 −0
Original line number Diff line number Diff line
@@ -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 {
+22 −0
Original line number Diff line number Diff line
@@ -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
@@ -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
    }

@@ -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
@@ -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?
+21 −0
Original line number Diff line number Diff line
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)
        }
    }
}
 No newline at end of file
+49 −0
Original line number Diff line number Diff line
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"
    }
}
 No newline at end of file
+13 −3
Original line number Diff line number Diff line
@@ -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
@@ -42,11 +43,18 @@ class SettingsFragment : ResourceSettingsFragment() {
            findNavController().navigate(requireContext(), R.id.openLocationSettings)
            true
        }
        findPreference<Preference>(PREF_ABOUT)!!.onPreferenceClickListener = Preference.OnPreferenceClickListener {
        findPreference<Preference>(PREF_PLAY)!!.onPreferenceClickListener = Preference.OnPreferenceClickListener {
            findNavController().navigate(requireContext(), R.id.openPlaySettings)
            true
        }

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