본문 바로가기

Android

[Gmail API] MailList

코드 정리를 하지는 못했지만,,

중요한 부분들이 가장 많은 부분임.

1. mail filter query 넣는 방법

2. batch를 통해 과도한 API를 호출하지 않는 방법

3. 페이징 처리를 하는 방법

    public static void loadMailList(Context context, Account account, List<String> labelIds, long maxResults, String nextPageToken, String filter, GmailNetworkListener listener) {
        try {
            GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2(context, Collections.singleton(MAIL_SCOPE));
            credential.setSelectedAccount(account);

            Gmail service = new Gmail.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential).build();
            new GetMailListTask(account, service, labelIds, maxResults, nextPageToken, filter, listener).execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static class GetMailListTask extends AsyncTask<Void, Void, GmailMailListObject> {

        private Account account;
        private Gmail service;
        private List<String> labelIds;
        private GmailNetworkListener listener;
        private Long maxResults;
        private String nextPageToken;
        private String filter;
        private List<Message> messages;

        public GetMailListTask(Account account, Gmail service, List<String> labelIds, long maxResults, String nextPageToken, String filter, GmailNetworkListener listener) {
            this.account = account;
            this.service = service;
            this.labelIds = labelIds;
            this.listener = listener;
            this.maxResults = maxResults;
            this.nextPageToken = nextPageToken == "0" ? null : nextPageToken;
            this.filter = filter;
            this.messages = new ArrayList<>();
        }

        final JsonBatchCallback<Message> callback = new JsonBatchCallback<Message>() {
            @Override
            public void onSuccess(Message message, HttpHeaders responseHeaders) throws IOException {
                messages.add(message);
            }

            @Override
            public void onFailure(GoogleJsonError e, HttpHeaders responseHeaders) throws IOException {

            }
        };

        @Override
        protected GmailMailListObject doInBackground(Void... voids) {
            if (listener == null) return null;

            try {
                int filterOption = Integer.parseInt(filter);
                String query = "";
                switch (filterOption) {
                    case 0: {
                        // all
                        query = "";
                    }
                    break;
                    case 1: {
                        // unread -> in:[FOLDER_ID] is:unread
                        query = "is:unread";
                    }
                    break;
                    case 2: {
                        // starred -> in:[FOLDER_ID] is:starred
                        query = "is:starred";
                    }
                    break;
                    case 3: {
                        // attachment -> in:[FOLDER_ID] has:attachment
                        query = "has:attachment";
                    }
                    break;
                }

                ListMessagesResponse response;
                if (nextPageToken == null) {
                    response = service.users().messages().list(account.name).setLabelIds(labelIds).setMaxResults(maxResults).setQ(query).execute();
                } else {
                    response = service.users().messages().list(account.name).setLabelIds(labelIds).setMaxResults(maxResults).setPageToken(nextPageToken).setQ(query).execute();
                }

                GmailMailListObject mailList;
                if (response.getMessages() == null) {
                    mailList = new GmailMailListObject(null, null);
                } else {
                    BatchRequest batch = service.batch();
                    for (Message item : response.getMessages()) {
                        service.users().messages().get(account.name, item.getId()).queue(batch, callback);
                    }
                    batch.execute();

                    mailList = new GmailMailListObject(response.getNextPageToken(), messages);
                }

                return mailList;
            } catch (IOException e) {
                e.printStackTrace();
                listener.onFailure(e.toString());
            }
            return null;
        }

        @Override
        protected void onPostExecute(GmailMailListObject result) {
            super.onPostExecute(result);
            if (listener != null) {
                listener.onSuccess(result);
            }
        }
    }