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.NonNull;
24 import android.annotation.Nullable;
25 import android.view.View;
26 
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 the design support lib.
32  */
33 public class DesignLibUtil {
34     public static final String[] CN_COORDINATOR_LAYOUT = {
35             "android.support.design.widget.CoordinatorLayout",
36             "androidx.coordinatorlayout.widget.CoordinatorLayout"
37     };
38     public static final String[] CN_APPBAR_LAYOUT = {
39             "android.support.design.widget.AppBarLayout",
40             "com.google.android.material.widget.AppBarLayout"
41     };
42     public static final String[] CN_COLLAPSING_TOOLBAR_LAYOUT = {
43             "android.support.design.widget.CollapsingToolbarLayout",
44             "com.google.android.material.widget.CollapsingToolbarLayout"
45     };
46     public static final String[] CN_TOOLBAR = {
47             "android.support.v7.widget.Toolbar",
48             "androidx.appcompat.widget.Toolbar"
49     };
50 
51     /**
52      * Tries to set the title of a view. This is used to set the title in a
53      * CollapsingToolbarLayout.
54      * <p/>
55      * Any exceptions thrown during the process are logged in {@link Bridge#getLog()}
56      */
setTitle(@onNull View view, @Nullable String title)57     public static void setTitle(@NonNull View view, @Nullable String title) {
58         if (title == null) {
59             return;
60         }
61         try {
62             invoke(getMethod(view.getClass(), "setTitle", CharSequence.class), view, title);
63         } catch (ReflectionException e) {
64             Bridge.getLog().warning(LayoutLog.TAG_INFO,
65                     "Error occurred while trying to set title.", e);
66         }
67     }
68 }
69