1 /* 2 * Copyright (C) 2025 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.testing; 18 19 import android.net.Uri; 20 21 import com.google.android.libraries.mobiledatadownload.downloader.DownloadConstraints; 22 import com.google.android.libraries.mobiledatadownload.downloader.DownloadRequest; 23 import com.google.android.libraries.mobiledatadownload.downloader.FileDownloader; 24 import com.google.android.libraries.mobiledatadownload.internal.logging.LogUtil; 25 import com.google.android.libraries.mobiledatadownload.file.SynchronousFileStorage; 26 import com.google.common.util.concurrent.ListenableFuture; 27 import com.google.common.util.concurrent.ListeningExecutorService; 28 29 /** 30 * A {@link FileDownloader} that "downloads" by copying the file from the application's assets. 31 * 32 * <p>The filename is the Last Path Segment of the provided `urlToDownload`. For example, the URL 33 * https://www.gstatic.com/icing/idd/sample_group/step1.txt will be mapped to the asset named 34 * "step1.txt". 35 * 36 * <p>Note that TestFileDownloader ignores the DownloadConditions. 37 */ 38 public final class TestFileDownloader implements FileDownloader { 39 40 private static final String TAG = "TestDataFileDownloader"; 41 42 private final FileDownloader delegateDownloader; 43 TestFileDownloader( SynchronousFileStorage fileStorage, ListeningExecutorService executor)44 public TestFileDownloader( 45 SynchronousFileStorage fileStorage, ListeningExecutorService executor) { 46 this.delegateDownloader = new LocalFileDownloader(fileStorage, executor); 47 } 48 49 @Override startDownloading(DownloadRequest downloadRequest)50 public ListenableFuture<Void> startDownloading(DownloadRequest downloadRequest) { 51 LogUtil.d( 52 "%s: startDownloading; urlToDownload: %s; uriToDownload: %s;", 53 TAG, downloadRequest.urlToDownload(), downloadRequest.fileUri()); 54 55 Uri fileUri = downloadRequest.fileUri(); 56 String urlToDownload = downloadRequest.urlToDownload(); 57 Uri uriToDownload = Uri.parse(urlToDownload); 58 DownloadConstraints downloadConstraints = downloadRequest.downloadConstraints(); 59 60 return delegateDownloader.startDownloading( 61 DownloadRequest.newBuilder() 62 .setFileUri(fileUri) 63 .setUrlToDownload(uriToDownload.getLastPathSegment()) 64 .setDownloadConstraints(downloadConstraints) 65 .build()); 66 } 67 } 68