From ab817dcaedd6731c8dccf7153c5915b2295f79b0 Mon Sep 17 00:00:00 2001 From: Charles Lombardo Date: Thu, 26 Jan 2023 21:14:35 -0500 Subject: [PATCH] Android: Convert Analytics to Kotlin --- .../dolphinemu/utils/Analytics.java | 84 ------------------- .../dolphinemu/dolphinemu/utils/Analytics.kt | 67 +++++++++++++++ 2 files changed, 67 insertions(+), 84 deletions(-) delete mode 100644 Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/Analytics.java create mode 100644 Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/Analytics.kt diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/Analytics.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/Analytics.java deleted file mode 100644 index b1537f260e..0000000000 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/Analytics.java +++ /dev/null @@ -1,84 +0,0 @@ -// SPDX-License-Identifier: GPL-2.0-or-later - -package org.dolphinemu.dolphinemu.utils; - -import android.os.Build; - -import androidx.annotation.Keep; -import androidx.fragment.app.FragmentActivity; - -import com.android.volley.Request; -import com.android.volley.toolbox.StringRequest; - -import org.dolphinemu.dolphinemu.DolphinApplication; -import org.dolphinemu.dolphinemu.dialogs.AnalyticsDialog; -import org.dolphinemu.dolphinemu.features.settings.model.BooleanSetting; -import org.dolphinemu.dolphinemu.features.settings.model.Settings; - -public class Analytics -{ - private static final String DEVICE_MANUFACTURER = "DEVICE_MANUFACTURER"; - private static final String DEVICE_OS = "DEVICE_OS"; - private static final String DEVICE_MODEL = "DEVICE_MODEL"; - private static final String DEVICE_TYPE = "DEVICE_TYPE"; - - public static void checkAnalyticsInit(FragmentActivity activity) - { - new AfterDirectoryInitializationRunner().runWithoutLifecycle(() -> - { - if (!BooleanSetting.MAIN_ANALYTICS_PERMISSION_ASKED.getBooleanGlobal()) - { - new AnalyticsDialog().show(activity.getSupportFragmentManager(), AnalyticsDialog.TAG); - } - }); - } - - public static void firstAnalyticsAdd(boolean enabled) - { - try (Settings settings = new Settings()) - { - settings.loadSettings(); - - BooleanSetting.MAIN_ANALYTICS_ENABLED.setBoolean(settings, enabled); - BooleanSetting.MAIN_ANALYTICS_PERMISSION_ASKED.setBoolean(settings, true); - - // Context is set to null to avoid toasts - settings.saveSettings(null, null); - } - } - - @Keep - public static void sendReport(String endpoint, byte[] data) - { - StringRequest request = new StringRequest(Request.Method.POST, endpoint, - null, error -> Log.debug("Failed to send report")) - { - @Override - public byte[] getBody() - { - return data; - } - }; - - VolleyUtil.getQueue().add(request); - } - - @Keep - public static String getValue(String key) - { - switch (key) - { - case DEVICE_MODEL: - return Build.MODEL; - case DEVICE_MANUFACTURER: - return Build.MANUFACTURER; - case DEVICE_OS: - return String.valueOf(Build.VERSION.SDK_INT); - case DEVICE_TYPE: - return TvUtil.isLeanback(DolphinApplication.getAppContext()) ? "android-tv" : - "android-mobile"; - default: - return ""; - } - } -} diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/Analytics.kt b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/Analytics.kt new file mode 100644 index 0000000000..83c0168bec --- /dev/null +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/Analytics.kt @@ -0,0 +1,67 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + +package org.dolphinemu.dolphinemu.utils + +import android.os.Build +import androidx.annotation.Keep +import androidx.fragment.app.FragmentActivity +import com.android.volley.Response +import com.android.volley.toolbox.StringRequest +import org.dolphinemu.dolphinemu.DolphinApplication +import org.dolphinemu.dolphinemu.dialogs.AnalyticsDialog +import org.dolphinemu.dolphinemu.features.settings.model.BooleanSetting +import org.dolphinemu.dolphinemu.features.settings.model.Settings + +object Analytics { + private const val DEVICE_MANUFACTURER = "DEVICE_MANUFACTURER" + private const val DEVICE_OS = "DEVICE_OS" + private const val DEVICE_MODEL = "DEVICE_MODEL" + private const val DEVICE_TYPE = "DEVICE_TYPE" + + @JvmStatic + fun checkAnalyticsInit(activity: FragmentActivity) { + AfterDirectoryInitializationRunner().runWithoutLifecycle { + if (!BooleanSetting.MAIN_ANALYTICS_PERMISSION_ASKED.booleanGlobal) { + AnalyticsDialog().show(activity.supportFragmentManager, AnalyticsDialog.TAG) + } + } + } + + fun firstAnalyticsAdd(enabled: Boolean) { + Settings().use { settings -> + settings.loadSettings() + BooleanSetting.MAIN_ANALYTICS_ENABLED.setBoolean(settings, enabled) + BooleanSetting.MAIN_ANALYTICS_PERMISSION_ASKED.setBoolean(settings, true) + + // Context is set to null to avoid toasts + settings.saveSettings(null, null) + } + } + + @Keep + @JvmStatic + fun sendReport(endpoint: String, data: ByteArray) { + val request: StringRequest = object : StringRequest( + Method.POST, + endpoint, + null, + Response.ErrorListener { Log.debug("Failed to send report") }) { + override fun getBody(): ByteArray { + return data + } + } + VolleyUtil.getQueue().add(request) + } + + @Keep + @JvmStatic + fun getValue(key: String?): String { + return when (key) { + DEVICE_MODEL -> Build.MODEL + DEVICE_MANUFACTURER -> Build.MANUFACTURER + DEVICE_OS -> Build.VERSION.SDK_INT.toString() + DEVICE_TYPE -> if (TvUtil.isLeanback(DolphinApplication.getAppContext())) "android-tv" else "android-mobile" + else -> "" + } + } +}