dniz fadcb7fc31 Add a MaterialAttributes#resolveAttribute method that doesn't throw, a
MaterialColors#getColor method that allows for a default value, and a MaterialResources#getColorStateList that accepts a TintTypedArray

PiperOrigin-RevId: 219170329
2018-11-01 16:07:21 -07:00

62 lines
2.1 KiB
Java

/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.android.material.resources;
import static android.support.annotation.RestrictTo.Scope.LIBRARY_GROUP;
import android.content.Context;
import android.support.annotation.AttrRes;
import android.support.annotation.Nullable;
import android.support.annotation.RestrictTo;
import android.util.TypedValue;
import android.view.View;
/**
* Utility methods to work with attributes.
*
* @hide
*/
@RestrictTo(LIBRARY_GROUP)
public class MaterialAttributes {
public static TypedValue resolveAttributeOrThrow(
View componentView, @AttrRes int attributeResId) {
Context context = componentView.getContext();
TypedValue typedValue = resolveAttribute(context, attributeResId);
if (typedValue == null) {
String errorMessage =
"The %1$s view requires a value for the %2$s attribute to be set in your app theme. "
+ "You can either set the attribute in your theme or "
+ "update your theme to inherit from Theme.MaterialComponents (or a descendant).";
throw new IllegalArgumentException(
String.format(
errorMessage,
componentView.getClass().getCanonicalName(),
context.getResources().getResourceName(attributeResId)));
}
return typedValue;
}
@Nullable
public static TypedValue resolveAttribute(Context context, @AttrRes int attributeResId) {
TypedValue typedValue = new TypedValue();
if (context.getTheme().resolveAttribute(attributeResId, typedValue, true)) {
return typedValue;
}
return null;
}
}