1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.layoutlib.bridge.android.support; 18 19 import com.android.ide.common.rendering.api.LayoutLog; 20 import com.android.layoutlib.bridge.Bridge; 21 import com.android.layoutlib.bridge.util.ReflectionUtils.ReflectionException; 22 23 import android.content.Context; 24 import android.widget.TabHost; 25 26 import static com.android.layoutlib.bridge.util.ReflectionUtils.getCause; 27 import static com.android.layoutlib.bridge.util.ReflectionUtils.getMethod; 28 import static com.android.layoutlib.bridge.util.ReflectionUtils.invoke; 29 30 /** 31 * Utility class for working with android.support.v4.app.FragmentTabHost 32 */ 33 public class FragmentTabHostUtil { 34 35 public static final String[] CN_FRAGMENT_TAB_HOST = { 36 "android.support.v4.app.FragmentTabHost", 37 "androidx.fragment.app.FragmentTabHost" 38 }; 39 40 private static final String[] CN_FRAGMENT_MANAGER = { 41 "android.support.v4.app.FragmentManager", 42 "androidx.fragment.app.FragmentManager" 43 }; 44 45 /** 46 * Calls the setup method for the FragmentTabHost tabHost 47 */ setup(TabHost tabHost, Context context)48 public static void setup(TabHost tabHost, Context context) { 49 Class<?> fragmentManager = null; 50 51 for (int i = CN_FRAGMENT_MANAGER.length - 1; i >= 0; i--) { 52 String className = CN_FRAGMENT_MANAGER[i]; 53 try { 54 fragmentManager = Class.forName(className, true, 55 tabHost.getClass().getClassLoader()); 56 break; 57 } catch (ClassNotFoundException ignore) { 58 } 59 } 60 61 if (fragmentManager == null) { 62 Bridge.getLog().error(LayoutLog.TAG_BROKEN, 63 "Unable to find FragmentManager.", null); 64 return; 65 } 66 67 try { 68 invoke(getMethod(tabHost.getClass(), "setup", Context.class, 69 fragmentManager, int.class), tabHost, context, null, 70 android.R.id.tabcontent); 71 } catch (ReflectionException e) { 72 Throwable cause = getCause(e); 73 Bridge.getLog().error(LayoutLog.TAG_BROKEN, 74 "Error occurred while trying to setup FragmentTabHost.", cause, null); 75 } 76 } 77 } 78