From 2f0622dd76eb5c89a0ca36e03886c2ed243c8c13 Mon Sep 17 00:00:00 2001 From: Thiago Filipe Soares da Rocha Date: Tue, 7 Feb 2017 10:43:56 -0300 Subject: [PATCH] Finish refactoring --- Assets/Scenes/Main.unity | 10 +++++----- Assets/Scripts/Export Video/AndroidJavaUti.cs | 21 ++++++++++++++++++--- Assets/Scripts/Export Video/AndroidToast.cs | 22 +++++++++++++++++++--- Assets/Scripts/Export Video/ExportLayers.cs | 8 ++++++++ Assets/Scripts/Export Video/ExportVideo.cs | 118 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------------- Assets/Scripts/Export Video/MediaShareAndroid.cs | 8 ++++---- Assets/Scripts/PlayerManager.cs | 10 +++++----- Assets/Scripts/UI/ScreenManager.cs | 2 +- 8 files changed, 147 insertions(+), 52 deletions(-) diff --git a/Assets/Scenes/Main.unity b/Assets/Scenes/Main.unity index 0bfcb26..c1dc692 100644 --- a/Assets/Scenes/Main.unity +++ b/Assets/Scenes/Main.unity @@ -515,7 +515,7 @@ GameObject: m_Icon: {fileID: 0} m_NavMeshLayer: 0 m_StaticEditorFlags: 0 - m_IsActive: 0 + m_IsActive: 1 --- !u!224 &126648712 RectTransform: m_ObjectHideFlags: 0 @@ -574,12 +574,12 @@ MonoBehaviour: m_OnClick: m_PersistentCalls: m_Calls: - - m_Target: {fileID: 0} - m_MethodName: + - m_Target: {fileID: 210788816} + m_MethodName: OnCancel m_Mode: 1 m_Arguments: m_ObjectArgument: {fileID: 0} - m_ObjectArgumentAssemblyTypeName: + m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine m_IntArgument: 0 m_FloatArgument: 0 m_StringArgument: @@ -8431,7 +8431,7 @@ MonoBehaviour: m_PersistentCalls: m_Calls: - m_Target: {fileID: 210788816} - m_MethodName: OnDownloadCancel + m_MethodName: OnCancel m_Mode: 1 m_Arguments: m_ObjectArgument: {fileID: 0} diff --git a/Assets/Scripts/Export Video/AndroidJavaUti.cs b/Assets/Scripts/Export Video/AndroidJavaUti.cs index 8877616..7048fb5 100644 --- a/Assets/Scripts/Export Video/AndroidJavaUti.cs +++ b/Assets/Scripts/Export Video/AndroidJavaUti.cs @@ -1,4 +1,21 @@ -using UnityEngine; +/********************** +********LAVID********** +***VLibras Project***** +*------------------------------------------------------------------------ +*Description: +*This class is an Android Plugin that gets the current sd card path from +* Android Device, making available the access to files folder. +*--------------------------------------------------------------------------- +*Plugin directory: +* - Assets/Plugins/Android +*References: +* - http://docs.unity3d.com/ScriptReference/AndroidJavaClass.html +*------------------------------------------------------------------------ +*Author: Thiago Filipe +*thiago.filipe@lavid.ufpb.br +***********************/ + +using UnityEngine; using System.Collections; using System; @@ -26,8 +43,6 @@ public class AndroidJavaUti{ return m_pkgName; } } - - public static string CurrentSDCardPath { get diff --git a/Assets/Scripts/Export Video/AndroidToast.cs b/Assets/Scripts/Export Video/AndroidToast.cs index d308366..bbf4d6c 100644 --- a/Assets/Scripts/Export Video/AndroidToast.cs +++ b/Assets/Scripts/Export Video/AndroidToast.cs @@ -1,4 +1,21 @@ -using UnityEngine; +/********************** +********LAVID********** +***VLibras Project***** +*------------------------------------------------------------------------ +*Description: +*This class is a Android Plugin that allows the creation of Toast +* notifications. +*--------------------------------------------------------------------------- +*Plugin directory: +* - Assets/Plugins/Android +*References: +* - http://docs.unity3d.com/ScriptReference/AndroidJavaClass.html +*------------------------------------------------------------------------ +*Author: Thiago Filipe +*thiago.filipe@lavid.ufpb.br +***********************/ + +using UnityEngine; using System.Collections; public class AndroidToast : MonoBehaviour @@ -7,7 +24,6 @@ public class AndroidToast : MonoBehaviour AndroidJavaClass UnityPlayer; AndroidJavaObject context; - public void showToast(string toastString){ if (Application.platform == RuntimePlatform.Android) { @@ -17,7 +33,7 @@ public class AndroidToast : MonoBehaviour AndroidJavaClass Toast = new AndroidJavaClass("android.widget.Toast"); AndroidJavaObject javaString = new AndroidJavaObject("java.lang.String", toastString); - AndroidJavaObject toast = Toast.CallStatic("makeText", context, javaString, Toast.GetStatic("LENGTH_SHORT")); + AndroidJavaObject toast = Toast.CallStatic("makeText", context, javaString, Toast.GetStatic("LENGTH_LONG")); //Toast Duration Time toast.Call("show"); } } diff --git a/Assets/Scripts/Export Video/ExportLayers.cs b/Assets/Scripts/Export Video/ExportLayers.cs index f02062d..42329c0 100644 --- a/Assets/Scripts/Export Video/ExportLayers.cs +++ b/Assets/Scripts/Export Video/ExportLayers.cs @@ -13,4 +13,12 @@ public class ExportLayers{ OnLockExport, All } + + public enum ConnectionStatusError + { + INTERNET_CONNECTION_FAILURE, + DOWNLOAD_CONNECTION_FAILURE, + CONNECTION_TIMEOUT_FAILURE, + ERROR_CONNECTION_FAILURE + } } diff --git a/Assets/Scripts/Export Video/ExportVideo.cs b/Assets/Scripts/Export Video/ExportVideo.cs index 831f8b6..b343fec 100644 --- a/Assets/Scripts/Export Video/ExportVideo.cs +++ b/Assets/Scripts/Export Video/ExportVideo.cs @@ -32,22 +32,34 @@ public class ExportVideo : MonoBehaviour { public ScreenManager screenManager; private MobileNativeDialog nativeDialog; - public static string gloss = "Gloss"; + private static string gloss = "Gloss"; private string currentGloss = "CurrentGloss"; private string currentTime = ""; private const string SERVER_URL = "http://150.165.205.38/video/"; + /* Strings related to the Android Dialog*/ private string title = "Download"; private string message = "Para compartilhar é preciso realizar o download do vídeo. Tamanho do Download: "; private string yesOp = "DOWNLOAD"; private string noOp = "CANCELAR"; + + private float videoSize = 0; - private string videoId = ""; - private bool CancelDownload = false; + private string videoId = ""; private byte[] videoContent = null; - + private bool videoRequestRunning = false; + private bool videoDownloadRunning = false; + + + /*Getting called by PlayerManager, setting the gloss property*/ + + public static void SetGloss(string _gloss) + { + PlayerLogger.Log("ExportVideo", "SetGloss", "Setting current gloss to gloss property from ExportVideo"); + gloss = _gloss; + } /* Function that hides the export button and * shows the progress bar while getting video info*/ @@ -61,7 +73,7 @@ public class ExportVideo : MonoBehaviour { StartCoroutine("requestVideoInfo"); Debug.Log("Hiding export button and showing progress loading"); - } + } /* Function that gets called by the web request * hiding the progress bar and showing the download @@ -116,16 +128,30 @@ public class ExportVideo : MonoBehaviour { screenManager.changeExportStates(ExportLayers.ExportLayer.Share_Layer, true); } - public void OnDownloadCancel() - { - Debug.Log("User canceled the download, resetting states to default"); - CancelDownload = true; - screenManager.changeExportStates(ExportLayers.ExportLayer.All, true); - videoContent = null; + /*Cancels the VideoRequest or VideoDownload by interrupting the coroutine and resetting values*/ + public void OnCancel() + { + if (videoRequestRunning) + { + StopCoroutine("requestVideoInfo"); + PlayerLogger.Log("ExportVideo", "OnRequestCancel", "User canceled the request, resetting states to default."); + + }else if (videoDownloadRunning) + { + StopCoroutine("videoDownload"); + PlayerLogger.Log("ExportVideo", "videoDownload", "User canceled the download, resetting states to default."); + } + videoId = ""; + videoSize = 0; + videoContent = null; + screenManager.changeExportStates(ExportLayers.ExportLayer.All, true); } + /*With sharing layer active, gets called when the user clicks on it and makes + * the video available to share*/ + public void OnShareVideo() { Debug.Log("Sharing video content"); @@ -136,6 +162,7 @@ public class ExportVideo : MonoBehaviour { else { new AndroidToast().showToast("Erro ao compartilhar vídeo"); + screenManager.changeExportStates(ExportLayers.ExportLayer.All, true); } }else @@ -147,6 +174,7 @@ public class ExportVideo : MonoBehaviour { new MediaShareAndroid().ShareActivity(videoContent, currentTime); else { + screenManager.changeExportStates(ExportLayers.ExportLayer.All, true); new AndroidToast().showToast("Erro ao compartilhar vídeo"); } @@ -154,85 +182,87 @@ public class ExportVideo : MonoBehaviour { } + /*Coroutine that downloads the video and update the progress download layer*/ private IEnumerator videoDownload() { + videoDownloadRunning = true; WWW DownloadVideo = new WWW(SERVER_URL + videoId); - while (!CancelDownload && !DownloadVideo.isDone) + while (!DownloadVideo.isDone) { screenManager.updateProgressDownloadSprite(DownloadVideo.progress); yield return null; } - if (!CancelDownload) + try { if (DownloadVideo.bytesDownloaded > 0 && DownloadVideo.bytesDownloaded <= videoSize) { videoContent = DownloadVideo.bytes; - OnDownloadFinished(); - }else + } + else { - new AndroidToast().showToast("Erro ao efetuar download. Tente novamente"); - screenManager.changeExportStates(ExportLayers.ExportLayer.All, true); + GetConnectionStatusError(ExportLayers.ConnectionStatusError.DOWNLOAD_CONNECTION_FAILURE); } - }else + + + } + finally { - CancelDownload = false; - yield break; + videoDownloadRunning = false; } } + /*Coroutine that makes the video information request*/ + private IEnumerator requestVideoInfo() { - + videoRequestRunning = true; WWWForm JsonRequest = new WWWForm(); JsonRequest.AddField("gloss", gloss); WWW videoInfoRequest = new WWW(SERVER_URL,JsonRequest); - - - PlayerLogger.Log("ExportVideo", "requestVideoInfo", "Request for: " + SERVER_URL); yield return WaitForResponse(videoInfoRequest); - - try { if (!videoInfoRequest.isDone) { screenManager.changeExportStates(ExportLayers.ExportLayer.All, true); - new AndroidToast().showToast("Erro de conexão, tenta novamente."); + GetConnectionStatusError(ExportLayers.ConnectionStatusError.CONNECTION_TIMEOUT_FAILURE); PlayerLogger.Log("ExportVideo", "requestVideoInfo", "TimeOut"); } else if (videoInfoRequest.error != null) { screenManager.changeExportStates(ExportLayers.ExportLayer.All, true); - new AndroidToast().showToast("Erro de conexão, tenta novamente."); + GetConnectionStatusError(ExportLayers.ConnectionStatusError.INTERNET_CONNECTION_FAILURE); PlayerLogger.Log("ExportVideo", "requestVideoInfo", "(WWW) Error: " + videoInfoRequest.error); } else if (videoInfoRequest.responseHeaders.Count == 0) { PlayerLogger.Log("ExportVideo", "requestVideoInfo", "(WWW) Unsucessful Answer"); - new AndroidToast().showToast("Erro de conexão, tenta novamente."); + GetConnectionStatusError(ExportLayers.ConnectionStatusError.ERROR_CONNECTION_FAILURE); screenManager.changeExportStates(ExportLayers.ExportLayer.All, true); } else if (!videoInfoRequest.responseHeaders["STATUS"].Contains("200")) { - new AndroidToast().showToast("Erro de conexão, tenta novamente."); + 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)) { - new AndroidToast().showToast("Erro de conexão, tenta novamente."); + screenManager.changeExportStates(ExportLayers.ExportLayer.All, true); + GetConnectionStatusError(ExportLayers.ConnectionStatusError.ERROR_CONNECTION_FAILURE); PlayerLogger.Log("ExportVideo", "requestVideoInfo", "Empty answer."); } else @@ -252,12 +282,15 @@ public class ExportVideo : MonoBehaviour { } finally { + videoRequestRunning = false; } } + + public IEnumerator WaitForResponse(WWW www, Events.RequestSuccess success, Events.RequestError error) { yield return Methods.WaitForResponse(www, 60f, success, error); @@ -269,6 +302,29 @@ public class ExportVideo : MonoBehaviour { yield return WaitForResponse(www, null, null); } + public static void GetConnectionStatusError(ExportLayers.ConnectionStatusError error) + { + switch (error) + { + case ExportLayers.ConnectionStatusError.CONNECTION_TIMEOUT_FAILURE: + new AndroidToast().showToast("Erro de conexão. Tente novamente."); + break; + case ExportLayers.ConnectionStatusError.DOWNLOAD_CONNECTION_FAILURE: + new AndroidToast().showToast("Erro ao efetuar download, tente novamente."); + break; + case ExportLayers.ConnectionStatusError.INTERNET_CONNECTION_FAILURE: + new AndroidToast().showToast("Não há acesso à internet. Verifique sua conexão"); + break; + case ExportLayers.ConnectionStatusError.ERROR_CONNECTION_FAILURE: + new AndroidToast().showToast("Problema na conexão. Aguarde e tente novamente."); + break; + default: + new AndroidToast().showToast("Ocorreu um erro. Estamos trabalhando para solucioná-lo!"); + break; + } + + } + diff --git a/Assets/Scripts/Export Video/MediaShareAndroid.cs b/Assets/Scripts/Export Video/MediaShareAndroid.cs index dfa760d..2114933 100644 --- a/Assets/Scripts/Export Video/MediaShareAndroid.cs +++ b/Assets/Scripts/Export Video/MediaShareAndroid.cs @@ -32,13 +32,13 @@ public class MediaShareAndroid : MonoBehaviour private string FileName = "/VLibrasVideo_"; + /*Makes sure that Vlibras Folder gets created and write the video bytes in it.*/ + private void WriteOnAndroid(byte[] videoContent) { if(!Directory.Exists(AndroidPath + Vlibras)) { - Directory.CreateDirectory(AndroidPath + Vlibras); - - + Directory.CreateDirectory(AndroidPath + Vlibras); } if (!Directory.Exists(AndroidPath + Vlibras + FileName)) File.WriteAllBytes(AndroidPath + Vlibras + FileName, videoContent); @@ -47,7 +47,7 @@ public class MediaShareAndroid : MonoBehaviour } - + /* Creates the sharing activity and shows the chooser to the user*/ public void ShareActivity(byte[] videoContent, string CurrentTime) { diff --git a/Assets/Scripts/PlayerManager.cs b/Assets/Scripts/PlayerManager.cs index 15c6d73..cf2252f 100644 --- a/Assets/Scripts/PlayerManager.cs +++ b/Assets/Scripts/PlayerManager.cs @@ -85,6 +85,8 @@ public class PlayerManager : GenericPlayerManager { this.signs = this.gameObject.GetComponent().Signs; } + + public void playDict(string word) { PlayerLogger.Log("Requesting dictionary: " + word); @@ -93,7 +95,7 @@ public class PlayerManager : GenericPlayerManager { base.gloss = word; base.playNow(word); - ExportVideo.gloss = word; + ExportVideo.SetGloss(word); this.screenManager.changeExportStates(ExportLayers.ExportLayer.OnLockExport, false); this.screenManager.hideScreen(); @@ -279,10 +281,8 @@ public class PlayerManager : GenericPlayerManager { gloss = glossRequest.text; base.gloss = gloss; - ExportVideo.gloss = gloss; - - base.playNow(base.gloss); - + ExportVideo.SetGloss(gloss); + base.playNow(base.gloss); yield break; } diff --git a/Assets/Scripts/UI/ScreenManager.cs b/Assets/Scripts/UI/ScreenManager.cs index 044a109..5aa129b 100644 --- a/Assets/Scripts/UI/ScreenManager.cs +++ b/Assets/Scripts/UI/ScreenManager.cs @@ -371,7 +371,7 @@ public class ScreenManager : MonoBehaviour { setShareLayerState(show_Layer); break; case ExportLayers.ExportLayer.OnLockExport: - onLockExport = show_Layer; + onLockExport = show_Layer; break; default: onLockExport = false; -- libgit2 0.21.2