ApplicationUtils.java revision 5c9e677c2abc6529c19fcc858ef987756d4341ef
1/*
2 * Copyright (C) 2013 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.inputmethod.latin.utils;
18
19import android.app.Activity;
20import android.content.ComponentName;
21import android.content.Context;
22import android.content.pm.ActivityInfo;
23import android.content.pm.PackageInfo;
24import android.content.pm.PackageManager.NameNotFoundException;
25import android.util.Log;
26
27public final class ApplicationUtils {
28    private static final String TAG = ApplicationUtils.class.getSimpleName();
29
30    private ApplicationUtils() {
31        // This utility class is not publicly instantiable.
32    }
33
34    public static int getActivityTitleResId(final Context context,
35            final Class<? extends Activity> cls) {
36        final ComponentName cn = new ComponentName(context, cls);
37        try {
38            final ActivityInfo ai = context.getPackageManager().getActivityInfo(cn, 0);
39            if (ai != null) {
40                return ai.labelRes;
41            }
42        } catch (final NameNotFoundException e) {
43            Log.e(TAG, "Failed to get settings activity title res id.", e);
44        }
45        return 0;
46    }
47
48    /**
49     * A utility method to get the application's PackageInfo.versionName
50     * @return the application's PackageInfo.versionName
51     */
52    public static String getVersionName(final Context context) {
53        try {
54            if (context == null) {
55                return "";
56            }
57            final String packageName = context.getPackageName();
58            final PackageInfo info = context.getPackageManager().getPackageInfo(packageName, 0);
59            return info.versionName;
60        } catch (final NameNotFoundException e) {
61            Log.e(TAG, "Could not find version info.", e);
62        }
63        return "";
64    }
65
66    /**
67     * A utility method to get the application's PackageInfo.versionCode
68     * @return the application's PackageInfo.versionCode
69     */
70    public static int getVersionCode(final Context context) {
71        try {
72            if (context == null) {
73                return 0;
74            }
75            final String packageName = context.getPackageName();
76            final PackageInfo info = context.getPackageManager().getPackageInfo(packageName, 0);
77            return info.versionCode;
78        } catch (final NameNotFoundException e) {
79            Log.e(TAG, "Could not find version info.", e);
80        }
81        return 0;
82    }
83}
84