UiUtilities.java revision 3d9b8e76f0a396370a5c0be99a34bf7c24bd20dd
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 com.android.email.R;
20
21import android.app.Activity;
22import android.app.Fragment;
23import android.content.Context;
24import android.content.res.Resources;
25import android.view.View;
26
27public class UiUtilities {
28    private UiUtilities() {
29    }
30
31    /**
32     * Formats the given size as a String in bytes, kB, MB or GB.  Ex: 12,315,000 = 11 MB
33     */
34    public static String formatSize(Context context, long size) {
35        final Resources res = context.getResources();
36        final long KB = 1024;
37        final long MB = (KB * 1024);
38        final long GB  = (MB * 1024);
39
40        int resId;
41        int value;
42
43        if (size < KB) {
44            resId = R.plurals.message_view_attachment_bytes;
45            value = (int) size;
46        } else if (size < MB) {
47            resId = R.plurals.message_view_attachment_kilobytes;
48            value = (int) (size / KB);
49        } else if (size < GB) {
50            resId = R.plurals.message_view_attachment_megabytes;
51            value = (int) (size / MB);
52        } else {
53            resId = R.plurals.message_view_attachment_gigabytes;
54            value = (int) (size / GB);
55        }
56        return res.getQuantityString(resId, value, value);
57    }
58
59    public static String getMessageCountForUi(Context context, int count,
60            boolean replaceZeroWithBlank) {
61        if (replaceZeroWithBlank && (count == 0)) {
62            return "";
63        } else if (count > 999) {
64            return context.getString(R.string.more_than_999);
65        } else {
66            return Integer.toString(count);
67        }
68    }
69
70    /** Generics version of {@link Activity#findViewById} */
71    @SuppressWarnings("unchecked")
72    public static <T extends View> T getViewOrNull(Activity parent, int viewId) {
73        return (T) parent.findViewById(viewId);
74    }
75
76    /** Generics version of {@link View#findViewById} */
77    @SuppressWarnings("unchecked")
78    public static <T extends View> T getViewOrNull(View parent, int viewId) {
79        return (T) parent.findViewById(viewId);
80    }
81
82    /**
83     * Same as {@link Activity#findViewById}, but crashes if there's no view.
84     */
85    @SuppressWarnings("unchecked")
86    public static <T extends View> T getView(Activity parent, int viewId) {
87        return (T) checkView(parent.findViewById(viewId));
88    }
89
90    /**
91     * Same as {@link View#findViewById}, but crashes if there's no view.
92     */
93    @SuppressWarnings("unchecked")
94    public static <T extends View> T getView(View parent, int viewId) {
95        return (T) checkView(parent.findViewById(viewId));
96    }
97
98    private static View checkView(View v) {
99        if (v == null) {
100            throw new IllegalArgumentException("View doesn't exist");
101        }
102        return v;
103    }
104
105    /**
106     * Same as {@link View#setVisibility(int)}, but doesn't crash even if {@code view} is null.
107     */
108    public static void setVisibilitySafe(View v, int visibility) {
109        if (v != null) {
110            v.setVisibility(visibility);
111        }
112    }
113
114    /**
115     * Same as {@link View#setVisibility(int)}, but doesn't crash even if {@code view} is null.
116     */
117    public static void setVisibilitySafe(Activity parent, int viewId, int visibility) {
118        setVisibilitySafe(parent.findViewById(viewId), visibility);
119    }
120
121    /**
122     * Same as {@link View#setVisibility(int)}, but doesn't crash even if {@code view} is null.
123     */
124    public static void setVisibilitySafe(View parent, int viewId, int visibility) {
125        setVisibilitySafe(parent.findViewById(viewId), visibility);
126    }
127
128    /**
129     * Used by an {@link Fragment} to install itself to the host activity.
130     *
131     * @see FragmentInstallable
132     */
133    public static void installFragment(Fragment fragment) {
134        final Activity a = fragment.getActivity();
135        if (a instanceof FragmentInstallable) {
136            ((FragmentInstallable) a).onInstallFragment(fragment);
137        }
138    }
139}
140