1 /* 2 * Copyright 2022 Google LLC 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.google.android.libraries.mobiledatadownload.internal.util; 17 18 import com.google.mobiledatadownload.DownloadConfigProto.DataFile; 19 import com.google.mobiledatadownload.DownloadConfigProto.DataFileGroup; 20 import com.google.mobiledatadownload.DownloadConfigProto.DeltaFile; 21 import com.google.mobiledatadownload.DownloadConfigProto.DownloadConditions; 22 import com.google.mobiledatadownload.internal.MetadataProto; 23 import com.google.mobiledatadownload.internal.MetadataProto.DataFileGroupInternal; 24 import com.google.protobuf.ExtensionRegistryLite; 25 import com.google.protobuf.InvalidProtocolBufferException; 26 27 /** The util class that does conversion between protos. */ 28 public final class ProtoConversionUtil { ProtoConversionUtil()29 private ProtoConversionUtil() {} 30 31 /** 32 * Converts external configuration proto {@link DataFileGroup} into internal storage proto {@link 33 * DataFileGroupInternal}. 34 */ 35 // TODO(b/176103639): Use automated proto converter instead convert(DataFileGroup group)36 public static DataFileGroupInternal convert(DataFileGroup group) 37 throws InvalidProtocolBufferException { 38 // Cannot use generated registry here, because it may cause NPE to clients. 39 // For more detail, see b/140135059. 40 return DataFileGroupInternal.parseFrom( 41 group.toByteArray(), ExtensionRegistryLite.getEmptyRegistry()); 42 } 43 reverse(DataFileGroupInternal group)44 public static DataFileGroup reverse(DataFileGroupInternal group) 45 throws InvalidProtocolBufferException { 46 // Cannot use generated registry here, because it may cause NPE to clients. 47 // For more detail, see b/140135059. 48 return DataFileGroup.parseFrom(group.toByteArray(), ExtensionRegistryLite.getEmptyRegistry()); 49 } 50 51 /** 52 * Converts external proto {@link DownloadConditions} into internal proto {@link 53 * MetadataProto.DownloadConditions}. 54 */ 55 // TODO(b/176103639): Use automated proto converter instead convert(DownloadConditions downloadConditions)56 public static MetadataProto.DownloadConditions convert(DownloadConditions downloadConditions) 57 throws InvalidProtocolBufferException { 58 // Cannot use generated registry here, because it may cause NPE to clients. 59 // For more detail, see b/140135059. 60 return MetadataProto.DownloadConditions.parseFrom( 61 downloadConditions.toByteArray(), ExtensionRegistryLite.getEmptyRegistry()); 62 } 63 64 /** 65 * Converts external configuration proto {@link DataFile} to internal storage proto {@link 66 * MetadataProto.DataFile}. 67 */ 68 // TODO(b/176103639): Use automated proto converter instead 69 // LINT.IfChange(data_file_convert) convertDataFile(DataFile dataFile)70 public static MetadataProto.DataFile convertDataFile(DataFile dataFile) { 71 // incompatible argument for parameter value of setChecksumType. 72 // incompatible argument for parameter value of setAndroidSharingType. 73 // incompatible argument for parameter value of setAndroidSharingChecksumType. 74 @SuppressWarnings("nullness:argument.type.incompatible") 75 MetadataProto.DataFile.Builder dataFileBuilder = 76 MetadataProto.DataFile.newBuilder() 77 .setFileId(dataFile.getFileId()) 78 .setUrlToDownload(dataFile.getUrlToDownload()) 79 .setByteSize(dataFile.getByteSize()) 80 .setChecksumType( 81 MetadataProto.DataFile.ChecksumType.forNumber( 82 dataFile.getChecksumType().getNumber())) 83 .setChecksum(dataFile.getChecksum()) 84 .setDownloadedFileChecksum(dataFile.getDownloadedFileChecksum()) 85 .setDownloadedFileByteSize(dataFile.getDownloadedFileByteSize()) 86 .setAndroidSharingType( 87 MetadataProto.DataFile.AndroidSharingType.forNumber( 88 dataFile.getAndroidSharingType().getNumber())) 89 .setAndroidSharingChecksumType( 90 MetadataProto.DataFile.AndroidSharingChecksumType.forNumber( 91 dataFile.getAndroidSharingChecksumType().getNumber())) 92 .setAndroidSharingChecksum(dataFile.getAndroidSharingChecksum()) 93 .setRelativeFilePath(dataFile.getRelativeFilePath()); 94 95 if (dataFile.hasCustomMetadata()) { 96 dataFileBuilder.setCustomMetadata(dataFile.getCustomMetadata()); 97 } 98 if (dataFile.hasDownloadTransforms()) { 99 dataFileBuilder.setDownloadTransforms(dataFile.getDownloadTransforms()); 100 } 101 102 if (dataFile.hasReadTransforms()) { 103 dataFileBuilder.setReadTransforms(dataFile.getReadTransforms()); 104 } 105 106 for (DeltaFile deltaFile : dataFile.getDeltaFileList()) { 107 dataFileBuilder.addDeltaFile(convertDeltaFile(deltaFile)); 108 } 109 110 return dataFileBuilder.build(); 111 } 112 // LINT.ThenChange( 113 // 114 // <internal>, 115 // 116 // <internal>) 117 118 /** 119 * Converts external configuration proto {@link DeltaFile} to internal storage proto {@link 120 * DeltaFile}. 121 */ 122 // TODO(b/176103639): Use automated proto converter instead 123 // LINT.IfChange(delta_file_convert) 124 // incompatible argument for parameter value of setDiffDecoder. 125 @SuppressWarnings("nullness:argument.type.incompatible") convertDeltaFile(DeltaFile deltaFile)126 public static MetadataProto.DeltaFile convertDeltaFile(DeltaFile deltaFile) { 127 return MetadataProto.DeltaFile.newBuilder() 128 .setUrlToDownload(deltaFile.getUrlToDownload()) 129 .setByteSize(deltaFile.getByteSize()) 130 .setChecksum(deltaFile.getChecksum()) 131 .setDiffDecoder( 132 MetadataProto.DeltaFile.DiffDecoder.forNumber(deltaFile.getDiffDecoder().getNumber())) 133 .setBaseFile( 134 MetadataProto.BaseFile.newBuilder() 135 .setChecksum(deltaFile.getBaseFile().getChecksum()) 136 .build()) 137 .build(); 138 } 139 // LINT.ThenChange( 140 // 141 // <internal>, 142 // 143 // <internal>) 144 } 145