본문 바로가기

Android

[Gmail API] Update Label

폴더명 변경

 public static void updateLabel(Context context, Account account, String labelId, String labelName, 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 UpdateLabelTask(account, service, labelId, labelName, listener).execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static class UpdateLabelTask extends AsyncTask<Void, Void, Label> {

        private Account account;
        private Gmail service;
        private String labelId;
        private String labelName;
        private GmailNetworkListener listener;

        public UpdateLabelTask(Account account, Gmail service, String labelId, String labelName, GmailNetworkListener listener) {
            this.account = account;
            this.service = service;
            this.labelId = labelId;
            this.labelName = labelName;
            this.listener = listener;
        }

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

            try {
                Label updateLabel = new Label().setId(labelId).setName(labelName).setLabelListVisibility("labelShow").setMessageListVisibility("hide");
                return service.users().labels().update(account.name, labelId, updateLabel).execute();
            } catch (IOException e) {
                e.printStackTrace();
                listener.onFailure(e.toString());
            }
            return null;
        }

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