Introducing the Google Drive API Extension by Mr_koder
Overview:
The Google Drive API Extension, developed by Mr_koder, is a powerful tool that enables seamless integration with the Google Drive API within your Android applications. This extension provides a wide range of features and functionalities to enhance your app's file management capabilities. With this extension, users can effortlessly upload, edit, download, rename, move, and even change permissions of files and more on Google Drive.
Let's delve into the remarkable features of this extension and explore how it can revolutionize your app's file management capabilities.
Installing:
first, you have to Enable google Drive API and get ClientId
and ClientSecret
from the Google Cloud console so you can watch this video
authenticate process preview:
All Blocks:
Documentation (Blocks)
SetClientId(String clientId)
:
- Description: Sets the client ID for the Google Drive API.
- Parameters:
clientId
(String): The client ID to be set.
SetClientSecret(String clientSecret)
:
- Description: Sets the client secret for the Google Drive API.
- Parameters:
clientSecret
(String): The client secret to be set.
SetRefreshToken(String refreshToken)
:
- Description: Sets the refresh token for the Google Drive API.
- Parameters:
refreshToken
(String): The refresh token to be set.
SetAccessToken(String accessToken)
:
- Description: Sets the access token for the Google Drive API.
- Parameters:
accessToken
(String): The access token to be set.
StartGoogleDriveAuthFlow
Description:
Initiates the Google Drive OAuth2 authentication flow using the device code grant type. This flow is suitable for devices that don't have a web browser or when users can't easily copy and paste verification codes.
AccessTokenReceived Event:
Description:
Fired when an access token is successfully received from the Google OAuth token endpoint.
Event Parameters:
accessToken
(String): The obtained access token.expiresIn
(int): The duration in seconds for which the access token is valid.refreshToken
(String): The refresh token for obtaining a new access token.scope
(String): The scope of access granted.tokenType
(String): The type of token received.
GoogleOAuthErrorReceived Event:
Description:
Fired when the Google OAuth2 API returns an error in response to the device code request.
Event Parameters:
errorType
(String): The type of error returned by the Google OAuth2 API.errorDescription
(String): A description of the error returned by the Google OAuth2 API.
GoogleOAuthRequestError Event:
Description:
Fired when an error occurs during the Google OAuth request, providing a general indication of an error.
Event Parameters:
errorMessage
(String): The error message describing the issue encountered during the Google OAuth request.
AccessTokenErrorReceived Event:
Description:
Fired when an error occurs during the access token request to the Google OAuth token endpoint.
Event Parameters:
errorType
(String): The type of error returned by the Google OAuth token endpoint.errorDescription
(String): A description of the error returned by the Google OAuth token endpoint.
AccessTokenRequestError Event:
Description:
Fired when an error occurs during the entire Google OAuth access token request process.
GetAccessToken()
:
- Description: Retrieves the currently set access token.
- Returns:
accessToken
(String): The currently set access token.
GetAccessTokenWithRefresh()
:
- Description: Retrieves the access token using the refresh token.
- Implementation:
- Creates an instance of the
OkHttpClient
class.- Builds the request body with the client ID, client secret, refresh token, and grant type.
- Builds the HTTP request with the request URL and the previously created request body.
- Enqueues the request with a callback for handling the response.
- On successful response, extracts the access token from the JSON response and sets it as the current access token.
- Notifies the main thread with the obtained access token using the
AccessTokenObtained(String accessToken)
method.- On failure or exception, notifies the main thread with an empty access token using the
AccessTokenObtained(String accessToken)
method.
AccessTokenObtained(String accessToken)
:
- Description: Event triggered when an access token is obtained.
- Parameters:
accessToken
(String): The obtained access token.
UploadFile(String filePath, String folderId, String fileName, String mimeType)
:
- Description: Uploads a file to Google Drive.
- Parameters:
filePath
(String): The local file path of the file to be uploaded.folderId
(String): The ID of the Google Drive folder where the file will be uploaded.fileName
(String): The desired name of the file on Google Drive.mimeType
(String): The MIME type of the file.
UploadProgress(int progress)
:
- Description: Event triggered to provide progress updates during file upload.
- Parameters:
progress
(int): The current progress of the file upload, represented as a percentage.
FileUploaded(String fileId, String mimeType, String name, int responseCode)
:
- Description: Event triggered when a file is uploaded.
- Parameters:
fileId
(String): The ID of the uploaded file.mimeType
(String): The MIME type of the uploaded file.name
(String): The name of the uploaded file.responseCode
(int): The HTTP response code indicating the upload status.
UploadFileFromURL
The
UploadFileFromURL
function uploads a file from a specified URL to Google Drive. It takes the file URL, folder ID, file name, and MIME type as parameters. The implementation of the function handles the necessary steps to upload the file, including opening the URL, reading the file content into a byte array, creating the request body, executing the HTTP request, and handling the response.
FileUploadedFromURL
The
FileUploadedFromURL
function is an event triggered when a file is successfully uploaded from a URL.
GetFileNameFromPath(String filePath)
:
- Description: Retrieves the file name from a given file path.
- Parameters:
filePath
(String): The path of the file.- Returns:
fileName
(String): The name of the file extracted from the file path.
StopUpload
:
- Description: stop and open the upload process.
true
for stop andfalse
for open
UploadStopped
:
- Description: it fires after the uploading process stops.
GetMimeTypeFromPath(String filePath)
:
- Description: Retrieves the MIME type from a given file path.
- Parameters:
filePath
(String): The path of the file.- Returns:
mimeType
(String): The MIME type of the file.
DeleteFile(String fileId)
:
- Description: Deletes a file from Google Drive.
- Parameters:
fileId
(String): The ID of the file to be deleted.
DeleteFolder(String folderId)
:
- Description: Deletes a folder from Google Drive.
- Parameters:
folderId
(String): The ID of the folder to be deleted.
ItemDeleted(String itemId, int responseCode)
:
- Description: Event triggered when an item (file or folder) is deleted.
- Parameters:
itemId
(String): The ID of the deleted item.responseCode
(int): The HTTP response code indicating the delete status.
CreateFolder(String folderName)
:
Description: Creates a folder in Google Drive.
Parameters:
folderName
(String): The name of the folder to be created.
createFolder(String folderName)
:Description: Creates a folder in Google Drive.
Parameters:
folderName
(String): The name of the folder to be created.Implementation:
- Creates an instance of
OkHttpClient
to handle the HTTP request.- Defines the JSON media type for the request body.
- Builds the JSON string with the folder name and the MIME type for a folder.
- Creates a request body with the JSON data.
- Builds the HTTP request with the request URL, authorization header (including the access token), and request body.
- Sends the request asynchronously using
client.newCall(request).enqueue(...)
.- Handles the response in the callback.
- On failure, logs the exception and triggers the
FolderCreated
event with empty values.- On success, extracts the folder ID, name, and MIME type from the JSON response and triggers the
FolderCreated
event with the obtained values.Note: This function is used internally and not intended to be called directly by the user.
FolderCreated(String folderId, String folderName, String mimeType, int responseCode)
:
- Description: Event triggered when a folder is created in Google Drive.
- Parameters:
folderId
(String): The ID of the created folder.folderName
(String): The name of the created folder.mimeType
(String): The MIME type of the created folder.responseCode
(int): The HTTP response code indicating the creation status.
ChangePermissions(String fileId, String role, String type)
:
- Description: Changes the permissions of a file or folder in Google Drive.
- Parameters:
fileId
(String): The ID of the file or folder for which permissions will be changed.role
(String): The desired role for the new permission.type
(String): The type of the user or group to which the new permission will be applied.
Input options for
type
:
"user"
: Represents an individual user in Google Drive."group"
: Represents a group of users in Google Drive."domain"
: Represents a Google Workspace domain."anyone"
: Represents anyone with the link (public access).
Input options for
role
:
"owner"
: Grants the highest level of permission, allowing full control over the file or folder, including the ability to delete it."organizer"
: Similar to an owner, but specific to Google Workspace."fileOrganizer"
: Similar to an organizer but limited to files only."writer"
: Allows editing of the file or folder, but not managing permissions."commenter"
: Allows adding comments to the file or folder."reader"
: Provides read-only access to the file or folder."none"
: Removes the permission for the specified user or group.
PermissionsChanged(String kind, String id, String type, boolean allowFileDiscovery, int responseCode)
:
- Description: Event triggered when the permissions of a file or folder are changed in Google Drive.
- Parameters:
kind
(String): The kind of the permission.id
(String): The ID of the permission.type
(String): The type of the user or group the permission is applied to.allowFileDiscovery
(boolean): Indicates whether the file discovery is allowed for the user or group.responseCode
(int): The HTTP response code indicating the permission change status.
RenameFileOrFolder(String fileId, String newName)
:
- Description: Renames a file or folder in Google Drive.
- Parameters:
fileId
(String): The ID of the file or folder to be renamed.newName
(String): The new name to assign to the file or folder.
FileOrFolderRenamed(String kind, String id, String name, String mimeType, int responseCode)
:
- Description: Event triggered when a file or folder is renamed in Google Drive.
- Parameters:
kind
(String): The kind of the file or folder.id
(String): The ID of the renamed file or folder.name
(String): The new name of the file or folder.mimeType
(String): The MIME type of the file or folder.responseCode
(int): The HTTP response code indicating the rename status.
ReplaceUploadedFile(String fileId, String newFilePath)
:
- Description: Replaces an uploaded file in Google Drive with another file.
- Parameters:
fileId
(String): The ID of the file to be replaced.newFilePath
(String): The local file path of the new file to replace the existing file.
FileReplaced(String id, String name, String mimeType, int responseCode)
:
- Description: Event triggered when a file is replaced in Google Drive.
- Parameters:
id
(String): The ID of the replaced file.name
(String): The name of the replaced file.mimeType
(String): The MIME type of the replaced file.responseCode
(int): The HTTP response code indicating the replace status.
ReplaceFileFromURL
, is a public method that replaces a file in Google Drive with a file from a specified URL. It takes the following parameters:
fileId
: The ID of the file to be replaced.fileUrl
: The URL of the file to replace with.fileName
: The name of the file.mimeType
: The MIME type of the file.
FileReplacedFromURL
, is an event triggered when a file is successfully replaced with a file from a URL. It takes the following parameters:
fileId
: The ID of the replaced file.mimeType
: The MIME type of the replaced file.fileName
: The name of the replaced file.responseCode
: The HTTP response code of the file replacement operation.
GetFileSizeFromLocalPath(String localPath)
:
- Description: Retrieves the file size of a file from its local path.
- Parameters:
localPath
(String): The local file path of the file.- Returns:
long
: The file size in bytes, or -1 if the file does not exist or is not a regular file.
GetDirectDownloadLink(String fileId)
:
- Description: Generates a direct download URL for a Google Drive file based on its file ID.
- Parameters:
fileId
(String): The ID of the Google Drive file.- Returns:
String
: The direct download URL for the specified file.
GetAllFilesFromFolder(String folderId)
:
- Description: Retrieves all files from the specified Google Drive folder.
- Parameters:
folderId
(String): The ID of the Google Drive folder.- Implementation:
- Creates a new thread to perform the network request asynchronously.
- Constructs the API URL to retrieve files from the specified folder using the provided folder ID.
- Opens a connection to the URL and sets the request method to "GET".
- Sets the authorization header using the access token.
- Retrieves the response code, input stream, and reads the response content.
- Converts the JSON response to a
JsonObject
using Gson.- Extracts the array of files from the JSON response.
- Iterates over each file in the array and extracts the file ID, MIME type, and name.
- Calls the
AfterGetFiles
event function on the main thread with the retrieved data.- Note: This function runs in a separate thread to avoid blocking the main UI thread.
AfterGetFiles(List<String> fileIds, List<String> mimeTypes, List<String> fileNames)
:
- Description: Event triggered after retrieving files from the specified Google Drive folder.
- Parameters:
fileIds
(List of Strings): The list of file IDs.mimeTypes
(List of Strings): The list of MIME types of the files.fileNames
(List of Strings): The list of file names.
GetAllFoldersFromRoot()
:
- Description: Retrieves all folders from the root directory of Google Drive.
- Implementation:
- Creates a new thread to perform the network request asynchronously.
- Constructs the API URL to retrieve folders from the root directory.
- Opens a connection to the URL and sets the request method to "GET".
- Sets the authorization header using the access token.
- Retrieves the response code, input stream, and reads the response content.
- Converts the JSON response to a
JsonObject
using Gson.- Extracts the array of folders from the JSON response.
- Iterates over each folder in the array and extracts the folder ID, MIME type, and name.
- Calls the
AfterGetFolders
event function on the main thread with the retrieved data.- Note: This function runs in a separate thread to avoid blocking the main UI thread.
.
AfterGetFolders(List<String> folderIds, List<String> mimeTypes, List<String> folderNames)
:
- Description: Event triggered after retrieving all folders from the root directory of Google Drive.
- Parameters:
folderIds
(List of Strings): The list of folder IDs.mimeTypes
(List of Strings): The list of MIME types of the folders.folderNames
(List of Strings): The list of folder names.
MoveFileToFolder(String fileId, String newFolderId)
:
- Description: Moves a file from one folder to another in Google Drive.
- Parameters:
fileId
(String): The ID of the file to be moved.newFolderId
(String): The ID of the new folder where the file should be moved.- Implementation:
- Creates a new thread to perform the network request asynchronously.
- Constructs the API URL to move the file by adding the new folder as a parent.
- Opens a connection to the URL and sets the request method to "PATCH".
- Sets the authorization header using the access token and content type header to "application/json".
- Creates a
JsonObject
payload with the new folder ID.- Writes the payload to the connection's output stream.
- Retrieves the response code, input stream, and reads the response content.
- Converts the JSON response to a
JsonObject
using Gson.- Extracts the file name, ID, and MIME type from the JSON response.
- Calls the
AfterMoveFile
event function on the main thread with the retrieved data.
AfterMoveFile(String fileName, String id, String mimeType, int responseCode)
:
- Description: Event triggered after a file has been moved to a new folder.
- Parameters:
fileName
(String): The name of the moved file.id
(String): The ID of the moved file.mimeType
(String): The MIME type of the moved file.responseCode
(int): The HTTP response code indicating the success or failure of the move operation.
DownloadFileFromURL(String fileUrl, String localPath)
:
- Description: Downloads a file from a given URL and saves it to the specified local path.
- Parameters:
fileUrl
(String): The URL of the file to be downloaded.localPath
(String): The local path where the downloaded file should be saved.- Implementation:
- Creates a new thread to perform the download operation asynchronously.
- Opens a connection to the file URL.
- Checks if the connection response code is OK.
- Retrieves the input stream and file length.
- Creates a file at the specified local path and an output stream to write the downloaded data.
- Reads the input stream and writes the data to the output stream.
- Closes the streams and disconnects the connection.
- Calls the
handleDownloadResult
function to handle the download result.
handleDownloadResult(boolean success, String localPath, String errorMessage)
:- Description: Handles the result of a file download operation.
- Parameters:
success
(boolean): Indicates whether the download operation was successful.localPath
(String): The local path where the file was downloaded.errorMessage
(String): The error message, if any, encountered during the download operation.- Implementation:
- Executes the handling logic on the main thread using the
mainHandler
.- Calls the
AfterDownloadComplete
event function with the download result.
AfterDownloadComplete(boolean success, String localPath, String errorMessage)
:
- Description: Event triggered after a file has been downloaded.
- Parameters:
success
(boolean): Indicates whether the download operation was successful.localPath
(String): The local path where the file was downloaded.errorMessage
(String): The error message, if any, encountered during the download operation.
convertUriToFilePath(String uriString)
:
- Description: Converts a file URI to a file path.
- Parameters:
uriString
(String): The string representation of the file URI.- Returns:
String
: The file path corresponding to the given URI.- Implementation:
- Parses the URI string into a
Uri
object.- Checks if the URI is a document URI using
DocumentsContract.isDocumentUri(context, uri)
.- If it is a document URI, checks different document types using helper methods (
isExternalStorageDocument
,isDownloadsDocument
,isMediaDocument
) to determine the type of document and extract the relevant information.- Calls
getDataColumn
method to retrieve the file path using the content resolver and selection arguments.- If the URI scheme is "content", directly calls
getDataColumn
to retrieve the file path.- If the URI scheme is "file", returns the path directly from the URI.
- If none of the conditions match, returns
null
.
CheckAccessTokenValidity
Description: Checks the validity of the access token and triggers an event with token details.
Usage:
- Call this function to check the access token's validity.
AccessTokenValidityChecked Event
Description: Triggered when the access token's validity is checked, providing details about the token.
Parameters:
isValid
(boolean): Indicates token validity.accessType
(string): Type of access granted.expiresInSeconds
(string): Remaining time until token expiration (seconds).exp
(string): Token expiration timestamp.scope
(string): Scope of access granted.aud
(string): Intended audience for the token.azp
(string): Authorized party for the token.
GetSdkVersion
:
Authentication process
See also this graph
video:
AIX_File :
you can buy the GoogleDrive.aix and demo aia files from Here via PayPal and after you complete the payment you will be direclty redirected to the download URL of extension and if you faced any problems you can DM me