Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e49ba391 authored by Diego Perez's avatar Diego Perez
Browse files

Workaround for broken BitmapShader in AdaptiveIconDrawable

This removes the need of using BitmapShader while we debug the problem
in layoutlib.

Bug: 36204957
Test: Added new test for adaptive icons
Change-Id: I4ff9968b996a1563be8caa0873e7aec8fb5cb151
parent 376c6471
Loading
Loading
Loading
Loading
+59 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 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
 *
 *      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 android.graphics.drawable;

import com.android.tools.layoutlib.annotations.LayoutlibDelegate;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.drawable.AdaptiveIconDrawable.LayerState;

/**
 * Delegate used to provide new implementation of a select few methods of {@link
 * AdaptiveIconDrawable}
 * <p>
 * Through the layoutlib_create tool, the original  methods of AdaptiveIconDrawable have been
 * replaced by calls to methods of the same name in this delegate class.
 */
@SuppressWarnings("unused")
public class AdaptiveIconDrawable_Delegate {
    @LayoutlibDelegate
    /*package*/ static void draw(AdaptiveIconDrawable thisDrawable, Canvas canvas) {
        // This is a workaround for the broken BitmapShader in layoutlib. This new draw methods
        // avoids the use of the shader.

        for (int i = 0; i < LayerState.N_CHILDREN; i++) {
            if (thisDrawable.mLayerState.mChildren[i] == null) {
                continue;
            }
            final Drawable dr = thisDrawable.mLayerState.mChildren[i].mDrawable;
            if (dr != null) {
                dr.draw(canvas);
            }
        }

        if (thisDrawable.mMaskBitmap != null) {
            Rect bounds = thisDrawable.getBounds();
            Paint paint = new Paint();
            paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
            canvas.drawBitmap(thisDrawable.mMaskBitmap, bounds.left, bounds.top, paint);
        }
    }
}
+6.2 KiB
Loading image diff...
+6 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>

<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
    <background android:drawable="@android:color/red" />
    <foreground android:drawable="@drawable/headset" />
</adaptive-icon>
 No newline at end of file
+14 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:padding="16dp"
              android:orientation="horizontal"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent">
    <ImageView
             android:layout_height="wrap_content"
             android:layout_width="wrap_content"
             android:src="@drawable/adaptive" />

</LinearLayout>
+16 −0
Original line number Diff line number Diff line
@@ -395,6 +395,22 @@ public class RenderTests extends RenderTestBase {
        renderAndVerify("fonts_test.xml", "font_test.png");
    }

    @Test
    public void testAdaptiveIcon() throws ClassNotFoundException {
        // Create the layout pull parser.
        LayoutPullParser parser = createLayoutPullParser("adaptive_icon.xml");
        // Create LayoutLibCallback.
        LayoutLibTestCallback layoutLibCallback =
                new LayoutLibTestCallback(getLogger(), mDefaultClassLoader);
        layoutLibCallback.initResources();

        SessionParams params = getSessionParams(parser, ConfigGenerator.NEXUS_5,
                layoutLibCallback, "Theme.Material.NoActionBar.Fullscreen", false,
                RenderingMode.V_SCROLL, 22);

        renderAndVerify(params, "adaptive_icon.png");
    }

    @Test
    public void testColorTypedValue() throws Exception {
        // Setup
Loading