Brad Stenning | e07508c | 2018-11-03 21:45:07 -0700 | [diff] [blame] | 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 | package com.android.car.theme; |
| 17 | |
| 18 | import android.content.Context; |
| 19 | import android.content.res.TypedArray; |
| 20 | import android.graphics.drawable.Drawable; |
| 21 | |
| 22 | /** |
| 23 | * Various utility methods associated with theming. |
| 24 | */ |
| 25 | public class Themes { |
| 26 | |
| 27 | /** |
davidln | 81a0299 | 2018-11-06 17:17:38 -0800 | [diff] [blame^] | 28 | * Returns the color assigned to the given attribute. |
Brad Stenning | e07508c | 2018-11-03 21:45:07 -0700 | [diff] [blame] | 29 | */ |
| 30 | public static int getAttrColor(Context context, int attr) { |
| 31 | TypedArray ta = context.obtainStyledAttributes(new int[]{attr}); |
| 32 | int colorAccent = ta.getColor(0, 0); |
| 33 | ta.recycle(); |
| 34 | return colorAccent; |
| 35 | } |
| 36 | |
| 37 | /** |
davidln | 81a0299 | 2018-11-06 17:17:38 -0800 | [diff] [blame^] | 38 | * Returns the boolean assigned to the given attribute. |
Brad Stenning | e07508c | 2018-11-03 21:45:07 -0700 | [diff] [blame] | 39 | */ |
| 40 | public static boolean getAttrBoolean(Context context, int attr) { |
| 41 | TypedArray ta = context.obtainStyledAttributes(new int[]{attr}); |
| 42 | boolean value = ta.getBoolean(0, false); |
| 43 | ta.recycle(); |
| 44 | return value; |
| 45 | } |
| 46 | |
| 47 | /** |
davidln | 81a0299 | 2018-11-06 17:17:38 -0800 | [diff] [blame^] | 48 | * Returns the Drawable assigned to the given attribute. |
Brad Stenning | e07508c | 2018-11-03 21:45:07 -0700 | [diff] [blame] | 49 | */ |
| 50 | public static Drawable getAttrDrawable(Context context, int attr) { |
| 51 | TypedArray ta = context.obtainStyledAttributes(new int[]{attr}); |
| 52 | Drawable value = ta.getDrawable(0); |
| 53 | ta.recycle(); |
| 54 | return value; |
| 55 | } |
| 56 | |
| 57 | /** |
davidln | 81a0299 | 2018-11-06 17:17:38 -0800 | [diff] [blame^] | 58 | * Returns the Integer assigned to the given attribute. |
Brad Stenning | e07508c | 2018-11-03 21:45:07 -0700 | [diff] [blame] | 59 | */ |
| 60 | public static int getAttrInteger(Context context, int attr) { |
| 61 | TypedArray ta = context.obtainStyledAttributes(new int[]{attr}); |
| 62 | int value = ta.getInteger(0, 0); |
| 63 | ta.recycle(); |
| 64 | return value; |
| 65 | } |
davidln | 81a0299 | 2018-11-06 17:17:38 -0800 | [diff] [blame^] | 66 | |
| 67 | /** |
| 68 | * Returns the identifier of the resolved resource assigned to the given attribute. |
| 69 | */ |
| 70 | public static int getAttrResourceId(Context context, int attr) { |
| 71 | TypedArray ta = context.obtainStyledAttributes(new int[]{attr}); |
| 72 | int resId = ta.getResourceId(0, 0); |
| 73 | ta.recycle(); |
| 74 | return resId; |
| 75 | } |
Brad Stenning | e07508c | 2018-11-03 21:45:07 -0700 | [diff] [blame] | 76 | } |