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 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.annotation.Nullable; 24 import android.view.View; 25 26 import static android.view.Gravity.END; 27 import static android.view.Gravity.LEFT; 28 import static android.view.Gravity.RIGHT; 29 import static android.view.Gravity.START; 30 import static com.android.layoutlib.bridge.util.ReflectionUtils.getCause; 31 import static com.android.layoutlib.bridge.util.ReflectionUtils.getMethod; 32 import static com.android.layoutlib.bridge.util.ReflectionUtils.invoke; 33 34 public class DrawerLayoutUtil { 35 36 public static final String CN_DRAWER_LAYOUT = "android.support.v4.widget.DrawerLayout"; 37 openDrawer(View drawerLayout, @Nullable String drawerGravity)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 openDrawer(View drawerLayout, int gravity)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