summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Diego Perez <diegoperez@google.com> 2017-03-14 12:01:53 +0000
committer Diego Perez <diegoperez@google.com> 2017-03-14 14:45:36 +0000
commite49ba3917240f2c5e9df75132850a4db45f57b9c (patch)
tree05cdad55b7643300d924dd7713fe3df896d46744
parent376c6471da391d09e6a9d840321865a6871ccf14 (diff)
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
-rw-r--r--tools/layoutlib/bridge/src/android/graphics/drawable/AdaptiveIconDrawable_Delegate.java59
-rw-r--r--tools/layoutlib/bridge/tests/res/testApp/MyApplication/golden/adaptive_icon.pngbin0 -> 6350 bytes
-rw-r--r--tools/layoutlib/bridge/tests/res/testApp/MyApplication/src/main/res/drawable/adaptive.xml6
-rw-r--r--tools/layoutlib/bridge/tests/res/testApp/MyApplication/src/main/res/layout/adaptive_icon.xml14
-rw-r--r--tools/layoutlib/bridge/tests/src/com/android/layoutlib/bridge/intensive/RenderTests.java16
-rw-r--r--tools/layoutlib/create/src/com/android/tools/layoutlib/create/CreateInfo.java3
6 files changed, 98 insertions, 0 deletions
diff --git a/tools/layoutlib/bridge/src/android/graphics/drawable/AdaptiveIconDrawable_Delegate.java b/tools/layoutlib/bridge/src/android/graphics/drawable/AdaptiveIconDrawable_Delegate.java
new file mode 100644
index 000000000000..7e9432dd71ef
--- /dev/null
+++ b/tools/layoutlib/bridge/src/android/graphics/drawable/AdaptiveIconDrawable_Delegate.java
@@ -0,0 +1,59 @@
+/*
+ * 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);
+ }
+ }
+}
diff --git a/tools/layoutlib/bridge/tests/res/testApp/MyApplication/golden/adaptive_icon.png b/tools/layoutlib/bridge/tests/res/testApp/MyApplication/golden/adaptive_icon.png
new file mode 100644
index 000000000000..7014ddbbb7da
--- /dev/null
+++ b/tools/layoutlib/bridge/tests/res/testApp/MyApplication/golden/adaptive_icon.png
Binary files differ
diff --git a/tools/layoutlib/bridge/tests/res/testApp/MyApplication/src/main/res/drawable/adaptive.xml b/tools/layoutlib/bridge/tests/res/testApp/MyApplication/src/main/res/drawable/adaptive.xml
new file mode 100644
index 000000000000..8f862c86873c
--- /dev/null
+++ b/tools/layoutlib/bridge/tests/res/testApp/MyApplication/src/main/res/drawable/adaptive.xml
@@ -0,0 +1,6 @@
+<?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
diff --git a/tools/layoutlib/bridge/tests/res/testApp/MyApplication/src/main/res/layout/adaptive_icon.xml b/tools/layoutlib/bridge/tests/res/testApp/MyApplication/src/main/res/layout/adaptive_icon.xml
new file mode 100644
index 000000000000..ca9fa55d5424
--- /dev/null
+++ b/tools/layoutlib/bridge/tests/res/testApp/MyApplication/src/main/res/layout/adaptive_icon.xml
@@ -0,0 +1,14 @@
+<?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>
+
diff --git a/tools/layoutlib/bridge/tests/src/com/android/layoutlib/bridge/intensive/RenderTests.java b/tools/layoutlib/bridge/tests/src/com/android/layoutlib/bridge/intensive/RenderTests.java
index 7199781349a6..2b5e0f9dd5fa 100644
--- a/tools/layoutlib/bridge/tests/src/com/android/layoutlib/bridge/intensive/RenderTests.java
+++ b/tools/layoutlib/bridge/tests/src/com/android/layoutlib/bridge/intensive/RenderTests.java
@@ -396,6 +396,22 @@ public class RenderTests extends RenderTestBase {
}
@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
// Create the layout pull parser for our resources (empty.xml can not be part of the test
diff --git a/tools/layoutlib/create/src/com/android/tools/layoutlib/create/CreateInfo.java b/tools/layoutlib/create/src/com/android/tools/layoutlib/create/CreateInfo.java
index b0aa3c2989a5..4f226cbad01f 100644
--- a/tools/layoutlib/create/src/com/android/tools/layoutlib/create/CreateInfo.java
+++ b/tools/layoutlib/create/src/com/android/tools/layoutlib/create/CreateInfo.java
@@ -163,6 +163,7 @@ public final class CreateInfo implements ICreateInfo {
"android.content.res.TypedArray#obtain",
"android.graphics.BitmapFactory#finishDecode",
"android.graphics.BitmapFactory#setDensityFromOptions",
+ "android.graphics.drawable.AdaptiveIconDrawable#draw",
"android.graphics.drawable.AnimatedVectorDrawable$VectorDrawableAnimatorRT#useLastSeenTarget",
"android.graphics.drawable.AnimatedVectorDrawable$VectorDrawableAnimatorRT#onDraw",
"android.graphics.drawable.GradientDrawable#buildRing",
@@ -332,6 +333,8 @@ public final class CreateInfo implements ICreateInfo {
* needed when access from the delegate classes is needed.
*/
private final static String[] PROMOTED_FIELDS = new String[] {
+ "android.graphics.drawable.AdaptiveIconDrawable#mMaskBitmap",
+ "android.graphics.drawable.AdaptiveIconDrawable#mPaint",
"android.graphics.drawable.VectorDrawable#mVectorState",
"android.view.Choreographer#mLastFrameTimeNanos",
"android.graphics.FontFamily#mBuilderPtr"