summaryrefslogtreecommitdiff
path: root/javatests/com/google/android/libraries/mobiledatadownload/TwoStepPopulator.java
blob: 16f5357fd5c03ced10f2b0fffeb310d869ac425a (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
/*
 * 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 java.nio.charset.StandardCharsets.UTF_8;

import android.content.Context;
import android.net.Uri;
import android.util.Log;
import com.google.android.libraries.mobiledatadownload.file.SynchronousFileStorage;
import com.google.android.libraries.mobiledatadownload.file.common.testing.StreamUtils;
import com.google.android.libraries.mobiledatadownload.internal.logging.LogUtil;
import com.google.common.base.Preconditions;
import com.google.common.util.concurrent.FluentFuture;
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.MoreExecutors;
import com.google.mobiledatadownload.ClientConfigProto.ClientFileGroup;
import com.google.mobiledatadownload.DownloadConfigProto.DataFileGroup;
import com.google.mobiledatadownload.DownloadConfigProto.DownloadConditions.DeviceNetworkPolicy;
import java.io.IOException;

/**
 * A file group populator that demonstrate the 2-step download. The populator will read the content
 * from first group and then add the second group to MDD.
 */
public class TwoStepPopulator implements FileGroupPopulator {
  private static final String TAG = "TwoStepPopulator";

  private final Context context;
  private final SynchronousFileStorage fileStorage;

  public TwoStepPopulator(Context context, SynchronousFileStorage fileStorage) {
    this.context = context;
    this.fileStorage = fileStorage;
  }

  @Override
  public ListenableFuture<Void> refreshFileGroups(MobileDataDownload mobileDataDownload) {
    ListenableFuture<ClientFileGroup> step1Future =
        mobileDataDownload.getFileGroup(
            GetFileGroupRequest.newBuilder().setGroupName("step1-file-group").build());

    return FluentFuture.from(step1Future)
        .transformAsync(
            clientFileGroup -> {
              if (clientFileGroup == null) {
                return Futures.immediateFuture(null);
              }

              LogUtil.d("%s: Retrieved step1-file-group from MDD", TAG);

              // Now read the url from the step1.txt
              Uri fileUri = Uri.parse(clientFileGroup.getFile(0).getFileUri());
              String step1Content = null;
              try {
                step1Content = new String(StreamUtils.readFileInBytes(fileStorage, fileUri), UTF_8);
              } catch (IOException e) {
                LogUtil.e(e, "Fail to read from step1.txt");
              }

              // Add a file group where the url is read from step1.txt
              DataFileGroup step2FileGroup =
                  TestFileGroupPopulator.createDataFileGroup(
                      "step2-file-group",
                      context.getPackageName(),
                      new String[] {"step2_id"},
                      new int[] {13},
                      new String[] {""},
                      new String[] {step1Content},
                      DeviceNetworkPolicy.DOWNLOAD_ON_ANY_NETWORK);

              ListenableFuture<Boolean> addFileGroupFuture =
                  mobileDataDownload.addFileGroup(
                      AddFileGroupRequest.newBuilder().setDataFileGroup(step2FileGroup).build());
              Futures.addCallback(
                  addFileGroupFuture,
                  new FutureCallback<Boolean>() {
                    @Override
                    public void onSuccess(Boolean result) {
                      Preconditions.checkState(result.booleanValue());
                      if (result.booleanValue()) {
                        Log.d(TAG, "Added file group " + step2FileGroup.getGroupName());
                      } else {
                        Log.d(TAG, "Failed to add file group " + step2FileGroup.getGroupName());
                      }
                    }

                    @Override
                    public void onFailure(Throwable t) {
                      Log.e(TAG, "Failed to add file group", t);
                    }
                  },
                  MoreExecutors.directExecutor());
              return addFileGroupFuture;
            },
            MoreExecutors.directExecutor())
        .transform(addFileGroupSucceeded -> null, MoreExecutors.directExecutor());
  }
}