diff options
author | Jerome Gaillard <jgaillard@google.com> | 2017-07-04 15:52:11 +0000 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2017-07-04 15:52:11 +0000 |
commit | 3480a2e88b1c31c49bbc577e7431437e741b6bff (patch) | |
tree | f6ae04b0446fb7eb843af43f00dd201603867ae8 | |
parent | d0adfd1ec04ec12d478b5816a89c29a2bf763fd5 (diff) | |
parent | f59abd4701479e496d955cb8521a813e535aefa3 (diff) | |
download | layoutlib-3480a2e88b1c31c49bbc577e7431437e741b6bff.tar.gz |
Merge "Add support for FragmentTabHost from the support library"
-rw-r--r-- | bridge/src/com/android/layoutlib/bridge/android/support/FragmentTabHostUtil.java | 52 | ||||
-rw-r--r-- | bridge/src/com/android/layoutlib/bridge/impl/RenderSessionImpl.java | 8 |
2 files changed, 59 insertions, 1 deletions
diff --git a/bridge/src/com/android/layoutlib/bridge/android/support/FragmentTabHostUtil.java b/bridge/src/com/android/layoutlib/bridge/android/support/FragmentTabHostUtil.java new file mode 100644 index 0000000000..730ac1300e --- /dev/null +++ b/bridge/src/com/android/layoutlib/bridge/android/support/FragmentTabHostUtil.java @@ -0,0 +1,52 @@ +/* + * 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 com.android.layoutlib.bridge.android.support; + +import com.android.ide.common.rendering.api.LayoutLog; +import com.android.layoutlib.bridge.Bridge; +import com.android.layoutlib.bridge.util.ReflectionUtils.ReflectionException; + +import android.content.Context; +import android.widget.TabHost; + +import static com.android.layoutlib.bridge.util.ReflectionUtils.getCause; +import static com.android.layoutlib.bridge.util.ReflectionUtils.getMethod; +import static com.android.layoutlib.bridge.util.ReflectionUtils.invoke; + +/** + * Utility class for working with android.support.v4.app.FragmentTabHost + */ +public class FragmentTabHostUtil { + + public static final String CN_FRAGMENT_TAB_HOST = "android.support.v4.app.FragmentTabHost"; + + /** + * Calls the setup method for the FragmentTabHost tabHost + */ + public static void setup(TabHost tabHost, Context context) { + try { + invoke(getMethod(tabHost.getClass(), "setup", Context.class, + Class.forName("android.support.v4.app.FragmentManager", true, + tabHost.getClass().getClassLoader()), int.class), tabHost, context, null, + android.R.id.tabcontent); + } catch (ReflectionException | ClassNotFoundException e) { + Throwable cause = getCause(e); + Bridge.getLog().error(LayoutLog.TAG_BROKEN, + "Error occurred while trying to setup FragmentTabHost.", cause, null); + } + } +} diff --git a/bridge/src/com/android/layoutlib/bridge/impl/RenderSessionImpl.java b/bridge/src/com/android/layoutlib/bridge/impl/RenderSessionImpl.java index a1d614da2a..5adba23b2a 100644 --- a/bridge/src/com/android/layoutlib/bridge/impl/RenderSessionImpl.java +++ b/bridge/src/com/android/layoutlib/bridge/impl/RenderSessionImpl.java @@ -41,9 +41,11 @@ import com.android.layoutlib.bridge.android.BridgeXmlBlockParser; import com.android.layoutlib.bridge.android.RenderParamsFlags; import com.android.layoutlib.bridge.android.graphics.NopCanvas; import com.android.layoutlib.bridge.android.support.DesignLibUtil; +import com.android.layoutlib.bridge.android.support.FragmentTabHostUtil; import com.android.layoutlib.bridge.android.support.SupportPreferencesUtil; import com.android.layoutlib.bridge.impl.binding.FakeAdapter; import com.android.layoutlib.bridge.impl.binding.FakeExpandableAdapter; +import com.android.layoutlib.bridge.util.ReflectionUtils; import com.android.resources.ResourceType; import com.android.tools.layoutlib.java.System_Delegate; import com.android.util.Pair; @@ -828,7 +830,11 @@ public class RenderSessionImpl extends RenderAction<SessionParams> { // this must be called before addTab() so that the TabHost searches its TabWidget // and FrameLayout. - tabHost.setup(); + if (ReflectionUtils.isInstanceOf(tabHost, FragmentTabHostUtil.CN_FRAGMENT_TAB_HOST)) { + FragmentTabHostUtil.setup(tabHost, getContext()); + } else { + tabHost.setup(); + } if (count == 0) { // Create a dummy child to get a single tab |