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.NonNull;
24import android.annotation.Nullable;
25import android.view.View;
26
27import static com.android.layoutlib.bridge.util.ReflectionUtils.getMethod;
28import static com.android.layoutlib.bridge.util.ReflectionUtils.invoke;
29
30/**
31 * Utility class for working with the design support lib.
32 */
33public class DesignLibUtil {
34
35    private static final String PKG_PREFIX = "android.support.design.widget.";
36    public static final String CN_COORDINATOR_LAYOUT = PKG_PREFIX + "CoordinatorLayout";
37    public static final String CN_APPBAR_LAYOUT = PKG_PREFIX + "AppBarLayout";
38    public static final String CN_COLLAPSING_TOOLBAR_LAYOUT =
39            PKG_PREFIX + "CollapsingToolbarLayout";
40    public static final String CN_TOOLBAR = "android.support.v7.widget.Toolbar";
41    public static final int SCROLL_AXIS_VERTICAL = 1 << 1;
42
43    /**
44     * Tries to set the title of a view. This is used to set the title in a
45     * CollapsingToolbarLayout.
46     * <p/>
47     * Any exceptions thrown during the process are logged in {@link Bridge#getLog()}
48     */
49    public static void setTitle(@NonNull View view, @Nullable String title) {
50        if (title == null) {
51            return;
52        }
53        try {
54            invoke(getMethod(view.getClass(), "setTitle", CharSequence.class), view, title);
55        } catch (ReflectionException e) {
56            Bridge.getLog().warning(LayoutLog.TAG_INFO,
57                    "Error occurred while trying to set title.", e);
58        }
59    }
60}
61