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.internal.util; 17 18 import com.google.android.libraries.mobiledatadownload.SingleFileDownloadListener; 19 import com.google.android.libraries.mobiledatadownload.SingleFileDownloadRequest; 20 import com.google.android.libraries.mobiledatadownload.lite.DownloadListener; 21 import com.google.android.libraries.mobiledatadownload.lite.DownloadRequest; 22 import com.google.common.base.Optional; 23 import com.google.common.util.concurrent.ListenableFuture; 24 25 /** 26 * Utility Class to help converting MDD Lib's SingleFile classes/interfaces to the MDD Lite 27 * equivalent. 28 */ 29 public final class MddLiteConversionUtil { 30 MddLiteConversionUtil()31 private MddLiteConversionUtil() {} 32 33 /** Convert {@link SingleFileDownloadRequest} to MDD Lite's {@link DownloadRequest}. */ 34 // TODO(b/176103639): Use AutoConverter if @AutoValue to @AutoValue is supported convertToDownloadRequest( SingleFileDownloadRequest singleFileDownloadRequest)35 public static DownloadRequest convertToDownloadRequest( 36 SingleFileDownloadRequest singleFileDownloadRequest) { 37 return DownloadRequest.newBuilder() 38 .setDestinationFileUri(singleFileDownloadRequest.destinationFileUri()) 39 .setUrlToDownload(singleFileDownloadRequest.urlToDownload()) 40 .setDownloadConstraints(singleFileDownloadRequest.downloadConstraints()) 41 .setListenerOptional( 42 convertToDownloadListenerOptional(singleFileDownloadRequest.listenerOptional())) 43 .setTrafficTag(singleFileDownloadRequest.trafficTag()) 44 .setExtraHttpHeaders(singleFileDownloadRequest.extraHttpHeaders()) 45 .setFileSizeBytes(singleFileDownloadRequest.fileSizeBytes()) 46 .setNotificationContentTitle(singleFileDownloadRequest.notificationContentTitle()) 47 .setNotificationContentTextOptional( 48 singleFileDownloadRequest.notificationContentTextOptional()) 49 .setShowDownloadedNotification(singleFileDownloadRequest.showDownloadedNotification()) 50 .build(); 51 } 52 53 /** Convenience method for handling optionals. */ convertToDownloadListenerOptional( Optional<SingleFileDownloadListener> singleFileDownloadListenerOptional)54 public static Optional<DownloadListener> convertToDownloadListenerOptional( 55 Optional<SingleFileDownloadListener> singleFileDownloadListenerOptional) { 56 if (!singleFileDownloadListenerOptional.isPresent()) { 57 return Optional.absent(); 58 } 59 60 return Optional.of(convertToDownloadListener(singleFileDownloadListenerOptional.get())); 61 } 62 63 /** Convert {@link SingleFileDownloadListener} to MDD Lite's {@link DownloadListener}. */ convertToDownloadListener( SingleFileDownloadListener singleFileDownloadListener)64 public static DownloadListener convertToDownloadListener( 65 SingleFileDownloadListener singleFileDownloadListener) { 66 return new ConvertedSingleFileDownloadListener(singleFileDownloadListener); 67 } 68 69 /** 70 * Wrapper around given {@link SingleFileDownloadListener} that implements MDD Lite's {@link 71 * DownloadListener} interface. 72 */ 73 private static final class ConvertedSingleFileDownloadListener implements DownloadListener { 74 private final SingleFileDownloadListener sourceListener; 75 ConvertedSingleFileDownloadListener(SingleFileDownloadListener sourceListener)76 private ConvertedSingleFileDownloadListener(SingleFileDownloadListener sourceListener) { 77 this.sourceListener = sourceListener; 78 } 79 80 @Override onProgress(long currentSize)81 public void onProgress(long currentSize) { 82 sourceListener.onProgress(currentSize); 83 } 84 85 @Override onComplete()86 public ListenableFuture<Void> onComplete() { 87 return sourceListener.onComplete(); 88 } 89 90 @Override onFailure(Throwable t)91 public void onFailure(Throwable t) { 92 sourceListener.onFailure(t); 93 } 94 95 @Override onPausedForConnectivity()96 public void onPausedForConnectivity() { 97 sourceListener.onPausedForConnectivity(); 98 } 99 } 100 } 101