import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.res.ColorStateList;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.media.ExifInterface;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.support.v7.app.AlertDialog;
import android.telephony.TelephonyManager;
import android.util.Base64;
import android.util.Log;
import android.util.TypedValue;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.cert.CertificateException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import static android.content.Context.MODE_PRIVATE;
/**
* Created by Roy.Leung on 4/1/17.
*/
public class Utility {
public static boolean checkEmail(String email) {
String rexRule = "([\\w-+]+(?:\\.[\\w-+]+)*@(?:[\\w-]+\\.)+[a-zA-Z]{2,7})";
if (email.matches(rexRule))
return true;
else
return false;
}
public static void showDialog(Context context, String title, String message, DialogInterface.OnClickListener onClickListener) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage(message).setTitle(title);
// Add the buttons
builder.setPositiveButton("OK", onClickListener);
builder.setCancelable(false);
// Create the AlertDialog
AlertDialog dialog = builder.create();
dialog.show();
}
public static void showOneButtonDialog(Context context, String title, String message, String buttonText, DialogInterface.OnClickListener onClickListener) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage(message).setTitle(title);
// Add the buttons
builder.setPositiveButton(buttonText, onClickListener);
builder.setCancelable(false);
// Create the AlertDialog
AlertDialog dialog = builder.create();
dialog.show();
}
public static void showTowButtonDialog(Context context, String title, String message, String leftButton, String rightButton,
DialogInterface.OnClickListener rightOnClickListener,
DialogInterface.OnClickListener leftOnClickListener) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage(message).setTitle(title);
// Add the buttons
builder.setPositiveButton(rightButton, rightOnClickListener);
builder.setNegativeButton(leftButton, leftOnClickListener);
builder.setCancelable(false);
// Create the AlertDialog
AlertDialog dialog = builder.create();
dialog.show();
}
public static String getDeviceID(Context context) {
String deviceID = null;
if (deviceID == null) {
TelephonyManager tManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
deviceID = tManager.getDeviceId();
}
return deviceID;
}
public static String getDeviceModel() {
return Build.MANUFACTURER + " - " + Build.MODEL;
}
public static String getDeviceOS() {
return "Android";
}
public static String getAppVersion() {
return BuildConfig.VERSION_CODE + "";
}
private static Bitmap resizeBitmap(Bitmap image, int maxWidth, int maxHeight) {
if (maxHeight > 0 && maxWidth > 0) {
int width = image.getWidth();
int height = image.getHeight();
float ratioBitmap = (float) width / (float) height;
float ratioMax = (float) maxWidth / (float) maxHeight;
int finalWidth = maxWidth;
int finalHeight = maxHeight;
if (ratioMax > 1) {
finalWidth = (int) ((float) maxHeight * ratioBitmap);
} else {
finalHeight = (int) ((float) maxWidth / ratioBitmap);
}
image = Bitmap.createScaledBitmap(image, finalWidth, finalHeight, true);
return image;
} else {
return image;
}
}
public static Bitmap decodeSampledBitmapFromUri(ContentResolver cr, Uri uri,
int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
try {
BitmapFactory.decodeStream(cr.openInputStream(uri), null, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeStream(cr.openInputStream(uri), null, options);
} catch (Exception ex) {
return null;
}
}
public static Bitmap getBitmapFromUri(Uri uri, Context context) {
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(context.getContentResolver(), uri);
return bitmap;
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
public static String formatTheDate(int day, int month, int year) {
// return String.format("%02d/%02d/%04d", day, month, year);
return String.format("%04d-%02d-%02d", year, month, day);
}
public static Bitmap scaleImageBitmap(String path, int imgWidth, int imgHeight) {
Bitmap srcBitmap = null;
// Configure.Log("scaleImageBitmap path: " + path);
try {
double ratio;
srcBitmap = getBitmapFromPathWithHighQuality(path);
double w = srcBitmap.getWidth();
double h = srcBitmap.getHeight();
// Determine the Image baseLength
double baseLength = Math.max(w, h);
if (w == baseLength && w > imgWidth) {
// Scale Down the Image base on weight
ratio = w / imgWidth;
w = imgWidth;
h = (int) (h / ratio);
} else if (h == baseLength && h > imgHeight) {
// Scale Down the Image base on height
ratio = h / imgHeight;
h = imgHeight;
w = (int) (w / ratio);
}
srcBitmap = Bitmap.createScaledBitmap(srcBitmap, (int) w, (int) h, true);
} catch (Exception ex) {
ex.printStackTrace();
srcBitmap = null;
// Configure.Log("scaleImageBitmap fail!");
}
if (srcBitmap != null && !srcBitmap.isRecycled()) {
// Configure.Log("scaledBitmap w: " + srcBitmap.getWidth());
// Configure.Log("scaledBitmap h: " + srcBitmap.getHeight());
}
return srcBitmap;
}
public static Bitmap getBitmapFromPath(String path, int maxNumOfPixel) throws Exception {
// GET IMAGE FROM THE PATH
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, opts);
opts.inSampleSize = computeSampleSize(opts, -1, maxNumOfPixel);
opts.inJustDecodeBounds = false;
Bitmap bitmap = BitmapFactory.decodeFile(path, opts);
int degree = getBitmapOrientation(path);
if (degree != 0) {
Matrix matrix = new Matrix();
matrix.postRotate(degree);
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
}
return bitmap;
}
public static int getBitmapOrientation(String path) {
int orientation = -1;
int degree = 0;
ExifInterface exif = null;
try {
exif = new ExifInterface(path);
} catch (IOException e) {
e.printStackTrace();
}
if (exif != null) {
orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, -1);
if (orientation != -1) {
// We only recognize a subset of orientation tag values.
switch (orientation) {
case ExifInterface.ORIENTATION_NORMAL:
degree = 0;
break;
case ExifInterface.ORIENTATION_ROTATE_90:
degree = 90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
degree = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
degree = 270;
break;
}
// Configure.Log("orientation: " + orientation);
// Configure.Log("degree: " + degree);
}
}
return degree;
}
public static int computeSampleSize(BitmapFactory.Options options, int minSideLength, int maxNumOfPixels) {
int initialSize = computeInitialSampleSize(options, minSideLength, maxNumOfPixels);
int roundedSize;
if (initialSize <= 8) {
roundedSize = 1;
while (roundedSize < initialSize) {
roundedSize <<= 1;
}
} else {
roundedSize = (initialSize + 7) / 8 * 8;
}
return roundedSize;
}
private static int computeInitialSampleSize(BitmapFactory.Options options, int minSideLength, int maxNumOfPixels) {
double w = options.outWidth;
double h = options.outHeight;
int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math.sqrt(w * h / maxNumOfPixels));
int upperBound = (minSideLength == -1) ? 128 : (int) Math.min(Math.floor(w / minSideLength),
Math.floor(h / minSideLength));
if (upperBound < lowerBound) {
// return the larger one when there is no overlapping zone.
return lowerBound;
}
if ((maxNumOfPixels == -1) && (minSideLength == -1)) {
return 1;
} else if (minSideLength == -1) {
return lowerBound;
} else {
return upperBound;
}
}
public static String getPath(final Context context, final Uri uri) {
final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
// DocumentProvider
if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
// ExternalStorageProvider
if (isExternalStorageDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
if ("primary".equalsIgnoreCase(type)) {
return Environment.getExternalStorageDirectory() + "/" + split[1];
}
// TODO handle non-primary volumes
}
// DownloadsProvider
else if (isDownloadsDocument(uri)) {
final String id = DocumentsContract.getDocumentId(uri);
final Uri contentUri = ContentUris.withAppendedId(
Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
return getDataColumn(context, contentUri, null, null);
}
// MediaProvider
else if (isMediaDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
Uri contentUri = null;
if ("image".equals(type)) {
contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
} else if ("video".equals(type)) {
contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
} else if ("audio".equals(type)) {
contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
}
final String selection = "_id=?";
final String[] selectionArgs = new String[]{
split[1]
};
return getDataColumn(context, contentUri, selection, selectionArgs);
}
}
// MediaStore (and general)
else if ("content".equalsIgnoreCase(uri.getScheme())) {
return getDataColumn(context, uri, null, null);
}
// File
else if ("file".equalsIgnoreCase(uri.getScheme())) {
return uri.getPath();
}
return null;
}
/**
* Get the value of the data column for this Uri. This is useful for
* MediaStore Uris, and other file-based ContentProviders.
*
* @param context The context.
* @param uri The Uri to query.
* @param selection (Optional) Filter used in the query.
* @param selectionArgs (Optional) Selection arguments used in the query.
* @return The value of the _data column, which is typically a file path.
*/
public static String getDataColumn(Context context, Uri uri, String selection,
String[] selectionArgs) {
Cursor cursor = null;
final String column = "_data";
final String[] projection = {
column
};
try {
cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,
null);
if (cursor != null && cursor.moveToFirst()) {
final int column_index = cursor.getColumnIndexOrThrow(column);
return cursor.getString(column_index);
}
} finally {
if (cursor != null)
cursor.close();
}
return null;
}
/**
* @param uri The Uri to check.
* @return Whether the Uri authority is ExternalStorageProvider.
*/
public static boolean isExternalStorageDocument(Uri uri) {
return "com.android.externalstorage.documents".equals(uri.getAuthority());
}
/**
* @param uri The Uri to check.
* @return Whether the Uri authority is DownloadsProvider.
*/
public static boolean isDownloadsDocument(Uri uri) {
return "com.android.providers.downloads.documents".equals(uri.getAuthority());
}
/**
* @param uri The Uri to check.
* @return Whether the Uri authority is MediaProvider.
*/
public static boolean isMediaDocument(Uri uri) {
return "com.android.providers.media.documents".equals(uri.getAuthority());
}
public static Bitmap getBitmapFromPathWithHighQuality(String path) throws Exception {
return getBitmapFromPath(path, 1500 * 1500);
}
public static Bitmap getBitmapFromUriAndRotateWithExif(Uri imageUri, Context context) {
ExifInterface exif = null;
Bitmap bitmap = null;
try {
bitmap = resizeBitmap(MediaStore.Images.Media.getBitmap(context.getContentResolver(), imageUri), 1024, 1024);
String imagePath;
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
imagePath = Utility.getPath(context, imageUri);
} else {
File myFile = new File(imageUri.getPath());
imagePath = myFile.getAbsolutePath();
}
exif = new ExifInterface(imagePath);
} catch (IOException e) {
e.printStackTrace();
return null;
}
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_UNDEFINED);
return rotateBitmap(bitmap, orientation);
}
public static Bitmap rotateBitmap(Bitmap bitmap, int orientation) {
Matrix matrix = new Matrix();
switch (orientation) {
case ExifInterface.ORIENTATION_NORMAL:
return bitmap;
case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
matrix.setScale(-1, 1);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
matrix.setRotate(180);
break;
case ExifInterface.ORIENTATION_FLIP_VERTICAL:
matrix.setRotate(180);
matrix.postScale(-1, 1);
break;
case ExifInterface.ORIENTATION_TRANSPOSE:
matrix.setRotate(90);
matrix.postScale(-1, 1);
break;
case ExifInterface.ORIENTATION_ROTATE_90:
matrix.setRotate(90);
break;
case ExifInterface.ORIENTATION_TRANSVERSE:
matrix.setRotate(-90);
matrix.postScale(-1, 1);
break;
case ExifInterface.ORIENTATION_ROTATE_270:
matrix.setRotate(-90);
break;
default:
return bitmap;
}
try {
Bitmap bmRotated = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
bitmap.recycle();
return bmRotated;
} catch (OutOfMemoryError e) {
e.printStackTrace();
return null;
}
}
public static String bitmapToBase64String(Bitmap bitmap) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 50, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream.toByteArray();
return Base64.encodeToString(byteArray, Base64.DEFAULT);
}
public static String bitmapToBase64StringFromUri(Uri uri, Context context) {
return bitmapToBase64String(getBitmapFromUriAndRotateWithExif(uri, context));
}
public static OkHttpClient getUnsafeOkHttpClient() {
String fav = com.generali.hk.BuildConfig.FLAVOR;
if (fav.equals("production")) {
return new OkHttpClient();
} else {
try {
// Create a trust manager that does not validate certificate chains
final TrustManager[] trustAllCerts = new TrustManager[]{
new X509TrustManager() {
@Override
public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return new java.security.cert.X509Certificate[]{};
}
}
};
// Install the all-trusting trust manager
final SSLContext sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
// Create an ssl socket factory with our all-trusting manager
final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.sslSocketFactory(sslSocketFactory);
builder.hostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
OkHttpClient okHttpClient = builder.build();
return okHttpClient;
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
}
public static void removeWebviewCacheDatabase(Context context) {
try {
boolean deleted1 = context.deleteDatabase("webview.db");
boolean deleted2 = context.deleteDatabase("webviewCache.db");
} catch (Exception e) {
}
}
public static String convertDateFormat1To2(String targetDate, String dateFormat1, String dateFormat2) {
SimpleDateFormat df1 = new SimpleDateFormat(dateFormat1);
try {
Date parsedDate = df1.parse(targetDate);
return convertDateToStringENG(parsedDate, dateFormat2);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
public static String convertDateToString(Date targetDate, String dateFormat) {
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
String dateString = sdf.format(targetDate);
return dateString;
}
public static String convertDateToStringENG(Date targetDate, String dateFormat) {
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat, Locale.ENGLISH);
String dateString = sdf.format(targetDate);
return dateString;
}
public static String convertMillisToDate(long milliSeconds, String dateFormat) {
SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(milliSeconds);
return formatter.format(calendar.getTime());
}
public static String convertMillisToDateEN(long milliSeconds, String dateFormat) {
SimpleDateFormat formatter = new SimpleDateFormat(dateFormat, Locale.ENGLISH);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(milliSeconds);
return formatter.format(calendar.getTime());
}
public static long getTimeDifferent(String start, String end, String format) {
long startTime = 0;
long endTime = 0;
SimpleDateFormat df = new SimpleDateFormat(format);
try {
Date parsedDate1 = df.parse(start);
Date parsedDate2 = df.parse(end);
startTime = parsedDate1.getTime();
endTime = parsedDate2.getTime();
} catch (ParseException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return endTime - startTime;
}
public static Bitmap getBitmapFromUriAndRotateWithExif(Context context, Uri imageUri) throws IOException {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(context.getContentResolver(), imageUri);
ExifInterface exif = null;
File imageFile = new File(imageUri.getPath());
String imagePath = imageFile.getAbsolutePath();
try {
exif = new ExifInterface(imagePath);
} catch (IOException e) {
e.printStackTrace();
}
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_UNDEFINED);
return rotateBitmap(bitmap, orientation);
}
public static Bitmap getBitmapFromUri(Context context, Uri imageUri) throws IOException {
return MediaStore.Images.Media.getBitmap(context.getContentResolver(), imageUri);
}
public static Bitmap resizeWeixinBmp(Bitmap bmp, int dWidth) {
if (bmp == null)
return null;
int width = bmp.getWidth();
int height = bmp.getHeight();
float ratio = (float) height / width;
int desiredWidth = dWidth;
if (ratio > 2)
desiredWidth = (int) (dWidth * 2 / ratio);
Bitmap resizedBitmap = Bitmap.createScaledBitmap(bmp, desiredWidth, (int) (desiredWidth * ratio), false);
return resizedBitmap;
}
public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
if (width > height) {
inSampleSize = (int) Math.ceil((float) height / (float) reqHeight);
} else {
inSampleSize = (int) Math.ceil((float) width / (float) reqWidth);
}
}
// Utility.Log("Utility", "@calculateInSampleSize:" + inSampleSize);
return inSampleSize;
}
public static int calculateInSampleSizeForMaxWidthHeight(BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
int inSampleSize1 = (int) Math.ceil((float) height / (float) reqHeight);
int inSampleSize2 = (int) Math.ceil((float) width / (float) reqWidth);
inSampleSize = inSampleSize1 > inSampleSize2 ? inSampleSize1 : inSampleSize2;
}
// Utility.Log("Utility", "@calculateInSampleSize:" + inSampleSize);
return inSampleSize;
}
public static Bitmap decodeBitmapFromInputStream(InputStream originalIS,
int reqWidth, int reqHeight) {
Bitmap result;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
byte[] buffer = new byte[1024];
int len;
while ((len = originalIS.read(buffer)) > -1) {
baos.write(buffer, 0, len);
}
baos.flush();
} catch (Exception e) {
e.printStackTrace();
return null;
}
InputStream is1 = new ByteArrayInputStream(baos.toByteArray());
InputStream is2 = new ByteArrayInputStream(baos.toByteArray());
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
try {
BitmapFactory.decodeStream(is1, null, options);
int width = options.outWidth;
int height = options.outHeight;
if (reqWidth < options.outWidth || reqHeight < options.outHeight) {
options.inSampleSize = calculateInSampleSizeForMaxWidthHeight(options, reqWidth, reqHeight);
}
int size = options.inSampleSize;
options.inJustDecodeBounds = false;
Bitmap b = BitmapFactory.decodeStream(is2, null, options);
if (size != 1) {
if (width * reqHeight < height * reqWidth) {
result = Bitmap.createScaledBitmap(b, reqHeight * width / height, (int) (reqHeight), false);
} else {
result = Bitmap.createScaledBitmap(b, reqWidth, (int) (height * reqWidth / width), false);
}
b.recycle();
} else {
result = b;
}
} catch (Exception e) {
e.printStackTrace();
result = null;
} finally {
try {
if (is1 != null) {
is1.close();
is1 = null;
}
if (is2 != null) {
is2.close();
is2 = null;
}
baos = null;
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
public static Intent getUrlIntentAction(Context context, String url) {
Intent intent = null;
if (url.startsWith("market:")) {
intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
} else if (url.startsWith("sms:")) {
String[] tempStringArray = url.split("\\:");
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("sms:"));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (tempStringArray.length > 2) {
intent.putExtra("address", tempStringArray[1]);
intent.putExtra("sms_body", tempStringArray[2]);
} else if (tempStringArray.length > 1) {
intent.putExtra("address", tempStringArray[1]);
}
intent.setType("vnd.android-dir/mms-sms");
} else if (url.startsWith("tel:")) {
intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
} else if (url.startsWith("mailto:")) {
intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/html");
int colonIndex = url.indexOf(":") + 1;
String mailString = url.substring(colonIndex);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{mailString});
} else if (url.toLowerCase().startsWith("video:")) {
int colonIndex = url.indexOf(":") + 1;
String linkString = url.substring(colonIndex);
intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(Uri.parse(linkString), "video/*");
} else if (url.toLowerCase().startsWith("activity:")) {
int colonIndex = url.indexOf(":") + 1;
String linkString = url.substring(colonIndex);
intent = new Intent(Intent.ACTION_VIEW, Uri.parse(linkString));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
} else if (url.startsWith("https://play.google.com") || url.startsWith("http://play.google.com")) {
String appId = getGooglePlayAppId(url);
if (appId != null) {
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appId));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
} else if (url.startsWith("fb:")) {
intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
} else if (url.startsWith("vnd.youtube:")) {
intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
} else if (url.contains("youtube.com")) {
String videoId = getYoutubeVideoId(url);
if (videoId != null) {
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube:" + videoId));
intent.putExtra("VIDEO_ID", videoId);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
}
if (intent != null && !isIntentCanHandle(context, intent)) {
intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
return intent;
}
static boolean isIntentCanHandle(Context context, Intent intent) {
boolean canHandle = false;
try {
PackageManager manager = context.getPackageManager();
List<ResolveInfo> infos = manager.queryIntentActivities(intent, 0);
if (infos.size() > 0) {
canHandle = true;
} else {
canHandle = false;
}
} catch (Exception e) {
e.printStackTrace();
}
return canHandle;
}
public static String getYoutubeVideoId(String youtubeUrl) {
// android.util.Log.d("getYoutubeVideoId", "youtubeUrl: " + youtubeUrl);
String video_id = null;
if (youtubeUrl != null && youtubeUrl.trim().length() > 0 && youtubeUrl.startsWith("http")) {
String expression = "^.*((youtu.be" + "\\/)"
+ "|(v\\/)|(\\/u\\/w\\/)|(embed\\/)|(watch\\?))\\??v?=?([^#\\&\\?]*).*";
CharSequence input = youtubeUrl;
Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(input);
if (matcher.matches()) {
// for (int i = 0; i <= matcher.groupCount(); i++) {
// android.util.Log.d("getYoutubeVideoId", i + " : " +
// matcher.group(i));
// }
String groupIndex1 = matcher.group(matcher.groupCount());
if (groupIndex1 != null && groupIndex1.length() == 11)
video_id = groupIndex1;
}
}
return video_id;
}
public static float convertDipToPx(Context context, float dipValue) {
float px = 0;
if (context != null) {
px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dipValue, context.getResources()
.getDisplayMetrics());
}
return px;
}
public static boolean isValidMobile(String mobile) {
String regexStr = "^\\d{8}$";
if (mobile.matches(regexStr)) {
return true;
}
return false;
}
public static boolean saveImageToInternalCache(Context context, String imageName, Bitmap bitmap) {
boolean success = false;
FileOutputStream fos = null;
File mydir = context.getDir(TEMP_IMGE_FOLDER, MODE_PRIVATE); //Creating an internal dir;
if (!mydir.exists())
mydir.mkdirs();
File fileWithinMyDir = new File(mydir, imageName); //Getting a file within the dir.
try {
if (bitmap != null) {
fos = new FileOutputStream(fileWithinMyDir);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
success = true;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return success;
}
public static boolean saveImageToFile(Context context, File file, Bitmap bitmap) {
boolean success = false;
FileOutputStream fos = null;
file.mkdirs();
if (file.exists())
file.delete();
try {
if (bitmap != null) {
fos = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
success = true;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return success;
}
public static void deleteAllInternalTempImages(Context context) throws IOException {
// Context context = AppsApplication.getAppContext();
File imageFile;
File internalFileDir = context.getDir(TEMP_IMGE_FOLDER, MODE_PRIVATE);
for (String intFileName : internalFileDir.list()) {
imageFile = new File(internalFileDir, intFileName);
if (imageFile.exists() && imageFile.toString().contains(".png")) {
imageFile.delete();
}
}
}
public static Bitmap getImageFromCache(Context context, String imageName) {
Bitmap bitmap = null;
FileInputStream fis = null;
try {
fis = context.getApplicationContext().openFileInput(imageName);
bitmap = BitmapFactory.decodeStream(fis);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return bitmap;
}
public static File createTemporaryFile(String part, String ext) throws Exception {
File tempDir = Environment.getExternalStorageDirectory();
tempDir = new File(tempDir.getAbsolutePath() + "/.temp/");
if (!tempDir.exists()) {
tempDir.mkdir();
}
return File.createTempFile(part, ext, tempDir);
}
public static boolean appInstalledOrNot(Context context, String uri) {
PackageManager pm = context.getPackageManager();
boolean app_installed;
try {
pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
app_installed = true;
} catch (PackageManager.NameNotFoundException e) {
app_installed = false;
}
return app_installed;
}
public static Point getTheScreenSize(Context context) {
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
return size;
}
public static void setAViewSize(View v, int width, int height) {
LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(width, height);
v.setLayoutParams(parms);
}
public static Drawable tintDrawable(Drawable drawable, ColorStateList colors) {
final Drawable wrappedDrawable = DrawableCompat.wrap(drawable);
DrawableCompat.setTintList(wrappedDrawable, colors);
return wrappedDrawable;
}
public static void clearAllTheBackStack(FragmentActivity fActivity) {
FragmentManager fm = fActivity.getSupportFragmentManager();
fm.popBackStackImmediate(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
}
/* public static void setSpIsComeFromLoginFragment(Context context) {
SharedPreferences.Editor editor = context.getSharedPreferences(StaticString.SP_NAME, MODE_PRIVATE).edit();
editor.putBoolean(StaticString.SP_IS_FROM_PROPERTIES_LOFIN_FRAGMENT, true);
editor.commit();
}*/
/* static public boolean getSpIsComeFromLoginFragment(Context context) {
SharedPreferences sp = context.getSharedPreferences(StaticString.SP_NAME, MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
boolean answer = sp.getBoolean(StaticString.SP_IS_FROM_PROPERTIES_LOFIN_FRAGMENT, false);
editor.putBoolean(StaticString.SP_IS_FROM_PROPERTIES_LOFIN_FRAGMENT, false);
editor.commit();
return answer;
}*/
static public String getVersionName(Context context) {
PackageInfo pInfo = null;
try {
pInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
String version = pInfo.versionName;
return version;
}
/*public static void setNotificationOnatServer(Boolean on, Context context, final MainActivity acitivty) {
// final ProgressHUD progress = ProgressHUD.show(context);
// progress.show();
OkHttpClient client = new OkHttpClient();
String deviceID = acitivty.getDeviceID();
FirebaseInstanceId firebase = FirebaseInstanceId.getInstance();
if (firebase == null) {
Utility.Log("firebase is null");
return;
}
String token = FirebaseInstanceId.getInstance().getToken();
if (token == null) {
Utility.Log("Token is null");
return;
}
Request request = new Request.Builder().url(ApiManagerForProperties.getNotificationSettingOnServerURL(on, deviceID, token)).build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.e("IOException", e.toString());
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (acitivty == null)
return;
Utility.Log(response.toString());
// acitivty.runOnUiThread(new Runnable() {
// @Override
// public void run() {
// progress.dismiss();
//
// }
// });
}
});
}*/
public static Bitmap resizeBitmapCentralInside(Bitmap image, int maxWidth, int maxHeight) {
if (maxHeight > 0 && maxWidth > 0) {
int width = image.getWidth();
int height = image.getHeight();
float ratioBitmap = (float) width / (float) height;
float ratioMax = (float) maxWidth / (float) maxHeight;
int finalWidth = maxWidth;
int finalHeight = maxHeight;
if (ratioMax < 1) {
finalWidth = (int) ((float) maxHeight * ratioBitmap);
} else {
finalHeight = (int) ((float) maxWidth / ratioBitmap);
}
image = Bitmap.createScaledBitmap(image, finalWidth, finalHeight, false);
return image;
} else {
return image;
}
}
}