Merge "Call onApplyWindowInsets after requestApplyWindowInsets"

This commit is contained in:
Diego Perez
2017-01-11 15:57:12 +00:00
committed by Android (Google) Code Review
6 changed files with 132 additions and 0 deletions

View File

@@ -51,4 +51,8 @@ public class AttachInfo_Accessor {
view.dispatchDetachedFromWindow();
}
}
public static ViewRootImpl getRootView(View view) {
return view.mAttachInfo != null ? view.mAttachInfo.mViewRootImpl : null;
}
}

View File

@@ -0,0 +1,26 @@
/*
* Copyright (C) 2016 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.view;
/**
* Accessor to allow layoutlib to call {@link ViewRootImpl#dispatchApplyInsets} directly.
*/
public class ViewRootImpl_Accessor {
public static void dispatchApplyInsets(ViewRootImpl viewRoot, View host) {
viewRoot.dispatchApplyInsets(host);
}
}

View File

@@ -38,7 +38,10 @@ import android.annotation.NonNull;
import android.graphics.drawable.Drawable;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.AttachInfo_Accessor;
import android.view.View;
import android.view.ViewRootImpl;
import android.view.ViewRootImpl_Accessor;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
@@ -302,6 +305,17 @@ class Layout extends RelativeLayout {
return Bridge.getResourceId(ResourceType.ID, ID_PREFIX + name);
}
@Override
public void requestFitSystemWindows() {
// The framework call would usually bubble up to ViewRootImpl but, in layoutlib, Layout will
// act as view root for most purposes. That way, we can also save going through the Handler
// to dispatch the new applied insets.
ViewRootImpl root = AttachInfo_Accessor.getRootView(this);
if (root != null) {
ViewRootImpl_Accessor.dispatchApplyInsets(root, this);
}
}
/**
* A helper class to help initialize the Layout.
*/

View File

@@ -0,0 +1,43 @@
/*
* Copyright (C) 2016 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.android.layoutlib.test.myapplication.widgets;
import android.content.Context;
import android.util.AttributeSet;
import android.view.WindowInsets;
import android.widget.TextView;
public class InsetsWidget extends TextView {
public static boolean sApplyInsetsCalled = false;
public InsetsWidget(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
requestApplyInsets();
}
@Override
public WindowInsets onApplyWindowInsets(WindowInsets insets) {
sApplyInsetsCalled = true;
return super.onApplyWindowInsets(insets);
}
}

View File

@@ -0,0 +1,12 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="16dp"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<com.android.layoutlib.test.myapplication.widgets.InsetsWidget
android:text="Hello world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/text1"/>
</LinearLayout>

View File

@@ -63,6 +63,7 @@ import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.WeakReference;
import java.lang.reflect.Field;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
@@ -87,6 +88,7 @@ import java.util.zip.ZipFile;
import com.google.android.collect.Lists;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
@@ -434,6 +436,37 @@ public class Main {
renderAndVerify(params, "simple_activity.png");
}
@Test
public void testOnApplyInsetsCall()
throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException {
// We get the widget via reflection to avoid IntelliJ complaining about the class being
// located in the wrong package. (From the Bridge tests point of view, it is)
Class insetsWidgetClass = Class.forName("com.android.layoutlib.test.myapplication.widgets" +
".InsetsWidget");
Field field = insetsWidgetClass.getDeclaredField("sApplyInsetsCalled");
assertFalse((Boolean)field.get(null));
LayoutPullParser parser = createLayoutPullParser("insets.xml");
LayoutLibTestCallback layoutLibCallback =
new LayoutLibTestCallback(getLogger(), mDefaultClassLoader);
layoutLibCallback.initResources();
SessionParams params = getSessionParams(parser, ConfigGenerator.NEXUS_5,
layoutLibCallback, "Theme.Material.Light.NoActionBar", false,
RenderingMode.NORMAL, 22);
try {
renderAndVerify(params, "scrolled.png");
} catch(AssertionError e) {
// In this particular test we do not care about the image similarity.
// TODO: Create new render method that allows to not compare images.
if (!e.getLocalizedMessage().startsWith("Images differ")) {
throw e;
}
}
assertTrue((Boolean)field.get(null));
field.set(null, false);
}
@AfterClass
public static void tearDown() {
sLayoutLibLog = null;