public class

VectorEnabledTintResources

extends androidx.appcompat.widget.ResourcesWrapper

 java.lang.Object

↳Resources

↳androidx.appcompat.widget.ResourcesWrapper

↳androidx.appcompat.widget.VectorEnabledTintResources

Gradle dependencies

compile group: 'androidx.appcompat', name: 'appcompat-resources', version: '1.6.0-alpha04'

  • groupId: androidx.appcompat
  • artifactId: appcompat-resources
  • version: 1.6.0-alpha04

Artifact androidx.appcompat:appcompat-resources:1.6.0-alpha04 it located at Google repository (https://maven.google.com/)

Androidx class mapping:

androidx.appcompat.widget.VectorEnabledTintResources android.support.v7.widget.VectorEnabledTintResources

Overview

This class allows us to intercept calls so that we can tint resources (if applicable), and inflate vector resources from within drawable containers pre-L.

Summary

Fields
public static final intMAX_SDK_WHERE_REQUIRED

The maximum API level where this class is needed.

Constructors
publicVectorEnabledTintResources(Context context, Resources res)

Methods
public DrawablegetDrawable(int id)

We intercept this call so that we tint the result (if applicable).

public static booleanisCompatVectorFromResourcesEnabled()

Returns whether vector drawables on older platforms (< API 21) can be accessed from within resources.

public static voidsetCompatVectorFromResourcesEnabled(boolean enabled)

Sets whether vector drawables on older platforms (< API 21) can be used within resources.

public static booleanshouldBeUsed()

from androidx.appcompat.widget.ResourcesWrappergetAnimation, getBoolean, getColor, getColorStateList, getConfiguration, getDimension, getDimensionPixelOffset, getDimensionPixelSize, getDisplayMetrics, getDrawable, getDrawableForDensity, getDrawableForDensity, getFraction, getIdentifier, getIntArray, getInteger, getLayout, getMovie, getQuantityString, getQuantityString, getQuantityText, getResourceEntryName, getResourceName, getResourcePackageName, getResourceTypeName, getString, getString, getStringArray, getText, getText, getTextArray, getValue, getValue, getValueForDensity, getXml, obtainAttributes, obtainTypedArray, openRawResource, openRawResource, openRawResourceFd, parseBundleExtra, parseBundleExtras, updateConfiguration
from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Fields

public static final int MAX_SDK_WHERE_REQUIRED

The maximum API level where this class is needed.

Constructors

public VectorEnabledTintResources(Context context, Resources res)

Methods

public static boolean shouldBeUsed()

public Drawable getDrawable(int id)

We intercept this call so that we tint the result (if applicable). This is needed for things like s which can retrieve their children via this method.

public static void setCompatVectorFromResourcesEnabled(boolean enabled)

Sets whether vector drawables on older platforms (< API 21) can be used within resources.

public static boolean isCompatVectorFromResourcesEnabled()

Returns whether vector drawables on older platforms (< API 21) can be accessed from within resources.

See also: VectorEnabledTintResources.setCompatVectorFromResourcesEnabled(boolean)

Source

/*
 * Copyright (C) 2015 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 androidx.appcompat.widget;

import static androidx.annotation.RestrictTo.Scope.LIBRARY_GROUP_PREFIX;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Build;

import androidx.annotation.NonNull;
import androidx.annotation.RestrictTo;

import java.lang.ref.WeakReference;

/**
 * This class allows us to intercept calls so that we can tint resources (if applicable), and
 * inflate vector resources from within drawable containers pre-L.
 *
 * @hide
 */
@RestrictTo(LIBRARY_GROUP_PREFIX)
public class VectorEnabledTintResources extends ResourcesWrapper {
    private static boolean sCompatVectorFromResourcesEnabled = false;

    public static boolean shouldBeUsed() {
        return isCompatVectorFromResourcesEnabled()
                && Build.VERSION.SDK_INT <= MAX_SDK_WHERE_REQUIRED;
    }

    /**
     * The maximum API level where this class is needed.
     */
    public static final int MAX_SDK_WHERE_REQUIRED = 20;

    private final WeakReference<Context> mContextRef;

    @SuppressWarnings("deprecation")
    public VectorEnabledTintResources(@NonNull final Context context,
            @NonNull final Resources res) {
        super(res);
        mContextRef = new WeakReference<>(context);
    }

    /**
     * We intercept this call so that we tint the result (if applicable). This is needed for
     * things like {@link android.graphics.drawable.DrawableContainer}s which can retrieve
     * their children via this method.
     */
    @Override
    public Drawable getDrawable(int id) throws NotFoundException {
        final Context context = mContextRef.get();
        if (context != null) {
            return ResourceManagerInternal.get().onDrawableLoadedFromResources(context, this, id);
        } else {
            // Delegate to the Resources implementation, NOT the superclass implementation. This
            // method is re-entrant along the call path, e.g. for nested drawables, and we need to
            // avoid passing control to a separate (e.g. wrapped) Resources object.
            return getDrawableCanonical(id);
        }
    }

    /**
     * Sets whether vector drawables on older platforms (< API 21) can be used within
     * {@link android.graphics.drawable.DrawableContainer} resources.
     */
    public static void setCompatVectorFromResourcesEnabled(boolean enabled) {
        sCompatVectorFromResourcesEnabled = enabled;
    }

    /**
     * Returns whether vector drawables on older platforms (< API 21) can be accessed from within
     * resources.
     *
     * @see #setCompatVectorFromResourcesEnabled(boolean)
     */
    public static boolean isCompatVectorFromResourcesEnabled() {
        return sCompatVectorFromResourcesEnabled;
    }
}