public void DriveAccountSelectionCanceled(string nothing)
 {
     Debug.Log("DriveAccountSelectionCanceled()");
     uploadFailed          = true;
     failureType           = DriveFailureType.AccountSelectionCanceled;
     waitingForPermissions = false;
 }
    // Check if drive fileId still exists on drive
    public IEnumerator CheckFileId(string fileId, System.Action <DriveFileExistsResult> resultCallback)
    {
        Debug.Log("CheckFileId('" + fileId + "')");

        bool fileExists = false;

        if (Application.platform == RuntimePlatform.Android)
        {
                        #if UNITY_ANDROID
            using (AndroidJavaClass activityClass = new AndroidJavaClass("com.androidexperiments.sprayscape.unitydriveplugin.GoogleDriveUnityPlayerActivity"))
            {
                using (AndroidJavaObject activity = activityClass.GetStatic <AndroidJavaObject>("activityInstance"))
                {
                    fileExists = activity.Call <bool>("checkFileId", fileId);

                    resultCallback(new DriveFileExistsResult
                    {
                        failed = false,
                        exists = fileExists
                    });
                }
            }
                        #endif
        }
        else if (Application.platform == RuntimePlatform.IPhonePlayer)
        {
            fileExists = false;
                        #if UNITY_IOS
            _GoogleDrivePlugin_CheckFileId(fileId, this.gameObject.name, this.iOSClientId, this.iOSKeychainName);
                        #endif
        }

        float startTime = Time.time;

        while (waitingForFileCheck)
        {
            // check for timeout
            float ellapsed = Time.time - startTime;
            if (ellapsed > timeOutInSeconds)
            {
                // force a time-out here, seems like java-land is not getting back to us...
                waitingForFileCheck = false;
                uploadFailed        = true;
                failedReason        = "Operation timed-out";
                failureType         = DriveFailureType.Timeout;
            }

            yield return(null);
        }

        resultCallback(new DriveFileExistsResult
        {
            failed       = this.uploadFailed,
            exists       = fileExists,
            failedReason = this.failedReason,
            failureType  = this.failureType
        });
    }
 public void DrivePermissionChangeFailed(string reason)
 {
     Debug.Log("DrivePermissionChangeFailed('" + reason + "')");
     // we will still get the generic DriveUploadFailed() event as well, so don't stop the co-routine yet!
     //waitingForUpload = false;
     uploadFailed = true;
     failedReason = reason;
     failureType  = DriveFailureType.DrivePermissionIssue;
 }
 public void DriveAuthCanceled(string reason)
 {
     Debug.Log("DriveAuthCanceled('" + reason + "')");
     uploadFailed          = true;
     failedReason          = reason;
     failureType           = DriveFailureType.AuthCanceled;
     waitingForUpload      = false;
     waitingForPermissions = false;
     waitingForToken       = false;
 }
    // Coroutine uploading a local file to drive
    public IEnumerator UploadCoroutine(string driveFolderName, string driveFileName, string localPath, System.Action <DriveUploadResult> resultCallback)
    {
        if (resultCallback == null)
        {
            resultCallback = DummyResultCallback;
        }

        if (uploading)
        {
            resultCallback(new DriveUploadResult
            {
                failed       = true,
                failedReason = "Upload already in progress",
                fileId       = null,
                failureType  = DriveFailureType.UploadInProgress
            });
            yield break;             // stop co-routine
        }

        uploading        = true;
        waitingForUpload = true;
        uploadFailed     = false;
        failureType      = DriveFailureType.GenericFailure;
        fileId           = null;

        if (Application.platform == RuntimePlatform.Android)
        {
#if UNITY_ANDROID
            using (AndroidJavaClass activityClass = new AndroidJavaClass("com.androidexperiments.sprayscape.unitydriveplugin.GoogleDriveUnityPlayerActivity"))
            {
                using (AndroidJavaObject activity = activityClass.GetStatic <AndroidJavaObject>("activityInstance"))
                {
                    activity.Call <bool>("uploadFile", driveFolderName, driveFileName, localPath, this.gameObject.name);
                }
            }
#endif
        }
        else if (Application.platform == RuntimePlatform.IPhonePlayer)
        {
#if UNITY_IOS
            _GoogleDrivePlugin_UploadFile(driveFolderName, driveFileName, localPath, this.gameObject.name, this.iOSClientId, this.iOSKeychainName);
#endif
        }
        else
        {
            // PC/Editor, use editor properties to fake the result so we can test both cases...
            yield return(new WaitForSeconds(1.0f));

            waitingForUpload = false;
            uploadFailed     = fakeUploadFailed;
            failedReason     = fakeUploadFailReason;
            fileId           = fakeFileId;
            failureType      = fakeFailureType;
        }

        float startTime = Time.time;

        while (waitingForUpload)
        {
            // check for timeout
            float ellapsed = Time.time - startTime;
            if (ellapsed > timeOutInSeconds)
            {
                // force a time-out here, seems like java-land is not getting back to us...
                waitingForUpload = false;
                uploadFailed     = true;
                failedReason     = "Operation timed-out";
                fileId           = null;
                failureType      = DriveFailureType.Timeout;
            }

            yield return(null);
        }

        // set this before the call back just in case
        uploading = false;

        resultCallback(new DriveUploadResult
        {
            failed       = this.uploadFailed,
            failedReason = this.failedReason,
            fileId       = this.fileId,
            failureType  = this.failureType,
        });
    }
    // Coroutine checking for all required permissions and checks if account is selected
    public IEnumerator CheckPermissionsCoroutine(System.Action <DrivePermissionsResult> resultCallback)
    {
        Debug.Log("CheckPermissionsCoroutine()");

        if (resultCallback == null)
        {
            resultCallback = DummyPermissionsResultCallback;
        }

        uploadFailed          = false;
        failureType           = DriveFailureType.GenericFailure;
        waitingForPermissions = true;

        if (Application.platform == RuntimePlatform.Android)
        {
                        #if UNITY_ANDROID
            using (AndroidJavaClass activityClass = new AndroidJavaClass("com.androidexperiments.sprayscape.unitydriveplugin.GoogleDriveUnityPlayerActivity"))
            {
                using (AndroidJavaObject activity = activityClass.GetStatic <AndroidJavaObject>("activityInstance"))
                {
                    activity.Call <bool>("checkAccountPermissions", this.gameObject.name);
                }
            }
                        #endif
        }
        else if (Application.platform == RuntimePlatform.IPhonePlayer)
        {
                        #if UNITY_IOS
            _GoogleDrivePlugin_CheckPermissions(this.gameObject.name, this.iOSClientId, this.iOSKeychainName);
                        #endif
        }


        float startTime = Time.time;

        while (waitingForPermissions)
        {
            // check for timeout
            float ellapsed = Time.time - startTime;
            if (ellapsed > timeOutInSeconds)
            {
                // force a time-out here, seems like java-land is not getting back to us...
                waitingForPermissions = false;
                uploadFailed          = true;
                failedReason          = "Operation timed-out";
                failureType           = DriveFailureType.Timeout;
            }

            yield return(null);
        }


        // Get token
        waitingForToken = true;

        if (Application.platform == RuntimePlatform.Android)
        {
                        #if UNITY_ANDROID
            using (AndroidJavaClass activityClass = new AndroidJavaClass("com.androidexperiments.sprayscape.unitydriveplugin.GoogleDriveUnityPlayerActivity"))
            {
                using (AndroidJavaObject activity = activityClass.GetStatic <AndroidJavaObject>("activityInstance"))
                {
                    activity.Call <bool>("fetchAuthToken", this.gameObject.name);
                }
            }
                        #endif
        }
        else if (Application.platform == RuntimePlatform.IPhonePlayer)
        {
                        #if UNITY_IOS
            _GoogleDrivePlugin_CheckPermissions(this.gameObject.name, this.iOSClientId, this.iOSKeychainName);
                        #endif
        }


        startTime = Time.time;

        while (waitingForToken)
        {
            // check for timeout
            float ellapsed = Time.time - startTime;
            if (ellapsed > timeOutInSeconds)
            {
                // force a time-out here, seems like java-land is not getting back to us...
                waitingForToken = false;
                uploadFailed    = true;
                failedReason    = "Operation timed-out";
                failureType     = DriveFailureType.Timeout;
            }

            yield return(null);
        }

        resultCallback(new DrivePermissionsResult
        {
            failed       = this.uploadFailed,
            failedReason = this.failedReason,
            failureType  = this.failureType,
            accountName  = accountName
        });
    }