summaryrefslogtreecommitdiffstats
path: root/java/com/android/voicemail/impl/sync/OmtpVvmSyncService.java
blob: 83a3960dd7321258cbd2ef7e3621f42f1f9dfda6 (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
/*
 * 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.sync;

import android.annotation.TargetApi;
import android.content.Context;
import android.net.Network;
import android.net.Uri;
import android.os.Build.VERSION_CODES;
import android.support.v4.os.BuildCompat;
import android.telecom.PhoneAccountHandle;
import android.text.TextUtils;
import android.util.ArrayMap;
import com.android.dialer.logging.Logger;
import com.android.dialer.logging.nano.DialerImpression;
import com.android.voicemail.VoicemailComponent;
import com.android.voicemail.impl.ActivationTask;
import com.android.voicemail.impl.Assert;
import com.android.voicemail.impl.OmtpEvents;
import com.android.voicemail.impl.OmtpVvmCarrierConfigHelper;
import com.android.voicemail.impl.Voicemail;
import com.android.voicemail.impl.VoicemailStatus;
import com.android.voicemail.impl.VvmLog;
import com.android.voicemail.impl.fetch.VoicemailFetchedCallback;
import com.android.voicemail.impl.imap.ImapHelper;
import com.android.voicemail.impl.imap.ImapHelper.InitializingException;
import com.android.voicemail.impl.scheduling.BaseTask;
import com.android.voicemail.impl.settings.VisualVoicemailSettingsUtil;
import com.android.voicemail.impl.sync.VvmNetworkRequest.NetworkWrapper;
import com.android.voicemail.impl.sync.VvmNetworkRequest.RequestFailedException;
import com.android.voicemail.impl.utils.VoicemailDatabaseUtil;
import java.util.List;
import java.util.Map;

/** Sync OMTP visual voicemail. */
@TargetApi(VERSION_CODES.O)
public class OmtpVvmSyncService {

  private static final String TAG = OmtpVvmSyncService.class.getSimpleName();

  /** Signifies a sync with both uploading to the server and downloading from the server. */
  public static final String SYNC_FULL_SYNC = "full_sync";
  /** Only upload to the server. */
  public static final String SYNC_UPLOAD_ONLY = "upload_only";
  /** Only download from the server. */
  public static final String SYNC_DOWNLOAD_ONLY = "download_only";
  /** Only download single voicemail transcription. */
  public static final String SYNC_DOWNLOAD_ONE_TRANSCRIPTION = "download_one_transcription";
  /** Threshold for whether we should archive and delete voicemails from the remote VM server. */
  private static final float AUTO_DELETE_ARCHIVE_VM_THRESHOLD = 0.75f;

  private final Context mContext;

  private VoicemailsQueryHelper mQueryHelper;

  public OmtpVvmSyncService(Context context) {
    mContext = context;
    mQueryHelper = new VoicemailsQueryHelper(mContext);
  }

  public void sync(
      BaseTask task,
      String action,
      PhoneAccountHandle phoneAccount,
      Voicemail voicemail,
      VoicemailStatus.Editor status) {
    Assert.isTrue(phoneAccount != null);
    VvmLog.v(TAG, "Sync requested: " + action + " - for account: " + phoneAccount);
    setupAndSendRequest(task, phoneAccount, voicemail, action, status);
  }

  private void setupAndSendRequest(
      BaseTask task,
      PhoneAccountHandle phoneAccount,
      Voicemail voicemail,
      String action,
      VoicemailStatus.Editor status) {
    if (!VisualVoicemailSettingsUtil.isEnabled(mContext, phoneAccount)) {
      VvmLog.v(TAG, "Sync requested for disabled account");
      return;
    }
    if (!VvmAccountManager.isAccountActivated(mContext, phoneAccount)) {
      ActivationTask.start(mContext, phoneAccount, null);
      return;
    }

    OmtpVvmCarrierConfigHelper config = new OmtpVvmCarrierConfigHelper(mContext, phoneAccount);
    Logger.get(mContext).logImpression(DialerImpression.Type.VVM_SYNC_STARTED);
    // DATA_IMAP_OPERATION_STARTED posting should not be deferred. This event clears all data
    // channel errors, which should happen when the task starts, not when it ends. It is the
    // "Sync in progress..." status.
    config.handleEvent(
        VoicemailStatus.edit(mContext, phoneAccount), OmtpEvents.DATA_IMAP_OPERATION_STARTED);
    try (NetworkWrapper network = VvmNetworkRequest.getNetwork(config, phoneAccount, status)) {
      if (network == null) {
        VvmLog.e(TAG, "unable to acquire network");
        task.fail();
        return;
      }
      doSync(task, network.get(), phoneAccount, voicemail, action, status);
    } catch (RequestFailedException e) {
      config.handleEvent(status, OmtpEvents.DATA_NO_CONNECTION_CELLULAR_REQUIRED);
      task.fail();
    }
  }

  private void doSync(
      BaseTask task,
      Network network,
      PhoneAccountHandle phoneAccount,
      Voicemail voicemail,
      String action,
      VoicemailStatus.Editor status) {
    try (ImapHelper imapHelper = new ImapHelper(mContext, phoneAccount, network, status)) {
      boolean success;
      if (voicemail == null) {
        success = syncAll(action, imapHelper, phoneAccount);
      } else {
        success = syncOne(imapHelper, voicemail, phoneAccount);
      }
      if (success) {
        // TODO: b/30569269 failure should interrupt all subsequent task via exceptions
        imapHelper.updateQuota();
        autoDeleteAndArchiveVM(imapHelper, phoneAccount);
        imapHelper.handleEvent(OmtpEvents.DATA_IMAP_OPERATION_COMPLETED);
        Logger.get(mContext).logImpression(DialerImpression.Type.VVM_SYNC_COMPLETED);
      } else {
        task.fail();
      }
    } catch (InitializingException e) {
      VvmLog.w(TAG, "Can't retrieve Imap credentials.", e);
      return;
    }
  }

  /**
   * If the VM quota exceeds {@value AUTO_DELETE_ARCHIVE_VM_THRESHOLD}, we should archive the VMs
   * and delete them from the server to ensure new VMs can be received.
   */
  private void autoDeleteAndArchiveVM(
      ImapHelper imapHelper, PhoneAccountHandle phoneAccountHandle) {

    if (isArchiveAllowedAndEnabled(mContext, phoneAccountHandle)) {
      if ((float) imapHelper.getOccuupiedQuota() / (float) imapHelper.getTotalQuota()
          > AUTO_DELETE_ARCHIVE_VM_THRESHOLD) {
        deleteAndArchiveVM(imapHelper);
        imapHelper.updateQuota();
        Logger.get(mContext)
            .logImpression(DialerImpression.Type.VVM_ARCHIVE_AUTO_DELETED_VM_FROM_SERVER);
      } else {
        VvmLog.i(TAG, "no need to archive and auto delete VM, quota below threshold");
      }
    } else {
      VvmLog.i(TAG, "isArchiveAllowedAndEnabled is false");
      Logger.get(mContext).logImpression(DialerImpression.Type.VVM_ARCHIVE_AUTO_DELETE_TURNED_OFF);
    }
  }

  private static boolean isArchiveAllowedAndEnabled(
      Context context, PhoneAccountHandle phoneAccountHandle) {

    if (!VoicemailComponent.get(context)
        .getVoicemailClient()
        .isVoicemailArchiveAvailable(context)) {
      VvmLog.i("isArchiveAllowedAndEnabled", "voicemail archive is not available");
      return false;
    }
    if (!VisualVoicemailSettingsUtil.isArchiveEnabled(context, phoneAccountHandle)) {
      VvmLog.i("isArchiveAllowedAndEnabled", "voicemail archive is turned off");
      return false;
    }
    if (!VisualVoicemailSettingsUtil.isEnabled(context, phoneAccountHandle)) {
      VvmLog.i("isArchiveAllowedAndEnabled", "voicemail is turned off");
      return false;
    }
    return true;
  }

  private void deleteAndArchiveVM(ImapHelper imapHelper) {
    // Archive column should only be used for 0 and above
    Assert.isTrue(BuildCompat.isAtLeastO());
    // The number of voicemails that exceed our threshold and should be deleted from the server
    int numVoicemails =
        imapHelper.getOccuupiedQuota()
            - (int) (AUTO_DELETE_ARCHIVE_VM_THRESHOLD * imapHelper.getTotalQuota());
    List<Voicemail> oldestVoicemails = mQueryHelper.oldestVoicemailsOnServer(numVoicemails);
    if (!oldestVoicemails.isEmpty()) {
      mQueryHelper.markArchivedInDatabase(oldestVoicemails);
      imapHelper.markMessagesAsDeleted(oldestVoicemails);
      VvmLog.i(
          TAG,
          String.format(
              "successfully archived and deleted %d voicemails", oldestVoicemails.size()));
    } else {
      VvmLog.w(TAG, "remote voicemail server is empty");
    }
  }

  private boolean syncAll(String action, ImapHelper imapHelper, PhoneAccountHandle account) {
    boolean uploadSuccess = true;
    boolean downloadSuccess = true;

    if (SYNC_FULL_SYNC.equals(action) || SYNC_UPLOAD_ONLY.equals(action)) {
      uploadSuccess = upload(imapHelper);
    }
    if (SYNC_FULL_SYNC.equals(action) || SYNC_DOWNLOAD_ONLY.equals(action)) {
      downloadSuccess = download(imapHelper, account);
    }

    VvmLog.v(
        TAG,
        "upload succeeded: ["
            + String.valueOf(uploadSuccess)
            + "] download succeeded: ["
            + String.valueOf(downloadSuccess)
            + "]");

    return uploadSuccess && downloadSuccess;
  }

  private boolean syncOne(ImapHelper imapHelper, Voicemail voicemail, PhoneAccountHandle account) {
    if (shouldPerformPrefetch(account, imapHelper)) {
      VoicemailFetchedCallback callback =
          new VoicemailFetchedCallback(mContext, voicemail.getUri(), account);
      imapHelper.fetchVoicemailPayload(callback, voicemail.getSourceData());
    }

    return imapHelper.fetchTranscription(
        new TranscriptionFetchedCallback(mContext, voicemail), voicemail.getSourceData());
  }

  private boolean upload(ImapHelper imapHelper) {
    List<Voicemail> readVoicemails = mQueryHelper.getReadVoicemails();
    List<Voicemail> deletedVoicemails = mQueryHelper.getDeletedVoicemails();

    boolean success = true;

    if (deletedVoicemails.size() > 0) {
      if (imapHelper.markMessagesAsDeleted(deletedVoicemails)) {
        // We want to delete selectively instead of all the voicemails for this provider
        // in case the state changed since the IMAP query was completed.
        mQueryHelper.deleteFromDatabase(deletedVoicemails);
      } else {
        success = false;
      }
    }

    if (readVoicemails.size() > 0) {
      if (imapHelper.markMessagesAsRead(readVoicemails)) {
        mQueryHelper.markCleanInDatabase(readVoicemails);
      } else {
        success = false;
      }
    }

    return success;
  }

  private boolean download(ImapHelper imapHelper, PhoneAccountHandle account) {
    List<Voicemail> serverVoicemails = imapHelper.fetchAllVoicemails();
    List<Voicemail> localVoicemails = mQueryHelper.getAllVoicemails();

    if (localVoicemails == null || serverVoicemails == null) {
      // Null value means the query failed.
      return false;
    }

    Map<String, Voicemail> remoteMap = buildMap(serverVoicemails);

    // Go through all the local voicemails and check if they are on the server.
    // They may be read or deleted on the server but not locally. Perform the
    // appropriate local operation if the status differs from the server. Remove
    // the messages that exist both locally and on the server to know which server
    // messages to insert locally.
    // Voicemails that were removed automatically from the server, are marked as
    // archived and are stored locally. We do not delete them, as they were removed from the server
    // by design (to make space).
    for (int i = 0; i < localVoicemails.size(); i++) {
      Voicemail localVoicemail = localVoicemails.get(i);
      Voicemail remoteVoicemail = remoteMap.remove(localVoicemail.getSourceData());

      // Do not delete voicemails that are archived marked as archived.
      if (remoteVoicemail == null) {
        mQueryHelper.deleteNonArchivedFromDatabase(localVoicemail);
      } else {
        if (remoteVoicemail.isRead() != localVoicemail.isRead()) {
          mQueryHelper.markReadInDatabase(localVoicemail);
        }

        if (!TextUtils.isEmpty(remoteVoicemail.getTranscription())
            && TextUtils.isEmpty(localVoicemail.getTranscription())) {
          Logger.get(mContext).logImpression(DialerImpression.Type.VVM_TRANSCRIPTION_DOWNLOADED);
          mQueryHelper.updateWithTranscription(localVoicemail, remoteVoicemail.getTranscription());
        }
      }
    }

    // The leftover messages are messages that exist on the server but not locally.
    boolean prefetchEnabled = shouldPerformPrefetch(account, imapHelper);
    for (Voicemail remoteVoicemail : remoteMap.values()) {
      if (!TextUtils.isEmpty(remoteVoicemail.getTranscription())) {
        Logger.get(mContext).logImpression(DialerImpression.Type.VVM_TRANSCRIPTION_DOWNLOADED);
      }
      Uri uri = VoicemailDatabaseUtil.insert(mContext, remoteVoicemail);
      if (prefetchEnabled) {
        VoicemailFetchedCallback fetchedCallback =
            new VoicemailFetchedCallback(mContext, uri, account);
        imapHelper.fetchVoicemailPayload(fetchedCallback, remoteVoicemail.getSourceData());
      }
    }

    return true;
  }

  private boolean shouldPerformPrefetch(PhoneAccountHandle account, ImapHelper imapHelper) {
    OmtpVvmCarrierConfigHelper carrierConfigHelper =
        new OmtpVvmCarrierConfigHelper(mContext, account);
    return carrierConfigHelper.isPrefetchEnabled() && !imapHelper.isRoaming();
  }

  /** Builds a map from provider data to message for the given collection of voicemails. */
  private Map<String, Voicemail> buildMap(List<Voicemail> messages) {
    Map<String, Voicemail> map = new ArrayMap<String, Voicemail>();
    for (Voicemail message : messages) {
      map.put(message.getSourceData(), message);
    }
    return map;
  }

  /** Callback for {@link ImapHelper#fetchTranscription(TranscriptionFetchedCallback, String)} */
  public static class TranscriptionFetchedCallback {

    private Context mContext;
    private Voicemail mVoicemail;

    public TranscriptionFetchedCallback(Context context, Voicemail voicemail) {
      mContext = context;
      mVoicemail = voicemail;
    }

    public void setVoicemailTranscription(String transcription) {
      VoicemailsQueryHelper queryHelper = new VoicemailsQueryHelper(mContext);
      queryHelper.updateWithTranscription(mVoicemail, transcription);
    }
  }
}