1 /*
2  * Copyright (C) 2017 The Android Open Source Project
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 
17 package androidx.core.provider;
18 
19 import static androidx.annotation.RestrictTo.Scope.LIBRARY_GROUP;
20 
21 import android.util.Base64;
22 
23 import androidx.annotation.ArrayRes;
24 import androidx.annotation.NonNull;
25 import androidx.annotation.Nullable;
26 import androidx.annotation.RestrictTo;
27 import androidx.core.util.Preconditions;
28 
29 import java.util.List;
30 
31 /**
32  * Information about a font request that may be sent to a Font Provider.
33  */
34 public final class FontRequest {
35     private final String mProviderAuthority;
36     private final String mProviderPackage;
37     private final String mQuery;
38     private final List<List<byte[]>> mCertificates;
39     private final int mCertificatesArray;
40 
41     // Used for key of the cache
42     private final String mIdentifier;
43 
44     /**
45      * @param providerAuthority The authority of the Font Provider to be used for the request.
46      * @param query The query to be sent over to the provider. Refer to your font provider's
47      *         documentation on the format of this string.
48      * @param providerPackage The package for the Font Provider to be used for the request. This is
49      *         used to verify the identity of the provider.
50      * @param certificates The list of sets of hashes for the certificates the provider should be
51      *         signed with. This is used to verify the identity of the provider. Each set in the
52      *         list represents one collection of signature hashes. Refer to your font provider's
53      *         documentation for these values.
54      */
FontRequest(@onNull String providerAuthority, @NonNull String providerPackage, @NonNull String query, @NonNull List<List<byte[]>> certificates)55     public FontRequest(@NonNull String providerAuthority, @NonNull String providerPackage,
56             @NonNull String query, @NonNull List<List<byte[]>> certificates) {
57         mProviderAuthority = Preconditions.checkNotNull(providerAuthority);
58         mProviderPackage = Preconditions.checkNotNull(providerPackage);
59         mQuery = Preconditions.checkNotNull(query);
60         mCertificates = Preconditions.checkNotNull(certificates);
61         mCertificatesArray = 0;
62         mIdentifier = new StringBuilder(mProviderAuthority).append("-").append(mProviderPackage)
63                 .append("-").append(mQuery).toString();
64     }
65 
66     /**
67      * @param providerAuthority The authority of the Font Provider to be used for the request.
68      * @param query The query to be sent over to the provider. Refer to your font provider's
69      *         documentation on the format of this string.
70      * @param providerPackage The package for the Font Provider to be used for the request. This is
71      *         used to verify the identity of the provider.
72      * @param certificates A resource array with the list of sets of hashes for the certificates the
73      *         provider should be signed with. This is used to verify the identity of the provider.
74      *         Each set in the list represents one collection of signature hashes. Refer to your
75      *         font provider's documentation for these values.
76      */
FontRequest(@onNull String providerAuthority, @NonNull String providerPackage, @NonNull String query, @ArrayRes int certificates)77     public FontRequest(@NonNull String providerAuthority, @NonNull String providerPackage,
78             @NonNull String query, @ArrayRes int certificates) {
79         mProviderAuthority = Preconditions.checkNotNull(providerAuthority);
80         mProviderPackage = Preconditions.checkNotNull(providerPackage);
81         mQuery = Preconditions.checkNotNull(query);
82         mCertificates = null;
83         Preconditions.checkArgument(certificates != 0);
84         mCertificatesArray = certificates;
85         mIdentifier = new StringBuilder(mProviderAuthority).append("-").append(mProviderPackage)
86                 .append("-").append(mQuery).toString();
87     }
88 
89     /**
90      * Returns the selected font provider's authority. This tells the system what font provider
91      * it should request the font from.
92      */
93     @NonNull
getProviderAuthority()94     public String getProviderAuthority() {
95         return mProviderAuthority;
96     }
97 
98     /**
99      * Returns the selected font provider's package. This helps the system verify that the provider
100      * identified by the given authority is the one requested.
101      */
102     @NonNull
getProviderPackage()103     public String getProviderPackage() {
104         return mProviderPackage;
105     }
106 
107     /**
108      * Returns the query string. Refer to your font provider's documentation on the format of this
109      * string.
110      */
111     @NonNull
getQuery()112     public String getQuery() {
113         return mQuery;
114     }
115 
116     /**
117      * Returns the list of certificate sets given for this provider. This helps the system verify
118      * that the provider identified by the given authority is the one requested. Note this might
119      * be null if the certificates were provided via a resource id.
120      *
121      * @see #getCertificatesArrayResId()
122      */
123     @Nullable
getCertificates()124     public List<List<byte[]>> getCertificates() {
125         return mCertificates;
126     }
127 
128     /**
129      * Returns the array resource id pointing to the certificate sets given for this provider. This
130      * helps the system verify that the provider identified by the given authority is the one
131      * requested. Note that this may be 0 if the certificates were provided as a list.
132      *
133      * @see #getCertificates()
134      */
135     @ArrayRes
getCertificatesArrayResId()136     public int getCertificatesArrayResId() {
137         return mCertificatesArray;
138     }
139 
140     /** @hide */
141     @RestrictTo(LIBRARY_GROUP)
getIdentifier()142     public String getIdentifier() {
143         return mIdentifier;
144     }
145 
146     @Override
toString()147     public String toString() {
148         StringBuilder builder = new StringBuilder();
149         builder.append("FontRequest {"
150                 + "mProviderAuthority: " + mProviderAuthority
151                 + ", mProviderPackage: " + mProviderPackage
152                 + ", mQuery: " + mQuery
153                 + ", mCertificates:");
154         for (int i = 0; i < mCertificates.size(); i++) {
155             builder.append(" [");
156             List<byte[]> set = mCertificates.get(i);
157             for (int j = 0; j < set.size(); j++) {
158                 builder.append(" \"");
159                 byte[] array = set.get(j);
160                 builder.append(Base64.encodeToString(array, Base64.DEFAULT));
161                 builder.append("\"");
162             }
163             builder.append(" ]");
164         }
165         builder.append("}");
166         builder.append("mCertificatesArray: " + mCertificatesArray);
167         return builder.toString();
168     }
169 }
170