summaryrefslogtreecommitdiffstats
path: root/java/com/android/voicemail/impl/imap/ImapHelper.java
blob: 6aa4158116bf8a517731b06e2bd0d20d3ffe571f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License
 */
package com.android.voicemail.impl.imap;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkInfo;
import android.provider.VoicemailContract;
import android.telecom.PhoneAccountHandle;
import android.util.Base64;
import com.android.voicemail.impl.OmtpConstants;
import com.android.voicemail.impl.OmtpConstants.ChangePinResult;
import com.android.voicemail.impl.OmtpEvents;
import com.android.voicemail.impl.OmtpVvmCarrierConfigHelper;
import com.android.voicemail.impl.VisualVoicemailPreferences;
import com.android.voicemail.impl.Voicemail;
import com.android.voicemail.impl.VoicemailStatus;
import com.android.voicemail.impl.VoicemailStatus.Editor;
import com.android.voicemail.impl.VvmLog;
import com.android.voicemail.impl.fetch.VoicemailFetchedCallback;
import com.android.voicemail.impl.mail.Address;
import com.android.voicemail.impl.mail.Body;
import com.android.voicemail.impl.mail.BodyPart;
import com.android.voicemail.impl.mail.FetchProfile;
import com.android.voicemail.impl.mail.Flag;
import com.android.voicemail.impl.mail.Message;
import com.android.voicemail.impl.mail.MessagingException;
import com.android.voicemail.impl.mail.Multipart;
import com.android.voicemail.impl.mail.TempDirectory;
import com.android.voicemail.impl.mail.internet.MimeMessage;
import com.android.voicemail.impl.mail.store.ImapConnection;
import com.android.voicemail.impl.mail.store.ImapFolder;
import com.android.voicemail.impl.mail.store.ImapStore;
import com.android.voicemail.impl.mail.store.imap.ImapConstants;
import com.android.voicemail.impl.mail.store.imap.ImapResponse;
import com.android.voicemail.impl.mail.utils.LogUtils;
import com.android.voicemail.impl.sync.OmtpVvmSyncService.TranscriptionFetchedCallback;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import org.apache.commons.io.IOUtils;

/** A helper interface to abstract commands sent across IMAP interface for a given account. */
public class ImapHelper implements Closeable {

  private static final String TAG = "ImapHelper";

  private ImapFolder mFolder;
  private ImapStore mImapStore;

  private final Context mContext;
  private final PhoneAccountHandle mPhoneAccount;
  private final Network mNetwork;
  private final Editor mStatus;

  VisualVoicemailPreferences mPrefs;
  private static final String PREF_KEY_QUOTA_OCCUPIED = "quota_occupied_";
  private static final String PREF_KEY_QUOTA_TOTAL = "quota_total_";

  private int mQuotaOccupied;
  private int mQuotaTotal;

  private final OmtpVvmCarrierConfigHelper mConfig;

  /** InitializingException */
  public static class InitializingException extends Exception {

    public InitializingException(String message) {
      super(message);
    }
  }

  public ImapHelper(
      Context context,
      PhoneAccountHandle phoneAccount,
      Network network,
      Editor status)
      throws InitializingException {
    this(
        context,
        new OmtpVvmCarrierConfigHelper(context, phoneAccount),
        phoneAccount,
        network,
        status);
  }

  public ImapHelper(
      Context context,
      OmtpVvmCarrierConfigHelper config,
      PhoneAccountHandle phoneAccount,
      Network network,
      Editor status)
      throws InitializingException {
    mContext = context;
    mPhoneAccount = phoneAccount;
    mNetwork = network;
    mStatus = status;
    mConfig = config;
    mPrefs = new VisualVoicemailPreferences(context, phoneAccount);

    try {
      TempDirectory.setTempDirectory(context);

      String username = mPrefs.getString(OmtpConstants.IMAP_USER_NAME, null);
      String password = mPrefs.getString(OmtpConstants.IMAP_PASSWORD, null);
      String serverName = mPrefs.getString(OmtpConstants.SERVER_ADDRESS, null);
      int port = Integer.parseInt(mPrefs.getString(OmtpConstants.IMAP_PORT, null));
      int auth = ImapStore.FLAG_NONE;

      int sslPort = mConfig.getSslPort();
      if (sslPort != 0) {
        port = sslPort;
        auth = ImapStore.FLAG_SSL;
      }

      mImapStore =
          new ImapStore(context, this, username, password, port, serverName, auth, network);
    } catch (NumberFormatException e) {
      handleEvent(OmtpEvents.DATA_INVALID_PORT);
      LogUtils.w(TAG, "Could not parse port number");
      throw new InitializingException("cannot initialize ImapHelper:" + e.toString());
    }

    mQuotaOccupied =
        mPrefs.getInt(PREF_KEY_QUOTA_OCCUPIED, VoicemailContract.Status.QUOTA_UNAVAILABLE);
    mQuotaTotal = mPrefs.getInt(PREF_KEY_QUOTA_TOTAL, VoicemailContract.Status.QUOTA_UNAVAILABLE);
  }

  @Override
  public void close() {
    mImapStore.closeConnection();
  }

  public boolean isRoaming() {
    ConnectivityManager connectivityManager =
        (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo info = connectivityManager.getNetworkInfo(mNetwork);
    if (info == null) {
      return false;
    }
    return info.isRoaming();
  }

  public OmtpVvmCarrierConfigHelper getConfig() {
    return mConfig;
  }

  public ImapConnection connect() {
    return mImapStore.getConnection();
  }

  /** The caller thread will block until the method returns. */
  public boolean markMessagesAsRead(List<Voicemail> voicemails) {
    return setFlags(voicemails, Flag.SEEN);
  }

  /** The caller thread will block until the method returns. */
  public boolean markMessagesAsDeleted(List<Voicemail> voicemails) {
    return setFlags(voicemails, Flag.DELETED);
  }

  public void handleEvent(OmtpEvents event) {
    mConfig.handleEvent(mStatus, event);
  }

  /**
   * Set flags on the server for a given set of voicemails.
   *
   * @param voicemails The voicemails to set flags for.
   * @param flags The flags to set on the voicemails.
   * @return {@code true} if the operation completes successfully, {@code false} otherwise.
   */
  private boolean setFlags(List<Voicemail> voicemails, String... flags) {
    if (voicemails.size() == 0) {
      return false;
    }
    try {
      mFolder = openImapFolder(ImapFolder.MODE_READ_WRITE);
      if (mFolder != null) {
        mFolder.setFlags(convertToImapMessages(voicemails), flags, true);
        return true;
      }
      return false;
    } catch (MessagingException e) {
      LogUtils.e(TAG, e, "Messaging exception");
      return false;
    } finally {
      closeImapFolder();
    }
  }

  /**
   * Fetch a list of voicemails from the server.
   *
   * @return A list of voicemail objects containing data about voicemails stored on the server.
   */
  public List<Voicemail> fetchAllVoicemails() {
    List<Voicemail> result = new ArrayList<Voicemail>();
    Message[] messages;
    try {
      mFolder = openImapFolder(ImapFolder.MODE_READ_WRITE);
      if (mFolder == null) {
        // This means we were unable to successfully open the folder.
        return null;
      }

      // This method retrieves lightweight messages containing only the uid of the message.
      messages = mFolder.getMessages(null);

      for (Message message : messages) {
        // Get the voicemail details (message structure).
        MessageStructureWrapper messageStructureWrapper = fetchMessageStructure(message);
        if (messageStructureWrapper != null) {
          result.add(getVoicemailFromMessageStructure(messageStructureWrapper));
        }
      }
      return result;
    } catch (MessagingException e) {
      LogUtils.e(TAG, e, "Messaging Exception");
      return null;
    } finally {
      closeImapFolder();
    }
  }

  /**
   * Extract voicemail details from the message structure. Also fetch transcription if a
   * transcription exists.
   */
  private Voicemail getVoicemailFromMessageStructure(
      MessageStructureWrapper messageStructureWrapper) throws MessagingException {
    Message messageDetails = messageStructureWrapper.messageStructure;

    TranscriptionFetchedListener listener = new TranscriptionFetchedListener();
    if (messageStructureWrapper.transcriptionBodyPart != null) {
      FetchProfile fetchProfile = new FetchProfile();
      fetchProfile.add(messageStructureWrapper.transcriptionBodyPart);

      mFolder.fetch(new Message[] {messageDetails}, fetchProfile, listener);
    }

    // Found an audio attachment, this is a valid voicemail.
    long time = messageDetails.getSentDate().getTime();
    String number = getNumber(messageDetails.getFrom());
    boolean isRead = Arrays.asList(messageDetails.getFlags()).contains(Flag.SEEN);
    return Voicemail.createForInsertion(time, number)
        .setPhoneAccount(mPhoneAccount)
        .setSourcePackage(mContext.getPackageName())
        .setSourceData(messageDetails.getUid())
        .setIsRead(isRead)
        .setTranscription(listener.getVoicemailTranscription())
        .build();
  }

  /**
   * The "from" field of a visual voicemail IMAP message is the number of the caller who left the
   * message. Extract this number from the list of "from" addresses.
   *
   * @param fromAddresses A list of addresses that comprise the "from" line.
   * @return The number of the voicemail sender.
   */
  private String getNumber(Address[] fromAddresses) {
    if (fromAddresses != null && fromAddresses.length > 0) {
      if (fromAddresses.length != 1) {
        LogUtils.w(TAG, "More than one from addresses found. Using the first one.");
      }
      String sender = fromAddresses[0].getAddress();
      int atPos = sender.indexOf('@');
      if (atPos != -1) {
        // Strip domain part of the address.
        sender = sender.substring(0, atPos);
      }
      return sender;
    }
    return null;
  }

  /**
   * Fetches the structure of the given message and returns a wrapper containing the message
   * structure and the transcription structure (if applicable).
   *
   * @throws MessagingException if fetching the structure of the message fails
   */
  private MessageStructureWrapper fetchMessageStructure(Message message) throws MessagingException {
    LogUtils.d(TAG, "Fetching message structure for " + message.getUid());

    MessageStructureFetchedListener listener = new MessageStructureFetchedListener();

    FetchProfile fetchProfile = new FetchProfile();
    fetchProfile.addAll(
        Arrays.asList(
            FetchProfile.Item.FLAGS, FetchProfile.Item.ENVELOPE, FetchProfile.Item.STRUCTURE));

    // The IMAP folder fetch method will call "messageRetrieved" on the listener when the
    // message is successfully retrieved.
    mFolder.fetch(new Message[] {message}, fetchProfile, listener);
    return listener.getMessageStructure();
  }

  public boolean fetchVoicemailPayload(VoicemailFetchedCallback callback, final String uid) {
    try {
      mFolder = openImapFolder(ImapFolder.MODE_READ_WRITE);
      if (mFolder == null) {
        // This means we were unable to successfully open the folder.
        return false;
      }
      Message message = mFolder.getMessage(uid);
      if (message == null) {
        return false;
      }
      VoicemailPayload voicemailPayload = fetchVoicemailPayload(message);
      callback.setVoicemailContent(voicemailPayload);
      return true;
    } catch (MessagingException e) {
    } finally {
      closeImapFolder();
    }
    return false;
  }

  /**
   * Fetches the body of the given message and returns the parsed voicemail payload.
   *
   * @throws MessagingException if fetching the body of the message fails
   */
  private VoicemailPayload fetchVoicemailPayload(Message message) throws MessagingException {
    LogUtils.d(TAG, "Fetching message body for " + message.getUid());

    MessageBodyFetchedListener listener = new MessageBodyFetchedListener();

    FetchProfile fetchProfile = new FetchProfile();
    fetchProfile.add(FetchProfile.Item.BODY);

    mFolder.fetch(new Message[] {message}, fetchProfile, listener);
    return listener.getVoicemailPayload();
  }

  public boolean fetchTranscription(TranscriptionFetchedCallback callback, String uid) {
    try {
      mFolder = openImapFolder(ImapFolder.MODE_READ_WRITE);
      if (mFolder == null) {
        // This means we were unable to successfully open the folder.
        return false;
      }

      Message message = mFolder.getMessage(uid);
      if (message == null) {
        return false;
      }

      MessageStructureWrapper messageStructureWrapper = fetchMessageStructure(message);
      if (messageStructureWrapper != null) {
        TranscriptionFetchedListener listener = new TranscriptionFetchedListener();
        if (messageStructureWrapper.transcriptionBodyPart != null) {
          FetchProfile fetchProfile = new FetchProfile();
          fetchProfile.add(messageStructureWrapper.transcriptionBodyPart);

          // This method is called synchronously so the transcription will be populated
          // in the listener once the next method is called.
          mFolder.fetch(new Message[] {message}, fetchProfile, listener);
          callback.setVoicemailTranscription(listener.getVoicemailTranscription());
        }
      }
      return true;
    } catch (MessagingException e) {
      LogUtils.e(TAG, e, "Messaging Exception");
      return false;
    } finally {
      closeImapFolder();
    }
  }

  @ChangePinResult
  public int changePin(String oldPin, String newPin) throws MessagingException {
    ImapConnection connection = mImapStore.getConnection();
    try {
      String command =
          getConfig().getProtocol().getCommand(OmtpConstants.IMAP_CHANGE_TUI_PWD_FORMAT);
      connection.sendCommand(String.format(Locale.US, command, newPin, oldPin), true);
      return getChangePinResultFromImapResponse(connection.readResponse());
    } catch (IOException ioe) {
      VvmLog.e(TAG, "changePin: ", ioe);
      return OmtpConstants.CHANGE_PIN_SYSTEM_ERROR;
    } finally {
      connection.destroyResponses();
    }
  }

  public void changeVoicemailTuiLanguage(String languageCode) throws MessagingException {
    ImapConnection connection = mImapStore.getConnection();
    try {
      String command =
          getConfig().getProtocol().getCommand(OmtpConstants.IMAP_CHANGE_VM_LANG_FORMAT);
      connection.sendCommand(String.format(Locale.US, command, languageCode), true);
    } catch (IOException ioe) {
      LogUtils.e(TAG, ioe.toString());
    } finally {
      connection.destroyResponses();
    }
  }

  public void closeNewUserTutorial() throws MessagingException {
    ImapConnection connection = mImapStore.getConnection();
    try {
      String command = getConfig().getProtocol().getCommand(OmtpConstants.IMAP_CLOSE_NUT);
      connection.executeSimpleCommand(command, false);
    } catch (IOException ioe) {
      throw new MessagingException(MessagingException.SERVER_ERROR, ioe.toString());
    } finally {
      connection.destroyResponses();
    }
  }

  @ChangePinResult
  private static int getChangePinResultFromImapResponse(ImapResponse response)
      throws MessagingException {
    if (!response.isTagged()) {
      throw new MessagingException(MessagingException.SERVER_ERROR, "tagged response expected");
    }
    if (!response.isOk()) {
      String message = response.getStringOrEmpty(1).getString();
      LogUtils.d(TAG, "change PIN failed: " + message);
      if (OmtpConstants.RESPONSE_CHANGE_PIN_TOO_SHORT.equals(message)) {
        return OmtpConstants.CHANGE_PIN_TOO_SHORT;
      }
      if (OmtpConstants.RESPONSE_CHANGE_PIN_TOO_LONG.equals(message)) {
        return OmtpConstants.CHANGE_PIN_TOO_LONG;
      }
      if (OmtpConstants.RESPONSE_CHANGE_PIN_TOO_WEAK.equals(message)) {
        return OmtpConstants.CHANGE_PIN_TOO_WEAK;
      }
      if (OmtpConstants.RESPONSE_CHANGE_PIN_MISMATCH.equals(message)) {
        return OmtpConstants.CHANGE_PIN_MISMATCH;
      }
      if (OmtpConstants.RESPONSE_CHANGE_PIN_INVALID_CHARACTER.equals(message)) {
        return OmtpConstants.CHANGE_PIN_INVALID_CHARACTER;
      }
      return OmtpConstants.CHANGE_PIN_SYSTEM_ERROR;
    }
    LogUtils.d(TAG, "change PIN succeeded");
    return OmtpConstants.CHANGE_PIN_SUCCESS;
  }

  public void updateQuota() {
    try {
      mFolder = openImapFolder(ImapFolder.MODE_READ_WRITE);
      if (mFolder == null) {
        // This means we were unable to successfully open the folder.
        return;
      }
      updateQuota(mFolder);
    } catch (MessagingException e) {
      LogUtils.e(TAG, e, "Messaging Exception");
    } finally {
      closeImapFolder();
    }
  }

  public int getOccuupiedQuota() {
    return mQuotaOccupied;
  }

  public int getTotalQuota() {
    return mQuotaTotal;
  }

  private void updateQuota(ImapFolder folder) throws MessagingException {
    setQuota(folder.getQuota());
  }

  private void setQuota(ImapFolder.Quota quota) {
    if (quota == null) {
      return;
    }
    if (quota.occupied == mQuotaOccupied && quota.total == mQuotaTotal) {
      VvmLog.v(TAG, "Quota hasn't changed");
      return;
    }
    mQuotaOccupied = quota.occupied;
    mQuotaTotal = quota.total;
    VoicemailStatus.edit(mContext, mPhoneAccount).setQuota(mQuotaOccupied, mQuotaTotal).apply();
    mPrefs
        .edit()
        .putInt(PREF_KEY_QUOTA_OCCUPIED, mQuotaOccupied)
        .putInt(PREF_KEY_QUOTA_TOTAL, mQuotaTotal)
        .apply();
    VvmLog.v(TAG, "Quota changed to " + mQuotaOccupied + "/" + mQuotaTotal);
  }

  /**
   * A wrapper to hold a message with its header details and the structure for transcriptions (so
   * they can be fetched in the future).
   */
  public static class MessageStructureWrapper {

    public Message messageStructure;
    public BodyPart transcriptionBodyPart;

    public MessageStructureWrapper() {}
  }

  /** Listener for the message structure being fetched. */
  private final class MessageStructureFetchedListener
      implements ImapFolder.MessageRetrievalListener {

    private MessageStructureWrapper mMessageStructure;

    public MessageStructureFetchedListener() {}

    public MessageStructureWrapper getMessageStructure() {
      return mMessageStructure;
    }

    @Override
    public void messageRetrieved(Message message) {
      LogUtils.d(TAG, "Fetched message structure for " + message.getUid());
      LogUtils.d(TAG, "Message retrieved: " + message);
      try {
        mMessageStructure = getMessageOrNull(message);
        if (mMessageStructure == null) {
          LogUtils.d(TAG, "This voicemail does not have an attachment...");
          return;
        }
      } catch (MessagingException e) {
        LogUtils.e(TAG, e, "Messaging Exception");
        closeImapFolder();
      }
    }

    /**
     * Check if this IMAP message is a valid voicemail and whether it contains a transcription.
     *
     * @param message The IMAP message.
     * @return The MessageStructureWrapper object corresponding to an IMAP message and
     *     transcription.
     */
    private MessageStructureWrapper getMessageOrNull(Message message) throws MessagingException {
      if (!message.getMimeType().startsWith("multipart/")) {
        LogUtils.w(TAG, "Ignored non multi-part message");
        return null;
      }

      MessageStructureWrapper messageStructureWrapper = new MessageStructureWrapper();

      Multipart multipart = (Multipart) message.getBody();
      for (int i = 0; i < multipart.getCount(); ++i) {
        BodyPart bodyPart = multipart.getBodyPart(i);
        String bodyPartMimeType = bodyPart.getMimeType().toLowerCase();
        LogUtils.d(TAG, "bodyPart mime type: " + bodyPartMimeType);

        if (bodyPartMimeType.startsWith("audio/")) {
          messageStructureWrapper.messageStructure = message;
        } else if (bodyPartMimeType.startsWith("text/")) {
          messageStructureWrapper.transcriptionBodyPart = bodyPart;
        } else {
          VvmLog.v(TAG, "Unknown bodyPart MIME: " + bodyPartMimeType);
        }
      }

      if (messageStructureWrapper.messageStructure != null) {
        return messageStructureWrapper;
      }

      // No attachment found, this is not a voicemail.
      return null;
    }
  }

  /** Listener for the message body being fetched. */
  private final class MessageBodyFetchedListener implements ImapFolder.MessageRetrievalListener {

    private VoicemailPayload mVoicemailPayload;

    /** Returns the fetch voicemail payload. */
    public VoicemailPayload getVoicemailPayload() {
      return mVoicemailPayload;
    }

    @Override
    public void messageRetrieved(Message message) {
      LogUtils.d(TAG, "Fetched message body for " + message.getUid());
      LogUtils.d(TAG, "Message retrieved: " + message);
      try {
        mVoicemailPayload = getVoicemailPayloadFromMessage(message);
      } catch (MessagingException e) {
        LogUtils.e(TAG, "Messaging Exception:", e);
      } catch (IOException e) {
        LogUtils.e(TAG, "IO Exception:", e);
      }
    }

    private VoicemailPayload getVoicemailPayloadFromMessage(Message message)
        throws MessagingException, IOException {
      Multipart multipart = (Multipart) message.getBody();
      List<String> mimeTypes = new ArrayList<>();
      for (int i = 0; i < multipart.getCount(); ++i) {
        BodyPart bodyPart = multipart.getBodyPart(i);
        String bodyPartMimeType = bodyPart.getMimeType().toLowerCase();
        mimeTypes.add(bodyPartMimeType);
        if (bodyPartMimeType.startsWith("audio/")) {
          byte[] bytes = getDataFromBody(bodyPart.getBody());
          LogUtils.d(TAG, String.format("Fetched %s bytes of data", bytes.length));
          return new VoicemailPayload(bodyPartMimeType, bytes);
        }
      }
      LogUtils.e(TAG, "No audio attachment found on this voicemail, mimeTypes:" + mimeTypes);
      return null;
    }
  }

  /** Listener for the transcription being fetched. */
  private final class TranscriptionFetchedListener implements ImapFolder.MessageRetrievalListener {

    private String mVoicemailTranscription;

    /** Returns the fetched voicemail transcription. */
    public String getVoicemailTranscription() {
      return mVoicemailTranscription;
    }

    @Override
    public void messageRetrieved(Message message) {
      LogUtils.d(TAG, "Fetched transcription for " + message.getUid());
      try {
        mVoicemailTranscription = new String(getDataFromBody(message.getBody()));
      } catch (MessagingException e) {
        LogUtils.e(TAG, "Messaging Exception:", e);
      } catch (IOException e) {
        LogUtils.e(TAG, "IO Exception:", e);
      }
    }
  }

  private ImapFolder openImapFolder(String modeReadWrite) {
    try {
      if (mImapStore == null) {
        return null;
      }
      ImapFolder folder = new ImapFolder(mImapStore, ImapConstants.INBOX);
      folder.open(modeReadWrite);
      return folder;
    } catch (MessagingException e) {
      LogUtils.e(TAG, e, "Messaging Exception");
    }
    return null;
  }

  private Message[] convertToImapMessages(List<Voicemail> voicemails) {
    Message[] messages = new Message[voicemails.size()];
    for (int i = 0; i < voicemails.size(); ++i) {
      messages[i] = new MimeMessage();
      messages[i].setUid(voicemails.get(i).getSourceData());
    }
    return messages;
  }

  private void closeImapFolder() {
    if (mFolder != null) {
      mFolder.close(true);
    }
  }

  private byte[] getDataFromBody(Body body) throws IOException, MessagingException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    BufferedOutputStream bufferedOut = new BufferedOutputStream(out);
    try {
      body.writeTo(bufferedOut);
      return Base64.decode(out.toByteArray(), Base64.DEFAULT);
    } finally {
      IOUtils.closeQuietly(bufferedOut);
      IOUtils.closeQuietly(out);
    }
  }
}