1/* 2 * Copyright (C) 2015 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 17package com.android.layoutlib.bridge.android.support; 18 19import com.android.ide.common.rendering.api.LayoutLog; 20import com.android.layoutlib.bridge.Bridge; 21import com.android.layoutlib.bridge.util.ReflectionUtils.ReflectionException; 22 23import android.annotation.Nullable; 24import android.view.View; 25 26import static android.view.Gravity.END; 27import static android.view.Gravity.LEFT; 28import static android.view.Gravity.RIGHT; 29import static android.view.Gravity.START; 30import static com.android.layoutlib.bridge.util.ReflectionUtils.getCause; 31import static com.android.layoutlib.bridge.util.ReflectionUtils.getMethod; 32import static com.android.layoutlib.bridge.util.ReflectionUtils.invoke; 33 34public class DrawerLayoutUtil { 35 36 public static final String CN_DRAWER_LAYOUT = "android.support.v4.widget.DrawerLayout"; 37 38 public static void openDrawer(View drawerLayout, @Nullable String drawerGravity) { 39 int gravity = -1; 40 if ("left".equals(drawerGravity)) { 41 gravity = LEFT; 42 } else if ("right".equals(drawerGravity)) { 43 gravity = RIGHT; 44 } else if ("start".equals(drawerGravity)) { 45 gravity = START; 46 } else if ("end".equals(drawerGravity)) { 47 gravity = END; 48 } 49 if (gravity > 0) { 50 openDrawer(drawerLayout, gravity); 51 } 52 } 53 54 private static void openDrawer(View drawerLayout, int gravity) { 55 try { 56 invoke(getMethod(drawerLayout.getClass(), "openDrawer", int.class), drawerLayout, 57 gravity); 58 } catch (ReflectionException e) { 59 Bridge.getLog().error(LayoutLog.TAG_BROKEN, "Unable to open navigation drawer", 60 getCause(e), null); 61 } 62 } 63} 64