14be6198cb73cc24e10834153c4e049644ed187e3Tadashi G. Takaoka/*
24be6198cb73cc24e10834153c4e049644ed187e3Tadashi G. Takaoka * Copyright (C) 2013 The Android Open Source Project
34be6198cb73cc24e10834153c4e049644ed187e3Tadashi G. Takaoka *
44be6198cb73cc24e10834153c4e049644ed187e3Tadashi G. Takaoka * Licensed under the Apache License, Version 2.0 (the "License");
54be6198cb73cc24e10834153c4e049644ed187e3Tadashi G. Takaoka * you may not use this file except in compliance with the License.
64be6198cb73cc24e10834153c4e049644ed187e3Tadashi G. Takaoka * You may obtain a copy of the License at
74be6198cb73cc24e10834153c4e049644ed187e3Tadashi G. Takaoka *
84be6198cb73cc24e10834153c4e049644ed187e3Tadashi G. Takaoka *      http://www.apache.org/licenses/LICENSE-2.0
94be6198cb73cc24e10834153c4e049644ed187e3Tadashi G. Takaoka *
104be6198cb73cc24e10834153c4e049644ed187e3Tadashi G. Takaoka * Unless required by applicable law or agreed to in writing, software
114be6198cb73cc24e10834153c4e049644ed187e3Tadashi G. Takaoka * distributed under the License is distributed on an "AS IS" BASIS,
124be6198cb73cc24e10834153c4e049644ed187e3Tadashi G. Takaoka * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
134be6198cb73cc24e10834153c4e049644ed187e3Tadashi G. Takaoka * See the License for the specific language governing permissions and
144be6198cb73cc24e10834153c4e049644ed187e3Tadashi G. Takaoka * limitations under the License.
154be6198cb73cc24e10834153c4e049644ed187e3Tadashi G. Takaoka */
164be6198cb73cc24e10834153c4e049644ed187e3Tadashi G. Takaoka
174be6198cb73cc24e10834153c4e049644ed187e3Tadashi G. Takaokapackage com.android.inputmethod.latin.utils;
184be6198cb73cc24e10834153c4e049644ed187e3Tadashi G. Takaoka
194be6198cb73cc24e10834153c4e049644ed187e3Tadashi G. Takaokaimport android.app.Activity;
204be6198cb73cc24e10834153c4e049644ed187e3Tadashi G. Takaokaimport android.content.ComponentName;
214be6198cb73cc24e10834153c4e049644ed187e3Tadashi G. Takaokaimport android.content.Context;
224be6198cb73cc24e10834153c4e049644ed187e3Tadashi G. Takaokaimport android.content.pm.ActivityInfo;
234be6198cb73cc24e10834153c4e049644ed187e3Tadashi G. Takaokaimport android.content.pm.PackageInfo;
244be6198cb73cc24e10834153c4e049644ed187e3Tadashi G. Takaokaimport android.content.pm.PackageManager.NameNotFoundException;
254be6198cb73cc24e10834153c4e049644ed187e3Tadashi G. Takaokaimport android.util.Log;
264be6198cb73cc24e10834153c4e049644ed187e3Tadashi G. Takaoka
274be6198cb73cc24e10834153c4e049644ed187e3Tadashi G. Takaokapublic final class ApplicationUtils {
284be6198cb73cc24e10834153c4e049644ed187e3Tadashi G. Takaoka    private static final String TAG = ApplicationUtils.class.getSimpleName();
294be6198cb73cc24e10834153c4e049644ed187e3Tadashi G. Takaoka
304be6198cb73cc24e10834153c4e049644ed187e3Tadashi G. Takaoka    private ApplicationUtils() {
314be6198cb73cc24e10834153c4e049644ed187e3Tadashi G. Takaoka        // This utility class is not publicly instantiable.
324be6198cb73cc24e10834153c4e049644ed187e3Tadashi G. Takaoka    }
334be6198cb73cc24e10834153c4e049644ed187e3Tadashi G. Takaoka
345c9e677c2abc6529c19fcc858ef987756d4341efJean Chalard    public static int getActivityTitleResId(final Context context,
354be6198cb73cc24e10834153c4e049644ed187e3Tadashi G. Takaoka            final Class<? extends Activity> cls) {
364be6198cb73cc24e10834153c4e049644ed187e3Tadashi G. Takaoka        final ComponentName cn = new ComponentName(context, cls);
374be6198cb73cc24e10834153c4e049644ed187e3Tadashi G. Takaoka        try {
384be6198cb73cc24e10834153c4e049644ed187e3Tadashi G. Takaoka            final ActivityInfo ai = context.getPackageManager().getActivityInfo(cn, 0);
394be6198cb73cc24e10834153c4e049644ed187e3Tadashi G. Takaoka            if (ai != null) {
404be6198cb73cc24e10834153c4e049644ed187e3Tadashi G. Takaoka                return ai.labelRes;
414be6198cb73cc24e10834153c4e049644ed187e3Tadashi G. Takaoka            }
424be6198cb73cc24e10834153c4e049644ed187e3Tadashi G. Takaoka        } catch (final NameNotFoundException e) {
434be6198cb73cc24e10834153c4e049644ed187e3Tadashi G. Takaoka            Log.e(TAG, "Failed to get settings activity title res id.", e);
444be6198cb73cc24e10834153c4e049644ed187e3Tadashi G. Takaoka        }
454be6198cb73cc24e10834153c4e049644ed187e3Tadashi G. Takaoka        return 0;
464be6198cb73cc24e10834153c4e049644ed187e3Tadashi G. Takaoka    }
474be6198cb73cc24e10834153c4e049644ed187e3Tadashi G. Takaoka
484be6198cb73cc24e10834153c4e049644ed187e3Tadashi G. Takaoka    /**
494be6198cb73cc24e10834153c4e049644ed187e3Tadashi G. Takaoka     * A utility method to get the application's PackageInfo.versionName
504be6198cb73cc24e10834153c4e049644ed187e3Tadashi G. Takaoka     * @return the application's PackageInfo.versionName
514be6198cb73cc24e10834153c4e049644ed187e3Tadashi G. Takaoka     */
524be6198cb73cc24e10834153c4e049644ed187e3Tadashi G. Takaoka    public static String getVersionName(final Context context) {
534be6198cb73cc24e10834153c4e049644ed187e3Tadashi G. Takaoka        try {
544be6198cb73cc24e10834153c4e049644ed187e3Tadashi G. Takaoka            if (context == null) {
554be6198cb73cc24e10834153c4e049644ed187e3Tadashi G. Takaoka                return "";
564be6198cb73cc24e10834153c4e049644ed187e3Tadashi G. Takaoka            }
574be6198cb73cc24e10834153c4e049644ed187e3Tadashi G. Takaoka            final String packageName = context.getPackageName();
584be6198cb73cc24e10834153c4e049644ed187e3Tadashi G. Takaoka            final PackageInfo info = context.getPackageManager().getPackageInfo(packageName, 0);
594be6198cb73cc24e10834153c4e049644ed187e3Tadashi G. Takaoka            return info.versionName;
604be6198cb73cc24e10834153c4e049644ed187e3Tadashi G. Takaoka        } catch (final NameNotFoundException e) {
614be6198cb73cc24e10834153c4e049644ed187e3Tadashi G. Takaoka            Log.e(TAG, "Could not find version info.", e);
624be6198cb73cc24e10834153c4e049644ed187e3Tadashi G. Takaoka        }
634be6198cb73cc24e10834153c4e049644ed187e3Tadashi G. Takaoka        return "";
644be6198cb73cc24e10834153c4e049644ed187e3Tadashi G. Takaoka    }
652fa3693c264a4c150ac307d9bb7f6f8f18cc4ffcKen Wakasa
662fa3693c264a4c150ac307d9bb7f6f8f18cc4ffcKen Wakasa    /**
672fa3693c264a4c150ac307d9bb7f6f8f18cc4ffcKen Wakasa     * A utility method to get the application's PackageInfo.versionCode
682fa3693c264a4c150ac307d9bb7f6f8f18cc4ffcKen Wakasa     * @return the application's PackageInfo.versionCode
692fa3693c264a4c150ac307d9bb7f6f8f18cc4ffcKen Wakasa     */
702fa3693c264a4c150ac307d9bb7f6f8f18cc4ffcKen Wakasa    public static int getVersionCode(final Context context) {
712fa3693c264a4c150ac307d9bb7f6f8f18cc4ffcKen Wakasa        try {
722fa3693c264a4c150ac307d9bb7f6f8f18cc4ffcKen Wakasa            if (context == null) {
732fa3693c264a4c150ac307d9bb7f6f8f18cc4ffcKen Wakasa                return 0;
742fa3693c264a4c150ac307d9bb7f6f8f18cc4ffcKen Wakasa            }
752fa3693c264a4c150ac307d9bb7f6f8f18cc4ffcKen Wakasa            final String packageName = context.getPackageName();
762fa3693c264a4c150ac307d9bb7f6f8f18cc4ffcKen Wakasa            final PackageInfo info = context.getPackageManager().getPackageInfo(packageName, 0);
772fa3693c264a4c150ac307d9bb7f6f8f18cc4ffcKen Wakasa            return info.versionCode;
782fa3693c264a4c150ac307d9bb7f6f8f18cc4ffcKen Wakasa        } catch (final NameNotFoundException e) {
792fa3693c264a4c150ac307d9bb7f6f8f18cc4ffcKen Wakasa            Log.e(TAG, "Could not find version info.", e);
802fa3693c264a4c150ac307d9bb7f6f8f18cc4ffcKen Wakasa        }
812fa3693c264a4c150ac307d9bb7f6f8f18cc4ffcKen Wakasa        return 0;
822fa3693c264a4c150ac307d9bb7f6f8f18cc4ffcKen Wakasa    }
834be6198cb73cc24e10834153c4e049644ed187e3Tadashi G. Takaoka}
84