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.populator; 17 18 import static com.google.common.base.Preconditions.checkArgument; 19 20 import com.google.android.libraries.mobiledatadownload.FileGroupPopulator; 21 import com.google.android.libraries.mobiledatadownload.MobileDataDownload; 22 import com.google.android.libraries.mobiledatadownload.internal.logging.LogUtil; 23 import com.google.common.base.Joiner; 24 import com.google.common.base.Optional; 25 import com.google.common.base.Supplier; 26 import com.google.common.collect.ImmutableList; 27 import com.google.common.collect.Lists; 28 import com.google.common.util.concurrent.ListenableFuture; 29 import com.google.errorprone.annotations.CanIgnoreReturnValue; 30 import com.google.mobiledatadownload.DownloadConfigProto.DataFileGroup; 31 import com.google.mobiledatadownload.DownloadConfigProto.ManifestConfig; 32 33 /** 34 * FileGroupPopulator that can process the ManifestConfig Flag from phenotype. 35 * 36 * <p>The {@link ManifestConfigFlagPopulator#manifestConfigFlagName} should point to a PH flag of 37 * type {@link ManifestConfig}. 38 * 39 * <p>Client can set an optional ManifestConfigOverrider to return a list of {@link DataFileGroup} 40 * which will be added to MDD. The Overrider will enable the on device targeting. 41 * 42 * <p>NOTE: if an app uses Flag Name Obfuscation, then the passed in flag name must be an obfuscated 43 * name. For more info, see <internal> 44 */ 45 public final class ManifestConfigFlagPopulator implements FileGroupPopulator { 46 47 private static final String TAG = "ManifestConfigFlagPopulator"; 48 49 /** 50 * Builder for {@link ManifestConfigFlagPopulator}. 51 * 52 * <p>Either {@code manifestConfigSupplier} or both {@code phPackageName} and {@code 53 * manifestConfigFlagName} should be set. 54 */ 55 public static final class Builder { 56 private Supplier<ManifestConfig> manifestConfigSupplier; 57 58 private Optional<ManifestConfigOverrider> overriderOptional = Optional.absent(); 59 60 /** Set the ManifestConfig supplier. */ 61 @CanIgnoreReturnValue setManifestConfigSupplier(Supplier<ManifestConfig> manifestConfigSupplier)62 public Builder setManifestConfigSupplier(Supplier<ManifestConfig> manifestConfigSupplier) { 63 this.manifestConfigSupplier = manifestConfigSupplier; 64 return this; 65 } 66 67 /** 68 * Sets the optional Overrider that takes a {@link ManifestConfig} and returns a list of {@link 69 * DataFileGroup} which will be added to MDD. The Overrider will enable the on device targeting. 70 */ 71 @CanIgnoreReturnValue setOverriderOptional(Optional<ManifestConfigOverrider> overriderOptional)72 public Builder setOverriderOptional(Optional<ManifestConfigOverrider> overriderOptional) { 73 this.overriderOptional = overriderOptional; 74 return this; 75 } 76 build()77 public ManifestConfigFlagPopulator build() { 78 checkArgument(manifestConfigSupplier != null, "Supplier should be provided."); 79 return new ManifestConfigFlagPopulator(manifestConfigSupplier, overriderOptional); 80 } 81 } 82 83 private final Supplier<ManifestConfig> manifestConfigSupplier; 84 private final Optional<ManifestConfigOverrider> overriderOptional; 85 86 /** Returns a Builder for ManifestConfigFlagPopulator. */ builder()87 public static Builder builder() { 88 return new Builder(); 89 } 90 ManifestConfigFlagPopulator( Supplier<ManifestConfig> manifestConfigSupplier, Optional<ManifestConfigOverrider> overriderOptional)91 private ManifestConfigFlagPopulator( 92 Supplier<ManifestConfig> manifestConfigSupplier, 93 Optional<ManifestConfigOverrider> overriderOptional) { 94 this.manifestConfigSupplier = manifestConfigSupplier; 95 this.overriderOptional = overriderOptional; 96 } 97 98 @Override refreshFileGroups(MobileDataDownload mobileDataDownload)99 public ListenableFuture<Void> refreshFileGroups(MobileDataDownload mobileDataDownload) { 100 ManifestConfig manifestConfig = manifestConfigSupplier.get(); 101 102 String groups = 103 Joiner.on(",") 104 .join( 105 Lists.transform( 106 manifestConfig.getEntryList(), 107 entry -> entry.getDataFileGroup().getGroupName())); 108 LogUtil.d("%s: Add groups [%s] from ManifestConfig to MDD.", TAG, groups); 109 110 return ManifestConfigHelper.refreshFromManifestConfig( 111 mobileDataDownload, 112 manifestConfigSupplier.get(), 113 overriderOptional, 114 /* accounts= */ ImmutableList.of(), 115 /* addGroupsWithVariantId= */ false); 116 } 117 } 118