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 package android.provider;
17 
18 import android.annotation.NonNull;
19 import android.util.Base64;
20 
21 import com.android.internal.util.Preconditions;
22 
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.List;
26 
27 /**
28  * Information about a font request that may be sent to a Font Provider.
29  */
30 public final class FontRequest {
31     private final String mProviderAuthority;
32     private final String mProviderPackage;
33     private final String mQuery;
34     private final List<List<byte[]>> mCertificates;
35 
36     // Used for key of the cache.
37     private final String mIdentifier;
38 
39     /**
40      * @param providerAuthority The authority of the Font Provider to be used for the request. This
41      *         should be a system installed app.
42      * @param providerPackage The package for the Font Provider to be used for the request. This is
43      *         used to verify the identity of the provider.
44      * @param query The query to be sent over to the provider. Refer to your font provider's
45      *         documentation on the format of this string.
46      */
FontRequest(@onNull String providerAuthority, @NonNull String providerPackage, @NonNull String query)47     public FontRequest(@NonNull String providerAuthority, @NonNull String providerPackage,
48             @NonNull String query) {
49         mProviderAuthority = Preconditions.checkNotNull(providerAuthority);
50         mQuery = Preconditions.checkNotNull(query);
51         mProviderPackage = Preconditions.checkNotNull(providerPackage);
52         mCertificates = Collections.emptyList();
53         mIdentifier = new StringBuilder(mProviderAuthority).append("-").append(mProviderPackage)
54                 .append("-").append(mQuery).toString();
55     }
56 
57     /**
58      * @param providerAuthority The authority of the Font Provider to be used for the request.
59      * @param query The query to be sent over to the provider. Refer to your font provider's
60      *         documentation on the format of this string.
61      * @param providerPackage The package for the Font Provider to be used for the request. This is
62      *         used to verify the identity of the provider.
63      * @param certificates The list of sets of hashes for the certificates the provider should be
64      *         signed with. This is used to verify the identity of the provider. Each set in the
65      *         list represents one collection of signature hashes. Refer to your font provider's
66      *         documentation for these values.
67      */
FontRequest(@onNull String providerAuthority, @NonNull String providerPackage, @NonNull String query, @NonNull List<List<byte[]>> certificates)68     public FontRequest(@NonNull String providerAuthority, @NonNull String providerPackage,
69             @NonNull String query, @NonNull List<List<byte[]>> certificates) {
70         mProviderAuthority = Preconditions.checkNotNull(providerAuthority);
71         mProviderPackage = Preconditions.checkNotNull(providerPackage);
72         mQuery = Preconditions.checkNotNull(query);
73         mCertificates = Preconditions.checkNotNull(certificates);
74         mIdentifier = new StringBuilder(mProviderAuthority).append("-").append(mProviderPackage)
75                 .append("-").append(mQuery).toString();
76     }
77 
78     /**
79      * Returns the selected font provider's authority. This tells the system what font provider
80      * it should request the font from.
81      */
getProviderAuthority()82     public String getProviderAuthority() {
83         return mProviderAuthority;
84     }
85 
86     /**
87      * Returns the selected font provider's package. This helps the system verify that the provider
88      * identified by the given authority is the one requested.
89      */
getProviderPackage()90     public String getProviderPackage() {
91         return mProviderPackage;
92     }
93 
94     /**
95      * Returns the query string. Refer to your font provider's documentation on the format of this
96      * string.
97      */
getQuery()98     public String getQuery() {
99         return mQuery;
100     }
101 
102     /**
103      * Returns the list of certificate sets given for this provider. This helps the system verify
104      * that the provider identified by the given authority is the one requested.
105      */
getCertificates()106     public List<List<byte[]>> getCertificates() {
107         return mCertificates;
108     }
109 
110     /** @hide */
getIdentifier()111     public String getIdentifier() {
112         return mIdentifier;
113     }
114 
115     @Override
toString()116     public String toString() {
117         StringBuilder builder = new StringBuilder();
118         builder.append("FontRequest {"
119                 + "mProviderAuthority: " + mProviderAuthority
120                 + ", mProviderPackage: " + mProviderPackage
121                 + ", mQuery: " + mQuery
122                 + ", mCertificates:");
123         for (int i = 0; i < mCertificates.size(); i++) {
124             builder.append(" [");
125             List<byte[]> set = mCertificates.get(i);
126             for (int j = 0; j < set.size(); j++) {
127                 builder.append(" \"");
128                 byte[] array = set.get(j);
129                 builder.append(Base64.encodeToString(array, Base64.DEFAULT));
130                 builder.append("\"");
131             }
132             builder.append(" ]");
133         }
134         builder.append("}");
135         return builder.toString();
136     }
137 }
138