Last active
April 9, 2017 08:59
-
-
Save Joisar/77f2f11759bae13892f7 to your computer and use it in GitHub Desktop.
Common UTILS pattern
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class UTILS { | |
public static boolean LOG_ENABLED = true; | |
public static class Log { | |
public static void e(String tag, String message) { | |
if (LOG_ENABLED) { | |
android.util.Log.e(tag, message); | |
} | |
} | |
public static void d(String tag, String message) { | |
if (LOG_ENABLED) { | |
android.util.Log.d(tag, message); | |
} | |
} | |
} | |
public static boolean isNetworkAvailable(Context ctx) { | |
ConnectivityManager cm = (ConnectivityManager) ctx | |
.getSystemService(Context.CONNECTIVITY_SERVICE); | |
NetworkInfo netInfo = cm.getActiveNetworkInfo(); | |
if (netInfo != null && netInfo.isConnectedOrConnecting() | |
&& cm.getActiveNetworkInfo().isAvailable() | |
&& cm.getActiveNetworkInfo().isConnected()) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
public static void showNetworkAlertDialog(Context mCtx) { | |
// TODO Auto-generated method stub | |
AlertDialog.Builder builder = new AlertDialog.Builder(mCtx); | |
builder.setCancelable(false); | |
builder.setTitle("Network Error"); | |
builder.setMessage("Internet is not available!"); | |
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { | |
@Override | |
public void onClick(DialogInterface dialog, int which) { | |
dialog.dismiss(); | |
} | |
}); | |
builder.create().show(); | |
} | |
public final static boolean isValidEmail(CharSequence target) { | |
return android.util.Patterns.EMAIL_ADDRESS.matcher(target).matches(); | |
} | |
public static String getAppPackageName(Context mContext) | |
{ | |
String app_package_name = mContext.getApplicationContext().getPackageName(); | |
return app_package_name; | |
} | |
public static String getAppVersion(Context mContext) | |
{ | |
boolean shouldPretendAsLatest = true; | |
String app_version = "0"; | |
PackageManager mPackageManager = mContext.getApplicationContext().getPackageManager(); | |
try { | |
app_version = mPackageManager.getPackageInfo(getAppPackageName(mContext), 0).versionName; | |
} catch (PackageManager.NameNotFoundException e) { | |
e.printStackTrace(); | |
} | |
if(shouldPretendAsLatest) | |
{ | |
app_version = "1.0.0.0"; | |
} | |
return app_version; | |
} | |
public static boolean isAppDirCreated() | |
{ | |
if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) && !Environment.MEDIA_MOUNTED_READ_ONLY.equals(Environment.getExternalStorageState())) | |
{ | |
File root = android.os.Environment.getExternalStorageDirectory(); | |
File dir = new File(root.getAbsolutePath() + "/"+CONSTANTS.app_dir); | |
dir.mkdirs(); | |
return true; | |
} | |
return false; | |
} | |
public static boolean copyFile(String from, String to) { | |
try { | |
File sd = Environment.getExternalStorageDirectory(); | |
if (sd.canWrite()) { | |
int end = from.toString().lastIndexOf("/"); | |
String str1 = from.toString().substring(0, end); | |
String str2 = from.toString().substring(end+1, from.length()); | |
File source = new File(str1, str2); | |
File destination= new File(to, str2); | |
Log.d("source", source.getPath()); | |
Log.d("destination", destination.getPath()); | |
if (source.exists()) { | |
FileChannel src = new FileInputStream(source).getChannel(); | |
FileChannel dst = new FileOutputStream(destination).getChannel(); | |
dst.transferFrom(src, 0, src.size()); | |
src.close(); | |
dst.close(); | |
} | |
} | |
return true; | |
} catch (Exception e) { | |
return false; | |
} | |
} | |
public static String getAppDirPath() | |
{ | |
if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) && !Environment.MEDIA_MOUNTED_READ_ONLY.equals(Environment.getExternalStorageState())) | |
{ | |
File root = android.os.Environment.getExternalStorageDirectory(); | |
File dir = new File(root.getAbsolutePath() + "/"+CONSTANTS.app_dir); | |
dir.mkdirs(); | |
return dir.getPath(); | |
/* File mTargetFile = new File(dir, filename); | |
return mTargetFile.getPath(); | |
*/ } | |
return ""; | |
} | |
public static int getRandomNumber() { | |
Random r = new Random( System.currentTimeMillis() ); | |
return (1 + r.nextInt(2)) * 10000 + r.nextInt(10000); | |
} | |
// 2014-05-01T00:00:00 | |
public static String getFormattedDate(Date date) | |
{ | |
String formatted_date = ""; | |
String pattern = "yyyy-MM-dd'T'HH:mm:ss"; | |
SimpleDateFormat sdf = new SimpleDateFormat(pattern); | |
formatted_date = sdf.format(date); | |
return formatted_date; | |
} | |
public static Intent getRateIntent(String target_package_name) | |
{ | |
final String mRate_Url = "http://play.google.com/store/apps/details?id="; | |
return new Intent(Intent.ACTION_VIEW, Uri.parse(mRate_Url+target_package_name)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment