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 = {
37             "android.support.v4.widget.DrawerLayout",
38             "androidx.drawerlayout.widget.DrawerLayout"
39     };
40 
openDrawer(View drawerLayout, @Nullable String drawerGravity)41     public static void openDrawer(View drawerLayout, @Nullable String drawerGravity) {
42         int gravity = -1;
43         if ("left".equals(drawerGravity)) {
44             gravity = LEFT;
45         } else if ("right".equals(drawerGravity)) {
46             gravity = RIGHT;
47         } else if ("start".equals(drawerGravity)) {
48             gravity = START;
49         } else if ("end".equals(drawerGravity)) {
50             gravity = END;
51         }
52         if (gravity > 0) {
53             openDrawer(drawerLayout, gravity);
54         }
55     }
56 
openDrawer(View drawerLayout, int gravity)57     private static void openDrawer(View drawerLayout, int gravity) {
58         try {
59             invoke(getMethod(drawerLayout.getClass(), "openDrawer", int.class), drawerLayout,
60                     gravity);
61         } catch (ReflectionException e) {
62             Bridge.getLog().error(LayoutLog.TAG_BROKEN, "Unable to open navigation drawer",
63                     getCause(e), null, null);
64         }
65     }
66 }
67