• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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;
17 
18 import android.content.Context;
19 import android.util.Log;
20 import com.google.common.util.concurrent.Futures;
21 import com.google.common.util.concurrent.ListenableFuture;
22 import com.google.common.util.concurrent.MoreExecutors;
23 import com.google.mobiledatadownload.DownloadConfigProto.DataFile;
24 import com.google.mobiledatadownload.DownloadConfigProto.DataFileGroup;
25 import com.google.mobiledatadownload.DownloadConfigProto.DownloadConditions;
26 import com.google.mobiledatadownload.DownloadConfigProto.DownloadConditions.DeviceNetworkPolicy;
27 import com.google.mobiledatadownload.TransformProto.Transform;
28 import com.google.mobiledatadownload.TransformProto.Transforms;
29 import com.google.mobiledatadownload.TransformProto.ZipTransform;
30 
31 /** Test FileGroup Populator with zip file which contains 3 files and one sub-folder. */
32 public class ZipFolderFileGroupPopulator implements FileGroupPopulator {
33 
34   private static final String TAG = "MDD ZipFolderFileGroupPopulator";
35 
36   static final String FILE_GROUP_NAME = "test-zip-group";
37   static final String FILE_ID = "test-zip-file-id";
38   static final int FILE_SIZE = 373;
39   private static final String FILE_CHECKSUM = "7024b6bcddf2b2897656e9353f7fc715df5ea986";
40   private static final String FILE_URL =
41       "https://www.gstatic.com/icing/idd/apitest/zip_test_folder.zip";
42 
43   private final Context context;
44 
ZipFolderFileGroupPopulator(Context context)45   public ZipFolderFileGroupPopulator(Context context) {
46     this.context = context;
47   }
48 
49   @Override
refreshFileGroups(MobileDataDownload mobileDataDownload)50   public ListenableFuture<Void> refreshFileGroups(MobileDataDownload mobileDataDownload) {
51     DataFileGroup dataFileGroup =
52         createDataFileGroup(
53             FILE_GROUP_NAME,
54             context.getPackageName(),
55             new String[] {FILE_ID},
56             new int[] {FILE_SIZE},
57             new String[] {FILE_CHECKSUM},
58             new String[] {FILE_URL},
59             DeviceNetworkPolicy.DOWNLOAD_ON_ANY_NETWORK);
60 
61     ListenableFuture<Boolean> addFileGroupFuture =
62         mobileDataDownload.addFileGroup(
63             AddFileGroupRequest.newBuilder().setDataFileGroup(dataFileGroup).build());
64     return Futures.transform(
65         addFileGroupFuture,
66         result -> {
67           if (result.booleanValue()) {
68             Log.d(TAG, "Added file groups " + dataFileGroup.getGroupName());
69           } else {
70             Log.d(TAG, "Failed to add file group " + dataFileGroup.getGroupName());
71           }
72           return null;
73         },
74         MoreExecutors.directExecutor());
75   }
76 
77   public static DataFileGroup createDataFileGroup(
78       String groupName,
79       String ownerPackage,
80       String[] fileId,
81       int[] byteSize,
82       String[] checksum,
83       String[] url,
84       DeviceNetworkPolicy deviceNetworkPolicy) {
85     if (fileId.length != byteSize.length
86         || fileId.length != checksum.length
87         || fileId.length != url.length) {
88       throw new IllegalArgumentException();
89     }
90 
91     DataFileGroup.Builder dataFileGroupBuilder =
92         DataFileGroup.newBuilder()
93             .setGroupName(groupName)
94             .setOwnerPackage(ownerPackage)
95             .setDownloadConditions(
96                 DownloadConditions.newBuilder().setDeviceNetworkPolicy(deviceNetworkPolicy));
97 
98     for (int i = 0; i < fileId.length; ++i) {
99       DataFile file =
100           DataFile.newBuilder()
101               .setFileId(fileId[i])
102               .setByteSize(byteSize[i])
103               .setDownloadedFileChecksum(checksum[i])
104               .setUrlToDownload(url[i])
105               .setDownloadTransforms(
106                   Transforms.newBuilder()
107                       .addTransform(
108                           Transform.newBuilder()
109                               .setZip(ZipTransform.newBuilder().setTarget("*").build())))
110               .build();
111       dataFileGroupBuilder.addFile(file);
112     }
113 
114     return dataFileGroupBuilder.build();
115   }
116 }
117