1/*
2 * Copyright (C) 2011 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.email.activity;
18
19import android.app.Activity;
20import android.content.Context;
21import android.content.res.Resources;
22import android.view.View;
23
24import com.android.email.R;
25
26public class UiUtilities {
27    private UiUtilities() {
28    }
29
30    /**
31     * Same as {@link View#findViewById}, but crashes if there's no view.
32     */
33    @SuppressWarnings("unchecked")
34    public static <T extends View> T getView(View parent, int viewId) {
35        return (T) checkView(parent.findViewById(viewId));
36    }
37
38    private static View checkView(View v) {
39        if (v == null) {
40            throw new IllegalArgumentException("View doesn't exist");
41        }
42        return v;
43    }
44
45    /**
46     * Same as {@link View#setVisibility(int)}, but doesn't crash even if {@code view} is null.
47     */
48    public static void setVisibilitySafe(View v, int visibility) {
49        if (v != null) {
50            v.setVisibility(visibility);
51        }
52    }
53
54    /**
55     * Same as {@link View#setVisibility(int)}, but doesn't crash even if {@code view} is null.
56     */
57    public static void setVisibilitySafe(View parent, int viewId, int visibility) {
58        setVisibilitySafe(parent.findViewById(viewId), visibility);
59    }
60}
61