目录

Commenting on Files

Use Cases

Drive Kit allows your app to provide file commenting functions for users, including commenting on files in the cloud as well as viewing, updating, and deleting comments.

Prerequisites

1. You have made all necessary preparations.

2. You have downloaded the client sample code and tried it out.

Creating a Drive Object

Call the Drive.Builder.build API to create a Drive object.
private Drive buildDrive() {  
    Drive service = new Drive.Builder(CredentialManager.getInstance().getCredential(), context).build();  
    return service;  
}

Creating a Comment

Call the Comments.Create API to create a comment.

/**
 * Create a comment.
 *
 * @param fileId File ID.
 */
private Comment createComments(String fileId) {
    Comment comment = null;
    Drive drive = buildDrive();
    try {
        Comment content = new Comment();
        content.setDescription("interface test");
        content.setCreatedTime(new DateTime(System.currentTimeMillis()));
        comment = drive.comments().create(fileId, content).setFields("*").execute();
    } catch (Exception ex) {
        Log.e(TAG, "createComments error: " + ex.toString());
    }
    return comment;
}

Listing Comments

Call the Comments.List API to list comments.

/**
 * List comments. 
 *
 * @param fileId File ID.
 */
private List<Comment> listComments(String fileId) {
    Drive drive = buildDrive();
    ArrayList<Comment> commentArrayList = new ArrayList<>();
    String nextCursor = null;
    try {
        Drive.Comments.List request = drive.comments().list(fileId);
        do {
            if (nextCursor != null) {
                request.setCursor(nextCursor);
            }
            CommentList commentList = request.setPageSize(100).setFields("*").execute();
            ArrayList<Comment> comments = (ArrayList<Comment>) commentList.getComments();
            if (comments == null) {
                break;
            }
            commentArrayList.addAll(comments);
            nextCursor = commentList.getNextCursor();
        } while (!StringUtils.isNullOrEmpty(nextCursor));
    } catch (IOException e) {
        Logger.e(TAG, "comments list error: " + e.toString());
    }
    return commentArrayList;
}

Obtaining Comment Details

Call the Comments.Get API to obtain comment details.

/**
 * Obtain comment details.
 *
 * @param fileId File ID.
 * @param commentId Comment ID. 
 */
private Comment getComments(String fileId, String commentId) {
    Comment comment = null;
    Drive drive = buildDrive();
    try {
        comment = drive.comments().get(fileId, commentId).setFields("*").execute();
        Log.d(TAG, "comment: " + comment.getDescription());
    } catch (Exception ex) {
        Log.e(TAG, "getComments error: " + ex.toString());
    }
    return comment;
}

Updating a Comment

Call the Comments.Update API to update a comment.

/**
 * Update a comment. 
 *
 * @param fileId File ID.
 * @param commentId Comment ID. 
 */
private void updateComments(String fileId, String commentId) {
    Drive drive = buildDrive();
    try {
        Comment content = new Comment();
        content.setDescription("update interface test");
        drive.comments().update(fileId, commentId, content).execute();
    } catch (Exception ex) {
        Log.e(TAG, "updateComments error: " + ex.toString());
    }
}

Deleting a Comment

Call the Comments.Delete API to delete a comment.

/**
 * Delete a comment.
 *
 * @param fileId File ID.
 * @param commentId Comment ID. 
 */
private void deleteComments(String fileId, String commentId) {
    Drive drive = buildDrive();
    try {
        drive.comments().delete(fileId, commentId).execute();
    } catch (Exception ex) {
        Log.e(TAG, "deleteComments error: " + ex.toString());
    }
}

Call the Comments APIs in the subthread. File commenting takes a while and requires an Internet connection.