1 /* 2 * Copyright (C) 2022 The Android Open Source Project 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 17 package com.google.android.libraries.mobiledatadownload.populator; 18 19 import android.net.Uri; 20 21 import com.google.android.libraries.mobiledatadownload.internal.logging.LogUtil; 22 import com.google.android.libraries.mobiledatadownload.populator.ManifestFileGroupPopulator.ManifestConfigParser; 23 import com.google.android.libraries.mobiledatadownload.file.SynchronousFileStorage; 24 import com.google.android.libraries.mobiledatadownload.file.openers.ReadProtoOpener; 25 import com.google.android.libraries.mobiledatadownload.tracing.PropagatedFutures; 26 import com.google.common.util.concurrent.ListenableFuture; 27 import com.google.mobiledatadownload.DownloadConfigProto.ManifestConfig; 28 29 import java.util.concurrent.Executor; 30 31 /** 32 * The default manifest parser that parses a given manifest file to {@link ManifestConfig}. 33 * 34 * <p>The format of the manifest file supported by this {@link ManifestConfigFileParser} is proto. 35 * That is, the content of manifest file should be accepted by {@link 36 * ManifestConfig#parseFrom(byte[])}. 37 */ 38 public final class ManifestConfigFileParser implements ManifestConfigParser { 39 40 private static final String TAG = "ManifestConfigFileParser"; 41 42 private final SynchronousFileStorage fileStorage; 43 private final Executor backgroundExecutor; 44 ManifestConfigFileParser(SynchronousFileStorage fileStorage, Executor backgroundExecutor)45 public ManifestConfigFileParser(SynchronousFileStorage fileStorage, Executor backgroundExecutor) { 46 this.fileStorage = fileStorage; 47 this.backgroundExecutor = backgroundExecutor; 48 } 49 50 @Override parse(Uri fileUri)51 public ListenableFuture<ManifestConfig> parse(Uri fileUri) { 52 return PropagatedFutures.submit( 53 () -> { 54 LogUtil.d("%s: Start parsing manifest file at %s", TAG, fileUri); 55 ManifestConfig manifestConfig = 56 fileStorage.open(fileUri, ReadProtoOpener.create(ManifestConfig.parser())); 57 return manifestConfig; 58 }, 59 backgroundExecutor); 60 } 61 }