summaryrefslogtreecommitdiff
path: root/java/com/google/android/libraries/mobiledatadownload/internal/util/ProtoConversionUtil.java
blob: cdc8a58a28e56c94e6727eaef9a0ae40d0a3ce66 (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
/*
 * Copyright 2022 Google LLC
 *
 * 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.google.android.libraries.mobiledatadownload.internal.util;

import com.google.mobiledatadownload.DownloadConfigProto.DataFile;
import com.google.mobiledatadownload.DownloadConfigProto.DataFileGroup;
import com.google.mobiledatadownload.DownloadConfigProto.DeltaFile;
import com.google.mobiledatadownload.DownloadConfigProto.DownloadConditions;
import com.google.mobiledatadownload.internal.MetadataProto;
import com.google.mobiledatadownload.internal.MetadataProto.DataFileGroupInternal;
import com.google.protobuf.ExtensionRegistryLite;
import com.google.protobuf.InvalidProtocolBufferException;

/** The util class that does conversion between protos. */
public final class ProtoConversionUtil {
  private ProtoConversionUtil() {}

  /**
   * Converts external configuration proto {@link DataFileGroup} into internal storage proto {@link
   * DataFileGroupInternal}.
   */
  // TODO(b/176103639): Use automated proto converter instead
  public static DataFileGroupInternal convert(DataFileGroup group)
      throws InvalidProtocolBufferException {
    // Cannot use generated registry here, because it may cause NPE to clients.
    // For more detail, see b/140135059.
    return DataFileGroupInternal.parseFrom(
        group.toByteArray(), ExtensionRegistryLite.getEmptyRegistry());
  }

  public static DataFileGroup reverse(DataFileGroupInternal group)
      throws InvalidProtocolBufferException {
    // Cannot use generated registry here, because it may cause NPE to clients.
    // For more detail, see b/140135059.
    return DataFileGroup.parseFrom(group.toByteArray(), ExtensionRegistryLite.getEmptyRegistry());
  }

  /**
   * Converts external proto {@link DownloadConditions} into internal proto {@link
   * MetadataProto.DownloadConditions}.
   */
  // TODO(b/176103639): Use automated proto converter instead
  public static MetadataProto.DownloadConditions convert(DownloadConditions downloadConditions)
      throws InvalidProtocolBufferException {
    // Cannot use generated registry here, because it may cause NPE to clients.
    // For more detail, see b/140135059.
    return MetadataProto.DownloadConditions.parseFrom(
        downloadConditions.toByteArray(), ExtensionRegistryLite.getEmptyRegistry());
  }

  /**
   * Converts external configuration proto {@link DataFile} to internal storage proto {@link
   * MetadataProto.DataFile}.
   */
  // TODO(b/176103639): Use automated proto converter instead
  // LINT.IfChange(data_file_convert)
  public static MetadataProto.DataFile convertDataFile(DataFile dataFile) {
    // incompatible argument for parameter value of setChecksumType.
    // incompatible argument for parameter value of setAndroidSharingType.
    // incompatible argument for parameter value of setAndroidSharingChecksumType.
    @SuppressWarnings("nullness:argument.type.incompatible")
    MetadataProto.DataFile.Builder dataFileBuilder =
        MetadataProto.DataFile.newBuilder()
            .setFileId(dataFile.getFileId())
            .setUrlToDownload(dataFile.getUrlToDownload())
            .setByteSize(dataFile.getByteSize())
            .setChecksumType(
                MetadataProto.DataFile.ChecksumType.forNumber(
                    dataFile.getChecksumType().getNumber()))
            .setChecksum(dataFile.getChecksum())
            .setDownloadedFileChecksum(dataFile.getDownloadedFileChecksum())
            .setDownloadedFileByteSize(dataFile.getDownloadedFileByteSize())
            .setAndroidSharingType(
                MetadataProto.DataFile.AndroidSharingType.forNumber(
                    dataFile.getAndroidSharingType().getNumber()))
            .setAndroidSharingChecksumType(
                MetadataProto.DataFile.AndroidSharingChecksumType.forNumber(
                    dataFile.getAndroidSharingChecksumType().getNumber()))
            .setAndroidSharingChecksum(dataFile.getAndroidSharingChecksum())
            .setRelativeFilePath(dataFile.getRelativeFilePath());

    if (dataFile.hasCustomMetadata()) {
      dataFileBuilder.setCustomMetadata(dataFile.getCustomMetadata());
    }
    if (dataFile.hasDownloadTransforms()) {
      dataFileBuilder.setDownloadTransforms(dataFile.getDownloadTransforms());
    }

    if (dataFile.hasReadTransforms()) {
      dataFileBuilder.setReadTransforms(dataFile.getReadTransforms());
    }

    for (DeltaFile deltaFile : dataFile.getDeltaFileList()) {
      dataFileBuilder.addDeltaFile(convertDeltaFile(deltaFile));
    }

    return dataFileBuilder.build();
  }
  // LINT.ThenChange(
  //
  // <internal>,
  //
  // <internal>)

  /**
   * Converts external configuration proto {@link DeltaFile} to internal storage proto {@link
   * DeltaFile}.
   */
  // TODO(b/176103639): Use automated proto converter instead
  // LINT.IfChange(delta_file_convert)
  // incompatible argument for parameter value of setDiffDecoder.
  @SuppressWarnings("nullness:argument.type.incompatible")
  public static MetadataProto.DeltaFile convertDeltaFile(DeltaFile deltaFile) {
    return MetadataProto.DeltaFile.newBuilder()
        .setUrlToDownload(deltaFile.getUrlToDownload())
        .setByteSize(deltaFile.getByteSize())
        .setChecksum(deltaFile.getChecksum())
        .setDiffDecoder(
            MetadataProto.DeltaFile.DiffDecoder.forNumber(deltaFile.getDiffDecoder().getNumber()))
        .setBaseFile(
            MetadataProto.BaseFile.newBuilder()
                .setChecksum(deltaFile.getBaseFile().getChecksum())
                .build())
        .build();
  }
  // LINT.ThenChange(
  //
  // <internal>,
  //
  // <internal>)
}