From 1f82cfb41fbc298f3fdac12701de9e8086121bfd Mon Sep 17 00:00:00 2001 From: Thiago Filipe Soares da Rocha Date: Tue, 14 Feb 2017 18:13:17 -0300 Subject: [PATCH] Finish Handle Download --- Assets/Scenes/Main.unity | 11 +++++------ Assets/Scripts/Export Video/ExportLayers.cs | 4 +++- Assets/Scripts/Export Video/ExportVideo.cs | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------------------- Assets/Scripts/Export Video/MediaShareAndroid.cs | 143 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------------------------------------------- Assets/Scripts/UI/ScreenManager.cs | 5 +++++ 5 files changed, 144 insertions(+), 108 deletions(-) diff --git a/Assets/Scenes/Main.unity b/Assets/Scenes/Main.unity index 37774b1..346f827 100644 --- a/Assets/Scenes/Main.unity +++ b/Assets/Scenes/Main.unity @@ -811,7 +811,7 @@ RectTransform: m_AnchorMin: {x: 0, y: 1.7} m_AnchorMax: {x: 0.97, y: 2.2} m_AnchoredPosition: {x: 0, y: 0} - m_SizeDelta: {x: 0, y: 0} + m_SizeDelta: {x: -239.8714, y: 0} m_Pivot: {x: 1, y: 0} --- !u!114 &160532053 MonoBehaviour: @@ -3963,7 +3963,7 @@ RectTransform: m_AnchorMin: {x: 0, y: 1.7} m_AnchorMax: {x: 0.97, y: 2.2} m_AnchoredPosition: {x: 0, y: 0} - m_SizeDelta: {x: 0, y: 0} + m_SizeDelta: {x: -239.8714, y: 0} m_Pivot: {x: 1, y: 0} --- !u!114 &581668678 MonoBehaviour: @@ -7238,13 +7238,12 @@ RectTransform: m_AnchorMin: {x: 0, y: 1.7} m_AnchorMax: {x: 0.97, y: 2.2} m_AnchoredPosition: {x: 0, y: 0} - m_SizeDelta: {x: 0, y: 0} + m_SizeDelta: {x: -239.8714, y: 0} m_Pivot: {x: 1, y: 0} --- !u!114 &1059619784 MonoBehaviour: m_ObjectHideFlags: 0 - m_PrefabParentObject: {fileID: 11431640, guid: 5ccb0ba277304a14b9bb8d4e34161675, - type: 2} + m_PrefabParentObject: {fileID: 0} m_PrefabInternal: {fileID: 0} m_GameObject: {fileID: 1059619782} m_Enabled: 1 @@ -7253,7 +7252,7 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: m_Navigation: - m_Mode: 0 + m_Mode: 3 m_SelectOnUp: {fileID: 0} m_SelectOnDown: {fileID: 0} m_SelectOnLeft: {fileID: 0} diff --git a/Assets/Scripts/Export Video/ExportLayers.cs b/Assets/Scripts/Export Video/ExportLayers.cs index 42329c0..a3079b0 100644 --- a/Assets/Scripts/Export Video/ExportLayers.cs +++ b/Assets/Scripts/Export Video/ExportLayers.cs @@ -11,6 +11,7 @@ public class ExportLayers{ Share_Layer, Progress_Download_Layer, OnLockExport, + OnLockShare, All } @@ -19,6 +20,7 @@ public class ExportLayers{ INTERNET_CONNECTION_FAILURE, DOWNLOAD_CONNECTION_FAILURE, CONNECTION_TIMEOUT_FAILURE, - ERROR_CONNECTION_FAILURE + ERROR_CONNECTION_FAILURE, + DEFAULT } } diff --git a/Assets/Scripts/Export Video/ExportVideo.cs b/Assets/Scripts/Export Video/ExportVideo.cs index c37549c..8df50b5 100644 --- a/Assets/Scripts/Export Video/ExportVideo.cs +++ b/Assets/Scripts/Export Video/ExportVideo.cs @@ -34,14 +34,9 @@ public class ExportVideo : MonoBehaviour { private const string SERVER_URL = "http://150.165.205.57/video/"; - /*Strings to Compare glosses*/ - private static string gloss = "Gloss"; - private string currentGloss = "CurrentGloss"; - - /* Sharing Current Time - Video Name*/ - private string currentTime = ""; - + private static string gloss = "Gloss"; + /* Strings related to the Android Dialog*/ private string title = "Download"; @@ -101,15 +96,12 @@ public class ExportVideo : MonoBehaviour { public void onDownloadClick() { - MobileNativeDialog nativeDialog; - - + MobileNativeDialog nativeDialog; nativeDialog = new MobileNativeDialog(title, message + ((videoSize/1024f)/1024f).ToString("0.00") + " MB.", yesOp, noOp); nativeDialog.OnComplete = OnComplete; - } - + } /** Delegate function which waits for YES or NO from the MobileDialog * and attach a action to each option**/ @@ -165,33 +157,19 @@ public class ExportVideo : MonoBehaviour { public void OnShareVideo() { - PlayerLogger.Log("ExportVideo", "OnShareVideo", "Sharing Video..."); - - if (gloss == currentGloss) //Checking if user is trying to share the same animation - { - Share(); - } - else - { - currentGloss = gloss; - currentTime = System.DateTime.Now.ToString("dd_MM_yyyy_HH_mm_ss"); - Share(); - } - } - - private void Share() - { try { if (videoContent != null) { + Action errorAction = () => { screenManager.changeExportStates(ExportLayers.ExportLayer.All, true); + new AndroidToast().showToast("Erro ao compartilhar mídia."); }; - MediaShareAndroid.ShareActivity(videoContent, bytes, currentTime); + StartCoroutine(MediaShareAndroid.ShareIntent(errorAction)); } else { - new AndroidToast().showToast("Erro ao compartilhar vídeo"); + new AndroidToast().showToast("Erro ao compartilhar mídia."); screenManager.changeExportStates(ExportLayers.ExportLayer.All, true); } } @@ -235,24 +213,25 @@ public class ExportVideo : MonoBehaviour { } else if (videoInfoRequest.responseHeaders.Count == 0) { + screenManager.changeExportStates(ExportLayers.ExportLayer.All, true); PlayerLogger.Log("ExportVideo", "requestVideoInfo", "(WWW) Unsucessful Answer"); GetConnectionStatusError(ExportLayers.ConnectionStatusError.ERROR_CONNECTION_FAILURE); - screenManager.changeExportStates(ExportLayers.ExportLayer.All, true); + } else if (!videoInfoRequest.responseHeaders["STATUS"].Contains("200")) { - + screenManager.changeExportStates(ExportLayers.ExportLayer.All, true); PlayerLogger.Log("ExportVideo", "requestVideoInfo", "Unsuccessful answer (" + videoInfoRequest.responseHeaders["STATUS"] + ")."); GetConnectionStatusError(ExportLayers.ConnectionStatusError.ERROR_CONNECTION_FAILURE); - screenManager.changeExportStates(ExportLayers.ExportLayer.All, true); + } else if (String.IsNullOrEmpty(videoInfoRequest.text)) { screenManager.changeExportStates(ExportLayers.ExportLayer.All, true); - GetConnectionStatusError(ExportLayers.ConnectionStatusError.ERROR_CONNECTION_FAILURE); + GetConnectionStatusError(ExportLayers.ConnectionStatusError.DEFAULT); PlayerLogger.Log("ExportVideo", "requestVideoInfo", "Empty answer."); } else @@ -285,24 +264,49 @@ public class ExportVideo : MonoBehaviour { { videoDownloadRunning = true; WWW DownloadVideo = new WWW(SERVER_URL + videoId); - // WWW DownloadVideo = new WWW("http://caiomcg.com/rural.mp4"); + while (!DownloadVideo.isDone) { screenManager.updateProgressDownloadSprite(DownloadVideo.progress); + yield return null; } try { - if (DownloadVideo.bytesDownloaded > 0 && DownloadVideo.bytesDownloaded<=videoSize) + + if(DownloadVideo.error != null) + { + GetConnectionStatusError(ExportLayers.ConnectionStatusError.INTERNET_CONNECTION_FAILURE); + ErrorDownloading(); + + } + else if(DownloadVideo.responseHeaders.Count == 0) + { + GetConnectionStatusError(ExportLayers.ConnectionStatusError.ERROR_CONNECTION_FAILURE); + ErrorDownloading(); + + } + else if (!DownloadVideo.responseHeaders["STATUS"].Contains("200")) + { + GetConnectionStatusError(ExportLayers.ConnectionStatusError.ERROR_CONNECTION_FAILURE); + ErrorDownloading(); + + } + else if ( DownloadVideo.bytesDownloaded == videoSize) { bytes = DownloadVideo.bytesDownloaded; videoContent = DownloadVideo.bytes; - OnDownloadFinished(); + Action callBack = () => { OnDownloadFinished(); new AndroidToast().showToast("Vídeo salvo na Galeria."); }; + Action errorWriting = () => { screenManager.changeExportStates(ExportLayers.ExportLayer.All, true); }; + videoDownloadRunning = false; + StartCoroutine(MediaShareAndroid.WriteOnAndroid(videoContent, bytes, System.DateTime.Now.ToString("dd_MM_yyyy_HH_mm_ss"),callBack,errorWriting)); + } else { GetConnectionStatusError(ExportLayers.ConnectionStatusError.DOWNLOAD_CONNECTION_FAILURE); + ErrorDownloading(); } @@ -313,6 +317,16 @@ public class ExportVideo : MonoBehaviour { } } + /* In case of error during the video download, the user gets a new chance to download the file, but + * is also able to start a new fresh animation and export it */ + private void ErrorDownloading() + { + screenManager.changeExportStates(ExportLayers.ExportLayer.Progress_Download_Layer, false); + screenManager.changeExportStates(ExportLayers.ExportLayer.Download_Layer, true); + screenManager.changeExportStates(ExportLayers.ExportLayer.OnLockExport, false); + screenManager.changeExportStates(ExportLayers.ExportLayer.OnLockShare, false); + } + public IEnumerator WaitForResponse(WWW www, Events.RequestSuccess success, Events.RequestError error) { yield return Methods.WaitForResponse(www, 60*5f, success, error); @@ -324,6 +338,7 @@ public class ExportVideo : MonoBehaviour { yield return WaitForResponse(www, null, null); } + /* Error states to show a Toast to user*/ private static void GetConnectionStatusError(ExportLayers.ConnectionStatusError error) { switch (error) diff --git a/Assets/Scripts/Export Video/MediaShareAndroid.cs b/Assets/Scripts/Export Video/MediaShareAndroid.cs index 5e821a3..b536553 100644 --- a/Assets/Scripts/Export Video/MediaShareAndroid.cs +++ b/Assets/Scripts/Export Video/MediaShareAndroid.cs @@ -18,6 +18,7 @@ using UnityEngine; using System.Collections; using System.IO; using System; +using LAViD.Unity.Utils; @@ -30,9 +31,10 @@ public class MediaShareAndroid : MonoBehaviour private string AndroidPath; - private string destination = ""; + private static string destination = ""; private string Vlibras = "DCIM/VLibras"; - private string File = "VLibrasVideo_"; + private string Filen = "VLibrasVideo_"; + private static string FilePath = ""; private byte[] videoContent; private int bytes; @@ -45,25 +47,12 @@ public class MediaShareAndroid : MonoBehaviour /*Makes sure that Vlibras Folder gets created and write the video bytes in it.*/ - /* Creates the sharing activity and shows the chooser to the user*/ - - public void ShareActivity(byte[] videoContent,int bytes, string CurrentTime) + public IEnumerator WriteOnAndroid(byte[] videoContent, int bytes,string CurrentTime, Action finishWriting, Action errorWriting) { - #if UNITY_ANDROID - - this.videoContent = videoContent; - this.bytes = bytes; - - - StartCoroutine(WriteOnAndroid(File + CurrentTime + ".mp4")); - -#endif - } - - private IEnumerator WriteOnAndroid(string FileName) - { + FilePath = Filen + CurrentTime + ".mp4"; destination = Path.Combine(AndroidPath, Vlibras); + /* Checking if folder Vlibras exists*/ @@ -72,72 +61,98 @@ public class MediaShareAndroid : MonoBehaviour { dir.Create(); } - - /*Checking if there's a file with same name - If so, means user's trying - * to share the same video*/ - - dir = new DirectoryInfo(Path.Combine(destination, FileName)); - if (!dir.Exists) + + try + { + FileStream SourceStream = new FileStream(Path.Combine(destination, FilePath), FileMode.OpenOrCreate); + if (SourceStream.CanWrite) + { + SourceStream.Write(videoContent, 0, bytes); + SourceStream.Close(); + + Refresh(FilePath); + finishWriting(); + yield break; + + } + else + { + new AndroidToast().showToast("Erro ao salvar vídeo."); + errorWriting(); + } + + + } + catch (Exception e) { - FileStream SourceStream = new FileStream(Path.Combine(destination, FileName), FileMode.OpenOrCreate); - if (SourceStream.CanWrite) - { - SourceStream.Write(videoContent, 0, bytes); - SourceStream.Close(); - - } - else - { - new AndroidToast().showToast("Erro ao compartilhar arquivo."); - } - + PlayerLogger.Log("MediaShareAndroid", "WriteOnAndroid","Message: " + e.Message); + PlayerLogger.Log("MediaShareAndroid", "WriteOnAndroid", "Message: " + e.StackTrace); + new AndroidToast().showToast("Você precisa dá permissão ao aplicativo"); + errorWriting(); } - yield return null; - Refresh(FileName); + +#endif + } - private void Refresh(string FileName) - { + /* Calling Android Services to refresh the video path, making available to show in gallery*/ + private void Refresh(string FilePath) + { + AndroidJavaClass jcUnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); AndroidJavaObject joActivity = jcUnityPlayer.GetStatic("currentActivity"); AndroidJavaObject joContext = joActivity.Call("getApplicationContext"); AndroidJavaClass jcMediaScannerConnection = new AndroidJavaClass("android.media.MediaScannerConnection"); - //AndroidJavaClass jcEnvironment = new AndroidJavaClass("android.os.Environment"); - // AndroidJavaObject joExDir = jcEnvironment.CallStatic("getExternalStorageDirectory"); - string path = Path.Combine(destination,FileName); - jcMediaScannerConnection.CallStatic("scanFile", joContext, new string[] { path }, null, null); - StartCoroutine(ShareIntent(FileName)); + //AndroidJavaClass jcEnvironment = new AndroidJavaClass("android.os.Environment"); + // AndroidJavaObject joExDir = jcEnvironment.CallStatic("getExternalStorageDirectory"); + string path = destination; + jcMediaScannerConnection.CallStatic("scanFile", joContext, new string[] { path }, new string[] { "video/mp4" },null ); + + } - private IEnumerator ShareIntent(string FileName) { - // block to open the file and share it ------------START - AndroidJavaClass intentClass = new AndroidJavaClass("android.content.Intent"); - AndroidJavaObject intentObject = new AndroidJavaObject("android.content.Intent"); - intentObject.Call("setAction", intentClass.GetStatic("ACTION_SEND")); + /* Calling Android Services to create Share Intent*/ - AndroidJavaClass uriClass = new AndroidJavaClass("android.net.Uri"); - AndroidJavaObject uriObject = uriClass.CallStatic("parse", "file://" + Path.Combine(destination,FileName)); - - intentObject.Call("putExtra", intentClass.GetStatic("EXTRA_STREAM"), uriObject); - intentObject.Call("setType", MediaType); + public IEnumerator ShareIntent(Action errorAction) { + + + if(File.Exists(Path.Combine(destination, FilePath))) + { - //add data to be passed to the other activity i.e., the data to be sent - intentObject.Call("putExtra", intentClass.GetStatic("EXTRA_SUBJECT"), "VLibras"); - intentObject.Call("putExtra", intentClass.GetStatic("EXTRA_TEXT"), SubtitleMessage); - intentObject.Call("setType", "video/*"); - AndroidJavaClass unity = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); - AndroidJavaObject jChooser = intentClass.CallStatic("createChooser", intentObject, "Compartilhar via"); - AndroidJavaObject currentActivity = unity.GetStatic("currentActivity"); - currentActivity.Call("startActivity", jChooser); - yield break; + // block to open the file and share it ------------START + AndroidJavaClass intentClass = new AndroidJavaClass("android.content.Intent"); + AndroidJavaObject intentObject = new AndroidJavaObject("android.content.Intent"); + intentObject.Call("setAction", intentClass.GetStatic("ACTION_SEND")); + + AndroidJavaClass uriClass = new AndroidJavaClass("android.net.Uri"); + AndroidJavaObject uriObject = uriClass.CallStatic("parse", "file://" + Path.Combine(destination, FilePath)); + + intentObject.Call("putExtra", intentClass.GetStatic("EXTRA_STREAM"), uriObject); + intentObject.Call("setType", MediaType); + + //add data to be passed to the other activity i.e., the data to be sent + intentObject.Call("putExtra", intentClass.GetStatic("EXTRA_SUBJECT"), "VLibras"); + intentObject.Call("putExtra", intentClass.GetStatic("EXTRA_TEXT"), SubtitleMessage); + intentObject.Call("setType", "video/*"); + AndroidJavaClass unity = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); + AndroidJavaObject jChooser = intentClass.CallStatic("createChooser", intentObject, "Compartilhar via"); + AndroidJavaObject currentActivity = unity.GetStatic("currentActivity"); + currentActivity.Call("startActivity", jChooser); + yield break; + }else + { + errorAction(); + + yield break; + } } diff --git a/Assets/Scripts/UI/ScreenManager.cs b/Assets/Scripts/UI/ScreenManager.cs index 79142f4..30d596c 100644 --- a/Assets/Scripts/UI/ScreenManager.cs +++ b/Assets/Scripts/UI/ScreenManager.cs @@ -317,6 +317,8 @@ public class ScreenManager : MonoBehaviour { this.shareLayer.SetActive(active); } + + public void setTranslateButtonActive(bool active) { this.textButton.SetActive(active); @@ -379,6 +381,9 @@ public class ScreenManager : MonoBehaviour { case ExportLayers.ExportLayer.OnLockExport: onLockExport = show_Layer; break; + case ExportLayers.ExportLayer.OnLockShare: + onLockShare = show_Layer; + break; default: onLockShare = false; onLockExport = false; -- libgit2 0.21.2