• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2022 Google LLC
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.google.android.libraries.mobiledatadownload.foreground;
17 
18 import static com.google.android.libraries.mobiledatadownload.internal.MddConstants.SPLIT_CHAR;
19 
20 import android.accounts.Account;
21 import android.net.Uri;
22 import com.google.android.libraries.mobiledatadownload.account.AccountUtil;
23 import com.google.auto.value.AutoValue;
24 import com.google.common.base.Optional;
25 import com.google.common.hash.Hasher;
26 import com.google.common.hash.Hashing;
27 
28 /**
29  * Container class for unique key of a foreground download.
30  *
31  * <p>There are two kinds of foreground downloads supported: file group and single files.
32  *
33  * <p>Each kind has different requirements to build the unique key that must be provided when
34  * building a ForegroundDownloadKey.
35  */
36 @AutoValue
37 public abstract class ForegroundDownloadKey {
38 
39   /**
40    * Kind of {@link ForegroundDownloadKey}.
41    *
42    * <p>Only two types of foreground downloads are supported, file groups and single files.
43    */
44   public enum Kind {
45     FILE_GROUP,
46     SINGLE_FILE,
47   }
48 
kind()49   public abstract Kind kind();
50 
key()51   public abstract String key();
52 
53   /**
54    * Unique Identifier of a File Group used to identify a group during a foreground download.
55    *
56    * <p><b>NOTE:</b> Properties set here <em>must</em> match the properties set in {@link
57    * DownloadFileGroupRequest} when starting a Foreground Download.
58    *
59    * @param groupName The name of the group to download (required)
60    * @param account An associated account of the group, if applicable (optional)
61    * @param variantId An associated variantId fo the group, if applicable (optional)
62    */
ofFileGroup( String groupName, Optional<Account> account, Optional<String> variantId)63   public static ForegroundDownloadKey ofFileGroup(
64       String groupName, Optional<Account> account, Optional<String> variantId) {
65     Hasher keyHasher = Hashing.sha256().newHasher().putUnencodedChars(groupName);
66 
67     if (account.isPresent()) {
68       keyHasher
69           .putUnencodedChars(SPLIT_CHAR)
70           .putUnencodedChars(AccountUtil.serialize(account.get()));
71     }
72 
73     if (variantId.isPresent()) {
74       keyHasher.putUnencodedChars(SPLIT_CHAR).putUnencodedChars(variantId.get());
75     }
76     return new AutoValue_ForegroundDownloadKey(Kind.FILE_GROUP, keyHasher.hash().toString());
77   }
78 
79   /**
80    * Unique Identifier of a File used to identify it during a foreground download.
81    *
82    * <p><b>NOTE:</b> Properties set here <em>must</em> match the properties set in {@link
83    * SingleFileDownloadRequest} or {@link DownloadRequest} when starting a Foreground Download.
84    *
85    * @param destinationUri The on-device location where the file will be downloaded (required)
86    */
ofSingleFile(Uri destinationUri)87   public static ForegroundDownloadKey ofSingleFile(Uri destinationUri) {
88     Hasher keyHasher =
89         Hashing.sha256()
90             .newHasher()
91             .putUnencodedChars(destinationUri.toString())
92             .putUnencodedChars(SPLIT_CHAR);
93     return new AutoValue_ForegroundDownloadKey(Kind.SINGLE_FILE, keyHasher.hash().toString());
94   }
95 
96   @Override
toString()97   public final String toString() {
98     return key();
99   }
100 }
101