| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package android.provider; |
| 17 |
|
| 18 |
import android.annotation.IntDef; |
| 19 |
import android.annotation.IntRange; |
| 20 |
import android.annotation.NonNull; |
| 21 |
import android.annotation.Nullable; |
| 22 |
import android.content.ContentResolver; |
| 23 |
import android.content.ContentUris; |
| 24 |
import android.content.Context; |
| 25 |
import android.content.pm.PackageInfo; |
| 26 |
import android.content.pm.PackageManager; |
| 27 |
import android.content.pm.PackageManager.NameNotFoundException; |
| 28 |
import android.content.pm.ProviderInfo; |
| 29 |
import android.content.pm.Signature; |
| 30 |
import android.database.Cursor; |
| 31 |
import android.graphics.Typeface; |
| 32 |
import android.graphics.fonts.Font; |
| 33 |
import android.graphics.fonts.FontFamily; |
| 34 |
import android.graphics.fonts.FontStyle; |
| 35 |
import android.graphics.fonts.FontVariationAxis; |
| 36 |
import android.net.Uri; |
| 37 |
import android.os.CancellationSignal; |
| 38 |
import android.os.Handler; |
| 39 |
import android.os.HandlerThread; |
| 40 |
import android.os.ParcelFileDescriptor; |
| 41 |
import android.os.Process; |
| 42 |
import android.util.Log; |
| 43 |
import android.util.LruCache; |
| 44 |
|
| 45 |
import com.android.internal.annotations.GuardedBy; |
| 46 |
import com.android.internal.annotations.VisibleForTesting; |
| 47 |
import com.android.internal.util.Preconditions; |
| 48 |
|
| 49 |
import java.io.FileInputStream; |
| 50 |
import java.io.IOException; |
| 51 |
import java.lang.annotation.Retention; |
| 52 |
import java.lang.annotation.RetentionPolicy; |
| 53 |
import java.nio.ByteBuffer; |
| 54 |
import java.nio.channels.FileChannel; |
| 55 |
import java.util.ArrayList; |
| 56 |
import java.util.Arrays; |
| 57 |
import java.util.Collections; |
| 58 |
import java.util.Comparator; |
| 59 |
import java.util.HashMap; |
| 60 |
import java.util.List; |
| 61 |
import java.util.Map; |
| 62 |
import java.util.Set; |
| 63 |
import java.util.concurrent.TimeUnit; |
| 64 |
import java.util.concurrent.atomic.AtomicBoolean; |
| 65 |
import java.util.concurrent.atomic.AtomicReference; |
| 66 |
import java.util.concurrent.locks.Condition; |
| 67 |
import java.util.concurrent.locks.Lock; |
| 68 |
import java.util.concurrent.locks.ReentrantLock; |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
@Deprecated |
| 77 |
public class FontsContract { |
| 78 |
private static final String TAG = "FontsContract"; |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
@Deprecated |
| 91 |
public static final class Columns implements BaseColumns { |
| 92 |
|
| 93 |
|
| 94 |
private Columns() {} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
public static final String FILE_ID = "file_id"; |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
public static final String TTC_INDEX = "font_ttc_index"; |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
public static final String VARIATION_SETTINGS = "font_variation_settings"; |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
public static final String WEIGHT = "font_weight"; |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
public static final String ITALIC = "font_italic"; |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
public static final String RESULT_CODE = "result_code"; |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
public static final int RESULT_CODE_OK = 0; |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
public static final int RESULT_CODE_FONT_NOT_FOUND = 1; |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
public static final int RESULT_CODE_FONT_UNAVAILABLE = 2; |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
public static final int RESULT_CODE_MALFORMED_QUERY = 3; |
| 161 |
} |
| 162 |
|
| 163 |
private static final Object sLock = new Object(); |
| 164 |
@GuardedBy("sLock") |
| 165 |
private static Handler sHandler; |
| 166 |
@GuardedBy("sLock") |
| 167 |
private static HandlerThread sThread; |
| 168 |
@GuardedBy("sLock") |
| 169 |
private static Set<String> sInQueueSet; |
| 170 |
|
| 171 |
private volatile static Context sContext; |
| 172 |
|
| 173 |
private static final LruCache<String, Typeface> sTypefaceCache = new LruCache<>(16); |
| 174 |
|
| 175 |
private FontsContract() { |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
public static void setApplicationContextForResources(Context context) { |
| 180 |
sContext = context.getApplicationContext(); |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
@Deprecated |
| 190 |
public static class FontInfo { |
| 191 |
private final Uri mUri; |
| 192 |
private final int mTtcIndex; |
| 193 |
private final FontVariationAxis[] mAxes; |
| 194 |
private final int mWeight; |
| 195 |
private final boolean mItalic; |
| 196 |
private final int mResultCode; |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
public FontInfo(@NonNull Uri uri, @IntRange(from = 0) int ttcIndex, |
| 209 |
@Nullable FontVariationAxis[] axes, @IntRange(from = 1, to = 1000) int weight, |
| 210 |
boolean italic, int resultCode) { |
| 211 |
mUri = Preconditions.checkNotNull(uri); |
| 212 |
mTtcIndex = ttcIndex; |
| 213 |
mAxes = axes; |
| 214 |
mWeight = weight; |
| 215 |
mItalic = italic; |
| 216 |
mResultCode = resultCode; |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
public @NonNull Uri getUri() { |
| 223 |
return mUri; |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
public @IntRange(from = 0) int getTtcIndex() { |
| 230 |
return mTtcIndex; |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
public @Nullable FontVariationAxis[] getAxes() { |
| 237 |
return mAxes; |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
public @IntRange(from = 1, to = 1000) int getWeight() { |
| 244 |
return mWeight; |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
public boolean isItalic() { |
| 251 |
return mItalic; |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
public int getResultCode() { |
| 260 |
return mResultCode; |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
@Deprecated |
| 271 |
public static class FontFamilyResult { |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
public static final int STATUS_OK = 0; |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
public static final int STATUS_WRONG_CERTIFICATES = 1; |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
public static final int STATUS_UNEXPECTED_DATA_PROVIDED = 2; |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
public static final int STATUS_REJECTED = 3; |
| 297 |
|
| 298 |
|
| 299 |
@IntDef(prefix = { "STATUS_" }, value = { |
| 300 |
STATUS_OK, |
| 301 |
STATUS_WRONG_CERTIFICATES, |
| 302 |
STATUS_UNEXPECTED_DATA_PROVIDED |
| 303 |
}) |
| 304 |
@Retention(RetentionPolicy.SOURCE) |
| 305 |
@interface FontResultStatus {} |
| 306 |
|
| 307 |
private final @FontResultStatus int mStatusCode; |
| 308 |
private final FontInfo[] mFonts; |
| 309 |
|
| 310 |
|
| 311 |
public FontFamilyResult(@FontResultStatus int statusCode, @Nullable FontInfo[] fonts) { |
| 312 |
mStatusCode = statusCode; |
| 313 |
mFonts = fonts; |
| 314 |
} |
| 315 |
|
| 316 |
public @FontResultStatus int getStatusCode() { |
| 317 |
return mStatusCode; |
| 318 |
} |
| 319 |
|
| 320 |
public @NonNull FontInfo[] getFonts() { |
| 321 |
return mFonts; |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
private static final int THREAD_RENEWAL_THRESHOLD_MS = 10000; |
| 326 |
|
| 327 |
private static final long SYNC_FONT_FETCH_TIMEOUT_MS = 500; |
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
private static final Runnable sReplaceDispatcherThreadRunnable = new Runnable() { |
| 333 |
@Override |
| 334 |
public void run() { |
| 335 |
synchronized (sLock) { |
| 336 |
if (sThread != null) { |
| 337 |
sThread.quitSafely(); |
| 338 |
sThread = null; |
| 339 |
sHandler = null; |
| 340 |
} |
| 341 |
} |
| 342 |
} |
| 343 |
}; |
| 344 |
|
| 345 |
|
| 346 |
public static Typeface getFontSync(FontRequest request) { |
| 347 |
final String id = request.getIdentifier(); |
| 348 |
Typeface cachedTypeface = sTypefaceCache.get(id); |
| 349 |
if (cachedTypeface != null) { |
| 350 |
return cachedTypeface; |
| 351 |
} |
| 352 |
|
| 353 |
Log.w(TAG, "Platform version of downloadable fonts is deprecated. Please use" |
| 354 |
+ " androidx version instead."); |
| 355 |
|
| 356 |
synchronized (sLock) { |
| 357 |
|
| 358 |
|
| 359 |
cachedTypeface = sTypefaceCache.get(id); |
| 360 |
if (cachedTypeface != null) { |
| 361 |
return cachedTypeface; |
| 362 |
} |
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
if (sHandler == null) { |
| 368 |
|
| 369 |
sThread = new HandlerThread("fonts", Process.THREAD_PRIORITY_FOREGROUND); |
| 370 |
sThread.start(); |
| 371 |
sHandler = new Handler(sThread.getLooper()); |
| 372 |
} |
| 373 |
final Lock lock = new ReentrantLock(); |
| 374 |
final Condition cond = lock.newCondition(); |
| 375 |
final AtomicReference<Typeface> holder = new AtomicReference<>(); |
| 376 |
final AtomicBoolean waiting = new AtomicBoolean(true); |
| 377 |
final AtomicBoolean timeout = new AtomicBoolean(false); |
| 378 |
|
| 379 |
sHandler.post(() -> { |
| 380 |
try { |
| 381 |
FontFamilyResult result = fetchFonts(sContext, null, request); |
| 382 |
if (result.getStatusCode() == FontFamilyResult.STATUS_OK) { |
| 383 |
Typeface typeface = buildTypeface(sContext, null, result.getFonts()); |
| 384 |
if (typeface != null) { |
| 385 |
sTypefaceCache.put(id, typeface); |
| 386 |
} |
| 387 |
holder.set(typeface); |
| 388 |
} |
| 389 |
} catch (NameNotFoundException e) { |
| 390 |
|
| 391 |
} |
| 392 |
lock.lock(); |
| 393 |
try { |
| 394 |
if (!timeout.get()) { |
| 395 |
waiting.set(false); |
| 396 |
cond.signal(); |
| 397 |
} |
| 398 |
} finally { |
| 399 |
lock.unlock(); |
| 400 |
} |
| 401 |
}); |
| 402 |
sHandler.removeCallbacks(sReplaceDispatcherThreadRunnable); |
| 403 |
sHandler.postDelayed(sReplaceDispatcherThreadRunnable, THREAD_RENEWAL_THRESHOLD_MS); |
| 404 |
|
| 405 |
long remaining = TimeUnit.MILLISECONDS.toNanos(SYNC_FONT_FETCH_TIMEOUT_MS); |
| 406 |
lock.lock(); |
| 407 |
try { |
| 408 |
if (!waiting.get()) { |
| 409 |
return holder.get(); |
| 410 |
} |
| 411 |
for (;;) { |
| 412 |
try { |
| 413 |
remaining = cond.awaitNanos(remaining); |
| 414 |
} catch (InterruptedException e) { |
| 415 |
|
| 416 |
} |
| 417 |
if (!waiting.get()) { |
| 418 |
return holder.get(); |
| 419 |
} |
| 420 |
if (remaining <= 0) { |
| 421 |
timeout.set(true); |
| 422 |
Log.w(TAG, "Remote font fetch timed out: " + |
| 423 |
request.getProviderAuthority() + "/" + request.getQuery()); |
| 424 |
return null; |
| 425 |
} |
| 426 |
} |
| 427 |
} finally { |
| 428 |
lock.unlock(); |
| 429 |
} |
| 430 |
} |
| 431 |
} |
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
@Deprecated |
| 440 |
public static class FontRequestCallback { |
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
public static final int FAIL_REASON_PROVIDER_NOT_FOUND = -1; |
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
public static final int FAIL_REASON_WRONG_CERTIFICATES = -2; |
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
public static final int FAIL_REASON_FONT_LOAD_ERROR = -3; |
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
public static final int FAIL_REASON_FONT_NOT_FOUND = Columns.RESULT_CODE_FONT_NOT_FOUND; |
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
public static final int FAIL_REASON_FONT_UNAVAILABLE = Columns.RESULT_CODE_FONT_UNAVAILABLE; |
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
public static final int FAIL_REASON_MALFORMED_QUERY = Columns.RESULT_CODE_MALFORMED_QUERY; |
| 471 |
|
| 472 |
|
| 473 |
@IntDef(prefix = { "FAIL_" }, value = { |
| 474 |
FAIL_REASON_PROVIDER_NOT_FOUND, |
| 475 |
FAIL_REASON_FONT_LOAD_ERROR, |
| 476 |
FAIL_REASON_FONT_NOT_FOUND, |
| 477 |
FAIL_REASON_FONT_UNAVAILABLE, |
| 478 |
FAIL_REASON_MALFORMED_QUERY |
| 479 |
}) |
| 480 |
@Retention(RetentionPolicy.SOURCE) |
| 481 |
@interface FontRequestFailReason {} |
| 482 |
|
| 483 |
public FontRequestCallback() {} |
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
public void onTypefaceRetrieved(Typeface typeface) {} |
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
public void onTypefaceRequestFailed(@FontRequestFailReason int reason) {} |
| 504 |
} |
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
public static void requestFonts(@NonNull Context context, @NonNull FontRequest request, |
| 526 |
@NonNull Handler handler, @Nullable CancellationSignal cancellationSignal, |
| 527 |
@NonNull FontRequestCallback callback) { |
| 528 |
|
| 529 |
final Handler callerThreadHandler = new Handler(); |
| 530 |
final Typeface cachedTypeface = sTypefaceCache.get(request.getIdentifier()); |
| 531 |
if (cachedTypeface != null) { |
| 532 |
callerThreadHandler.post(() -> callback.onTypefaceRetrieved(cachedTypeface)); |
| 533 |
return; |
| 534 |
} |
| 535 |
|
| 536 |
handler.post(() -> { |
| 537 |
FontFamilyResult result; |
| 538 |
try { |
| 539 |
result = fetchFonts(context, cancellationSignal, request); |
| 540 |
} catch (NameNotFoundException e) { |
| 541 |
callerThreadHandler.post(() -> callback.onTypefaceRequestFailed( |
| 542 |
FontRequestCallback.FAIL_REASON_PROVIDER_NOT_FOUND)); |
| 543 |
return; |
| 544 |
} |
| 545 |
|
| 546 |
|
| 547 |
final Typeface anotherCachedTypeface = sTypefaceCache.get(request.getIdentifier()); |
| 548 |
if (anotherCachedTypeface != null) { |
| 549 |
callerThreadHandler.post(() -> callback.onTypefaceRetrieved(anotherCachedTypeface)); |
| 550 |
return; |
| 551 |
} |
| 552 |
|
| 553 |
if (result.getStatusCode() != FontFamilyResult.STATUS_OK) { |
| 554 |
switch (result.getStatusCode()) { |
| 555 |
case FontFamilyResult.STATUS_WRONG_CERTIFICATES: |
| 556 |
callerThreadHandler.post(() -> callback.onTypefaceRequestFailed( |
| 557 |
FontRequestCallback.FAIL_REASON_WRONG_CERTIFICATES)); |
| 558 |
return; |
| 559 |
case FontFamilyResult.STATUS_UNEXPECTED_DATA_PROVIDED: |
| 560 |
callerThreadHandler.post(() -> callback.onTypefaceRequestFailed( |
| 561 |
FontRequestCallback.FAIL_REASON_FONT_LOAD_ERROR)); |
| 562 |
return; |
| 563 |
default: |
| 564 |
|
| 565 |
callerThreadHandler.post(() -> callback.onTypefaceRequestFailed( |
| 566 |
FontRequestCallback.FAIL_REASON_FONT_LOAD_ERROR)); |
| 567 |
return; |
| 568 |
} |
| 569 |
} |
| 570 |
|
| 571 |
final FontInfo[] fonts = result.getFonts(); |
| 572 |
if (fonts == null || fonts.length == 0) { |
| 573 |
callerThreadHandler.post(() -> callback.onTypefaceRequestFailed( |
| 574 |
FontRequestCallback.FAIL_REASON_FONT_NOT_FOUND)); |
| 575 |
return; |
| 576 |
} |
| 577 |
for (final FontInfo font : fonts) { |
| 578 |
if (font.getResultCode() != Columns.RESULT_CODE_OK) { |
| 579 |
|
| 580 |
|
| 581 |
final int resultCode = font.getResultCode(); |
| 582 |
if (resultCode < 0) { |
| 583 |
|
| 584 |
callerThreadHandler.post(() -> callback.onTypefaceRequestFailed( |
| 585 |
FontRequestCallback.FAIL_REASON_FONT_LOAD_ERROR)); |
| 586 |
} else { |
| 587 |
callerThreadHandler.post(() -> callback.onTypefaceRequestFailed( |
| 588 |
resultCode)); |
| 589 |
} |
| 590 |
return; |
| 591 |
} |
| 592 |
} |
| 593 |
|
| 594 |
final Typeface typeface = buildTypeface(context, cancellationSignal, fonts); |
| 595 |
if (typeface == null) { |
| 596 |
|
| 597 |
|
| 598 |
callerThreadHandler.post(() -> callback.onTypefaceRequestFailed( |
| 599 |
FontRequestCallback.FAIL_REASON_FONT_LOAD_ERROR)); |
| 600 |
return; |
| 601 |
} |
| 602 |
|
| 603 |
sTypefaceCache.put(request.getIdentifier(), typeface); |
| 604 |
callerThreadHandler.post(() -> callback.onTypefaceRetrieved(typeface)); |
| 605 |
}); |
| 606 |
} |
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
public static @NonNull FontFamilyResult fetchFonts( |
| 624 |
@NonNull Context context, @Nullable CancellationSignal cancellationSignal, |
| 625 |
@NonNull FontRequest request) throws NameNotFoundException { |
| 626 |
if (context.isRestricted()) { |
| 627 |
|
| 628 |
return new FontFamilyResult(FontFamilyResult.STATUS_REJECTED, null); |
| 629 |
} |
| 630 |
ProviderInfo providerInfo = getProvider(context.getPackageManager(), request); |
| 631 |
if (providerInfo == null) { |
| 632 |
return new FontFamilyResult(FontFamilyResult.STATUS_WRONG_CERTIFICATES, null); |
| 633 |
|
| 634 |
} |
| 635 |
try { |
| 636 |
FontInfo[] fonts = getFontFromProvider( |
| 637 |
context, request, providerInfo.authority, cancellationSignal); |
| 638 |
return new FontFamilyResult(FontFamilyResult.STATUS_OK, fonts); |
| 639 |
} catch (IllegalArgumentException e) { |
| 640 |
return new FontFamilyResult(FontFamilyResult.STATUS_UNEXPECTED_DATA_PROVIDED, null); |
| 641 |
} |
| 642 |
} |
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
public static Typeface buildTypeface(@NonNull Context context, |
| 657 |
@Nullable CancellationSignal cancellationSignal, @NonNull FontInfo[] fonts) { |
| 658 |
if (context.isRestricted()) { |
| 659 |
|
| 660 |
return null; |
| 661 |
} |
| 662 |
final Map<Uri, ByteBuffer> uriBuffer = |
| 663 |
prepareFontData(context, fonts, cancellationSignal); |
| 664 |
if (uriBuffer.isEmpty()) { |
| 665 |
return null; |
| 666 |
} |
| 667 |
|
| 668 |
FontFamily.Builder familyBuilder = null; |
| 669 |
for (FontInfo fontInfo : fonts) { |
| 670 |
final ByteBuffer buffer = uriBuffer.get(fontInfo.getUri()); |
| 671 |
if (buffer == null) { |
| 672 |
continue; |
| 673 |
} |
| 674 |
try { |
| 675 |
final Font font = new Font.Builder(buffer) |
| 676 |
.setWeight(fontInfo.getWeight()) |
| 677 |
.setSlant(fontInfo.isItalic() |
| 678 |
? FontStyle.FONT_SLANT_ITALIC : FontStyle.FONT_SLANT_UPRIGHT) |
| 679 |
.setTtcIndex(fontInfo.getTtcIndex()) |
| 680 |
.setFontVariationSettings(fontInfo.getAxes()) |
| 681 |
.build(); |
| 682 |
if (familyBuilder == null) { |
| 683 |
familyBuilder = new FontFamily.Builder(font); |
| 684 |
} else { |
| 685 |
familyBuilder.addFont(font); |
| 686 |
} |
| 687 |
} catch (IllegalArgumentException e) { |
| 688 |
|
| 689 |
|
| 690 |
return null; |
| 691 |
} catch (IOException e) { |
| 692 |
continue; |
| 693 |
} |
| 694 |
} |
| 695 |
if (familyBuilder == null) { |
| 696 |
return null; |
| 697 |
} |
| 698 |
|
| 699 |
final FontFamily family = familyBuilder.build(); |
| 700 |
|
| 701 |
final FontStyle normal = new FontStyle(FontStyle.FONT_WEIGHT_NORMAL, |
| 702 |
FontStyle.FONT_SLANT_UPRIGHT); |
| 703 |
Font bestFont = family.getFont(0); |
| 704 |
int bestScore = normal.getMatchScore(bestFont.getStyle()); |
| 705 |
for (int i = 1; i < family.getSize(); ++i) { |
| 706 |
final Font candidate = family.getFont(i); |
| 707 |
final int score = normal.getMatchScore(candidate.getStyle()); |
| 708 |
if (score < bestScore) { |
| 709 |
bestFont = candidate; |
| 710 |
bestScore = score; |
| 711 |
} |
| 712 |
} |
| 713 |
return new Typeface.CustomFallbackBuilder(family).setStyle(bestFont.getStyle()).build(); |
| 714 |
} |
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
private static Map<Uri, ByteBuffer> prepareFontData(Context context, FontInfo[] fonts, |
| 727 |
CancellationSignal cancellationSignal) { |
| 728 |
final HashMap<Uri, ByteBuffer> out = new HashMap<>(); |
| 729 |
final ContentResolver resolver = context.getContentResolver(); |
| 730 |
|
| 731 |
for (FontInfo font : fonts) { |
| 732 |
if (font.getResultCode() != Columns.RESULT_CODE_OK) { |
| 733 |
continue; |
| 734 |
} |
| 735 |
|
| 736 |
final Uri uri = font.getUri(); |
| 737 |
if (out.containsKey(uri)) { |
| 738 |
continue; |
| 739 |
} |
| 740 |
|
| 741 |
ByteBuffer buffer = null; |
| 742 |
try (final ParcelFileDescriptor pfd = |
| 743 |
resolver.openFileDescriptor(uri, "r", cancellationSignal)) { |
| 744 |
if (pfd != null) { |
| 745 |
try (final FileInputStream fis = |
| 746 |
new FileInputStream(pfd.getFileDescriptor())) { |
| 747 |
final FileChannel fileChannel = fis.getChannel(); |
| 748 |
final long size = fileChannel.size(); |
| 749 |
buffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, size); |
| 750 |
} catch (IOException e) { |
| 751 |
|
| 752 |
} |
| 753 |
} |
| 754 |
} catch (IOException e) { |
| 755 |
|
| 756 |
} |
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
out.put(uri, buffer); |
| 761 |
} |
| 762 |
return Collections.unmodifiableMap(out); |
| 763 |
} |
| 764 |
|
| 765 |
|
| 766 |
@VisibleForTesting |
| 767 |
public static @Nullable ProviderInfo getProvider( |
| 768 |
PackageManager packageManager, FontRequest request) throws NameNotFoundException { |
| 769 |
String providerAuthority = request.getProviderAuthority(); |
| 770 |
ProviderInfo info = packageManager.resolveContentProvider(providerAuthority, 0); |
| 771 |
if (info == null) { |
| 772 |
throw new NameNotFoundException("No package found for authority: " + providerAuthority); |
| 773 |
} |
| 774 |
|
| 775 |
if (!info.packageName.equals(request.getProviderPackage())) { |
| 776 |
throw new NameNotFoundException("Found content provider " + providerAuthority |
| 777 |
+ ", but package was not " + request.getProviderPackage()); |
| 778 |
} |
| 779 |
|
| 780 |
if (info.applicationInfo.isSystemApp()) { |
| 781 |
return info; |
| 782 |
} |
| 783 |
|
| 784 |
List<byte[]> signatures; |
| 785 |
PackageInfo packageInfo = packageManager.getPackageInfo(info.packageName, |
| 786 |
PackageManager.GET_SIGNATURES); |
| 787 |
signatures = convertToByteArrayList(packageInfo.signatures); |
| 788 |
Collections.sort(signatures, sByteArrayComparator); |
| 789 |
|
| 790 |
List<List<byte[]>> requestCertificatesList = request.getCertificates(); |
| 791 |
for (int i = 0; i < requestCertificatesList.size(); ++i) { |
| 792 |
|
| 793 |
List<byte[]> requestSignatures = new ArrayList<>(requestCertificatesList.get(i)); |
| 794 |
Collections.sort(requestSignatures, sByteArrayComparator); |
| 795 |
if (equalsByteArrayList(signatures, requestSignatures)) { |
| 796 |
return info; |
| 797 |
} |
| 798 |
} |
| 799 |
return null; |
| 800 |
} |
| 801 |
|
| 802 |
private static final Comparator<byte[]> sByteArrayComparator = (l, r) -> { |
| 803 |
if (l.length != r.length) { |
| 804 |
return l.length - r.length; |
| 805 |
} |
| 806 |
for (int i = 0; i < l.length; ++i) { |
| 807 |
if (l[i] != r[i]) { |
| 808 |
return l[i] - r[i]; |
| 809 |
} |
| 810 |
} |
| 811 |
return 0; |
| 812 |
}; |
| 813 |
|
| 814 |
private static boolean equalsByteArrayList( |
| 815 |
List<byte[]> signatures, List<byte[]> requestSignatures) { |
| 816 |
if (signatures.size() != requestSignatures.size()) { |
| 817 |
return false; |
| 818 |
} |
| 819 |
for (int i = 0; i < signatures.size(); ++i) { |
| 820 |
if (!Arrays.equals(signatures.get(i), requestSignatures.get(i))) { |
| 821 |
return false; |
| 822 |
} |
| 823 |
} |
| 824 |
return true; |
| 825 |
} |
| 826 |
|
| 827 |
private static List<byte[]> convertToByteArrayList(Signature[] signatures) { |
| 828 |
List<byte[]> shas = new ArrayList<>(); |
| 829 |
for (int i = 0; i < signatures.length; ++i) { |
| 830 |
shas.add(signatures[i].toByteArray()); |
| 831 |
} |
| 832 |
return shas; |
| 833 |
} |
| 834 |
|
| 835 |
|
| 836 |
@VisibleForTesting |
| 837 |
public static @NonNull FontInfo[] getFontFromProvider( |
| 838 |
Context context, FontRequest request, String authority, |
| 839 |
CancellationSignal cancellationSignal) { |
| 840 |
ArrayList<FontInfo> result = new ArrayList<>(); |
| 841 |
final Uri uri = new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT) |
| 842 |
.authority(authority) |
| 843 |
.build(); |
| 844 |
final Uri fileBaseUri = new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT) |
| 845 |
.authority(authority) |
| 846 |
.appendPath("file") |
| 847 |
.build(); |
| 848 |
try (Cursor cursor = context.getContentResolver().query(uri, new String[] { Columns._ID, |
| 849 |
Columns.FILE_ID, Columns.TTC_INDEX, Columns.VARIATION_SETTINGS, |
| 850 |
Columns.WEIGHT, Columns.ITALIC, Columns.RESULT_CODE }, |
| 851 |
"query = ?", new String[] { request.getQuery() }, null, cancellationSignal);) { |
| 852 |
|
| 853 |
|
| 854 |
if (cursor != null && cursor.getCount() > 0) { |
| 855 |
final int resultCodeColumnIndex = cursor.getColumnIndex(Columns.RESULT_CODE); |
| 856 |
result = new ArrayList<>(); |
| 857 |
final int idColumnIndex = cursor.getColumnIndexOrThrow(Columns._ID); |
| 858 |
final int fileIdColumnIndex = cursor.getColumnIndex(Columns.FILE_ID); |
| 859 |
final int ttcIndexColumnIndex = cursor.getColumnIndex(Columns.TTC_INDEX); |
| 860 |
final int vsColumnIndex = cursor.getColumnIndex(Columns.VARIATION_SETTINGS); |
| 861 |
final int weightColumnIndex = cursor.getColumnIndex(Columns.WEIGHT); |
| 862 |
final int italicColumnIndex = cursor.getColumnIndex(Columns.ITALIC); |
| 863 |
while (cursor.moveToNext()) { |
| 864 |
int resultCode = resultCodeColumnIndex != -1 |
| 865 |
? cursor.getInt(resultCodeColumnIndex) : Columns.RESULT_CODE_OK; |
| 866 |
final int ttcIndex = ttcIndexColumnIndex != -1 |
| 867 |
? cursor.getInt(ttcIndexColumnIndex) : 0; |
| 868 |
final String variationSettings = vsColumnIndex != -1 |
| 869 |
? cursor.getString(vsColumnIndex) : null; |
| 870 |
|
| 871 |
Uri fileUri; |
| 872 |
if (fileIdColumnIndex == -1) { |
| 873 |
long id = cursor.getLong(idColumnIndex); |
| 874 |
fileUri = ContentUris.withAppendedId(uri, id); |
| 875 |
} else { |
| 876 |
long id = cursor.getLong(fileIdColumnIndex); |
| 877 |
fileUri = ContentUris.withAppendedId(fileBaseUri, id); |
| 878 |
} |
| 879 |
int weight; |
| 880 |
boolean italic; |
| 881 |
if (weightColumnIndex != -1 && italicColumnIndex != -1) { |
| 882 |
weight = cursor.getInt(weightColumnIndex); |
| 883 |
italic = cursor.getInt(italicColumnIndex) == 1; |
| 884 |
} else { |
| 885 |
weight = Typeface.Builder.NORMAL_WEIGHT; |
| 886 |
italic = false; |
| 887 |
} |
| 888 |
FontVariationAxis[] axes = |
| 889 |
FontVariationAxis.fromFontVariationSettings(variationSettings); |
| 890 |
result.add(new FontInfo(fileUri, ttcIndex, axes, weight, italic, resultCode)); |
| 891 |
} |
| 892 |
} |
| 893 |
} |
| 894 |
return result.toArray(new FontInfo[0]); |
| 895 |
} |
| 896 |
} |