1 /* 2 * Copyright (C) 2019 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 com.android.customization.model.theme.custom; 17 18 import static com.android.customization.model.ResourceConstants.ANDROID_PACKAGE; 19 import static com.android.customization.model.ResourceConstants.CONFIG_BODY_FONT_FAMILY; 20 import static com.android.customization.model.ResourceConstants.CONFIG_HEADLINE_FONT_FAMILY; 21 import static com.android.customization.model.ResourceConstants.OVERLAY_CATEGORY_FONT; 22 23 import android.content.Context; 24 import android.content.pm.PackageManager; 25 import android.content.pm.PackageManager.NameNotFoundException; 26 import android.content.res.Resources; 27 import android.content.res.Resources.NotFoundException; 28 import android.graphics.Typeface; 29 import android.util.Log; 30 31 import com.android.customization.model.ResourceConstants; 32 import com.android.customization.model.theme.OverlayManagerCompat; 33 import com.android.customization.model.theme.custom.ThemeComponentOption.FontOption; 34 import com.android.wallpaper.R; 35 36 /** 37 * Implementation of {@link ThemeComponentOptionProvider} that reads {@link FontOption}s from 38 * font overlays. 39 */ 40 public class FontOptionsProvider extends ThemeComponentOptionProvider<FontOption> { 41 42 private static final String TAG = "FontOptionsProvider"; 43 FontOptionsProvider(Context context, OverlayManagerCompat manager)44 public FontOptionsProvider(Context context, OverlayManagerCompat manager) { 45 super(context, manager, OVERLAY_CATEGORY_FONT); 46 } 47 48 @Override loadOptions()49 protected void loadOptions() { 50 addDefault(); 51 for (String overlayPackage : mOverlayPackages) { 52 try { 53 Resources overlayRes = getOverlayResources(overlayPackage); 54 Typeface headlineFont = Typeface.create( 55 getFontFamily(overlayPackage, overlayRes, CONFIG_HEADLINE_FONT_FAMILY), 56 Typeface.NORMAL); 57 Typeface bodyFont = Typeface.create( 58 getFontFamily(overlayPackage, overlayRes, CONFIG_BODY_FONT_FAMILY), 59 Typeface.NORMAL); 60 PackageManager pm = mContext.getPackageManager(); 61 String label = pm.getApplicationInfo(overlayPackage, 0).loadLabel(pm).toString(); 62 mOptions.add(new FontOption(overlayPackage, label, headlineFont, bodyFont)); 63 } catch (NameNotFoundException | NotFoundException e) { 64 Log.w(TAG, String.format("Couldn't load font overlay %s, will skip it", 65 overlayPackage), e); 66 } 67 } 68 } 69 addDefault()70 private void addDefault() { 71 Resources system = Resources.getSystem(); 72 Typeface headlineFont = Typeface.create(system.getString(system.getIdentifier( 73 ResourceConstants.CONFIG_HEADLINE_FONT_FAMILY,"string", ANDROID_PACKAGE)), 74 Typeface.NORMAL); 75 Typeface bodyFont = Typeface.create(system.getString(system.getIdentifier( 76 ResourceConstants.CONFIG_BODY_FONT_FAMILY, 77 "string", ANDROID_PACKAGE)), 78 Typeface.NORMAL); 79 mOptions.add(new FontOption(null, mContext.getString(R.string.default_theme_title), 80 headlineFont, bodyFont)); 81 } 82 getFontFamily(String overlayPackage, Resources overlayRes, String configName)83 private String getFontFamily(String overlayPackage, Resources overlayRes, String configName) { 84 return overlayRes.getString(overlayRes.getIdentifier(configName, "string", overlayPackage)); 85 } 86 } 87