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.file.transforms; 17 18 import android.net.Uri; 19 import com.google.android.libraries.mobiledatadownload.file.common.internal.LiteTransformFragments; 20 import com.google.common.collect.ImmutableList; 21 import com.google.mobiledatadownload.TransformProto; 22 23 /** Helper for Uri classes to handle transform fragments using proto specs. */ 24 public final class TransformProtoFragments { 25 26 /** Adds this spec to the URI, replacing existing spec if present. */ addOrReplaceTransform(Uri uri, TransformProto.Transform spec)27 public static Uri addOrReplaceTransform(Uri uri, TransformProto.Transform spec) { 28 ImmutableList.Builder<String> result = ImmutableList.builder(); 29 ImmutableList<String> oldSpecs = LiteTransformFragments.parseTransformSpecs(uri); 30 String newSpec = TransformProtos.toEncodedSpec(spec); 31 String newSpecName = LiteTransformFragments.parseSpecName(newSpec); 32 for (String oldSpec : oldSpecs) { 33 String oldSpecName = LiteTransformFragments.parseSpecName(oldSpec); 34 if (!oldSpecName.equals(newSpecName)) { 35 result.add(oldSpec); 36 } 37 } 38 result.add(newSpec); 39 return uri.buildUpon() 40 .encodedFragment(LiteTransformFragments.joinTransformSpecs(result.build())) 41 .build(); 42 } 43 TransformProtoFragments()44 private TransformProtoFragments() {} 45 } 46