@Test public void testGetLineAppVersion() throws PackageManager.NameNotFoundException, NoSuchFieldException, IllegalAccessException { LineAppVersion lineAppVersion; setVersionName(packageInfo, "1.2.3"); lineAppVersion = LineAppVersion.getLineAppVersion(context); assertEquals(1, lineAppVersion.getMajor()); assertEquals(2, lineAppVersion.getMinor()); assertEquals(3, lineAppVersion.getRevision()); setVersionName(packageInfo, "illegalVersion"); assertNull(LineAppVersion.getLineAppVersion(context)); setVersionName(packageInfo, null); assertNull(LineAppVersion.getLineAppVersion(context)); }
@Nullable public static LineAppVersion getLineAppVersion(@NonNull Context context) { PackageInfo packageInfo; try { packageInfo = context.getPackageManager() .getPackageInfo(Constants.LINE_APP_PACKAGE_NAME, PackageManager.GET_META_DATA); } catch (PackageManager.NameNotFoundException e) { return null; } String versionName = packageInfo.versionName; if (TextUtils.isEmpty(versionName)) { return null; } @SuppressWarnings("UseOfStringTokenizer") StringTokenizer stringTokenizer = new StringTokenizer(versionName, "."); try { return new LineAppVersion( Integer.parseInt(stringTokenizer.nextToken()) , Integer.parseInt(stringTokenizer.nextToken()) , Integer.parseInt(stringTokenizer.nextToken()) ); } catch (NumberFormatException | NullPointerException e) { return null; } }
LineAppVersion { @Nullable public static LineAppVersion getLineAppVersion(@NonNull Context context) { PackageInfo packageInfo; try { packageInfo = context.getPackageManager() .getPackageInfo(Constants.LINE_APP_PACKAGE_NAME, PackageManager.GET_META_DATA); } catch (PackageManager.NameNotFoundException e) { return null; } String versionName = packageInfo.versionName; if (TextUtils.isEmpty(versionName)) { return null; } @SuppressWarnings("UseOfStringTokenizer") StringTokenizer stringTokenizer = new StringTokenizer(versionName, "."); try { return new LineAppVersion( Integer.parseInt(stringTokenizer.nextToken()) , Integer.parseInt(stringTokenizer.nextToken()) , Integer.parseInt(stringTokenizer.nextToken()) ); } catch (NumberFormatException | NullPointerException e) { return null; } } }
LineAppVersion { @Nullable public static LineAppVersion getLineAppVersion(@NonNull Context context) { PackageInfo packageInfo; try { packageInfo = context.getPackageManager() .getPackageInfo(Constants.LINE_APP_PACKAGE_NAME, PackageManager.GET_META_DATA); } catch (PackageManager.NameNotFoundException e) { return null; } String versionName = packageInfo.versionName; if (TextUtils.isEmpty(versionName)) { return null; } @SuppressWarnings("UseOfStringTokenizer") StringTokenizer stringTokenizer = new StringTokenizer(versionName, "."); try { return new LineAppVersion( Integer.parseInt(stringTokenizer.nextToken()) , Integer.parseInt(stringTokenizer.nextToken()) , Integer.parseInt(stringTokenizer.nextToken()) ); } catch (NumberFormatException | NullPointerException e) { return null; } } LineAppVersion(int major, int minor, int revision); }
LineAppVersion { @Nullable public static LineAppVersion getLineAppVersion(@NonNull Context context) { PackageInfo packageInfo; try { packageInfo = context.getPackageManager() .getPackageInfo(Constants.LINE_APP_PACKAGE_NAME, PackageManager.GET_META_DATA); } catch (PackageManager.NameNotFoundException e) { return null; } String versionName = packageInfo.versionName; if (TextUtils.isEmpty(versionName)) { return null; } @SuppressWarnings("UseOfStringTokenizer") StringTokenizer stringTokenizer = new StringTokenizer(versionName, "."); try { return new LineAppVersion( Integer.parseInt(stringTokenizer.nextToken()) , Integer.parseInt(stringTokenizer.nextToken()) , Integer.parseInt(stringTokenizer.nextToken()) ); } catch (NumberFormatException | NullPointerException e) { return null; } } LineAppVersion(int major, int minor, int revision); @Nullable static LineAppVersion getLineAppVersion(@NonNull Context context); int getMajor(); int getMinor(); int getRevision(); boolean isEqualOrGreaterThan(@Nullable LineAppVersion another); @Override boolean equals(Object o); @Override int hashCode(); }
LineAppVersion { @Nullable public static LineAppVersion getLineAppVersion(@NonNull Context context) { PackageInfo packageInfo; try { packageInfo = context.getPackageManager() .getPackageInfo(Constants.LINE_APP_PACKAGE_NAME, PackageManager.GET_META_DATA); } catch (PackageManager.NameNotFoundException e) { return null; } String versionName = packageInfo.versionName; if (TextUtils.isEmpty(versionName)) { return null; } @SuppressWarnings("UseOfStringTokenizer") StringTokenizer stringTokenizer = new StringTokenizer(versionName, "."); try { return new LineAppVersion( Integer.parseInt(stringTokenizer.nextToken()) , Integer.parseInt(stringTokenizer.nextToken()) , Integer.parseInt(stringTokenizer.nextToken()) ); } catch (NumberFormatException | NullPointerException e) { return null; } } LineAppVersion(int major, int minor, int revision); @Nullable static LineAppVersion getLineAppVersion(@NonNull Context context); int getMajor(); int getMinor(); int getRevision(); boolean isEqualOrGreaterThan(@Nullable LineAppVersion another); @Override boolean equals(Object o); @Override int hashCode();  }