cketcham 499261bb27 Add @NonNull and @Nullable annotations
PiperOrigin-RevId: 264899386
2019-08-27 16:32:43 -04:00

91 lines
2.8 KiB
Java

/*
* Copyright 2017 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
*
* https://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.drawable;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.Resources.NotFoundException;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.graphics.drawable.Drawable;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RestrictTo;
import androidx.annotation.RestrictTo.Scope;
import androidx.annotation.XmlRes;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Xml;
import java.io.IOException;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
/**
* Utils class for Drawables.
*
* @hide
*/
@RestrictTo(Scope.LIBRARY_GROUP)
public final class DrawableUtils {
private DrawableUtils() {}
/** Returns a tint filter for the given tint and mode. */
@Nullable
public static PorterDuffColorFilter updateTintFilter(
@NonNull Drawable drawable,
@Nullable ColorStateList tint,
@Nullable PorterDuff.Mode tintMode) {
if (tint == null || tintMode == null) {
return null;
}
final int color = tint.getColorForState(drawable.getState(), Color.TRANSPARENT);
return new PorterDuffColorFilter(color, tintMode);
}
@NonNull
public static AttributeSet parseDrawableXml(
@NonNull Context context, @XmlRes int id, @NonNull CharSequence startTag) {
try {
XmlPullParser parser = context.getResources().getXml(id);
int type;
do {
type = parser.next();
} while (type != XmlPullParser.START_TAG && type != XmlPullParser.END_DOCUMENT);
if (type != XmlPullParser.START_TAG) {
throw new XmlPullParserException("No start tag found");
}
if (!TextUtils.equals(parser.getName(), startTag)) {
throw new XmlPullParserException("Must have a <" + startTag + "> start tag");
}
AttributeSet attrs = Xml.asAttributeSet(parser);
return attrs;
} catch (XmlPullParserException | IOException e) {
NotFoundException exception =
new NotFoundException("Can't load badge resource ID #0x" + Integer.toHexString(id));
exception.initCause(e);
throw exception;
}
}
}