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 static com.google.common.util.concurrent.Futures.immediateFailedFuture; 20 import static com.google.common.util.concurrent.Futures.immediateVoidFuture; 21 22 import android.net.Uri; 23 24 import androidx.test.core.app.ApplicationProvider; 25 26 import com.google.android.libraries.mobiledatadownload.DownloadException; 27 import com.google.android.libraries.mobiledatadownload.DownloadException.DownloadResultCode; 28 import com.google.android.libraries.mobiledatadownload.downloader.DownloadRequest; 29 import com.google.android.libraries.mobiledatadownload.downloader.FileDownloader; 30 import com.google.android.libraries.mobiledatadownload.file.Opener; 31 import com.google.android.libraries.mobiledatadownload.file.SynchronousFileStorage; 32 import com.google.android.libraries.mobiledatadownload.file.openers.WriteStreamOpener; 33 import com.google.android.libraries.mobiledatadownload.internal.logging.LogUtil; 34 import com.google.common.io.ByteStreams; 35 import com.google.common.util.concurrent.Futures; 36 import com.google.common.util.concurrent.ListenableFuture; 37 import com.google.common.util.concurrent.ListeningExecutorService; 38 import java.io.IOException; 39 import java.io.InputStream; 40 import java.io.OutputStream; 41 import java.util.concurrent.Executor; 42 43 /** 44 * A {@link FileDownloader} that "downloads" by copying the file from the application's assets. 45 * 46 * <p>This downloader retrieves files by matching the filename in the provided `urlToDownload` with 47 * a file in the assets. For example, to "download" a file named "image.png" located in the assets, 48 * you would use a URL like "https://example.com/images/image.png". 49 * 50 * <p><b>Note:</b> This implementation ignores DownloadConditions. 51 */ 52 public final class LocalFileDownloader implements FileDownloader { 53 54 private static final String TAG = "LocalFileDownloader"; 55 56 private final Executor backgroudExecutor; 57 private final SynchronousFileStorage fileStorage; 58 LocalFileDownloader( SynchronousFileStorage fileStorage, ListeningExecutorService executor)59 public LocalFileDownloader( 60 SynchronousFileStorage fileStorage, ListeningExecutorService executor) { 61 this.fileStorage = fileStorage; 62 this.backgroudExecutor = executor; 63 } 64 65 @Override startDownloading(DownloadRequest downloadRequest)66 public ListenableFuture<Void> startDownloading(DownloadRequest downloadRequest) { 67 return Futures.submitAsync( 68 () -> startDownloadingInternal(downloadRequest), backgroudExecutor); 69 } 70 startDownloadingInternal(DownloadRequest downloadRequest)71 private ListenableFuture<Void> startDownloadingInternal(DownloadRequest downloadRequest) { 72 Uri fileUri = downloadRequest.fileUri(); 73 String urlToDownload = downloadRequest.urlToDownload(); 74 LogUtil.d( 75 "%s: startDownloadingInternal; urlToDownload: %s; fileUri: %s;", 76 TAG, urlToDownload, fileUri); 77 78 try { 79 Opener<OutputStream> writeStreamOpener = WriteStreamOpener.create(); 80 long writtenBytes; 81 try (InputStream in = 82 ApplicationProvider.getApplicationContext() 83 .getAssets() 84 .open(urlToDownload); 85 OutputStream out = fileStorage.open(fileUri, writeStreamOpener)) { 86 writtenBytes = ByteStreams.copy(in, out); 87 } 88 LogUtil.d( 89 "%s: File URI %s download complete, writtenBytes: %d", 90 TAG, fileUri, writtenBytes); 91 } catch (IOException e) { 92 LogUtil.e(e, "%s: startDownloading got exception", TAG); 93 return immediateFailedFuture( 94 DownloadException.builder() 95 .setDownloadResultCode(DownloadResultCode.ANDROID_DOWNLOADER_HTTP_ERROR) 96 .build()); 97 } 98 99 return immediateVoidFuture(); 100 } 101 } 102