summaryrefslogtreecommitdiff
path: root/java/com/google/android/libraries/mobiledatadownload/MobileDataDownloadImpl.java
blob: 04abda1ecc6f870ec13378890fd7b016ee990ed8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
/*
 * Copyright 2022 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.android.libraries.mobiledatadownload;

import static com.google.android.libraries.mobiledatadownload.tracing.TracePropagation.propagateAsyncFunction;
import static com.google.android.libraries.mobiledatadownload.tracing.TracePropagation.propagateRunnable;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.util.concurrent.Futures.getDone;
import static com.google.common.util.concurrent.Futures.immediateFailedFuture;
import static com.google.common.util.concurrent.Futures.immediateFuture;
import static com.google.common.util.concurrent.Futures.immediateVoidFuture;
import static com.google.common.util.concurrent.MoreExecutors.directExecutor;

import android.accounts.Account;
import android.content.Context;
import android.net.Uri;
import android.text.TextUtils;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import com.google.android.libraries.mobiledatadownload.DownloadException.DownloadResultCode;
import com.google.android.libraries.mobiledatadownload.TaskScheduler.ConstraintOverrides;
import com.google.android.libraries.mobiledatadownload.TaskScheduler.NetworkState;
import com.google.android.libraries.mobiledatadownload.account.AccountUtil;
import com.google.android.libraries.mobiledatadownload.file.SynchronousFileStorage;
import com.google.android.libraries.mobiledatadownload.foreground.ForegroundDownloadKey;
import com.google.android.libraries.mobiledatadownload.foreground.NotificationUtil;
import com.google.android.libraries.mobiledatadownload.internal.DownloadGroupState;
import com.google.android.libraries.mobiledatadownload.internal.ExceptionToMddResultMapper;
import com.google.android.libraries.mobiledatadownload.internal.MddConstants;
import com.google.android.libraries.mobiledatadownload.internal.MobileDataDownloadManager;
import com.google.android.libraries.mobiledatadownload.internal.collect.GroupKeyAndGroup;
import com.google.android.libraries.mobiledatadownload.internal.collect.GroupPair;
import com.google.android.libraries.mobiledatadownload.internal.logging.EventLogger;
import com.google.android.libraries.mobiledatadownload.internal.logging.LogUtil;
import com.google.android.libraries.mobiledatadownload.internal.util.DownloadFutureMap;
import com.google.android.libraries.mobiledatadownload.internal.util.MddLiteConversionUtil;
import com.google.android.libraries.mobiledatadownload.internal.util.ProtoConversionUtil;
import com.google.android.libraries.mobiledatadownload.lite.Downloader;
import com.google.android.libraries.mobiledatadownload.monitor.DownloadProgressMonitor;
import com.google.android.libraries.mobiledatadownload.tracing.PropagatedExecutionSequencer;
import com.google.android.libraries.mobiledatadownload.tracing.PropagatedFluentFuture;
import com.google.android.libraries.mobiledatadownload.tracing.PropagatedFutures;
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.util.concurrent.AsyncFunction;
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListenableFutureTask;
import com.google.mobiledatadownload.ClientConfigProto.ClientFile;
import com.google.mobiledatadownload.ClientConfigProto.ClientFileGroup;
import com.google.mobiledatadownload.DownloadConfigProto;
import com.google.mobiledatadownload.DownloadConfigProto.DataFileGroup;
import com.google.mobiledatadownload.internal.MetadataProto.DataFile;
import com.google.mobiledatadownload.internal.MetadataProto.DataFileGroupInternal;
import com.google.mobiledatadownload.internal.MetadataProto.DownloadConditions;
import com.google.mobiledatadownload.internal.MetadataProto.DownloadConditions.DeviceNetworkPolicy;
import com.google.mobiledatadownload.internal.MetadataProto.GroupKey;
import com.google.mobiledatadownload.LogProto.DataDownloadFileGroupStats;
import com.google.protobuf.Any;
import com.google.protobuf.InvalidProtocolBufferException;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import javax.annotation.Nullable;

/**
 * Default implementation for {@link
 * com.google.android.libraries.mobiledatadownload.MobileDataDownload}.
 */
class MobileDataDownloadImpl implements MobileDataDownload {

  private static final String TAG = "MobileDataDownload";
  private static final long DUMP_DEBUG_INFO_TIMEOUT = 3;

  private final Context context;
  private final EventLogger eventLogger;
  private final List<FileGroupPopulator> fileGroupPopulatorList;
  private final Optional<TaskScheduler> taskSchedulerOptional;
  private final MobileDataDownloadManager mobileDataDownloadManager;
  private final SynchronousFileStorage fileStorage;
  private final Flags flags;
  private final Downloader singleFileDownloader;

  // Track all the on-going foreground downloads. This map is keyed by ForegroundDownloadKey.
  private final DownloadFutureMap<ClientFileGroup> foregroundDownloadFutureMap;

  // Track all on-going background download requests started by downloadFileGroup. This map is keyed
  // by ForegroundDownloadKey so request can be kept in sync with foregroundDownloadFutureMap.
  private final DownloadFutureMap<ClientFileGroup> downloadFutureMap;

  // This executor will execute tasks sequentially.
  private final Executor sequentialControlExecutor;
  // ExecutionSequencer will execute a ListenableFuture and its Futures.transforms before taking the
  // next task (<internal>). Most of MDD API should use
  // ExecutionSequencer to guarantee Metadata synchronization. Currently only downloadFileGroup and
  // handleTask APIs do not use ExecutionSequencer since their execution could take long time and
  // using ExecutionSequencer would block other APIs.
  private final PropagatedExecutionSequencer futureSerializer =
      PropagatedExecutionSequencer.create();
  private final Optional<DownloadProgressMonitor> downloadMonitorOptional;
  private final Optional<Class<?>> foregroundDownloadServiceClassOptional;
  private final AsyncFunction<DataFileGroupInternal, Boolean> customFileGroupValidator;
  private final TimeSource timeSource;

  MobileDataDownloadImpl(
      Context context,
      EventLogger eventLogger,
      MobileDataDownloadManager mobileDataDownloadManager,
      Executor sequentialControlExecutor,
      List<FileGroupPopulator> fileGroupPopulatorList,
      Optional<TaskScheduler> taskSchedulerOptional,
      SynchronousFileStorage fileStorage,
      Optional<DownloadProgressMonitor> downloadMonitorOptional,
      Optional<Class<?>> foregroundDownloadServiceClassOptional,
      Flags flags,
      Downloader singleFileDownloader,
      Optional<CustomFileGroupValidator> customValidatorOptional,
      TimeSource timeSource) {
    this.context = context;
    this.eventLogger = eventLogger;
    this.fileGroupPopulatorList = fileGroupPopulatorList;
    this.taskSchedulerOptional = taskSchedulerOptional;
    this.sequentialControlExecutor = sequentialControlExecutor;
    this.mobileDataDownloadManager = mobileDataDownloadManager;
    this.fileStorage = fileStorage;
    this.downloadMonitorOptional = downloadMonitorOptional;
    this.foregroundDownloadServiceClassOptional = foregroundDownloadServiceClassOptional;
    this.flags = flags;
    this.singleFileDownloader = singleFileDownloader;
    this.customFileGroupValidator =
        createCustomFileGroupValidator(
            customValidatorOptional,
            mobileDataDownloadManager,
            sequentialControlExecutor,
            fileStorage);
    this.downloadFutureMap = DownloadFutureMap.create(sequentialControlExecutor);
    this.foregroundDownloadFutureMap =
        DownloadFutureMap.create(
            sequentialControlExecutor,
            createCallbacksForForegroundService(context, foregroundDownloadServiceClassOptional));
    this.timeSource = timeSource;
  }

  // Wraps the custom validator because the validation at a lower level of the stack where
  // the ClientFileGroup is not available, yet ClientFileGroup is the client-facing API we'd
  // like to expose.
  private static AsyncFunction<DataFileGroupInternal, Boolean> createCustomFileGroupValidator(
      Optional<CustomFileGroupValidator> validatorOptional,
      MobileDataDownloadManager mobileDataDownloadManager,
      Executor executor,
      SynchronousFileStorage fileStorage) {
    if (!validatorOptional.isPresent()) {
      return unused -> immediateFuture(true);
    }

    return internalFileGroup ->
        PropagatedFutures.transformAsync(
            createClientFileGroup(
                internalFileGroup,
                /* account= */ null,
                ClientFileGroup.Status.PENDING_CUSTOM_VALIDATION,
                /* preserveZipDirectories= */ false,
                /* verifyIsolatedStructure= */ true,
                mobileDataDownloadManager,
                executor,
                fileStorage),
            propagateAsyncFunction(
                clientFileGroup -> validatorOptional.get().validateFileGroup(clientFileGroup)),
            executor);
  }

  /**
   * Functional interface used as callback for logging file group stats. Used to create file group
   * stats from the result of the future.
   *
   * @see attachMddApiLogging
   */
  private interface StatsFromApiResultCreator<T> {
    DataDownloadFileGroupStats create(T result);
  }

  /**
   * Functional interface used as callback when logging API result. Used to get the API result code
   * from the result of the API future if it succeeds.
   *
   * <p>Note: The need for this is due to {@link addFileGroup} returning false instead of an
   * exception if it fails. For other APIs with proper exception handling, it should suffice to
   * immediately return the success code.
   *
   * <p>TODO(b/143572409): Remove once addGroupForDownload is updated to return void.
   *
   * @see attachMddApiLogging
   */
  private interface ResultCodeFromApiResultGetter<T> {
    int get(T result);
  }

  /**
   * Helper function used to log mdd api stats. Adds FutureCallback to the {@code resultFuture}
   * which is the result of mdd api call and logs in onSuccess and onFailure functions of callback.
   *
   * @param apiName Code of the api being logged.
   * @param resultFuture Future result of the api call.
   * @param startTimeNs start time in ns.
   * @param defaultFileGroupStats Initial file group stats.
   * @param statsCreator This functional interface is invoked from the onSuccess of FutureCallback
   *     with the result of the future. File group stats returned here is merged with the initial
   *     stats and logged.
   */
  private <T> void attachMddApiLogging(
      int apiName,
      ListenableFuture<T> resultFuture,
      long startTimeNs,
      DataDownloadFileGroupStats defaultFileGroupStats,
      StatsFromApiResultCreator<T> statsCreator,
      ResultCodeFromApiResultGetter<T> resultCodeGetter) {
    // Using listener instead of transform since we need to log even if the future fails.
    // Note: Listener is being registered on directexecutor for accurate latency measurement.
    resultFuture.addListener(
        propagateRunnable(
            () -> {
              long latencyNs = timeSource.elapsedRealtimeNanos() - startTimeNs;
              // Log the stats asynchronously.
              // Note: To avoid adding latency to mdd api calls, log asynchronously.
              var unused =
                  PropagatedFutures.submit(
                      () -> {
                        int resultCode;
                        T result = null;
                        DataDownloadFileGroupStats fileGroupStats = defaultFileGroupStats;
                        try {
                          result = Futures.getDone(resultFuture);
                          resultCode = resultCodeGetter.get(result);
                        } catch (Throwable t) {
                          resultCode = ExceptionToMddResultMapper.map(t);
                        }

                        // Merge stats created from result of api with the default stats.
                        if (result != null) {
                          fileGroupStats =
                              fileGroupStats.toBuilder()
                                  .mergeFrom(statsCreator.create(result))
                                  .build();
                        }

                        Void resultLog = null;

                        eventLogger.logMddLibApiResultLog(resultLog);
                      },
                      sequentialControlExecutor);
            }),
        directExecutor());
  }

  @Override
  public ListenableFuture<Boolean> addFileGroup(AddFileGroupRequest addFileGroupRequest) {
    long startTimeNs = timeSource.elapsedRealtimeNanos();

    ListenableFuture<Boolean> resultFuture =
        futureSerializer.submitAsync(
            () -> addFileGroupHelper(addFileGroupRequest), sequentialControlExecutor);

    DataDownloadFileGroupStats defaultFileGroupStats =
        DataDownloadFileGroupStats.newBuilder()
            .setFileGroupName(addFileGroupRequest.dataFileGroup().getGroupName())
            .setBuildId(addFileGroupRequest.dataFileGroup().getBuildId())
            .setVariantId(addFileGroupRequest.dataFileGroup().getVariantId())
            .setHasAccount(addFileGroupRequest.accountOptional().isPresent())
            .setFileGroupVersionNumber(
                addFileGroupRequest.dataFileGroup().getFileGroupVersionNumber())
            .setOwnerPackage(addFileGroupRequest.dataFileGroup().getOwnerPackage())
            .setFileCount(addFileGroupRequest.dataFileGroup().getFileCount())
            .build();
    attachMddApiLogging(
        0,
        resultFuture,
        startTimeNs,
        defaultFileGroupStats,
        /* statsCreator= */ unused -> defaultFileGroupStats,
        /* resultCodeGetter= */ succeeded -> succeeded ? 0 : 0);

    return resultFuture;
  }

  private ListenableFuture<Boolean> addFileGroupHelper(AddFileGroupRequest addFileGroupRequest) {
    LogUtil.d(
        "%s: Adding for download group = '%s', variant = '%s', buildId = '%d' and"
            + " associating it with account = '%s', variant = '%s'",
        TAG,
        addFileGroupRequest.dataFileGroup().getGroupName(),
        addFileGroupRequest.dataFileGroup().getVariantId(),
        addFileGroupRequest.dataFileGroup().getBuildId(),
        String.valueOf(addFileGroupRequest.accountOptional().orNull()),
        String.valueOf(addFileGroupRequest.variantIdOptional().orNull()));

    DataFileGroup dataFileGroup = addFileGroupRequest.dataFileGroup();

    // Ensure that the owner package is always set as the host app.
    if (!dataFileGroup.hasOwnerPackage()) {
      dataFileGroup = dataFileGroup.toBuilder().setOwnerPackage(context.getPackageName()).build();
    } else if (!context.getPackageName().equals(dataFileGroup.getOwnerPackage())) {
      LogUtil.e(
          "%s: Added group = '%s' with wrong owner package: '%s' v.s. '%s' ",
          TAG,
          dataFileGroup.getGroupName(),
          context.getPackageName(),
          dataFileGroup.getOwnerPackage());
      return immediateFuture(false);
    }

    GroupKey.Builder groupKeyBuilder =
        GroupKey.newBuilder()
            .setGroupName(dataFileGroup.getGroupName())
            .setOwnerPackage(dataFileGroup.getOwnerPackage());

    if (addFileGroupRequest.accountOptional().isPresent()) {
      groupKeyBuilder.setAccount(
          AccountUtil.serialize(addFileGroupRequest.accountOptional().get()));
    }

    if (addFileGroupRequest.variantIdOptional().isPresent()) {
      groupKeyBuilder.setVariantId(addFileGroupRequest.variantIdOptional().get());
    }

    try {
      DataFileGroupInternal dataFileGroupInternal = ProtoConversionUtil.convert(dataFileGroup);
      return mobileDataDownloadManager.addGroupForDownloadInternal(
          groupKeyBuilder.build(), dataFileGroupInternal, customFileGroupValidator);
    } catch (InvalidProtocolBufferException e) {
      // TODO(b/118137672): Consider rethrow exception instead of returning false.
      LogUtil.e(e, "%s: Unable to convert from DataFileGroup to DataFileGroupInternal.", TAG);
      return immediateFuture(false);
    }
  }

  // TODO: Change to return ListenableFuture<Void>.
  @Override
  public ListenableFuture<Boolean> removeFileGroup(RemoveFileGroupRequest removeFileGroupRequest) {
    return futureSerializer.submitAsync(
        () -> {
          GroupKey.Builder groupKeyBuilder =
              GroupKey.newBuilder()
                  .setGroupName(removeFileGroupRequest.groupName())
                  .setOwnerPackage(context.getPackageName());
          if (removeFileGroupRequest.accountOptional().isPresent()) {
            groupKeyBuilder.setAccount(
                AccountUtil.serialize(removeFileGroupRequest.accountOptional().get()));
          }
          if (removeFileGroupRequest.variantIdOptional().isPresent()) {
            groupKeyBuilder.setVariantId(removeFileGroupRequest.variantIdOptional().get());
          }

          GroupKey groupKey = groupKeyBuilder.build();
          return PropagatedFutures.transform(
              mobileDataDownloadManager.removeFileGroup(
                  groupKey, removeFileGroupRequest.pendingOnly()),
              voidArg -> true,
              sequentialControlExecutor);
        },
        sequentialControlExecutor);
  }

  @Override
  public ListenableFuture<RemoveFileGroupsByFilterResponse> removeFileGroupsByFilter(
      RemoveFileGroupsByFilterRequest removeFileGroupsByFilterRequest) {
    return futureSerializer.submitAsync(
        () ->
            PropagatedFluentFuture.from(mobileDataDownloadManager.getAllFreshGroups())
                .transformAsync(
                    allFreshGroupKeyAndGroups -> {
                      ImmutableSet.Builder<GroupKey> groupKeysToRemoveBuilder =
                          ImmutableSet.builder();
                      for (GroupKeyAndGroup groupKeyAndGroup : allFreshGroupKeyAndGroups) {
                        if (applyRemoveFileGroupsFilter(
                            removeFileGroupsByFilterRequest, groupKeyAndGroup)) {
                          // Remove downloaded status so pending/downloaded versions of the same
                          // group are treated as one.
                          groupKeysToRemoveBuilder.add(
                              groupKeyAndGroup.groupKey().toBuilder().clearDownloaded().build());
                        }
                      }
                      ImmutableSet<GroupKey> groupKeysToRemove = groupKeysToRemoveBuilder.build();
                      if (groupKeysToRemove.isEmpty()) {
                        return immediateFuture(
                            RemoveFileGroupsByFilterResponse.newBuilder()
                                .setRemovedFileGroupsCount(0)
                                .build());
                      }
                      return PropagatedFutures.transform(
                          mobileDataDownloadManager.removeFileGroups(groupKeysToRemove.asList()),
                          unused ->
                              RemoveFileGroupsByFilterResponse.newBuilder()
                                  .setRemovedFileGroupsCount(groupKeysToRemove.size())
                                  .build(),
                          sequentialControlExecutor);
                    },
                    sequentialControlExecutor),
        sequentialControlExecutor);
  }

  // Perform filtering using options from RemoveFileGroupsByFilterRequest
  private static boolean applyRemoveFileGroupsFilter(
      RemoveFileGroupsByFilterRequest removeFileGroupsByFilterRequest,
      GroupKeyAndGroup groupKeyAndGroup) {
    // If request filters by account, ensure account is present and is equal
    Optional<Account> accountOptional = removeFileGroupsByFilterRequest.accountOptional();
    if (!accountOptional.isPresent() && groupKeyAndGroup.groupKey().hasAccount()) {
      // Account must explicitly be provided in order to remove account associated file groups.
      return false;
    }
    if (accountOptional.isPresent()
        && !AccountUtil.serialize(accountOptional.get())
            .equals(groupKeyAndGroup.groupKey().getAccount())) {
      return false;
    }

    return true;
  }

  /**
   * Helper function to create {@link DataDownloadFileGroupStats} object from {@link
   * GetFileGroupRequest} for getFileGroup() logging.
   *
   * <p>Used when the matching file group is not found or a failure occurred.
   * file_group_version_number and build_id are set to -1 by default.
   */
  private DataDownloadFileGroupStats createFileGroupStatsFromGetFileGroupRequest(
      GetFileGroupRequest getFileGroupRequest) {
    DataDownloadFileGroupStats.Builder fileGroupStatsBuilder =
        DataDownloadFileGroupStats.newBuilder();
    fileGroupStatsBuilder.setFileGroupName(getFileGroupRequest.groupName());
    if (getFileGroupRequest.variantIdOptional().isPresent()) {
      fileGroupStatsBuilder.setVariantId(getFileGroupRequest.variantIdOptional().get());
    }
    if (getFileGroupRequest.accountOptional().isPresent()) {
      fileGroupStatsBuilder.setHasAccount(true);
    } else {
      fileGroupStatsBuilder.setHasAccount(false);
    }

    fileGroupStatsBuilder.setFileGroupVersionNumber(
        MddConstants.FILE_GROUP_NOT_FOUND_FILE_GROUP_VERSION_NUMBER);
    fileGroupStatsBuilder.setBuildId(MddConstants.FILE_GROUP_NOT_FOUND_BUILD_ID);

    return fileGroupStatsBuilder.build();
  }

  // TODO: Futures.immediateFuture(null) uses a different annotation for Nullable.
  @SuppressWarnings("nullness")
  @Override
  public ListenableFuture<ClientFileGroup> getFileGroup(GetFileGroupRequest getFileGroupRequest) {
    long startTimeNs = timeSource.elapsedRealtimeNanos();

    ListenableFuture<ClientFileGroup> resultFuture =
        futureSerializer.submitAsync(
            () -> {
              GroupKey groupKey =
                  createGroupKey(
                      getFileGroupRequest.groupName(),
                      getFileGroupRequest.accountOptional(),
                      getFileGroupRequest.variantIdOptional());
              return PropagatedFutures.transformAsync(
                  mobileDataDownloadManager.getFileGroup(groupKey, /* downloaded= */ true),
                  dataFileGroup ->
                      createClientFileGroupAndLogQueryStats(
                          groupKey,
                          dataFileGroup,
                          /* downloaded= */ true,
                          getFileGroupRequest.preserveZipDirectories(),
                          getFileGroupRequest.verifyIsolatedStructure()),
                  sequentialControlExecutor);
            },
            sequentialControlExecutor);

    attachMddApiLogging(
        0,
        resultFuture,
        startTimeNs,
        createFileGroupStatsFromGetFileGroupRequest(getFileGroupRequest),
        /* statsCreator= */ result -> createFileGroupDetails(result),
        /* resultCodeGetter= */ unused -> 0);
    return resultFuture;
  }

  @SuppressWarnings("nullness")
  @Override
  public ListenableFuture<DataFileGroup> readDataFileGroup(
      ReadDataFileGroupRequest readDataFileGroupRequest) {
    return futureSerializer.submitAsync(
        () -> {
          GroupKey groupKey =
              createGroupKey(
                  readDataFileGroupRequest.groupName(),
                  readDataFileGroupRequest.accountOptional(),
                  readDataFileGroupRequest.variantIdOptional());
          return PropagatedFutures.transformAsync(
              mobileDataDownloadManager.getFileGroup(groupKey, /* downloaded= */ true),
              internalFileGroup -> immediateFuture(ProtoConversionUtil.reverse(internalFileGroup)),
              sequentialControlExecutor);
        },
        sequentialControlExecutor);
  }

  private GroupKey createGroupKey(
      String groupName, Optional<Account> accountOptional, Optional<String> variantOptional) {
    GroupKey.Builder groupKeyBuilder =
        GroupKey.newBuilder().setGroupName(groupName).setOwnerPackage(context.getPackageName());

    if (accountOptional.isPresent()) {
      groupKeyBuilder.setAccount(AccountUtil.serialize(accountOptional.get()));
    }

    if (variantOptional.isPresent()) {
      groupKeyBuilder.setVariantId(variantOptional.get());
    }

    return groupKeyBuilder.build();
  }

  private ListenableFuture<ClientFileGroup> createClientFileGroupAndLogQueryStats(
      GroupKey groupKey,
      @Nullable DataFileGroupInternal dataFileGroup,
      boolean downloaded,
      boolean preserveZipDirectories,
      boolean verifyIsolatedStructure) {
    return PropagatedFutures.transform(
        createClientFileGroup(
            dataFileGroup,
            groupKey.hasAccount() ? groupKey.getAccount() : null,
            downloaded ? ClientFileGroup.Status.DOWNLOADED : ClientFileGroup.Status.PENDING,
            preserveZipDirectories,
            verifyIsolatedStructure,
            mobileDataDownloadManager,
            sequentialControlExecutor,
            fileStorage),
        clientFileGroup -> {
          if (clientFileGroup != null) {
            eventLogger.logMddQueryStats(createFileGroupDetails(clientFileGroup));
          }
          return clientFileGroup;
        },
        sequentialControlExecutor);
  }

  @SuppressWarnings("nullness")
  private static ListenableFuture<ClientFileGroup> createClientFileGroup(
      @Nullable DataFileGroupInternal dataFileGroup,
      @Nullable String account,
      ClientFileGroup.Status status,
      boolean preserveZipDirectories,
      boolean verifyIsolatedStructure,
      MobileDataDownloadManager manager,
      Executor executor,
      SynchronousFileStorage fileStorage) {
    if (dataFileGroup == null) {
      return immediateFuture(null);
    }
    ClientFileGroup.Builder clientFileGroupBuilder =
        ClientFileGroup.newBuilder()
            .setGroupName(dataFileGroup.getGroupName())
            .setOwnerPackage(dataFileGroup.getOwnerPackage())
            .setVersionNumber(dataFileGroup.getFileGroupVersionNumber())
//            .setCustomProperty(dataFileGroup.getCustomProperty())
            .setBuildId(dataFileGroup.getBuildId())
            .setVariantId(dataFileGroup.getVariantId())
            .setStatus(status)
            .addAllLocale(dataFileGroup.getLocaleList());

    if (account != null) {
      clientFileGroupBuilder.setAccount(account);
    }

    if (dataFileGroup.hasCustomMetadata()) {
      clientFileGroupBuilder.setCustomMetadata(dataFileGroup.getCustomMetadata());
    }

    List<DataFile> dataFiles = dataFileGroup.getFileList();
    ListenableFuture<Void> addOnDeviceUrisFuture = immediateVoidFuture();
    if (status == ClientFileGroup.Status.DOWNLOADED
        || status == ClientFileGroup.Status.PENDING_CUSTOM_VALIDATION) {
      addOnDeviceUrisFuture =
          PropagatedFluentFuture.from(
                  manager.getDataFileUris(dataFileGroup, verifyIsolatedStructure))
              .transformAsync(
                  dataFileUriMap -> {
                    for (DataFile dataFile : dataFiles) {
                      if (!dataFileUriMap.containsKey(dataFile)) {
                        return immediateFailedFuture(
                            DownloadException.builder()
                                .setDownloadResultCode(
                                    DownloadResultCode.DOWNLOADED_FILE_NOT_FOUND_ERROR)
                                .setMessage("getDataFileUris() resolved to null")
                                .build());
                      }
                      Uri uri = dataFileUriMap.get(dataFile);

                      try {
                        if (!preserveZipDirectories && fileStorage.isDirectory(uri)) {
                          String rootPath = uri.getPath();
                          if (rootPath != null) {
                            clientFileGroupBuilder.addAllFile(
                                listAllClientFilesOfDirectory(fileStorage, uri, rootPath));
                          }
                        } else {
                          clientFileGroupBuilder.addFile(
                              createClientFile(
                                  dataFile.getFileId(),
                                  dataFile.getByteSize(),
                                  dataFile.getDownloadedFileByteSize(),
                                  uri.toString(),
                                  dataFile.hasCustomMetadata()
                                      ? dataFile.getCustomMetadata()
                                      : null));
                        }
                      } catch (IOException e) {
                        LogUtil.e(e, "Failed to list files under directory:" + uri);
                      }
                    }
                    return immediateVoidFuture();
                  },
                  executor);
    } else {
      for (DataFile dataFile : dataFiles) {
        clientFileGroupBuilder.addFile(
            createClientFile(
                dataFile.getFileId(),
                dataFile.getByteSize(),
                dataFile.getDownloadedFileByteSize(),
                /* uri= */ null,
                dataFile.hasCustomMetadata() ? dataFile.getCustomMetadata() : null));
      }
    }

    return PropagatedFluentFuture.from(addOnDeviceUrisFuture)
        .transform(unused -> clientFileGroupBuilder.build(), executor)
        .catching(DownloadException.class, exn -> null, executor);
  }

  private static ClientFile createClientFile(
      String fileId,
      int byteSize,
      int downloadByteSize,
      @Nullable String uri,
      @Nullable Any customMetadata) {
    ClientFile.Builder clientFileBuilder =
        ClientFile.newBuilder().setFileId(fileId).setFullSizeInBytes(byteSize);
    if (downloadByteSize > 0) {
      // Files with downloaded transforms like compress and zip could have different downloaded
      // file size than the final file size on disk. Return the downloaded file size for client to
      // track and calculate the download progress.
      clientFileBuilder.setDownloadSizeInBytes(downloadByteSize);
    }
    if (uri != null) {
      clientFileBuilder.setFileUri(uri);
    }
    if (customMetadata != null) {
      clientFileBuilder.setCustomMetadata(customMetadata);
    }
    return clientFileBuilder.build();
  }

  private static List<ClientFile> listAllClientFilesOfDirectory(
      SynchronousFileStorage fileStorage, Uri dirUri, String rootDir) throws IOException {
    List<ClientFile> clientFileList = new ArrayList<>();
    for (Uri childUri : fileStorage.children(dirUri)) {
      if (fileStorage.isDirectory(childUri)) {
        clientFileList.addAll(listAllClientFilesOfDirectory(fileStorage, childUri, rootDir));
      } else {
        String childPath = childUri.getPath();
        if (childPath != null) {
          ClientFile clientFile =
              ClientFile.newBuilder()
                  .setFileId(childPath.replaceFirst(rootDir, ""))
                  .setFullSizeInBytes((int) fileStorage.fileSize(childUri))
                  .setFileUri(childUri.toString())
                  .build();
          clientFileList.add(clientFile);
        }
      }
    }
    return clientFileList;
  }

  @Override
  public ListenableFuture<ImmutableList<ClientFileGroup>> getFileGroupsByFilter(
      GetFileGroupsByFilterRequest getFileGroupsByFilterRequest) {
    return futureSerializer.submitAsync(
        () ->
            PropagatedFutures.transformAsync(
                mobileDataDownloadManager.getAllFreshGroups(),
                allFreshGroupKeyAndGroups -> {
                  ListenableFuture<ImmutableList.Builder<ClientFileGroup>>
                      clientFileGroupsBuilderFuture =
                          immediateFuture(ImmutableList.<ClientFileGroup>builder());
                  for (GroupKeyAndGroup groupKeyAndGroup : allFreshGroupKeyAndGroups) {
                    clientFileGroupsBuilderFuture =
                        PropagatedFutures.transformAsync(
                            clientFileGroupsBuilderFuture,
                            clientFileGroupsBuilder -> {
                              GroupKey groupKey = groupKeyAndGroup.groupKey();
                              DataFileGroupInternal dataFileGroup =
                                  groupKeyAndGroup.dataFileGroup();
                              if (applyFilter(
                                  getFileGroupsByFilterRequest, groupKey, dataFileGroup)) {
                                return PropagatedFutures.transform(
                                    createClientFileGroupAndLogQueryStats(
                                        groupKey,
                                        dataFileGroup,
                                        groupKey.getDownloaded(),
                                        getFileGroupsByFilterRequest.preserveZipDirectories(),
                                        getFileGroupsByFilterRequest.verifyIsolatedStructure()),
                                    clientFileGroup -> {
                                      if (clientFileGroup != null) {
                                        clientFileGroupsBuilder.add(clientFileGroup);
                                      }
                                      return clientFileGroupsBuilder;
                                    },
                                    sequentialControlExecutor);
                              }
                              return immediateFuture(clientFileGroupsBuilder);
                            },
                            sequentialControlExecutor);
                  }

                  return PropagatedFutures.transform(
                      clientFileGroupsBuilderFuture,
                      ImmutableList.Builder::build,
                      sequentialControlExecutor);
                },
                sequentialControlExecutor),
        sequentialControlExecutor);
  }

  // Perform filtering using options from GetFileGroupsByFilterRequest
  private static boolean applyFilter(
      GetFileGroupsByFilterRequest getFileGroupsByFilterRequest,
      GroupKey groupKey,
      DataFileGroupInternal fileGroup) {
    if (getFileGroupsByFilterRequest.includeAllGroups()) {
      return true;
    }

    // If request filters by group name, ensure name is equal
    Optional<String> groupNameOptional = getFileGroupsByFilterRequest.groupNameOptional();
    if (groupNameOptional.isPresent()
        && !TextUtils.equals(groupNameOptional.get(), groupKey.getGroupName())) {
      return false;
    }

    // When the caller requests account independent groups only.
    if (getFileGroupsByFilterRequest.groupWithNoAccountOnly()) {
      return !groupKey.hasAccount();
    }

    // When the caller requests account dependent groups as well.
    if (getFileGroupsByFilterRequest.accountOptional().isPresent()
        && !AccountUtil.serialize(getFileGroupsByFilterRequest.accountOptional().get())
            .equals(groupKey.getAccount())) {
      return false;
    }
    return true;
  }

  /**
   * Creates {@link DataDownloadFileGroupStats} from {@link ClientFileGroup} for remote logging
   * purposes.
   */
  private static DataDownloadFileGroupStats createFileGroupDetails(
      ClientFileGroup clientFileGroup) {
    return DataDownloadFileGroupStats.newBuilder()
        .setFileGroupName(clientFileGroup.getGroupName())
        .setOwnerPackage(clientFileGroup.getOwnerPackage())
        .setFileGroupVersionNumber(clientFileGroup.getVersionNumber())
        .setFileCount(clientFileGroup.getFileCount())
        .setVariantId(clientFileGroup.getVariantId())
        .setBuildId(clientFileGroup.getBuildId())
        .build();
  }

  @Override
  public ListenableFuture<Void> importFiles(ImportFilesRequest importFilesRequest) {
    GroupKey.Builder groupKeyBuilder =
        GroupKey.newBuilder()
            .setGroupName(importFilesRequest.groupName())
            .setOwnerPackage(context.getPackageName());

    if (importFilesRequest.accountOptional().isPresent()) {
      groupKeyBuilder.setAccount(AccountUtil.serialize(importFilesRequest.accountOptional().get()));
    }

    GroupKey groupKey = groupKeyBuilder.build();

    ImmutableList.Builder<DataFile> updatedDataFileListBuilder =
        ImmutableList.builderWithExpectedSize(importFilesRequest.updatedDataFileList().size());
    for (DownloadConfigProto.DataFile dataFile : importFilesRequest.updatedDataFileList()) {
      updatedDataFileListBuilder.add(ProtoConversionUtil.convertDataFile(dataFile));
    }

    return futureSerializer.submitAsync(
        () ->
            mobileDataDownloadManager.importFiles(
                groupKey,
                importFilesRequest.buildId(),
                importFilesRequest.variantId(),
                updatedDataFileListBuilder.build(),
                importFilesRequest.inlineFileMap(),
                importFilesRequest.customPropertyOptional(),
                customFileGroupValidator),
        sequentialControlExecutor);
  }

  @Override
  public ListenableFuture<Void> downloadFile(SingleFileDownloadRequest singleFileDownloadRequest) {
    return singleFileDownloader.download(
        MddLiteConversionUtil.convertToDownloadRequest(singleFileDownloadRequest));
  }

  @Override
  public ListenableFuture<ClientFileGroup> downloadFileGroup(
      DownloadFileGroupRequest downloadFileGroupRequest) {
    // Submit the call to sequentialControlExecutor, but don't use futureSerializer. This will
    // ensure that multiple calls are enqueued to the executor in a FIFO order, but these calls
    // won't block each other when the download is in progress.
    return PropagatedFutures.submitAsync(
        () ->
            PropagatedFutures.transformAsync(
                // Check if requested file group has already been downloaded
                getDownloadGroupState(downloadFileGroupRequest),
                downloadGroupState -> {
                  switch (downloadGroupState.getKind()) {
                    case IN_PROGRESS_FUTURE:
                      // If the file group download is in progress, return that future immediately
                      return downloadGroupState.inProgressFuture();
                    case DOWNLOADED_GROUP:
                      // If the file group is already downloaded, return that immediately.
                      return immediateFuture(downloadGroupState.downloadedGroup());
                    case PENDING_GROUP:
                      return downloadPendingFileGroup(downloadFileGroupRequest);
                  }
                  throw new AssertionError(
                      String.format(
                          "received unsupported DownloadGroupState kind %s",
                          downloadGroupState.getKind()));
                },
                sequentialControlExecutor),
        sequentialControlExecutor);
  }

  /** Helper method to download a group after it's determined to be pending. */
  private ListenableFuture<ClientFileGroup> downloadPendingFileGroup(
      DownloadFileGroupRequest downloadFileGroupRequest) {
    String groupName = downloadFileGroupRequest.groupName();
    GroupKey.Builder groupKeyBuilder =
        GroupKey.newBuilder().setGroupName(groupName).setOwnerPackage(context.getPackageName());

    if (downloadFileGroupRequest.accountOptional().isPresent()) {
      groupKeyBuilder.setAccount(
          AccountUtil.serialize(downloadFileGroupRequest.accountOptional().get()));
    }
    if (downloadFileGroupRequest.variantIdOptional().isPresent()) {
      groupKeyBuilder.setVariantId(downloadFileGroupRequest.variantIdOptional().get());
    }

    GroupKey groupKey = groupKeyBuilder.build();

    if (downloadFileGroupRequest.listenerOptional().isPresent()) {
      if (downloadMonitorOptional.isPresent()) {
        downloadMonitorOptional
            .get()
            .addDownloadListener(groupName, downloadFileGroupRequest.listenerOptional().get());
      } else {
        return immediateFailedFuture(
            DownloadException.builder()
                .setDownloadResultCode(DownloadResultCode.DOWNLOAD_MONITOR_NOT_PROVIDED_ERROR)
                .setMessage(
                    "downloadFileGroup: DownloadListener is present but Download Monitor"
                        + " is not provided!")
                .build());
      }
    }

    Optional<DownloadConditions> downloadConditions;
    try {
      downloadConditions =
          downloadFileGroupRequest.downloadConditionsOptional().isPresent()
              ? Optional.of(
                  ProtoConversionUtil.convert(
                      downloadFileGroupRequest.downloadConditionsOptional().get()))
              : Optional.absent();
    } catch (InvalidProtocolBufferException e) {
      return immediateFailedFuture(e);
    }

    // Get the key used for the download future map
    ForegroundDownloadKey downloadKey =
        ForegroundDownloadKey.ofFileGroup(
            downloadFileGroupRequest.groupName(),
            downloadFileGroupRequest.accountOptional(),
            downloadFileGroupRequest.variantIdOptional());

    // Create a ListenableFutureTask to delay starting the downloadFuture until we can add the
    // future to our map.
    ListenableFutureTask<Void> startTask = ListenableFutureTask.create(() -> null);
    ListenableFuture<ClientFileGroup> downloadFuture =
        PropagatedFluentFuture.from(startTask)
            .transformAsync(
                unused ->
                    mobileDataDownloadManager.downloadFileGroup(
                        groupKey, downloadConditions, customFileGroupValidator),
                sequentialControlExecutor)
            .transformAsync(
                dataFileGroup ->
                    createClientFileGroup(
                        dataFileGroup,
                        downloadFileGroupRequest.accountOptional().isPresent()
                            ? AccountUtil.serialize(
                                downloadFileGroupRequest.accountOptional().get())
                            : null,
                        ClientFileGroup.Status.DOWNLOADED,
                        downloadFileGroupRequest.preserveZipDirectories(),
                        downloadFileGroupRequest.verifyIsolatedStructure(),
                        mobileDataDownloadManager,
                        sequentialControlExecutor,
                        fileStorage),
                sequentialControlExecutor)
            .transform(Preconditions::checkNotNull, sequentialControlExecutor);

    // Get a handle on the download task so we can get the CFG during transforms
    PropagatedFluentFuture<ClientFileGroup> downloadTaskFuture =
        PropagatedFluentFuture.from(downloadFutureMap.add(downloadKey.toString(), downloadFuture))
            .transformAsync(
                unused -> {
                  // Now that the download future is added, start the task and return the future
                  startTask.run();
                  return downloadFuture;
                },
                sequentialControlExecutor);

    ListenableFuture<ClientFileGroup> transformFuture =
        downloadTaskFuture
            .transformAsync(
                unused -> downloadFutureMap.remove(downloadKey.toString()),
                sequentialControlExecutor)
            .transformAsync(
                unused -> {
                  ClientFileGroup clientFileGroup = getDone(downloadTaskFuture);

                  if (downloadFileGroupRequest.listenerOptional().isPresent()) {
                    try {
                      downloadFileGroupRequest.listenerOptional().get().onComplete(clientFileGroup);
                    } catch (Exception e) {
                      LogUtil.w(
                          e,
                          "%s: Listener onComplete failed for group %s",
                          TAG,
                          clientFileGroup.getGroupName());
                    }
                    if (downloadMonitorOptional.isPresent()) {
                      downloadMonitorOptional.get().removeDownloadListener(groupName);
                    }
                  }
                  return immediateFuture(clientFileGroup);
                },
                sequentialControlExecutor);

    PropagatedFutures.addCallback(
        transformFuture,
        new FutureCallback<ClientFileGroup>() {
          @Override
          public void onSuccess(ClientFileGroup result) {}

          @Override
          public void onFailure(Throwable t) {
            if (downloadFileGroupRequest.listenerOptional().isPresent()) {
              downloadFileGroupRequest.listenerOptional().get().onFailure(t);

              if (downloadMonitorOptional.isPresent()) {
                downloadMonitorOptional.get().removeDownloadListener(groupName);
              }
            }

            // Remove future from map
            ListenableFuture<Void> unused = downloadFutureMap.remove(downloadKey.toString());
          }
        },
        sequentialControlExecutor);

    return transformFuture;
  }

  @Override
  public ListenableFuture<Void> downloadFileWithForegroundService(
      SingleFileDownloadRequest singleFileDownloadRequest) {
    return singleFileDownloader.downloadWithForegroundService(
        MddLiteConversionUtil.convertToDownloadRequest(singleFileDownloadRequest));
  }

  @Override
  public ListenableFuture<ClientFileGroup> downloadFileGroupWithForegroundService(
      DownloadFileGroupRequest downloadFileGroupRequest) {
    LogUtil.d("%s: downloadFileGroupWithForegroundService start.", TAG);
    if (!foregroundDownloadServiceClassOptional.isPresent()) {
      return immediateFailedFuture(
          new IllegalArgumentException(
              "downloadFileGroupWithForegroundService: ForegroundDownloadService is not"
                  + " provided!"));
    }

    if (!downloadMonitorOptional.isPresent()) {
      return immediateFailedFuture(
          DownloadException.builder()
              .setDownloadResultCode(DownloadResultCode.DOWNLOAD_MONITOR_NOT_PROVIDED_ERROR)
              .setMessage(
                  "downloadFileGroupWithForegroundService: Download Monitor is not provided!")
              .build());
    }

    // Submit the call to sequentialControlExecutor, but don't use futureSerializer. This will
    // ensure that multiple calls are enqueued to the executor in a FIFO order, but these calls
    // won't block each other when the download is in progress.
    return PropagatedFutures.submitAsync(
        () ->
            PropagatedFutures.transformAsync(
                // Check if requested file group has already been downloaded
                getDownloadGroupState(downloadFileGroupRequest),
                downloadGroupState -> {
                  switch (downloadGroupState.getKind()) {
                    case IN_PROGRESS_FUTURE:
                      // If the file group download is in progress, return that future immediately
                      return downloadGroupState.inProgressFuture();
                    case DOWNLOADED_GROUP:
                      // If the file group is already downloaded, return that immediately
                      return immediateFuture(downloadGroupState.downloadedGroup());
                    case PENDING_GROUP:
                      return downloadPendingFileGroupWithForegroundService(
                          downloadFileGroupRequest, downloadGroupState.pendingGroup());
                  }
                  throw new AssertionError(
                      String.format(
                          "received unsupported DownloadGroupState kind %s",
                          downloadGroupState.getKind()));
                },
                sequentialControlExecutor),
        sequentialControlExecutor);
  }

  /**
   * Helper method to download a file group in the foreground after it has been confirmed to be
   * pending.
   */
  private ListenableFuture<ClientFileGroup> downloadPendingFileGroupWithForegroundService(
      DownloadFileGroupRequest downloadFileGroupRequest, DataFileGroupInternal pendingGroup) {
    // It's OK to recreate the NotificationChannel since it can also be used to restore a
    // deleted channel and to update an existing channel's name, description, group, and/or
    // importance.
    NotificationUtil.createNotificationChannel(context);

    String groupName = downloadFileGroupRequest.groupName();
    GroupKey.Builder groupKeyBuilder =
        GroupKey.newBuilder().setGroupName(groupName).setOwnerPackage(context.getPackageName());

    if (downloadFileGroupRequest.accountOptional().isPresent()) {
      groupKeyBuilder.setAccount(
          AccountUtil.serialize(downloadFileGroupRequest.accountOptional().get()));
    }
    if (downloadFileGroupRequest.variantIdOptional().isPresent()) {
      groupKeyBuilder.setVariantId(downloadFileGroupRequest.variantIdOptional().get());
    }

    GroupKey groupKey = groupKeyBuilder.build();
    ForegroundDownloadKey foregroundDownloadKey =
        ForegroundDownloadKey.ofFileGroup(
            groupName,
            downloadFileGroupRequest.accountOptional(),
            downloadFileGroupRequest.variantIdOptional());

    DownloadListener downloadListenerWithNotification =
        createDownloadListenerWithNotification(downloadFileGroupRequest, pendingGroup);
    // The downloadMonitor will trigger the DownloadListener.
    downloadMonitorOptional
        .get()
        .addDownloadListener(
            downloadFileGroupRequest.groupName(), downloadListenerWithNotification);

    Optional<DownloadConditions> downloadConditions;
    try {
      downloadConditions =
          downloadFileGroupRequest.downloadConditionsOptional().isPresent()
              ? Optional.of(
                  ProtoConversionUtil.convert(
                      downloadFileGroupRequest.downloadConditionsOptional().get()))
              : Optional.absent();
    } catch (InvalidProtocolBufferException e) {
      return immediateFailedFuture(e);
    }

    // Create a ListenableFutureTask to delay starting the downloadFuture until we can add the
    // future to our map.
    ListenableFutureTask<Void> startTask = ListenableFutureTask.create(() -> null);
    PropagatedFluentFuture<ClientFileGroup> downloadFileGroupFuture =
        PropagatedFluentFuture.from(startTask)
            .transformAsync(
                unused ->
                    mobileDataDownloadManager.downloadFileGroup(
                        groupKey, downloadConditions, customFileGroupValidator),
                sequentialControlExecutor)
            .transformAsync(
                dataFileGroup ->
                    createClientFileGroup(
                        dataFileGroup,
                        downloadFileGroupRequest.accountOptional().isPresent()
                            ? AccountUtil.serialize(
                                downloadFileGroupRequest.accountOptional().get())
                            : null,
                        ClientFileGroup.Status.DOWNLOADED,
                        downloadFileGroupRequest.preserveZipDirectories(),
                        downloadFileGroupRequest.verifyIsolatedStructure(),
                        mobileDataDownloadManager,
                        sequentialControlExecutor,
                        fileStorage),
                sequentialControlExecutor)
            .transform(Preconditions::checkNotNull, sequentialControlExecutor);

    ListenableFuture<ClientFileGroup> transformFuture =
        PropagatedFutures.transformAsync(
            foregroundDownloadFutureMap.add(
                foregroundDownloadKey.toString(), downloadFileGroupFuture),
            unused -> {
              // Now that the download future is added, start the task and return the future
              startTask.run();
              return downloadFileGroupFuture;
            },
            sequentialControlExecutor);

    PropagatedFutures.addCallback(
        transformFuture,
        new FutureCallback<ClientFileGroup>() {
          @Override
          public void onSuccess(ClientFileGroup clientFileGroup) {
            // Currently the MobStore monitor does not support onSuccess so we have to add
            // callback to the download future here.
            try {
              downloadListenerWithNotification.onComplete(clientFileGroup);
            } catch (Exception e) {
              LogUtil.w(
                  e,
                  "%s: Listener onComplete failed for group %s",
                  TAG,
                  clientFileGroup.getGroupName());
            }
          }

          @Override
          public void onFailure(Throwable t) {
            // Currently the MobStore monitor does not support onFailure so we have to add
            // callback to the download future here.
            downloadListenerWithNotification.onFailure(t);
          }
        },
        sequentialControlExecutor);

    return transformFuture;
  }

  /** Helper method to return a {@link DownloadGroupState} for the given request. */
  private ListenableFuture<DownloadGroupState> getDownloadGroupState(
      DownloadFileGroupRequest downloadFileGroupRequest) {
    ForegroundDownloadKey foregroundDownloadKey =
        ForegroundDownloadKey.ofFileGroup(
            downloadFileGroupRequest.groupName(),
            downloadFileGroupRequest.accountOptional(),
            downloadFileGroupRequest.variantIdOptional());

    String groupName = downloadFileGroupRequest.groupName();
    GroupKey.Builder groupKeyBuilder =
        GroupKey.newBuilder().setGroupName(groupName).setOwnerPackage(context.getPackageName());

    if (downloadFileGroupRequest.accountOptional().isPresent()) {
      groupKeyBuilder.setAccount(
          AccountUtil.serialize(downloadFileGroupRequest.accountOptional().get()));
    }

    if (downloadFileGroupRequest.variantIdOptional().isPresent()) {
      groupKeyBuilder.setVariantId(downloadFileGroupRequest.variantIdOptional().get());
    }

    boolean isDownloadListenerPresent = downloadFileGroupRequest.listenerOptional().isPresent();
    GroupKey groupKey = groupKeyBuilder.build();

    return futureSerializer.submitAsync(
        () -> {
          ListenableFuture<Optional<ListenableFuture<ClientFileGroup>>>
              foregroundDownloadFutureOptional =
                  foregroundDownloadFutureMap.get(foregroundDownloadKey.toString());
          ListenableFuture<Optional<ListenableFuture<ClientFileGroup>>>
              backgroundDownloadFutureOptional =
                  downloadFutureMap.get(foregroundDownloadKey.toString());

          return PropagatedFutures.whenAllSucceed(
                  foregroundDownloadFutureOptional, backgroundDownloadFutureOptional)
              .callAsync(
                  () -> {
                    if (getDone(foregroundDownloadFutureOptional).isPresent()) {
                      return immediateFuture(
                          DownloadGroupState.ofInProgressFuture(
                              getDone(foregroundDownloadFutureOptional).get()));
                    } else if (getDone(backgroundDownloadFutureOptional).isPresent()) {
                      return immediateFuture(
                          DownloadGroupState.ofInProgressFuture(
                              getDone(backgroundDownloadFutureOptional).get()));
                    }

                    // Get pending and downloaded versions to tell if we should return downloaded
                    // version early
                    ListenableFuture<GroupPair> fileGroupVersionsFuture =
                        PropagatedFutures.transformAsync(
                            mobileDataDownloadManager.getFileGroup(
                                groupKey, /* downloaded= */ false),
                            pendingDataFileGroup ->
                                PropagatedFutures.transform(
                                    mobileDataDownloadManager.getFileGroup(
                                        groupKey, /* downloaded= */ true),
                                    downloadedDataFileGroup ->
                                        GroupPair.create(
                                            pendingDataFileGroup, downloadedDataFileGroup),
                                    sequentialControlExecutor),
                            sequentialControlExecutor);

                    return PropagatedFutures.transformAsync(
                        fileGroupVersionsFuture,
                        fileGroupVersionsPair -> {
                          // if pending version is not null, return pending version
                          if (fileGroupVersionsPair.pendingGroup() != null) {
                            return immediateFuture(
                                DownloadGroupState.ofPendingGroup(
                                    checkNotNull(fileGroupVersionsPair.pendingGroup())));
                          }
                          // If both groups are null, return group not found failure
                          if (fileGroupVersionsPair.downloadedGroup() == null) {
                            // TODO(b/174808410): Add Logging
                            // file group is not pending nor downloaded -- return failure.
                            DownloadException failure =
                                DownloadException.builder()
                                    .setDownloadResultCode(DownloadResultCode.GROUP_NOT_FOUND_ERROR)
                                    .setMessage(
                                        "Nothing to download for file group: "
                                            + groupKey.getGroupName())
                                    .build();
                            if (isDownloadListenerPresent) {
                              downloadFileGroupRequest.listenerOptional().get().onFailure(failure);
                            }
                            return immediateFailedFuture(failure);
                          }

                          DataFileGroupInternal downloadedDataFileGroup =
                              checkNotNull(fileGroupVersionsPair.downloadedGroup());

                          // Notify download listener (if present) that file group has been
                          // downloaded.
                          if (isDownloadListenerPresent) {
                            downloadMonitorOptional
                                .get()
                                .addDownloadListener(
                                    downloadFileGroupRequest.groupName(),
                                    downloadFileGroupRequest.listenerOptional().get());
                          }
                          PropagatedFluentFuture<ClientFileGroup> transformFuture =
                              PropagatedFluentFuture.from(
                                      createClientFileGroup(
                                          downloadedDataFileGroup,
                                          downloadFileGroupRequest.accountOptional().isPresent()
                                              ? AccountUtil.serialize(
                                                  downloadFileGroupRequest.accountOptional().get())
                                              : null,
                                          ClientFileGroup.Status.DOWNLOADED,
                                          downloadFileGroupRequest.preserveZipDirectories(),
                                          downloadFileGroupRequest.verifyIsolatedStructure(),
                                          mobileDataDownloadManager,
                                          sequentialControlExecutor,
                                          fileStorage))
                                  .transform(Preconditions::checkNotNull, sequentialControlExecutor)
                                  .transform(
                                      clientFileGroup -> {
                                        if (isDownloadListenerPresent) {
                                          try {
                                            downloadFileGroupRequest
                                                .listenerOptional()
                                                .get()
                                                .onComplete(clientFileGroup);
                                          } catch (Exception e) {
                                            LogUtil.w(
                                                e,
                                                "%s: Listener onComplete failed for group %s",
                                                TAG,
                                                clientFileGroup.getGroupName());
                                          }
                                          downloadMonitorOptional
                                              .get()
                                              .removeDownloadListener(groupName);
                                        }
                                        return clientFileGroup;
                                      },
                                      sequentialControlExecutor);
                          transformFuture.addCallback(
                              new FutureCallback<ClientFileGroup>() {
                                @Override
                                public void onSuccess(ClientFileGroup result) {}

                                @Override
                                public void onFailure(Throwable t) {
                                  if (isDownloadListenerPresent) {
                                    downloadMonitorOptional.get().removeDownloadListener(groupName);
                                  }
                                }
                              },
                              sequentialControlExecutor);

                          // Use directExecutor here since we are performing a trivial operation.
                          return transformFuture.transform(
                              DownloadGroupState::ofDownloadedGroup, directExecutor());
                        },
                        sequentialControlExecutor);
                  },
                  sequentialControlExecutor);
        },
        sequentialControlExecutor);
  }

  private DownloadListener createDownloadListenerWithNotification(
      DownloadFileGroupRequest downloadRequest, DataFileGroupInternal fileGroup) {

    String networkPausedMessage = getNetworkPausedMessage(downloadRequest, fileGroup);

    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
    ForegroundDownloadKey foregroundDownloadKey =
        ForegroundDownloadKey.ofFileGroup(
            downloadRequest.groupName(),
            downloadRequest.accountOptional(),
            downloadRequest.variantIdOptional());

    NotificationCompat.Builder notification =
        NotificationUtil.createNotificationBuilder(
            context,
            downloadRequest.groupSizeBytes(),
            downloadRequest.contentTitleOptional().or(downloadRequest.groupName()),
            downloadRequest.contentTextOptional().or(downloadRequest.groupName()));
    int notificationKey = NotificationUtil.notificationKeyForKey(downloadRequest.groupName());

    if (downloadRequest.showNotifications() == DownloadFileGroupRequest.ShowNotifications.ALL) {
      NotificationUtil.createCancelAction(
          context,
          foregroundDownloadServiceClassOptional.get(),
          foregroundDownloadKey.toString(),
          notification,
          notificationKey);

      notificationManager.notify(notificationKey, notification.build());
    }

    return new DownloadListener() {
      @Override
      public void onProgress(long currentSize) {
        // TODO(b/229123693): return this future once DownloadListener has an async api.
        // There can be a race condition, where onProgress can be called
        // after onComplete or onFailure which removes the future and the notification.
        // Check foregroundDownloadFutureMap first before updating notification.
        ListenableFuture<?> unused =
            PropagatedFutures.transformAsync(
                foregroundDownloadFutureMap.containsKey(foregroundDownloadKey.toString()),
                futureInProgress -> {
                  if (futureInProgress
                      && downloadRequest.showNotifications()
                          == DownloadFileGroupRequest.ShowNotifications.ALL) {
                    notification
                        .setCategory(NotificationCompat.CATEGORY_PROGRESS)
                        .setSmallIcon(android.R.drawable.stat_sys_download)
                        .setProgress(
                            downloadRequest.groupSizeBytes(),
                            (int) currentSize,
                            /* indeterminate= */ downloadRequest.groupSizeBytes() <= 0);
                    notificationManager.notify(notificationKey, notification.build());
                  }
                  if (downloadRequest.listenerOptional().isPresent()) {
                    downloadRequest.listenerOptional().get().onProgress(currentSize);
                  }
                  return immediateVoidFuture();
                },
                sequentialControlExecutor);
      }

      @Override
      public void pausedForConnectivity() {
        // TODO(b/229123693): return this future once DownloadListener has an async api.
        // There can be a race condition, where pausedForConnectivity can be called
        // after onComplete or onFailure which removes the future and the notification.
        // Check foregroundDownloadFutureMap first before updating notification.
        ListenableFuture<?> unused =
            PropagatedFutures.transformAsync(
                foregroundDownloadFutureMap.containsKey(foregroundDownloadKey.toString()),
                futureInProgress -> {
                  if (futureInProgress
                      && downloadRequest.showNotifications()
                          == DownloadFileGroupRequest.ShowNotifications.ALL) {
                    notification
                        .setCategory(NotificationCompat.CATEGORY_STATUS)
                        .setContentText(networkPausedMessage)
                        .setSmallIcon(android.R.drawable.stat_sys_download)
                        .setOngoing(true)
                        // hide progress bar.
                        .setProgress(0, 0, false);
                    notificationManager.notify(notificationKey, notification.build());
                  }
                  if (downloadRequest.listenerOptional().isPresent()) {
                    downloadRequest.listenerOptional().get().pausedForConnectivity();
                  }
                  return immediateVoidFuture();
                },
                sequentialControlExecutor);
      }

      @Override
      public void onComplete(ClientFileGroup clientFileGroup) {
        // TODO(b/229123693): return this future once DownloadListener has an async api.
        ListenableFuture<?> unused =
            PropagatedFutures.submitAsync(
                () -> {
                  boolean onCompleteFailed = false;
                  if (downloadRequest.listenerOptional().isPresent()) {
                    try {
                      downloadRequest.listenerOptional().get().onComplete(clientFileGroup);
                    } catch (Exception e) {
                      LogUtil.w(
                          e,
                          "%s: Delegate onComplete failed for group %s, showing failure"
                              + " notification.",
                          TAG,
                          clientFileGroup.getGroupName());
                      onCompleteFailed = true;
                    }
                  }

                  // Clear the notification action.
                  if (downloadRequest.showNotifications()
                      == DownloadFileGroupRequest.ShowNotifications.ALL) {
                    notification.mActions.clear();

                    if (onCompleteFailed) {
                      // Show download failed in notification.
                      notification
                          .setCategory(NotificationCompat.CATEGORY_STATUS)
                          .setContentText(NotificationUtil.getDownloadFailedMessage(context))
                          .setOngoing(false)
                          .setSmallIcon(android.R.drawable.stat_sys_warning)
                          // hide progress bar.
                          .setProgress(0, 0, false);

                      notificationManager.notify(notificationKey, notification.build());
                    } else {
                      NotificationUtil.cancelNotificationForKey(
                          context, downloadRequest.groupName());
                    }
                  }

                  downloadMonitorOptional.get().removeDownloadListener(downloadRequest.groupName());

                  return foregroundDownloadFutureMap.remove(foregroundDownloadKey.toString());
                },
                sequentialControlExecutor);
      }

      @Override
      public void onFailure(Throwable t) {
        // TODO(b/229123693): return this future once DownloadListener has an async api.
        ListenableFuture<?> unused =
            PropagatedFutures.submitAsync(
                () -> {
                  if (downloadRequest.showNotifications()
                      == DownloadFileGroupRequest.ShowNotifications.ALL) {
                    // Clear the notification action.
                    notification.mActions.clear();

                    // Show download failed in notification.
                    notification
                        .setCategory(NotificationCompat.CATEGORY_STATUS)
                        .setContentText(NotificationUtil.getDownloadFailedMessage(context))
                        .setOngoing(false)
                        .setSmallIcon(android.R.drawable.stat_sys_warning)
                        // hide progress bar.
                        .setProgress(0, 0, false);

                    notificationManager.notify(notificationKey, notification.build());
                  }

                  if (downloadRequest.listenerOptional().isPresent()) {
                    downloadRequest.listenerOptional().get().onFailure(t);
                  }
                  downloadMonitorOptional.get().removeDownloadListener(downloadRequest.groupName());

                  return foregroundDownloadFutureMap.remove(foregroundDownloadKey.toString());
                },
                sequentialControlExecutor);
      }
    };
  }

  // Helper method to get the correct network paused message
  private String getNetworkPausedMessage(
      DownloadFileGroupRequest downloadRequest, DataFileGroupInternal fileGroup) {
    DeviceNetworkPolicy networkPolicyForDownload =
        fileGroup.getDownloadConditions().getDeviceNetworkPolicy();
    if (downloadRequest.downloadConditionsOptional().isPresent()) {
      try {
        networkPolicyForDownload =
            ProtoConversionUtil.convert(downloadRequest.downloadConditionsOptional().get())
                .getDeviceNetworkPolicy();
      } catch (InvalidProtocolBufferException unused) {
        // Do nothing -- we will rely on the file group's network policy.
      }
    }

    switch (networkPolicyForDownload) {
      case DOWNLOAD_FIRST_ON_WIFI_THEN_ON_ANY_NETWORK: // fallthrough
      case DOWNLOAD_ONLY_ON_WIFI:
        return NotificationUtil.getDownloadPausedWifiMessage(context);
      default:
        return NotificationUtil.getDownloadPausedMessage(context);
    }
  }

  @Override
  public void cancelForegroundDownload(String downloadKey) {
    LogUtil.d("%s: CancelForegroundDownload for key = %s", TAG, downloadKey);
    ListenableFuture<?> unused =
        PropagatedFutures.transformAsync(
            foregroundDownloadFutureMap.get(downloadKey),
            downloadFuture -> {
              if (downloadFuture.isPresent()) {
                LogUtil.v(
                    "%s: CancelForegroundDownload future found for key = %s, cancelling...",
                    TAG, downloadKey);
                downloadFuture.get().cancel(false);
              }
              return immediateVoidFuture();
            },
            sequentialControlExecutor);
    // Attempt cancel with internal MDD Lite instance in case it's a single file uri (cancel call is
    // a noop if internal MDD Lite doesn't know about it).
    singleFileDownloader.cancelForegroundDownload(downloadKey);
  }

  @Override
  public void schedulePeriodicTasks() {
    schedulePeriodicTasksInternal(Optional.absent());
  }

  @Override
  public ListenableFuture<Void> schedulePeriodicBackgroundTasks() {
    return futureSerializer.submit(
        () -> {
          schedulePeriodicTasksInternal(/* constraintOverridesMap= */ Optional.absent());
          return null;
        },
        sequentialControlExecutor);
  }

  @Override
  public ListenableFuture<Void> schedulePeriodicBackgroundTasks(
      Optional<Map<String, ConstraintOverrides>> constraintOverridesMap) {
    return futureSerializer.submit(
        () -> {
          schedulePeriodicTasksInternal(constraintOverridesMap);
          return null;
        },
        sequentialControlExecutor);
  }

  private void schedulePeriodicTasksInternal(
      Optional<Map<String, ConstraintOverrides>> constraintOverridesMap) {
    if (!taskSchedulerOptional.isPresent()) {
      LogUtil.e(
          "%s: Called schedulePeriodicTasksInternal when taskScheduler is not provided.", TAG);
      return;
    }

    TaskScheduler taskScheduler = taskSchedulerOptional.get();

    // Schedule task that runs on charging without any network, every 6 hours.
    taskScheduler.schedulePeriodicTask(
        TaskScheduler.CHARGING_PERIODIC_TASK,
        flags.chargingGcmTaskPeriod(),
        NetworkState.NETWORK_STATE_ANY,
        getConstraintOverrides(constraintOverridesMap, TaskScheduler.CHARGING_PERIODIC_TASK));

    // Schedule maintenance task that runs on charging, once every day.
    // This task should run even if mdd is disabled, to handle cleanup.
    taskScheduler.schedulePeriodicTask(
        TaskScheduler.MAINTENANCE_PERIODIC_TASK,
        flags.maintenanceGcmTaskPeriod(),
        NetworkState.NETWORK_STATE_ANY,
        getConstraintOverrides(constraintOverridesMap, TaskScheduler.MAINTENANCE_PERIODIC_TASK));

    // Schedule task that runs on cellular+charging, every 6 hours.
    taskScheduler.schedulePeriodicTask(
        TaskScheduler.CELLULAR_CHARGING_PERIODIC_TASK,
        flags.cellularChargingGcmTaskPeriod(),
        NetworkState.NETWORK_STATE_CONNECTED,
        getConstraintOverrides(
            constraintOverridesMap, TaskScheduler.CELLULAR_CHARGING_PERIODIC_TASK));

    // Schedule task that runs on wifi+charging, every 6 hours.
    taskScheduler.schedulePeriodicTask(
        TaskScheduler.WIFI_CHARGING_PERIODIC_TASK,
        flags.wifiChargingGcmTaskPeriod(),
        NetworkState.NETWORK_STATE_UNMETERED,
        getConstraintOverrides(constraintOverridesMap, TaskScheduler.WIFI_CHARGING_PERIODIC_TASK));
  }

  private static Optional<ConstraintOverrides> getConstraintOverrides(
      Optional<Map<String, ConstraintOverrides>> constraintOverridesMap,
      String maintenancePeriodicTask) {
    return constraintOverridesMap.isPresent()
        ? Optional.fromNullable(constraintOverridesMap.get().get(maintenancePeriodicTask))
        : Optional.absent();
  }

  @Override
  public ListenableFuture<Void> cancelPeriodicBackgroundTasks() {
    return futureSerializer.submit(
        () -> {
          cancelPeriodicTasksInternal();
          return null;
        },
        sequentialControlExecutor);
  }

  private void cancelPeriodicTasksInternal() {
    if (!taskSchedulerOptional.isPresent()) {
      LogUtil.w("%s: Called cancelPeriodicTasksInternal when taskScheduler is not provided.", TAG);
      return;
    }

    TaskScheduler taskScheduler = taskSchedulerOptional.get();

    taskScheduler.cancelPeriodicTask(TaskScheduler.CHARGING_PERIODIC_TASK);
    taskScheduler.cancelPeriodicTask(TaskScheduler.MAINTENANCE_PERIODIC_TASK);
    taskScheduler.cancelPeriodicTask(TaskScheduler.CELLULAR_CHARGING_PERIODIC_TASK);
    taskScheduler.cancelPeriodicTask(TaskScheduler.WIFI_CHARGING_PERIODIC_TASK);
  }

  @Override
  public ListenableFuture<Void> handleTask(String tag) {
    // All work done here that touches metadata (MobileDataDownloadManager) should be serialized
    // through sequentialControlExecutor.
    switch (tag) {
      case TaskScheduler.MAINTENANCE_PERIODIC_TASK:
        return futureSerializer.submitAsync(
            mobileDataDownloadManager::maintenance, sequentialControlExecutor);

      case TaskScheduler.CHARGING_PERIODIC_TASK:
        ListenableFuture<Void> refreshFileGroupsFuture = refreshFileGroups();
        return PropagatedFutures.transformAsync(
            refreshFileGroupsFuture,
            propagateAsyncFunction(
                v -> mobileDataDownloadManager.verifyAllPendingGroups(customFileGroupValidator)),
            sequentialControlExecutor);

      case TaskScheduler.CELLULAR_CHARGING_PERIODIC_TASK:
        return refreshAndDownload(false /*onWifi*/);

      case TaskScheduler.WIFI_CHARGING_PERIODIC_TASK:
        return refreshAndDownload(true /*onWifi*/);

      default:
        LogUtil.d("%s: gcm task doesn't belong to MDD", TAG);
        return immediateFailedFuture(
            new IllegalArgumentException("Unknown task tag sent to MDD.handleTask() " + tag));
    }
  }

  private ListenableFuture<Void> refreshAndDownload(boolean onWifi) {
    // We will do 2 passes to support 2-step downloads. In each step, we will refresh and then
    // download.
    return PropagatedFluentFuture.from(refreshFileGroups())
        .transformAsync(
            v ->
                mobileDataDownloadManager.downloadAllPendingGroups(
                    onWifi, customFileGroupValidator),
            sequentialControlExecutor)
        .transformAsync(v -> refreshFileGroups(), sequentialControlExecutor)
        .transformAsync(
            v ->
                mobileDataDownloadManager.downloadAllPendingGroups(
                    onWifi, customFileGroupValidator),
            sequentialControlExecutor);
  }

  private ListenableFuture<Void> refreshFileGroups() {
    List<ListenableFuture<Void>> refreshFutures = new ArrayList<>();
    for (FileGroupPopulator fileGroupPopulator : fileGroupPopulatorList) {
      refreshFutures.add(fileGroupPopulator.refreshFileGroups(this));
    }

    return PropagatedFutures.whenAllComplete(refreshFutures)
        .call(() -> null, sequentialControlExecutor);
  }

  @Override
  public ListenableFuture<Void> maintenance() {
    return handleTask(TaskScheduler.MAINTENANCE_PERIODIC_TASK);
  }

  @Override
  public ListenableFuture<Void> collectGarbage() {
    return futureSerializer.submitAsync(
        mobileDataDownloadManager::removeExpiredGroupsAndFiles, sequentialControlExecutor);
  }

  @Override
  public ListenableFuture<Void> clear() {
    return futureSerializer.submitAsync(
        mobileDataDownloadManager::clear, sequentialControlExecutor);
  }

  // incompatible argument for parameter msg of e.
  // incompatible types in return.
  @Override
  public String getDebugInfoAsString() {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    PrintWriter writer = new PrintWriter(out);
    try {
      // Okay to block here because this method is for debugging only.
      mobileDataDownloadManager.dump(writer).get(DUMP_DEBUG_INFO_TIMEOUT, TimeUnit.SECONDS);
      writer.println("==== MOBSTORE_DEBUG_INFO ====");
      writer.print(fileStorage.getDebugInfo());
    } catch (ExecutionException | TimeoutException e) {
      String errString = String.format("%s: Couldn't get debug info: %s", TAG, e);
      LogUtil.e(errString);
      return errString;
    } catch (InterruptedException e) {
      // see <internal>
      Thread.currentThread().interrupt();
      String errString = String.format("%s: Couldn't get debug info: %s", TAG, e);
      LogUtil.e(errString);
      return errString;
    }
    writer.flush();
    return out.toString();
  }

  @Override
  public ListenableFuture<Void> reportUsage(UsageEvent usageEvent) {
    eventLogger.logMddUsageEvent(createFileGroupDetails(usageEvent.clientFileGroup()), null);

    return immediateVoidFuture();
  }

  private static DownloadFutureMap.StateChangeCallbacks createCallbacksForForegroundService(
      Context context, Optional<Class<?>> foregroundDownloadServiceClassOptional) {
    return new DownloadFutureMap.StateChangeCallbacks() {
      @Override
      public void onAdd(String key, int newSize) {
        // Only start foreground service if this is the first future we are adding.
        if (newSize == 1 && foregroundDownloadServiceClassOptional.isPresent()) {
          NotificationUtil.startForegroundDownloadService(
              context, foregroundDownloadServiceClassOptional.get(), key);
        }
      }

      @Override
      public void onRemove(String key, int newSize) {
        // Only stop foreground service if there are no more futures remaining.
        if (newSize == 0 && foregroundDownloadServiceClassOptional.isPresent()) {
          NotificationUtil.stopForegroundDownloadService(
              context, foregroundDownloadServiceClassOptional.get(), key);
        }
      }
    };
  }
}