본문 바로가기

Android

[Gmail API] Search Mail

Search Gmail in Android 

  public static void searchMailList(Context context, Account account, String searchType, String searchWord, 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 SearchMailListTask(account, service, searchType, searchWord, listener).execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

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

        private Account account;
        private Gmail service;
        private String searchType;
        private String searchWord;
        private List<Message> messages;
        private GmailNetworkListener listener;

        public SearchMailListTask(Account account, Gmail service, String searchType, String searchWord, GmailNetworkListener listener) {
            this.account = account;
            this.service = service;
            this.searchType = searchType;
            this.searchWord = searchWord;
            this.messages = new ArrayList<>();
            this.listener = listener;
        }

        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 searchOption = Integer.parseInt(searchType);
                String query = "";
                switch (searchOption) {
                    case 1: {
                        query = "subject:" + searchWord + " OR " + searchWord;
                    }
                    break;
                    case 2: {
                        query = "from:" + searchWord;
                    }
                    break;
                }

                ListMessagesResponse response = service.users().messages().list(account.name).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);
            }
        }
    }