public IEnumerator PhotoAlbumSaveImage() { //StartActivityIndicator (Localization.Get ("txt_loading_info_processing")); mProcessResult = ProcessResult.None; mProcessResultInfo = ""; if (Application.platform == RuntimePlatform.OSXEditor) { yield return(new WaitForSeconds(1.0f)); mProcessResult = ProcessResult.ProcessSucceed; } else { #if UNITY_IPHONE EtceteraManager.saveImageToPhotoAlbumSucceededEvent += PhotoAlbumSaveImageSucceed; EtceteraManager.saveImageToPhotoAlbumFailedEvent += PhotoAlbumSaveImageFailed; EtceteraBinding.saveImageToPhotoAlbum(ImagePath); if (Application.platform == RuntimePlatform.IPhonePlayer) { while (mProcessResult == ProcessResult.None) { yield return(new WaitForEndOfFrame()); } } else { yield return(new WaitForSeconds(1.0f)); mProcessResult = ProcessResult.ProcessSucceed; } EtceteraManager.saveImageToPhotoAlbumSucceededEvent -= PhotoAlbumSaveImageSucceed; EtceteraManager.saveImageToPhotoAlbumFailedEvent -= PhotoAlbumSaveImageFailed; #elif UNITY_ANDROID CocoPluginManager pluginManager = CocoMainController.PluginManager; bool res = false; yield return(pluginManager.StartCoroutine(pluginManager.RequestPermission( CocoPluginManager.PERMISSION_WRITE_EXTERNAL_STORAGE, GameLocalization.Get("txt_permission_rationale_save_to_gallery"), (passed) => { if (passed) { res = passed; } } ))); if (res) { res = EtceteraAndroid.saveImageToGallery(ImagePath, Path.GetFileNameWithoutExtension(ImagePath)); } mProcessResult = res ? ProcessResult.ProcessSucceed : ProcessResult.ProcessFailed; #endif } //StopActivityIndicator (); string msg = string.Empty; if (mProcessResult == ProcessResult.ProcessSucceed) { msg = CocoLocalization.Get("txt_prompt_info_share_photo_album_succeed"); } else { msg = CocoLocalization.Get("txt_prompt_info_share_photo_album_failed") + "\n" + mProcessResultInfo; } ShowPromptMessage(msg, 1.5f); System.IO.File.Delete(ImagePath); Release(); if (endAction != null) { endAction(mProcessResult == ProcessResult.ProcessSucceed); } }
public IEnumerator RequestPermission(string pPermission, string pRequestRationale, Action <bool> pEndAction) { bool granted = false; Debug.Log("RequestPermission: [" + pPermission + "] " + pRequestRationale); if (mPermissionFlagsMapping.ContainsKey(pPermission)) { AndroidPermissionsWrapper wrapper = AndroidPermissionsWrapper.Instance; bool shouldShowRationale = false; bool shouldRequested = true; // check if permission is granted if (wrapper.CheckSelfPermission(pPermission)) { granted = true; shouldRequested = false; } else { if (wrapper.ShouldShowRequestPermissionRationale(pPermission)) { Debug.Log("RequestPermission: ShouldShowRequestPermissionRationale -> true"); shouldShowRationale = true; } else { // check if already request before this if ((PlayerPrefs.GetInt(PermissionRequestFlag) & mPermissionFlagsMapping [pPermission]) != 0) // already request // maybe user checked "Don't show again" { shouldShowRationale = true; shouldRequested = false; Debug.Log("RequestPermission: already request"); } } } // show request rationale if (shouldShowRationale) { Debug.Log("RequestPermission: show request rationale"); string title = GameLocalization.Get("txt_prompt_title_info"); string okButton = GameLocalization.Get("txt_prompt_button_ok"); string cancelButton = string.Empty; if (!shouldRequested) { okButton = GameLocalization.Get("txt_permission_open_settings"); cancelButton = GameLocalization.Get("txt_prompt_button_cancel"); } bool clickOK = false; yield return(StartCoroutine(ShowAlert(title, pRequestRationale, okButton, cancelButton, (ok) => { clickOK = ok; }))); // open app settings if (!shouldRequested && clickOK) { Debug.Log("RequestPermission: OpenAppSettings start"); OpenAppSettings(); yield return(new WaitForEndOfFrame()); yield return(new WaitForEndOfFrame()); // assumed user allow request shouldRequested = true; Debug.Log("RequestPermission: OpenAppSettings end"); } } // request permission if (shouldRequested) { Debug.Log("RequestPermission: request start"); bool processing = true; Action <string[], bool[]> requestResultEvent = (permissions, grants) => { for (int i = 0; i < permissions.Length; i++) { Debug.Log("RequestPermission: [" + permissions [i] + "]<->[" + pPermission + "]: " + grants [i]); if (permissions [i] == pPermission) { granted = grants [i]; break; } } processing = false; }; wrapper.OnRequestPermissionsResultEvent += requestResultEvent; wrapper.RequestPermissions(new string[] { pPermission }); if (Application.platform == RuntimePlatform.Android) { while (processing) { yield return(new WaitForEndOfFrame()); } } else { yield return(new WaitForSeconds(1.0f)); granted = UnityEngine.Random.value < 0.5f; processing = false; } wrapper.OnRequestPermissionsResultEvent -= requestResultEvent; Debug.Log("RequestPermission: request end -> granted: " + granted); // record request flag if (!granted) { int recordPermissionRequestFlag = PlayerPrefs.GetInt(PermissionRequestFlag); recordPermissionRequestFlag |= mPermissionFlagsMapping [pPermission]; PlayerPrefs.SetInt(PermissionRequestFlag, recordPermissionRequestFlag); PlayerPrefs.Save(); } } } if (pEndAction != null) { pEndAction(granted); } }