diff --git a/WordPressUtils/src/main/java/org/wordpress/android/util/AppLog.java b/WordPressUtils/src/main/java/org/wordpress/android/util/AppLog.java index 292c974ec7fa..2a11d7ee6f8d 100644 --- a/WordPressUtils/src/main/java/org/wordpress/android/util/AppLog.java +++ b/WordPressUtils/src/main/java/org/wordpress/android/util/AppLog.java @@ -175,7 +175,7 @@ public static ArrayList toHtmlList(Context context) { ArrayList items = new ArrayList(); // add version & device info - be sure to change HEADER_LINE_COUNT if additional lines are added - items.add("WordPress Android version: " + ProfilingUtils.getVersionName(context) + ""); + items.add("WordPress Android version: " + PackageUtils.getVersionName(context) + ""); items.add("Android device name: " + DeviceUtils.getInstance().getDeviceName(context) + ""); Iterator it = mLogEntries.iterator(); @@ -193,7 +193,7 @@ public static String toPlainText(Context context) { StringBuilder sb = new StringBuilder(); // add version & device info - sb.append("WordPress Android version: " + ProfilingUtils.getVersionName(context)).append("\n") + sb.append("WordPress Android version: " + PackageUtils.getVersionName(context)).append("\n") .append("Android device name: " + DeviceUtils.getInstance().getDeviceName(context)).append("\n\n"); Iterator it = mLogEntries.iterator(); diff --git a/WordPressUtils/src/main/java/org/wordpress/android/util/PackageUtils.java b/WordPressUtils/src/main/java/org/wordpress/android/util/PackageUtils.java new file mode 100644 index 000000000000..52900a0bf27e --- /dev/null +++ b/WordPressUtils/src/main/java/org/wordpress/android/util/PackageUtils.java @@ -0,0 +1,45 @@ +package org.wordpress.android.util; + +import android.content.Context; +import android.content.pm.PackageInfo; +import android.content.pm.PackageManager; + +public class PackageUtils { + /** + * Return true if Debug build. false otherwise. + */ + public static boolean isDebugBuild() { + return BuildConfig.DEBUG; + } + + public static PackageInfo getPackageInfo(Context context) { + try { + PackageManager manager = context.getPackageManager(); + return manager.getPackageInfo(context.getPackageName(), 0); + } catch (PackageManager.NameNotFoundException e) { + return null; + } + } + + /** + * Return version code, or 0 if it can't be read + */ + public static int getVersionCode(Context context) { + PackageInfo packageInfo = getPackageInfo(context); + if (packageInfo != null) { + return packageInfo.versionCode; + } + return 0; + } + + /** + * Return version name, or the string "0" if it can't be read + */ + public static String getVersionName(Context context) { + PackageInfo packageInfo = getPackageInfo(context); + if (packageInfo != null) { + return packageInfo.versionName; + } + return "0"; + } +} diff --git a/WordPressUtils/src/main/java/org/wordpress/android/util/ProfilingUtils.java b/WordPressUtils/src/main/java/org/wordpress/android/util/ProfilingUtils.java index 251db2a3b7fb..991c7680b478 100644 --- a/WordPressUtils/src/main/java/org/wordpress/android/util/ProfilingUtils.java +++ b/WordPressUtils/src/main/java/org/wordpress/android/util/ProfilingUtils.java @@ -1,8 +1,5 @@ package org.wordpress.android.util; -import android.content.Context; -import android.content.pm.PackageInfo; -import android.content.pm.PackageManager; import android.os.SystemClock; import org.wordpress.android.util.AppLog.T; @@ -76,16 +73,5 @@ public void dumpToLog() { } AppLog.d(T.PROFILING, mLabel + ": end, " + (now - first) + " ms"); } - - // Returns app version name String - public static String getVersionName(Context context) { - PackageManager pm = context.getPackageManager(); - try { - PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0); - return pi.versionName == null ? "" : pi.versionName; - } catch (PackageManager.NameNotFoundException e) { - return ""; - } - } }