1 /* 2 * Copyright (C) 2018 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 com.android.dialer.theme.base.impl; 18 19 import android.content.Context; 20 import android.content.res.TypedArray; 21 import android.support.annotation.ColorInt; 22 import android.support.annotation.StyleRes; 23 import android.view.ContextThemeWrapper; 24 import android.view.LayoutInflater; 25 import com.android.dialer.common.Assert; 26 import com.android.dialer.theme.base.R; 27 import com.android.dialer.theme.base.Theme; 28 import javax.inject.Singleton; 29 30 /** Utility for fetching */ 31 @SuppressWarnings("unused") 32 @Singleton 33 public class AospThemeImpl implements Theme { 34 35 private int colorIcon = -1; 36 private int colorIconSecondary = -1; 37 private int colorPrimary = -1; 38 private int colorPrimaryDark = -1; 39 private int colorAccent = -1; 40 private int textColorPrimary = -1; 41 private int textColorSecondary = -1; 42 private int textColorPrimaryInverse = -1; 43 private int textColorHint = -1; 44 private int colorBackground = -1; 45 private int colorBackgroundFloating = -1; 46 private int colorTextOnUnthemedDarkBackground = -1; 47 private int colorIconOnUnthemedDarkBackground = -1; 48 AospThemeImpl(Context context)49 public AospThemeImpl(Context context) { 50 51 context = context.getApplicationContext(); 52 context.setTheme(getApplicationThemeRes()); 53 TypedArray array = 54 context 55 .getTheme() 56 .obtainStyledAttributes( 57 getApplicationThemeRes(), 58 new int[] { 59 android.R.attr.colorPrimary, 60 android.R.attr.colorPrimaryDark, 61 android.R.attr.colorAccent, 62 android.R.attr.textColorPrimary, 63 android.R.attr.textColorSecondary, 64 android.R.attr.textColorPrimaryInverse, 65 android.R.attr.textColorHint, 66 android.R.attr.colorBackground, 67 android.R.attr.colorBackgroundFloating, 68 R.attr.colorIcon, 69 R.attr.colorIconSecondary, 70 R.attr.colorTextOnUnthemedDarkBackground, 71 R.attr.colorIconOnUnthemedDarkBackground, 72 }); 73 colorPrimary = array.getColor(/* index= */ 0, /* defValue= */ -1); 74 colorPrimaryDark = array.getColor(/* index= */ 1, /* defValue= */ -1); 75 colorAccent = array.getColor(/* index= */ 2, /* defValue= */ -1); 76 textColorPrimary = array.getColor(/* index= */ 3, /* defValue= */ -1); 77 textColorSecondary = array.getColor(/* index= */ 4, /* defValue= */ -1); 78 textColorPrimaryInverse = array.getColor(/* index= */ 5, /* defValue= */ -1); 79 textColorHint = array.getColor(/* index= */ 6, /* defValue= */ -1); 80 colorBackground = array.getColor(/* index= */ 7, /* defValue= */ -1); 81 colorBackgroundFloating = array.getColor(/* index= */ 8, /* defValue= */ -1); 82 colorIcon = array.getColor(/* index= */ 9, /* defValue= */ -1); 83 colorIconSecondary = array.getColor(/* index= */ 10, /* defValue= */ -1); 84 colorTextOnUnthemedDarkBackground = array.getColor(/* index= */ 11, /* defValue= */ -1); 85 colorIconOnUnthemedDarkBackground = array.getColor(/* index= */ 12, /* defValue= */ -1); 86 array.recycle(); 87 } 88 89 /** 90 * Returns the {@link Theme} that the application is using. Activities should check this value if 91 * their custom style needs to customize further based on the application theme. 92 */ 93 @Override getTheme()94 public @Type int getTheme() { 95 // TODO(a bug): add share prefs check to configure this 96 return LIGHT; 97 } 98 99 @Override getApplicationThemeRes()100 public @StyleRes int getApplicationThemeRes() { 101 switch (getTheme()) { 102 case DARK: 103 return R.style.Dialer_Dark_ThemeBase_NoActionBar; 104 case LIGHT: 105 return R.style.Dialer_ThemeBase_NoActionBar; 106 case UNKNOWN: 107 default: 108 throw Assert.createIllegalStateFailException("Theme hasn't been set yet."); 109 } 110 } 111 112 @Override getThemedContext(Context context)113 public Context getThemedContext(Context context) { 114 return new ContextThemeWrapper(context, getApplicationThemeRes()); 115 } 116 117 @Override getThemedLayoutInflator(LayoutInflater inflater)118 public LayoutInflater getThemedLayoutInflator(LayoutInflater inflater) { 119 return inflater.cloneInContext(getThemedContext(inflater.getContext())); 120 } 121 122 @Override getColorIcon()123 public @ColorInt int getColorIcon() { 124 Assert.checkArgument(colorIcon != -1); 125 return colorIcon; 126 } 127 128 @Override getColorIconSecondary()129 public @ColorInt int getColorIconSecondary() { 130 Assert.checkArgument(colorIconSecondary != -1); 131 return colorIconSecondary; 132 } 133 134 @Override getColorPrimary()135 public @ColorInt int getColorPrimary() { 136 Assert.checkArgument(colorPrimary != -1); 137 return colorPrimary; 138 } 139 140 @Override getColorPrimaryDark()141 public int getColorPrimaryDark() { 142 Assert.checkArgument(colorPrimaryDark != -1); 143 return colorPrimaryDark; 144 } 145 146 @Override getColorAccent()147 public @ColorInt int getColorAccent() { 148 Assert.checkArgument(colorAccent != -1); 149 return colorAccent; 150 } 151 152 @Override getTextColorSecondary()153 public @ColorInt int getTextColorSecondary() { 154 Assert.checkArgument(textColorSecondary != -1); 155 return textColorSecondary; 156 } 157 158 @Override getTextColorPrimary()159 public @ColorInt int getTextColorPrimary() { 160 Assert.checkArgument(textColorPrimary != -1); 161 return textColorPrimary; 162 } 163 164 @Override getColorTextOnUnthemedDarkBackground()165 public @ColorInt int getColorTextOnUnthemedDarkBackground() { 166 Assert.checkArgument(colorTextOnUnthemedDarkBackground != -1); 167 return colorTextOnUnthemedDarkBackground; 168 } 169 170 @Override getColorIconOnUnthemedDarkBackground()171 public @ColorInt int getColorIconOnUnthemedDarkBackground() { 172 Assert.checkArgument(colorIconOnUnthemedDarkBackground != -1); 173 return colorIconOnUnthemedDarkBackground; 174 } 175 } 176