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

Verified Commit 6d45bfb7 authored by Marvin W.'s avatar Marvin W. 🐿️
Browse files

Add DroidGuard support

parent d8325870
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -27,7 +27,7 @@ buildscript {

    ext.supportLibraryVersion = '28.0.0'
    ext.slf4jVersion = '1.7.25'
    ext.volleyVersion = '1.2.0'
    ext.volleyVersion = '1.2.1'
    ext.wireVersion = '3.2.2'

    ext.androidBuildGradleVersion = '4.1.0'
+25 −17
Original line number Diff line number Diff line
@@ -16,32 +16,40 @@

package org.microg.gms.common;

import android.annotation.TargetApi;

import java.util.Locale;
import java.util.Random;

// TODO: Make flexible
public class Build {
    public String fingerprint = android.os.Build.FINGERPRINT;
    public String hardware = android.os.Build.HARDWARE;
    public String brand = android.os.Build.BRAND;
    public String radio = getRadio();
    public String board = android.os.Build.BOARD;
    public String bootloader = android.os.Build.BOOTLOADER;
    public long time = android.os.Build.TIME;
    public String brand = android.os.Build.BRAND;
    public String cpu_abi = android.os.Build.CPU_ABI;
    public String cpu_abi2 = android.os.Build.CPU_ABI2;
    @TargetApi(21)
    public String[] supported_abis = android.os.Build.VERSION.SDK_INT >= 21 ? android.os.Build.SUPPORTED_ABIS : new String[0];
    public String device = android.os.Build.DEVICE;
    public int sdk = android.os.Build.VERSION.SDK_INT;
    public String model = android.os.Build.MODEL;
    public String display = android.os.Build.DISPLAY;
    public String fingerprint = android.os.Build.FINGERPRINT;
    public String hardware = android.os.Build.HARDWARE;
    public String host = android.os.Build.HOST;
    public String id = android.os.Build.ID;
    public String manufacturer = android.os.Build.MANUFACTURER;
    public String model = android.os.Build.MODEL;
    public String product = android.os.Build.PRODUCT;
    public String id = android.os.Build.ID;
    public String radio = android.os.Build.RADIO;
    public String serial = generateSerialNumber(); // TODO: static

    @SuppressWarnings("deprecation")
    private static String getRadio() {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            return android.os.Build.getRadioVersion();
        } else {
            return android.os.Build.RADIO;
        }
    }
    public String tags = android.os.Build.TAGS;
    public long time = android.os.Build.TIME;
    public String type = android.os.Build.TYPE;
    public String user = android.os.Build.USER;
    public String version_codename = android.os.Build.VERSION.CODENAME;
    public String version_incremental = android.os.Build.VERSION.INCREMENTAL;
    public String version_release = android.os.Build.VERSION.RELEASE;
    public String version_sdk = android.os.Build.VERSION.SDK;
    public int version_sdk_int = android.os.Build.VERSION.SDK_INT;

    private String generateSerialNumber() {
        String serial = "008741";
+12 −0
Original line number Diff line number Diff line
@@ -152,6 +152,18 @@ public class PackageUtils {
        return getAndCheckCallingPackage(context, suggestedPackageName, 0);
    }

    @Nullable
    public static String getAndCheckCallingPackageOrExtendedAccess(Context context, String suggestedPackageName) {
        try {
            return getAndCheckCallingPackage(context, suggestedPackageName, 0);
        } catch (Exception e) {
            if (callerHasExtendedAccess(context)) {
                return suggestedPackageName;
            }
            throw e;
        }
    }

    @Nullable
    public static String getAndCheckCallingPackage(Context context, int suggestedCallerUid) {
        return getAndCheckCallingPackage(context, null, suggestedCallerUid);
+24 −0
Original line number Diff line number Diff line
@@ -107,6 +107,28 @@ object SettingsContract {
        private const val id = "safety-net"
        fun getContentUri(context: Context) = Uri.withAppendedPath(getAuthorityUri(context), id)
        fun getContentType(context: Context) = "vnd.android.cursor.item/vnd.${getAuthority(context)}.$id"

        const val ENABLED = "safetynet_enabled"

        val PROJECTION = arrayOf(
            ENABLED
        )
    }

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

        const val ENABLED = "droidguard_enabled"
        const val MODE = "droidguard_mode"
        const val NETWORK_SERVER_URL = "droidguard_network_server_url"

        val PROJECTION = arrayOf(
            ENABLED,
            MODE,
            NETWORK_SERVER_URL
        )
    }

    private fun <T> withoutCallingIdentity(f: () -> T): T {
@@ -118,6 +140,7 @@ object SettingsContract {
        }
    }

    @JvmStatic
    fun <T> getSettings(context: Context, uri: Uri, projection: Array<out String>?, f: (Cursor) -> T): T = withoutCallingIdentity {
        context.contentResolver.query(uri, projection, null, null, null).use { c ->
            require(c != null) { "Cursor for query $uri ${projection?.toList()} was null" }
@@ -126,6 +149,7 @@ object SettingsContract {
        }
    }

    @JvmStatic
    fun setSettings(context: Context, uri: Uri, v: ContentValues.() -> Unit) = withoutCallingIdentity {
        val values = ContentValues().apply { v.invoke(this) }
        val affected = context.contentResolver.update(uri, values, null, null)
+60 −3
Original line number Diff line number Diff line
@@ -17,8 +17,10 @@ import android.preference.PreferenceManager
import org.microg.gms.common.PackageUtils.warnIfNotMainProcess
import org.microg.gms.settings.SettingsContract.Auth
import org.microg.gms.settings.SettingsContract.CheckIn
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.SafetyNet
import org.microg.gms.settings.SettingsContract.getAuthority
import java.io.File

@@ -61,6 +63,8 @@ class SettingsProvider : ContentProvider() {
        Gcm.getContentUri(context!!) -> queryGcm(projection ?: Gcm.PROJECTION)
        Auth.getContentUri(context!!) -> queryAuth(projection ?: Auth.PROJECTION)
        Exposure.getContentUri(context!!) -> queryExposure(projection ?: Exposure.PROJECTION)
        SafetyNet.getContentUri(context!!) -> querySafetyNet(projection ?: SafetyNet.PROJECTION)
        DroidGuard.getContentUri(context!!) -> queryDroidGuard(projection ?: DroidGuard.PROJECTION)
        else -> null
    }

@@ -77,6 +81,8 @@ class SettingsProvider : ContentProvider() {
            Gcm.getContentUri(context!!) -> updateGcm(values)
            Auth.getContentUri(context!!) -> updateAuth(values)
            Exposure.getContentUri(context!!) -> updateExposure(values)
            SafetyNet.getContentUri(context!!) -> updateSafetyNet(values)
            DroidGuard.getContentUri(context!!) -> updateDroidGuard(values)
            else -> return 0
        }
        return 1
@@ -216,9 +222,51 @@ class SettingsProvider : ContentProvider() {
        editor.apply()
    }

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

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

    private fun queryDroidGuard(p: Array<out String>): Cursor = MatrixCursor(p).addRow(p) { key ->
        when (key) {
            DroidGuard.ENABLED -> getSettingsBoolean(key, false)
            DroidGuard.MODE -> getSettingsString(key)
            DroidGuard.NETWORK_SERVER_URL -> getSettingsString(key)
            else -> throw IllegalArgumentException("Unknown key: $key")
        }
    }

    private fun updateDroidGuard(values: ContentValues) {
        if (values.size() == 0) return
        val editor = preferences.edit()
        values.valueSet().forEach { (key, value) ->
            when (key) {
                DroidGuard.ENABLED -> editor.putBoolean(key, value as Boolean)
                DroidGuard.MODE -> editor.putString(key, value as String)
                DroidGuard.NETWORK_SERVER_URL -> editor.putString(key, value as String)
                else -> throw IllegalArgumentException("Unknown key: $key")
            }
        }
        editor.apply()
    }

    private fun MatrixCursor.addRow(
        p: Array<out String>,
        valueGetter: (String) -> Any
        valueGetter: (String) -> Any?
    ): MatrixCursor {
        val row = newRow()
        for (key in p) row.add(valueGetter.invoke(key))
@@ -243,7 +291,16 @@ class SettingsProvider : ContentProvider() {
     * @return the current setting as [Int], because [ContentProvider] does not support [Boolean].
     */
    private fun getSettingsBoolean(key: String, def: Boolean): Int {
        val default = systemDefaultPreferences?.getBoolean(key, def) ?: def
        return if (preferences.getBoolean(key, default)) 1 else 0
        return listOf(preferences, systemDefaultPreferences).getBooleanAsInt(key, def)
    }

    private fun getSettingsString(key: String, def: String? = null): String? = listOf(preferences, systemDefaultPreferences).getString(key, def)
    private fun getSettingsInt(key: String, def: Int): Int = listOf(preferences, systemDefaultPreferences).getInt(key, def)
    private fun getSettingsLong(key: String, def: Long): Long = listOf(preferences, systemDefaultPreferences).getLong(key, def)

    private fun List<SharedPreferences?>.getString(key: String, def: String?): String? = foldRight(def) { preferences, defValue -> preferences?.getString(key, defValue) ?: defValue }
    private fun List<SharedPreferences?>.getInt(key: String, def: Int): Int = foldRight(def) { preferences, defValue -> preferences?.getInt(key, defValue) ?: defValue }
    private fun List<SharedPreferences?>.getLong(key: String, def: Long): Long = foldRight(def) { preferences, defValue -> preferences?.getLong(key, defValue) ?: defValue }
    private fun List<SharedPreferences?>.getBoolean(key: String, def: Boolean): Boolean = foldRight(def) { preferences, defValue -> preferences?.getBoolean(key, defValue) ?: defValue }
    private fun List<SharedPreferences?>.getBooleanAsInt(key: String, def: Boolean): Int = if (getBoolean(key, def)) 1 else 0
}
Loading