Utils { public static boolean isValidEmail(String email) { if (email == null) { return false; } final String emailPattern = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$"; Matcher matcher; Pattern pattern = Pattern.compile(emailPattern); matcher = pattern.matcher(email); if (matcher != null) { return matcher.matches(); } else { return false; } }  static Toast showToast(Context ctx, CharSequence msg); static Toast showToast(Context ctx, CharSequence msg, int duration); static boolean isInternetAvailable(Context ctx); static boolean isSdCardMounted(); static void showAlertDialog(Context context, @StringRes int titleResId, @StringRes int bodyResId); static void showAlertDialog(Context context, String title, String body); static void showAlertDialog(Context context, String title, String body, DialogInterface.OnClickListener okListener); static String toBase64(Bitmap bitmap); static Bitmap drawableToBitmap(Drawable drawable); static InputStream bitmapToInputStream(Bitmap bitmap); static void showProgressDialog(Context ctx, String title, String body, boolean isCancellable); static void showProgressDialog(Context ctx, String title, String body, Drawable icon, boolean isCancellable); static boolean isProgressDialogVisible(); static void dismissProgressDialog(); static float getDensityMultiplier(Context context); static int getDip(int px, Context context); static void showConfirmDialog(Context ctx, String message, DialogInterface.OnClickListener yesListener, DialogInterface.OnClickListener noListener); static void showConfirmDialog(Context ctx, String message, DialogInterface.OnClickListener yesListener, DialogInterface.OnClickListener noListener, String yesLabel, String noLabel); static void showDialog(Context ctx, String message, String positiveBtnLabel, String negativeBtnLabel, DialogInterface.OnClickListener dialogClickListener); static String getApplicationVersionNumber(Context context); static int getApplicationVersionCode(Context ctx); static String getOsVersion(); static boolean isServiceRunning(Context ctx, String serviceName); static String getDeviceImei(Context ctx); static void share(Context ctx, String sharingMsg, String emailSubject, String title); static int getDataConnectionType(Context ctx); static boolean isValidEmail(String email); @Nullable /** * Capitalizes each word in the string. * @param string * @return */ static String capitalizeString(String string); static boolean isDatabasePresent(String packageName, String dbName); static String getPathForMediaUri(Context context, Uri mediaContentUri); static ArrayList<String> toStringArray(JSONArray jsonArr); static JSONArray toJSONArray(ArrayList<String> stringArr); static File getStorageDirectory(Context ctx, String dirName); static File getFile(Context ctx, String fileName); static String writeImage(Context ctx, byte[] imageData); static String writeImageToMedia(Context ctx, Bitmap image, String title, String description); static String getApplicationName(Context ctx); static URL getPathFromUrl(URL url); static String fromCalendar(final Calendar calendar); static String now(); static Calendar toCalendar(final String iso8601string); static String getElapsedTime(String time); static void setMockLocation(Context ctx, double longitude, double latitude); static String getDayOfWeek(String date); static int getRandomColor(); static byte[] toBytes(Bitmap bmp); static Bitmap resizeImage(Bitmap sourceBitmap, int newWidth, int newHeight, boolean filter); static Bitmap compressImage(Bitmap sourceBitmap, int compressionFactor); Bitmap resizeImageByHeight(int height, Bitmap sourceImage); static boolean isValidURL(String url); @Nullable /** * @return Lower case string for one of above listed media type * @deprecated Use {@link MediaUtils#getMediaType(Uri)} * Get the type of the media. Audio, Video or Image. */ static String getMediaType(String contentType); static boolean isMedia(String mimeType); static String removeUriFragment(String url); static String removeQueryParameters(Uri uri); static boolean isImage(String mimeType); static boolean isAudio(String mimeType); static boolean isVideo(String mimeType); @Nullable /** * @deprecated This is a monster that will lead to OutOfMemory exception some day and the world * will come to an end. * Gets the media data from the one of the following media {@link android.content.ContentProvider} This method * should not be called from the main thread of the application. Calling this method may have * performance issues as this may allocate a huge memory array. * <ul> * <li>{@link android.provider.MediaStore.Images.Media}</li> * <li>{@link android.provider.MediaStore.Audio.Media}</li> * <li>{@link android.provider.MediaStore.Video.Media}</li> * </ul> * * @param context Context object * @param uri Media content uri of the image, audio or video resource */ static byte[] getMediaData(Context context, Uri uri); static byte[] readStreamToBytes(InputStream inputStream); static long getMediaSize(Context context, Uri mediaUri); static String getMediaFileName(Context ctx, Uri mediaUri); @Nullable /** * @deprecated Use {@link MediaUtils#getMediaType(Uri)} * Gets media type from the Uri. */ static String getMediaType(Uri uri); static SpannableStringBuilder toBold(String sourceText); static SpannableStringBuilder toBold(String string, String subString); static String formatSize(long size); static String formatSize(long bytes, boolean si); static Uri createUri(Context ctx); static Intent createTakeVideoIntent(Activity ctx, Uri savingUri, int durationInSeconds); static Intent createTakePictureIntent(Activity ctx, Uri savingUri); @Nullable /** * @deprecated Use {@link MediaUtils#createImageUri(Context)} * Creates external content:// scheme uri to save the images at. The image saved at this * {@link android.net.Uri} will be visible via the gallery application on the device. */ static Uri createImageUri(Context ctx); @Nullable /** * @deprecated Use {@link MediaUtils#createVideoUri(Context)} * Creates external content:// scheme uri to save the videos at. */ static Uri createVideoUri(Context ctx); @Nullable /** * * @deprecated Use {#setTextValues} or {#getNullEmptyCheckedValue} * Get the correctly appended name from the given name parameters * * @param firstName * First name * @param lastName * Last name * * @return Returns correctly formatted full name. Returns null if both the values are null. **/ static String getName(String firstName, String lastName); static Bitmap roundBitmap(Bitmap bmp, int radius); static final boolean isRelativeUrl(String url); static boolean isContentUri(Uri uri); static void hideKeyboard(Context context); static boolean isBuildBelow(int buildVersion); static void setTextValues(@NonNull TextView textView, @Nullable String firstValue, @Nullable String secondValue); @Nullable /** * Returns concatenated values of atleast to or more strings provided to it * in a null safe manner. * @param firstValue * @param secondValue * @param delimiter Delimiter to be used to concatnate the parameter strings. If null, space characer will be used. * @param moreValues Optional * @return */ static String getNullEmptyCheckedValue(@Nullable String firstValue, @Nullable String secondValue,
                                                  @Nullable String delimiter, String... moreValues); @Nullable /** * Partially capitalizes the string from paramter start and offset. * * @param string String to be formatted * @param start Starting position of the substring to be capitalized * @param offset Offset of characters to be capitalized * @return */ static String capitalizeString(String string, int start, int offset); @Nullable /** * Generates SHA-512 hash for given binary data. * @param stringToHash * @return */ static String getSha512Hash(String stringToHash); @Nullable /** * Generates SHA-512 hash for given binary data. */ static String getSha512Hash(byte[] dataToHash); @Nullable /** * Gets the extension of a file. */ static String getExtension(File file);  }
@Test public void testIsValidEmailTrue() throws Exception { boolean valid = Utils.isValidEmail("jay@twitter.com"); boolean expected = true; assertEquals("Email invalid", expected, valid); }
@Test public void testIsValidEmailFalse() throws Exception { boolean invalid = Utils.isValidEmail("jay@twitter"); boolean expected = false; assertEquals("Email invalid", expected, invalid); }