@Test public void addNameAndVersion_whenApplicationContextIsNull_addsNoContextValues() { Context context = mock(Context.class); when(context.getApplicationContext()).thenReturn(null); Map<String, Object> paramsMap = new HashMap<>(); LoggingUtils.addNameAndVersion(paramsMap, context); assertEquals(LoggingUtils.NO_CONTEXT, paramsMap.get(LoggingUtils.FIELD_APP_NAME)); assertEquals(LoggingUtils.NO_CONTEXT, paramsMap.get(LoggingUtils.FIELD_APP_VERSION)); }
static void addNameAndVersion( @NonNull Map<String, Object> paramsObject, @NonNull Context context) { Context applicationContext = context.getApplicationContext(); if (applicationContext != null && applicationContext.getPackageManager() != null) { try { PackageInfo info = applicationContext.getPackageManager().getPackageInfo( applicationContext.getPackageName(), 0); String nameString = null; if (info.applicationInfo != null) { CharSequence name = info.applicationInfo.loadLabel(applicationContext.getPackageManager()); if (name != null) { nameString = name.toString(); } paramsObject.put(FIELD_APP_NAME, nameString); } if (StripeTextUtils.isBlank(nameString)) { paramsObject.put(FIELD_APP_NAME, info.packageName); } paramsObject.put(FIELD_APP_VERSION, info.versionCode); } catch (PackageManager.NameNotFoundException nameNotFound) { paramsObject.put(FIELD_APP_NAME, UNKNOWN); paramsObject.put(FIELD_APP_VERSION, UNKNOWN); } } else { paramsObject.put(FIELD_APP_NAME, NO_CONTEXT); paramsObject.put(FIELD_APP_VERSION, NO_CONTEXT); } }
LoggingUtils { static void addNameAndVersion( @NonNull Map<String, Object> paramsObject, @NonNull Context context) { Context applicationContext = context.getApplicationContext(); if (applicationContext != null && applicationContext.getPackageManager() != null) { try { PackageInfo info = applicationContext.getPackageManager().getPackageInfo( applicationContext.getPackageName(), 0); String nameString = null; if (info.applicationInfo != null) { CharSequence name = info.applicationInfo.loadLabel(applicationContext.getPackageManager()); if (name != null) { nameString = name.toString(); } paramsObject.put(FIELD_APP_NAME, nameString); } if (StripeTextUtils.isBlank(nameString)) { paramsObject.put(FIELD_APP_NAME, info.packageName); } paramsObject.put(FIELD_APP_VERSION, info.versionCode); } catch (PackageManager.NameNotFoundException nameNotFound) { paramsObject.put(FIELD_APP_NAME, UNKNOWN); paramsObject.put(FIELD_APP_VERSION, UNKNOWN); } } else { paramsObject.put(FIELD_APP_NAME, NO_CONTEXT); paramsObject.put(FIELD_APP_VERSION, NO_CONTEXT); } } }
LoggingUtils { static void addNameAndVersion( @NonNull Map<String, Object> paramsObject, @NonNull Context context) { Context applicationContext = context.getApplicationContext(); if (applicationContext != null && applicationContext.getPackageManager() != null) { try { PackageInfo info = applicationContext.getPackageManager().getPackageInfo( applicationContext.getPackageName(), 0); String nameString = null; if (info.applicationInfo != null) { CharSequence name = info.applicationInfo.loadLabel(applicationContext.getPackageManager()); if (name != null) { nameString = name.toString(); } paramsObject.put(FIELD_APP_NAME, nameString); } if (StripeTextUtils.isBlank(nameString)) { paramsObject.put(FIELD_APP_NAME, info.packageName); } paramsObject.put(FIELD_APP_VERSION, info.versionCode); } catch (PackageManager.NameNotFoundException nameNotFound) { paramsObject.put(FIELD_APP_NAME, UNKNOWN); paramsObject.put(FIELD_APP_VERSION, UNKNOWN); } } else { paramsObject.put(FIELD_APP_NAME, NO_CONTEXT); paramsObject.put(FIELD_APP_VERSION, NO_CONTEXT); } }  }
LoggingUtils { static void addNameAndVersion( @NonNull Map<String, Object> paramsObject, @NonNull Context context) { Context applicationContext = context.getApplicationContext(); if (applicationContext != null && applicationContext.getPackageManager() != null) { try { PackageInfo info = applicationContext.getPackageManager().getPackageInfo( applicationContext.getPackageName(), 0); String nameString = null; if (info.applicationInfo != null) { CharSequence name = info.applicationInfo.loadLabel(applicationContext.getPackageManager()); if (name != null) { nameString = name.toString(); } paramsObject.put(FIELD_APP_NAME, nameString); } if (StripeTextUtils.isBlank(nameString)) { paramsObject.put(FIELD_APP_NAME, info.packageName); } paramsObject.put(FIELD_APP_VERSION, info.versionCode); } catch (PackageManager.NameNotFoundException nameNotFound) { paramsObject.put(FIELD_APP_NAME, UNKNOWN); paramsObject.put(FIELD_APP_VERSION, UNKNOWN); } } else { paramsObject.put(FIELD_APP_NAME, NO_CONTEXT); paramsObject.put(FIELD_APP_VERSION, NO_CONTEXT); } }   }
LoggingUtils { static void addNameAndVersion( @NonNull Map<String, Object> paramsObject, @NonNull Context context) { Context applicationContext = context.getApplicationContext(); if (applicationContext != null && applicationContext.getPackageManager() != null) { try { PackageInfo info = applicationContext.getPackageManager().getPackageInfo( applicationContext.getPackageName(), 0); String nameString = null; if (info.applicationInfo != null) { CharSequence name = info.applicationInfo.loadLabel(applicationContext.getPackageManager()); if (name != null) { nameString = name.toString(); } paramsObject.put(FIELD_APP_NAME, nameString); } if (StripeTextUtils.isBlank(nameString)) { paramsObject.put(FIELD_APP_NAME, info.packageName); } paramsObject.put(FIELD_APP_VERSION, info.versionCode); } catch (PackageManager.NameNotFoundException nameNotFound) { paramsObject.put(FIELD_APP_NAME, UNKNOWN); paramsObject.put(FIELD_APP_VERSION, UNKNOWN); } } else { paramsObject.put(FIELD_APP_NAME, NO_CONTEXT); paramsObject.put(FIELD_APP_VERSION, NO_CONTEXT); } }    }