Android: Convert Analytics to Kotlin

This commit is contained in:
Charles Lombardo 2023-01-26 21:14:35 -05:00
parent 14c7d81b54
commit ab817dcaed
2 changed files with 67 additions and 84 deletions

View File

@ -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 "";
}
}
}

View File

@ -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 -> ""
}
}
}