blob: 34114bcce195ef3e5605524ecafb2e60af56546e [file] [log] [blame]
Clara Bayarrib0812a32016-10-20 10:42:13 +01001/*
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 */
Seigo Nonaka080b0542017-04-26 09:53:38 -070016package android.provider;
Clara Bayarrib0812a32016-10-20 10:42:13 +010017
18import android.annotation.NonNull;
Clara Bayarri3c4be772017-02-07 15:33:40 +000019import android.util.Base64;
Clara Bayarrib0812a32016-10-20 10:42:13 +010020
21import com.android.internal.util.Preconditions;
22
Clara Bayarri3c4be772017-02-07 15:33:40 +000023import java.util.ArrayList;
24import java.util.Collections;
25import java.util.List;
26
Clara Bayarrib0812a32016-10-20 10:42:13 +010027/**
28 * Information about a font request that may be sent to a Font Provider.
29 */
Seigo Nonaka080b0542017-04-26 09:53:38 -070030public final class FontRequest {
Clara Bayarrib0812a32016-10-20 10:42:13 +010031 private final String mProviderAuthority;
Clara Bayarri3c4be772017-02-07 15:33:40 +000032 private final String mProviderPackage;
Clara Bayarrib0812a32016-10-20 10:42:13 +010033 private final String mQuery;
Clara Bayarri3c4be772017-02-07 15:33:40 +000034 private final List<List<byte[]>> mCertificates;
35
Seigo Nonakadaa8dfc2017-04-19 12:16:39 -070036 // Used for key of the cache.
37 private final String mIdentifier;
38
Clara Bayarri3c4be772017-02-07 15:33:40 +000039 /**
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 */
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();
Seigo Nonakadaa8dfc2017-04-19 12:16:39 -070053 mIdentifier = new StringBuilder(mProviderAuthority).append("-").append(mProviderPackage)
54 .append("-").append(mQuery).toString();
Clara Bayarri3c4be772017-02-07 15:33:40 +000055 }
Clara Bayarrib0812a32016-10-20 10:42:13 +010056
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
Clara Bayarri3c4be772017-02-07 15:33:40 +000060 * 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.
Clara Bayarrib0812a32016-10-20 10:42:13 +010067 */
Clara Bayarri3c4be772017-02-07 15:33:40 +000068 public FontRequest(@NonNull String providerAuthority, @NonNull String providerPackage,
69 @NonNull String query, @NonNull List<List<byte[]>> certificates) {
Clara Bayarrib0812a32016-10-20 10:42:13 +010070 mProviderAuthority = Preconditions.checkNotNull(providerAuthority);
Clara Bayarri3c4be772017-02-07 15:33:40 +000071 mProviderPackage = Preconditions.checkNotNull(providerPackage);
Clara Bayarrib0812a32016-10-20 10:42:13 +010072 mQuery = Preconditions.checkNotNull(query);
Clara Bayarri3c4be772017-02-07 15:33:40 +000073 mCertificates = Preconditions.checkNotNull(certificates);
Seigo Nonakadaa8dfc2017-04-19 12:16:39 -070074 mIdentifier = new StringBuilder(mProviderAuthority).append("-").append(mProviderPackage)
75 .append("-").append(mQuery).toString();
Clara Bayarrib0812a32016-10-20 10:42:13 +010076 }
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 */
82 public String getProviderAuthority() {
83 return mProviderAuthority;
84 }
85
86 /**
Clara Bayarri3c4be772017-02-07 15:33:40 +000087 * 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 */
90 public String getProviderPackage() {
91 return mProviderPackage;
92 }
93
94 /**
Clara Bayarrib0812a32016-10-20 10:42:13 +010095 * Returns the query string. Refer to your font provider's documentation on the format of this
96 * string.
97 */
98 public String getQuery() {
99 return mQuery;
100 }
101
Clara Bayarri3c4be772017-02-07 15:33:40 +0000102 /**
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 */
106 public List<List<byte[]>> getCertificates() {
107 return mCertificates;
108 }
109
Seigo Nonakadaa8dfc2017-04-19 12:16:39 -0700110 /** @hide */
111 public String getIdentifier() {
112 return mIdentifier;
113 }
114
Clara Bayarrib0812a32016-10-20 10:42:13 +0100115 @Override
Clara Bayarrib0812a32016-10-20 10:42:13 +0100116 public String toString() {
Clara Bayarri3c4be772017-02-07 15:33:40 +0000117 StringBuilder builder = new StringBuilder();
118 builder.append("FontRequest {"
Clara Bayarrib0812a32016-10-20 10:42:13 +0100119 + "mProviderAuthority: " + mProviderAuthority
Clara Bayarri3c4be772017-02-07 15:33:40 +0000120 + ", mProviderPackage: " + mProviderPackage
Clara Bayarrib0812a32016-10-20 10:42:13 +0100121 + ", mQuery: " + mQuery
Clara Bayarri3c4be772017-02-07 15:33:40 +0000122 + ", 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();
Clara Bayarrib0812a32016-10-20 10:42:13 +0100136 }
137}