using System; using System.Collections.Generic; using UnityEngine; namespace Ubisoft.Hotel.Device { public class NativeDeviceAndroid : INativeDevice { private static readonly HashSet LEGIT_INSTALLERS = new HashSet { // Google Play "com.android.vending", // Old Google Play "com.google.android.feedback", // Amazon "com.amazon.venezia", // Samsung Store "com.sec.android.app.samsungapps" }; public long GetFreeDiskSpace(string path) { using (AndroidJavaObject statsFs = new AndroidJavaObject("android.os.StatFs", path)) { return statsFs.Call("getBlockSizeLong") * statsFs.Call("getAvailableBlocksLong"); } } public AdvertisingInfo GetAdvertisingId() { AdvertisingInfo adInfo = new AdvertisingInfo(); try { AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); AndroidJavaObject currentActivity = unityPlayer.GetStatic("currentActivity"); // We don't use androidX lib (androidx.ads.identifier) because it's still in alpha version (androidx.ads:ads-identifier:1.0.0-alpha04) AndroidJavaClass adClient = new AndroidJavaClass("com.google.android.gms.ads.identifier.AdvertisingIdClient"); bool isAvailable = true; // For some reason this always returns false: adClient.CallStatic("isAdvertisingIdProviderAvailable", currentActivity); if (isAvailable) { AndroidJavaObject androidAdInfo = adClient.CallStatic("getAdvertisingIdInfo", currentActivity); // When the value returned by isLimitAdTrackingEnabled() is 'true', getId() returns '00000000-0000-0000-0000-000000000000' (starting from Android 12) bool trackingEnabled = !androidAdInfo.Call("isLimitAdTrackingEnabled"); string advertisingId = androidAdInfo.Call("getId").ToString(); DeviceLogger.Channel.LogInformation($"AdvertisingID {advertisingId} - TrackingEnabled {trackingEnabled}"); adInfo.RequestSuccess = true; adInfo.AdvertisingId = advertisingId; adInfo.TrackingEnabled = trackingEnabled; } else { DeviceLogger.Channel.LogInformation("AdvertisingID is not available"); } } catch (Exception e) { DeviceLogger.Channel.LogWarning($"Error retrieving AdvertisingID: {e.Message}"); } return adInfo; } public void OpenAppInStore() { OpenAppInStore(Application.identifier); } public void OpenAppInStore(string appId) { Application.OpenURL($"market://details?id={appId}"); } public bool IsLegitInstallation() { try { bool returnValue = false; string installer = null; // Check which apk (software) has installed this build/apk (Play Store or sideloaded or ...) AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); AndroidJavaObject activity = unityPlayer.GetStatic("currentActivity"); AndroidJavaObject context = activity.Call("getApplicationContext"); if (context == null) { DeviceLogger.Channel.LogError($"IsLegitInstallation: context is null"); } else { AndroidJavaObject packageManager = context.Call("getPackageManager"); if (packageManager == null) { DeviceLogger.Channel.LogError($"IsLegitInstallation: packageManager is null"); } else { string pckName = context.Call("getPackageName"); if (string.IsNullOrEmpty(pckName)) { DeviceLogger.Channel.LogError($"IsLegitInstallation: packageName is null"); } else { installer = packageManager.Call("getInstallerPackageName", pckName); } } returnValue = !string.IsNullOrEmpty(installer) && LEGIT_INSTALLERS.Contains(installer); } DeviceLogger.Channel.LogInformation($"IsLegitInstallation installer: {installer} isLegit: {returnValue}"); return returnValue; } catch (Exception e) { DeviceLogger.Channel.LogWarning($"Error retrieving IsLegitInstallation: {e.Message}\n{e.StackTrace}"); return false; } } } }