using System; using System.IO; using UnityEngine; using UnityEngine.UI; using sam; using exprivia; using System.Collections; //using static NativeShare; public class ERSAreaVariableSubsession : AreaVariableSubsession { public Image lut; public Color fillColor; public SatelliteAreaDataConfig satelliteCfg; int curSelectedVarIndex = -1; // this is an index into satelliteCfg.areaVariables list Texture2D lutTexture; ProductLoader prodLoader; string productName; private void Awake() { autoSelectVariable = false; prodLoader = exprivia.Utils.GetOrAddComponent(gameObject); lutTexture = lut.sprite.texture; } public void SetSatelliteConfig(SatelliteAreaDataConfig cfg) { satelliteCfg = cfg; initiallySelectedVariableIndex = cfg.initiallySelectedVariableIndex; prodLoader.dateExtractor = cfg.dateExtractionFunc; prodLoader.productNameExtractor = cfg.productNameExtractionFunc; //if (initiallySelectedVariableIndex < 0 || initiallySelectedVariableIndex >= satelliteCfg.areaVariables.Length) // initiallySelectedVariableIndex = 0; /*string smallestName = satelliteCfg.areaVariables[0].variableName; int smallestIndex = 0; for (int i = 1; i < satelliteCfg.areaVariables.Length; i++) { if (satelliteCfg.areaVariables[i].variableName.CompareTo(smallestName) < 0) { smallestName = satelliteCfg.areaVariables[i].variableName; smallestIndex = i; } } initiallySelectedVariableIndex = smallestIndex;*/ for (int i = 0; i < satelliteCfg.areaVariables.Length; i++) { AreaVariableConfig avc = satelliteCfg.areaVariables[i]; avc.index = i; string fullpath = Path.Combine(Application.streamingAssetsPath, avc.profileFilename); avc.profile = ProductProfile.ParseConfigFile(fullpath); if (avc.profile == null) { Debug.LogError($"ERROR LOADING SINGLE-VARIABLE PRODUCT PROFILE: {fullpath}"); return; } satelliteCfg.areaVariables[i] = avc; } } public void SetProductName(string prodName) { productName = prodName; SelectVariable(initiallySelectedVariableIndex); } public override void SelectVariable(int varInd) { if (varInd < 0 || varInd >= satelliteCfg.areaVariables.Length) { Debug.LogWarning($"SelectVariable: invalid variable index {varInd}"); return; } if (varInd == curSelectedVarIndex) return; curSelectedVarIndex = varInd; prodLoader.profile = satelliteCfg.areaVariables[curSelectedVarIndex].profile; prodLoader.dateExtractor = satelliteCfg.dateExtractionFunc; prodLoader.productNameExtractor = satelliteCfg.productNameExtractionFunc; Debug.Log($"LOADER PROFILE SET TO {prodLoader.profile}"); string dataUrl = satelliteCfg.areaVariables[curSelectedVarIndex].url + "/" + productName; prodLoader.Download(dataUrl, // OnSuccess (loader) => { SelectProduct(loader.downloadedData); base.SelectVariable(0); }, // OnError (loader) => { }); } public override Color GetDataPointColor(ProductDataPoint dp) { if (!dp.varValid[variableIndex]) { return fillColor; } float val = dp.variables[variableIndex]; float frac = ((val - prodvar.scaledMinValue) / prodvar.scaledRange); frac = Mathf.Clamp(frac, 0.0f, 1.0f); float tc = frac * lutTexture.width; int intCoord = (int)tc; float fracCoord = tc - intCoord; Color val1 = lutTexture.GetPixel(intCoord, 0); Color val2 = lutTexture.GetPixel(Math.Min(intCoord+1, lutTexture.width-1), 0); Color c = Color.Lerp(val1, val2, fracCoord); c.a = 1f; return c; } public void OnMapScrolled(Vector2 xy) { if ((latLonDictionaries == null) || latLonDictionaries.Count == 0) return; float lat = Mathf.LerpUnclamped(-90f, 90f, xy.y); float lon = Mathf.LerpUnclamped(-180f, 180f, xy.x); SelectLatLon(lat, lon); } public void OnBackButtonPressed() { string previousSceneName = (string)GlobalDictionary.GetValue("DataVariableParentSceneName"); if (!string.IsNullOrEmpty(previousSceneName)) { Debug.Log($"SWITCHING BACK TO SCENE {previousSceneName}"); exprivia.ui.Utils.LoadScene(previousSceneName); } else { Debug.Log("SHOULD GO BACK TO PREVIOUS SCENE"); } } public void OnEmailButtonPressed() { string satelliteId = (string)GlobalDictionary.GetValue("SatelliteId"); string productId = data.productName; string subject = $"ESA {satelliteId} product: {productId}"; string body = $"{satelliteId} data\n\n" + $"- Product Name:{productId}\n\n" + $"- Variable: {prodvar.name}\n\n" + $"- Date: {dateText.text}\n\n" + $"- Longitude: {lonText.text}\n\n" + $"- Latitude: {latText.text}\n\n" + $"- Value: {curValText.text}\n\n" + $"Mail from ESA {satelliteId} app\n"; Debug.Log($"SEND EMAIL! Subject: {subject} body:\n{body}"); StartCoroutine(CaptureScreenshot(subject, body)); } IEnumerator CaptureScreenshot(string subject, string body) { yield return new WaitForEndOfFrame(); string fileName = "mailScreenshot"; string filePath = Path.Combine(Application.persistentDataPath, fileName + ".png"); Texture2D screenImage = new Texture2D(Screen.width, Screen.height); //Get Image from screen screenImage.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0); screenImage.Apply(); File.WriteAllBytes(filePath, screenImage.EncodeToPNG()); Destroy(screenImage); #if UNITY_ANDROID StartCoroutine(ShareAndroidScreenshot(subject, body, filePath)); #else NativeShare shareObject = new NativeShare(); shareObject.SetTitle("Send email"); // Dialog title shareObject.SetSubject(subject); shareObject.SetText(body); shareObject.AddFile(filePath); //shareObject.SetCallback(MyCallback); shareObject.Share(); #endif } IEnumerator ShareAndroidScreenshot(string subject, string body, string filePath) { yield return new WaitForEndOfFrame(); AndroidJavaClass unity = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); AndroidJavaObject currentActivity = unity.GetStatic("currentActivity"); AndroidJavaClass intentClass = new AndroidJavaClass("android.content.Intent"); AndroidJavaObject intentObject = new AndroidJavaObject("android.content.Intent"); intentObject.Call("setAction", intentClass.GetStatic("ACTION_SEND")); AndroidJavaObject fileObject = new AndroidJavaObject("java.io.File", filePath); AndroidJavaClass fileProviderClass = new AndroidJavaClass("android.support.v4.content.FileProvider"); object[] providerParams = new object[3]; providerParams[0] = currentActivity; providerParams[1] = Application.identifier + ".provider"; providerParams[2] = fileObject; AndroidJavaObject uriObject = fileProviderClass.CallStatic("getUriForFile", providerParams); intentObject.Call("putExtra", intentClass.GetStatic("EXTRA_STREAM"), uriObject); intentObject.Call("setType", "image/png"); intentObject.Call("putExtra", intentClass.GetStatic("EXTRA_SUBJECT"), subject); intentObject.Call("putExtra", intentClass.GetStatic("EXTRA_TEXT"), body); intentObject.Call("addFlags", intentClass.GetStatic("FLAG_GRANT_READ_URI_PERMISSION")); AndroidJavaObject chooser = intentClass.CallStatic("createChooser", intentObject, "Send email"); currentActivity.Call("startActivity", chooser); } }