summaryrefslogtreecommitdiffstats
path: root/java/com/android/dialer/backup/DialerBackupUtils.java
blob: fe714f63653cbaca12a8799f22b0bb6d5a5233d9 (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
/*
 * Copyright (C) 2016 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.dialer.backup;

import android.annotation.TargetApi;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build.VERSION_CODES;
import android.provider.VoicemailContract;
import android.provider.VoicemailContract.Voicemails;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.telecom.PhoneAccountHandle;
import android.telecom.TelecomManager;
import android.util.Pair;
import com.android.dialer.common.Assert;
import com.android.dialer.common.LogUtil;
import com.android.dialer.configprovider.ConfigProviderBindings;
import com.android.voicemail.VoicemailComponent;
import com.google.common.io.ByteStreams;
import com.google.common.io.Files;
import com.google.protobuf.ByteString;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

/** Helper functions for DialerBackupAgent */
public class DialerBackupUtils {
  // Backup voicemails up to 20MB
  static long maxVoicemailSizeToBackup = 20000000L;
  static final String RESTORED_COLUMN = "restored";

  private DialerBackupUtils() {}

  public static void copyAudioBytesToContentUri(
      @NonNull byte[] audioBytesArray, @NonNull OutputStream restoreStream) throws IOException {
    LogUtil.i("DialerBackupUtils.copyStream", "audioByteArray length: " + audioBytesArray.length);

    ByteArrayInputStream decodedStream = new ByteArrayInputStream(audioBytesArray);
    LogUtil.i(
        "DialerBackupUtils.copyStream", "decodedStream.available: " + decodedStream.available());

    ByteStreams.copy(decodedStream, restoreStream);
  }

  public static @Nullable ByteString audioStreamToByteString(@NonNull InputStream stream)
      throws IOException {
    if (stream.available() > 0) {
      return ByteString.readFrom(stream);
    } else {
      LogUtil.i("DialerBackupUtils.audioStreamToByteArray", "no audio stream to backup");
    }
    return ByteString.EMPTY;
  }

  public static void writeProtoToFile(@NonNull File file, @NonNull VoicemailInfo voicemailInfo)
      throws IOException {
    LogUtil.i(
        "DialerBackupUtils.writeProtoToFile",
        "backup " + voicemailInfo + " to " + file.getAbsolutePath());

    byte[] bytes = voicemailInfo.toByteArray();
    Files.write(bytes, file);
  }

  /** Only restore voicemails that have the restored column in calllog (NMR2+ builds) */
  @TargetApi(VERSION_CODES.M)
  public static boolean canRestoreVoicemails(ContentResolver contentResolver, Context context) {
    try (Cursor cursor = contentResolver.query(Voicemails.CONTENT_URI, null, null, null, null)) {
      // Restored column only exists in NMR2 and above builds.
      if (cursor.getColumnIndex(RESTORED_COLUMN) != -1) {
        LogUtil.i("DialerBackupUtils.canRestoreVoicemails", "Build supports restore");
        return true;
      } else {
        LogUtil.i("DialerBackupUtils.canRestoreVoicemails", "Build does not support restore");
        return false;
      }
    }
  }

  public static VoicemailInfo protoFileToVoicemailInfo(@NonNull File file) throws IOException {
    byte[] byteArray = Files.toByteArray(file);
    return VoicemailInfo.parseFrom(byteArray);
  }

  @TargetApi(VERSION_CODES.M)
  public static VoicemailInfo convertVoicemailCursorRowToProto(
      @NonNull Cursor cursor, @NonNull ContentResolver contentResolver) throws IOException {

    VoicemailInfo.Builder voicemailInfo = VoicemailInfo.newBuilder();

    for (int i = 0; i < cursor.getColumnCount(); ++i) {
      String name = cursor.getColumnName(i);
      String value = cursor.getString(i);

      LogUtil.i(
          "DialerBackupUtils.convertVoicemailCursorRowToProto",
          "column index: %d, column name: %s, column value: %s",
          i,
          name,
          value);

      switch (name) {
        case Voicemails.DATE:
          voicemailInfo.setDate(value);
          break;
        case Voicemails.DELETED:
          voicemailInfo.setDeleted(value);
          break;
        case Voicemails.DIRTY:
          voicemailInfo.setDirty(value);
          break;
        case Voicemails.DIR_TYPE:
          voicemailInfo.setDirType(value);
          break;
        case Voicemails.DURATION:
          voicemailInfo.setDuration(value);
          break;
        case Voicemails.HAS_CONTENT:
          voicemailInfo.setHasContent(value);
          break;
        case Voicemails.IS_READ:
          voicemailInfo.setIsRead(value);
          break;
        case Voicemails.ITEM_TYPE:
          voicemailInfo.setItemType(value);
          break;
        case Voicemails.LAST_MODIFIED:
          voicemailInfo.setLastModified(value);
          break;
        case Voicemails.MIME_TYPE:
          voicemailInfo.setMimeType(value);
          break;
        case Voicemails.NUMBER:
          voicemailInfo.setNumber(value);
          break;
        case Voicemails.PHONE_ACCOUNT_COMPONENT_NAME:
          voicemailInfo.setPhoneAccountComponentName(value);
          break;
        case Voicemails.PHONE_ACCOUNT_ID:
          voicemailInfo.setPhoneAccountId(value);
          break;
        case Voicemails.SOURCE_DATA:
          voicemailInfo.setSourceData(value);
          break;
        case Voicemails.SOURCE_PACKAGE:
          voicemailInfo.setSourcePackage(value);
          break;
        case Voicemails.TRANSCRIPTION:
          voicemailInfo.setTranscription(value);
          break;
        case DialerBackupAgent.VOICEMAIL_URI:
          try (InputStream audioStream = contentResolver.openInputStream(Uri.parse(value))) {
            voicemailInfo.setEncodedVoicemailKey(audioStreamToByteString(audioStream));
          }
          break;
        default:
          LogUtil.i(
              "DialerBackupUtils.convertVoicemailCursorRowToProto",
              "Not backing up column: %s, with value: %s",
              name,
              value);
          break;
      }
    }
    return voicemailInfo.build();
  }

  public static Pair<ContentValues, byte[]> convertVoicemailProtoFileToContentValueAndAudioBytes(
      @NonNull File file, Context context) throws IOException {

    VoicemailInfo voicemailInfo = DialerBackupUtils.protoFileToVoicemailInfo(file);
    LogUtil.i(
        "DialerBackupUtils.convertVoicemailProtoFileToContentValueAndEncodedAudio",
        "file name: "
            + file.getName()
            + " voicemailInfo size: "
            + voicemailInfo.getSerializedSize());

    if (isDuplicate(context, voicemailInfo)) {
      LogUtil.i(
          "DialerBackupUtils.convertVoicemailProtoFileToContentValueAndEncodedAudio",
          "voicemail already exists");
      return null;
    } else {
      ContentValues contentValues = new ContentValues();

      if (voicemailInfo.hasDate()) {
        contentValues.put(Voicemails.DATE, voicemailInfo.getDate());
      }
      if (voicemailInfo.hasDeleted()) {
        contentValues.put(Voicemails.DELETED, voicemailInfo.getDeleted());
      }
      if (!voicemailInfo.hasDirty()) {
        contentValues.put(Voicemails.DIRTY, voicemailInfo.getDirty());
      }
      if (!voicemailInfo.hasDuration()) {
        contentValues.put(Voicemails.DURATION, voicemailInfo.getDuration());
      }
      if (!voicemailInfo.hasIsRead()) {
        contentValues.put(Voicemails.IS_READ, voicemailInfo.getIsRead());
      }
      if (!voicemailInfo.hasLastModified()) {
        contentValues.put(Voicemails.LAST_MODIFIED, voicemailInfo.getLastModified());
      }
      if (!voicemailInfo.hasMimeType()) {
        contentValues.put(Voicemails.MIME_TYPE, voicemailInfo.getMimeType());
      }
      if (!voicemailInfo.hasNumber()) {
        contentValues.put(Voicemails.NUMBER, voicemailInfo.getNumber());
      }
      if (!voicemailInfo.hasPhoneAccountComponentName()) {
        contentValues.put(
            Voicemails.PHONE_ACCOUNT_COMPONENT_NAME, voicemailInfo.getPhoneAccountComponentName());
      }
      if (!voicemailInfo.hasPhoneAccountId()) {
        contentValues.put(Voicemails.PHONE_ACCOUNT_ID, voicemailInfo.getPhoneAccountId());
      }
      if (!voicemailInfo.hasSourceData()) {
        contentValues.put(Voicemails.SOURCE_DATA, voicemailInfo.getSourceData());
      }
      if (!voicemailInfo.hasSourcePackage()) {
        contentValues.put(Voicemails.SOURCE_PACKAGE, voicemailInfo.getSourcePackage());
      }
      if (!voicemailInfo.hasTranscription()) {
        contentValues.put(Voicemails.TRANSCRIPTION, voicemailInfo.getTranscription());
      }
      contentValues.put(VoicemailContract.Voicemails.HAS_CONTENT, 1);
      contentValues.put(RESTORED_COLUMN, "1");
      contentValues.put(Voicemails.SOURCE_PACKAGE, getSourcePackage(context, voicemailInfo));

      LogUtil.i(
          "DialerBackupUtils.convertVoicemailProtoFileToContentValueAndEncodedAudio",
          "cv: " + contentValues);

      return Pair.create(contentValues, voicemailInfo.getEncodedVoicemailKey().toByteArray());
    }
  }

  /**
   * We should be using the system package name as the source package if there is no endless VM/VM
   * archive present on the device. This is to separate pre-O (no endless VM) and O+ (endless VM)
   * devices. This ensures that the source of truth for VMs is the VM server when endless VM is not
   * enabled, and when endless VM/archived VMs is present, the source of truth for VMs is the device
   * itself.
   */
  private static String getSourcePackage(Context context, VoicemailInfo voicemailInfo) {
    if (ConfigProviderBindings.get(context)
        .getBoolean("voicemail_restore_force_system_source_package", false)) {
      LogUtil.i("DialerBackupUtils.getSourcePackage", "forcing system source package");
      return "com.android.phone";
    }
    if (ConfigProviderBindings.get(context)
        .getBoolean("voicemail_restore_check_archive_for_source_package", true)) {
      if ("1".equals(voicemailInfo.getArchived())) {
        LogUtil.i(
            "DialerBackupUtils.getSourcePackage",
            "voicemail was archived, using app source package");
        // Using our app's source package will prevent the archived voicemail from being deleted by
        // the system when it syncs with the voicemail server. In most cases the user will not see
        // duplicate voicemails because this voicemail was archived and likely deleted from the
        // voicemail server.
        return context.getPackageName();
      } else {
        // Use the system source package. This means that if the voicemail is not present on the
        // voicemail server then the system will delete it when it syncs.
        LogUtil.i(
            "DialerBackupUtils.getSourcePackage",
            "voicemail was not archived, using system source package");
        return "com.android.phone";
      }
    }
    // Use our app's source package. This means that if the system syncs voicemail from the server
    // the user could potentially get duplicate voicemails.
    LogUtil.i("DialerBackupUtils.getSourcePackage", "defaulting to using app source package");
    return context.getPackageName();
  }

  @TargetApi(VERSION_CODES.M)
  private static boolean isDuplicate(Context context, VoicemailInfo voicemailInfo) {
    // This checks for VM that might already exist, and doesn't restore them
    try (Cursor cursor =
        context
            .getContentResolver()
            .query(
                VoicemailContract.Voicemails.CONTENT_URI,
                null,
                String.format(
                    "(%s = ? AND %s = ? AND %s = ?)",
                    Voicemails.NUMBER, Voicemails.DATE, Voicemails.DURATION),
                new String[] {
                  voicemailInfo.getNumber(), voicemailInfo.getDate(), voicemailInfo.getDuration()
                },
                null,
                null)) {
      if (cursor.moveToFirst()
          && ConfigProviderBindings.get(context)
              .getBoolean("enable_vm_restore_no_duplicate", true)) {
        return true;
      }
    }
    return false;
  }

  public static String getPhoneAccountClause(List<PhoneAccountHandle> phoneAccountsToArchive) {
    Assert.checkArgument(!phoneAccountsToArchive.isEmpty());
    StringBuilder whereQuery = new StringBuilder();

    whereQuery.append("(");

    for (int i = 0; i < phoneAccountsToArchive.size(); i++) {
      whereQuery.append(
          Voicemails.PHONE_ACCOUNT_ID + " = " + phoneAccountsToArchive.get(i).getId());

      if (phoneAccountsToArchive.size() > 1 && i < phoneAccountsToArchive.size() - 1) {
        whereQuery.append(" OR ");
      }
    }
    whereQuery.append(")");
    return whereQuery.toString();
  }

  public static List<PhoneAccountHandle> getPhoneAccountsToArchive(Context context) {
    List<PhoneAccountHandle> phoneAccountsToBackUp = new ArrayList<>();

    for (PhoneAccountHandle handle :
        context.getSystemService(TelecomManager.class).getCallCapablePhoneAccounts()) {

      if (VoicemailComponent.get(context)
          .getVoicemailClient()
          .isVoicemailArchiveEnabled(context, handle)) {
        phoneAccountsToBackUp.add(handle);
        LogUtil.i(
            "DialerBackupUtils.getPhoneAccountsToArchive", "enabled for: " + handle.toString());
      } else {
        LogUtil.i(
            "DialerBackupUtils.getPhoneAccountsToArchive", "not enabled for: " + handle.toString());
      }
    }
    return phoneAccountsToBackUp;
  }
}