using System.Collections.Generic; using System.Xml.Linq; using UnityEngine; using sam; using exprivia; using System.Collections; using System.IO; public class CryosatPointVariableSubsession : PointVariableSubsession { public Color[] LUT; public int userDataIndex1 = 1; public int userDataIndex2 = 2; public int userDataIndex3 = 3; bool useColorArray = false; Color[] dpColors = null; public void OnVariableSelected(int vi) { XAttribute attr = prodvar.xmlElt.Attribute("cryosatcolors"); if ((attr == null) || (string.Compare(attr.Value, "1") != 0)) { useColorArray = false; dpColors = null; return; } useColorArray = true; dpColors = new Color[data.dataPoints.Count]; foreach (ProductDataPoint dp in data.dataPoints) { short v1 = (data.userData[userDataIndex1] as List)[dp.dataPointIndex]; short v2 = (data.userData[userDataIndex2] as List)[dp.dataPointIndex]; short v3 = (data.userData[userDataIndex3] as List)[dp.dataPointIndex]; dpColors[dp.dataPointIndex] = LUT[1 + HighestOfThree(v1, v2, v3)]; } } public override Color GetDataPointColor(ProductDataPoint dp) { return (useColorArray) ? dpColors[dp.dataPointIndex] : LUT[0]; } int HighestOfThree(short v1, short v2, short v3) { return (v1 > v2) ? ((v1 > v3)? 0 : 2) : ((v2 > v3) ? 1 : 2); } 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.temporaryCachePath, fileName + ".png"); //persistentDataPath 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")); // code working until Android 7 //AndroidJavaClass uriClass = new AndroidJavaClass ("android.net.Uri"); //AndroidJavaObject uriObject = uriClass.CallStatic ("parse", "file://" + filePath); 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); } }