Advertisement
ricky_yulianto

Untitled

Jan 30th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.79 KB | None | 0 0
  1. package codelabs.siloam.utils;
  2.  
  3. import android.app.Activity;
  4. import android.content.Context;
  5. import android.content.res.Configuration;
  6. import android.content.res.Resources;
  7. import android.database.Cursor;
  8. import android.graphics.PorterDuff;
  9. import android.graphics.drawable.Drawable;
  10. import android.net.ConnectivityManager;
  11. import android.net.NetworkInfo;
  12. import android.net.Uri;
  13. import android.preference.PreferenceManager;
  14. import android.support.v4.content.ContextCompat;
  15. import android.support.v4.graphics.drawable.DrawableCompat;
  16. import android.text.Html;
  17. import android.text.Spanned;
  18. import android.text.TextUtils;
  19. import android.util.DisplayMetrics;
  20. import android.view.View;
  21. import android.view.inputmethod.InputMethodManager;
  22. import android.widget.ImageView;
  23. import android.widget.TextView;
  24. import android.widget.Toast;
  25.  
  26. import java.net.URISyntaxException;
  27. import java.text.DecimalFormat;
  28. import java.text.DecimalFormatSymbols;
  29. import java.text.ParseException;
  30. import java.text.SimpleDateFormat;
  31. import java.util.Calendar;
  32. import java.util.Date;
  33. import java.util.Locale;
  34. import java.util.regex.Matcher;
  35. import java.util.regex.Pattern;
  36.  
  37. import codelabs.siloam.AppController;
  38. import codelabs.siloam.R;
  39. import codelabs.siloam.connection.DataManager;
  40.  
  41. /**
  42. * Created by indra on 11/16/2016.
  43. */
  44.  
  45. public class RecentUtils {
  46.  
  47. public static int ConvertDpToPx(Context context, int dp) {
  48. DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
  49. return Math.round(dp * (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
  50. }
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59. public static String getPath(Context context, Uri uri) throws URISyntaxException {
  60. if ("content".equalsIgnoreCase(uri.getScheme())) {
  61. String[] projection = { "_data" };
  62. Cursor cursor = null;
  63.  
  64. try {
  65. cursor = context.getContentResolver().query(uri, projection, null, null, null);
  66. int column_index = cursor.getColumnIndexOrThrow("_data");
  67. if (cursor.moveToFirst()) {
  68. return cursor.getString(column_index);
  69. }
  70. } catch (Exception e) {
  71. // Eat it
  72. }
  73. }
  74. else if ("file".equalsIgnoreCase(uri.getScheme())) {
  75. return uri.getPath();
  76. }
  77.  
  78. return null;
  79. }
  80.  
  81.  
  82. public static boolean isOnline(Context context) {
  83. ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
  84. NetworkInfo netInfo = null;
  85. if (connectivityManager != null) {
  86. netInfo = connectivityManager.getActiveNetworkInfo();
  87. }
  88. return (netInfo != null && netInfo.isConnected());
  89. }
  90.  
  91.  
  92. public static String printDifference(Date startDate, Date endDate){
  93.  
  94. //milliseconds
  95. long different = endDate.getTime() - startDate.getTime();
  96.  
  97. System.out.println("startDate : " + startDate);
  98. System.out.println("endDate : "+ endDate);
  99. System.out.println("different : " + different);
  100.  
  101. long secondsInMilli = 1000;
  102. long minutesInMilli = secondsInMilli * 60;
  103. long hoursInMilli = minutesInMilli * 60;
  104. long daysInMilli = hoursInMilli * 24;
  105.  
  106. long elapsedDays = different / daysInMilli;
  107. different = different % daysInMilli;
  108.  
  109. long elapsedHours = different / hoursInMilli;
  110. different = different % hoursInMilli;
  111.  
  112. long elapsedMinutes = different / minutesInMilli;
  113. different = different % minutesInMilli;
  114.  
  115. long elapsedSeconds = different / secondsInMilli;
  116.  
  117. System.out.printf(
  118. "%d days, %d hours, %d minutes, %d seconds%n",
  119. elapsedDays,
  120. elapsedHours, elapsedMinutes, elapsedSeconds);
  121.  
  122. return String.valueOf(elapsedDays);
  123. }
  124.  
  125. public static String printDifferenceHours(Date startDate, Date endDate){
  126.  
  127. //milliseconds
  128. long different = endDate.getTime() - startDate.getTime();
  129.  
  130. System.out.println("startDate : " + startDate);
  131. System.out.println("endDate : "+ endDate);
  132. System.out.println("different : " + different);
  133.  
  134. long secondsInMilli = 1000;
  135. long minutesInMilli = secondsInMilli * 60;
  136. long hoursInMilli = minutesInMilli * 60;
  137. long daysInMilli = hoursInMilli * 24;
  138.  
  139. long elapsedDays = different / daysInMilli;
  140. different = different % daysInMilli;
  141.  
  142. long elapsedHours = different / hoursInMilli;
  143. different = different % hoursInMilli;
  144.  
  145. long elapsedMinutes = different / minutesInMilli;
  146. different = different % minutesInMilli;
  147.  
  148. long elapsedSeconds = different / secondsInMilli;
  149.  
  150. System.out.printf(
  151. "%d days, %d hours, %d minutes, %d seconds%n",
  152. elapsedDays,
  153. elapsedHours, elapsedMinutes, elapsedSeconds);
  154.  
  155. return elapsedHours+" hr "+elapsedMinutes+" m";
  156. }
  157.  
  158.  
  159. public static String printDifferenceTransfer(Date startDate, Date endDate){
  160.  
  161. //milliseconds
  162. long different = endDate.getTime() - startDate.getTime();
  163.  
  164. System.out.println("startDate : " + startDate);
  165. System.out.println("endDate : "+ endDate);
  166. System.out.println("different : " + different);
  167.  
  168. long secondsInMilli = 1000;
  169. long minutesInMilli = secondsInMilli * 60;
  170. long hoursInMilli = minutesInMilli * 60;
  171. long daysInMilli = hoursInMilli * 24;
  172.  
  173. long elapsedDays = different / daysInMilli;
  174. different = different % daysInMilli;
  175.  
  176. long elapsedHours = different / hoursInMilli;
  177. different = different % hoursInMilli;
  178.  
  179. long elapsedMinutes = different / minutesInMilli;
  180. different = different % minutesInMilli;
  181.  
  182. long elapsedSeconds = different / secondsInMilli;
  183.  
  184. System.out.printf(
  185. "%d days, %d hours, %d minutes, %d seconds%n",
  186. elapsedDays,
  187. elapsedHours, elapsedMinutes, elapsedSeconds);
  188.  
  189. String print = "";
  190.  
  191. if(elapsedDays > 0){
  192. print +=elapsedDays+" Day(s) ";
  193. }
  194.  
  195. if(elapsedHours > 0){
  196. print +=elapsedHours+" Hour(s) ";
  197. }
  198.  
  199. if(elapsedMinutes > 0){
  200. print +=elapsedMinutes+" Minute(s)";
  201. }
  202.  
  203. return print;
  204. //return elapsedDays+" Day(s) "+elapsedHours+" Hour(s) "+elapsedMinutes+" Minute(s)";
  205. }
  206.  
  207. public static String toRupiah(String currency, String price){
  208. if(TextUtils.isEmpty(price)){
  209. price = "0";
  210. }
  211.  
  212. DecimalFormat kursIndonesia = (DecimalFormat) DecimalFormat.getCurrencyInstance(Locale.getDefault());
  213. //format.setCurrency(Currency.getInstance(currency));
  214. DecimalFormatSymbols formatRp = new DecimalFormatSymbols();
  215.  
  216. formatRp.setCurrencySymbol(currency);
  217. formatRp.setMonetaryDecimalSeparator(',');
  218. formatRp.setGroupingSeparator('.');
  219.  
  220. kursIndonesia.setMinimumFractionDigits(0);
  221. kursIndonesia.setDecimalFormatSymbols(formatRp);
  222. return kursIndonesia.format(Double.parseDouble(price));
  223. }
  224.  
  225. public static Date addDate(Date date, int val){
  226. Calendar c = Calendar.getInstance();
  227. c.setTime(date);
  228. c.add(Calendar.DATE, val); // number of days to add
  229. return c.getTime();
  230. }
  231.  
  232.  
  233. public static Date addMonth(Date date, int val){
  234. Calendar c = Calendar.getInstance();
  235. c.setTime(date);
  236. c.add(Calendar.MONTH, val); // number of days to add
  237. return c.getTime();
  238. }
  239.  
  240. public static Date addYear(Date date, int val){
  241. Calendar c = Calendar.getInstance();
  242. c.setTime(date);
  243. c.add(Calendar.YEAR, val); // number of days to add
  244. return c.getTime();
  245. }
  246.  
  247. public static Date addHour(Date date, int val){
  248. Calendar c = Calendar.getInstance();
  249. c.setTime(date);
  250. c.add(Calendar.HOUR, val); // number of days to add
  251. return c.getTime();
  252. }
  253.  
  254. public static Date addMinute(Date date, int val){
  255. Calendar c = Calendar.getInstance();
  256. c.setTime(date);
  257. c.add(Calendar.MINUTE, val); // number of days to add
  258. return c.getTime();
  259. }
  260.  
  261. public static void hideKeyboard(Activity activity) {
  262. InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
  263. //Find the currently focused view, so we can grab the correct window token from it.
  264. View view = activity.getCurrentFocus();
  265. //If no view currently has focus, create a new one, just so we can grab a window token from it
  266. if (view == null) {
  267. view = new View(activity);
  268. }
  269. imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
  270. }
  271.  
  272. public static boolean isEmailValid(String email) {
  273. boolean isValid = false;
  274.  
  275. String expression = "^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";
  276. CharSequence inputStr = email;
  277.  
  278. Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
  279. Matcher matcher = pattern.matcher(inputStr);
  280. if (matcher.matches()) {
  281. isValid = true;
  282. }
  283. return isValid;
  284. }
  285.  
  286. public static String getAge(int year, int month, int day){
  287. Calendar dob = Calendar.getInstance();
  288. Calendar today = Calendar.getInstance();
  289.  
  290. dob.set(year, month, day);
  291.  
  292. int age = today.get(Calendar.YEAR) - dob.get(Calendar.YEAR);
  293.  
  294. if (today.get(Calendar.DAY_OF_YEAR) < dob.get(Calendar.DAY_OF_YEAR)){
  295. age--;
  296. }
  297.  
  298. Integer ageInt = age;
  299. if(ageInt > 0) {
  300. return String.valueOf(ageInt);
  301. }else{
  302. return "0";
  303. }
  304. }
  305.  
  306. @SuppressWarnings("deprecation")
  307. public static Spanned fromHtml(String html){
  308. if(!TextUtils.isEmpty(html)) {
  309. html = html.replaceAll("\\\\r\\\\n", "<br />");
  310. html = html.replaceAll("\\r\\n", "<br />");
  311. html = html.replaceAll("\\n", "<br />");
  312. }
  313.  
  314. Spanned result;
  315. if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
  316.  
  317. if(!TextUtils.isEmpty(html))
  318. result = Html.fromHtml(html, Html.FROM_HTML_MODE_LEGACY);
  319. else
  320. result = Html.fromHtml("", Html.FROM_HTML_MODE_LEGACY);
  321.  
  322. } else {
  323. if(!TextUtils.isEmpty(html))
  324. result = Html.fromHtml(html);
  325. else
  326. result = Html.fromHtml("");
  327.  
  328. }
  329. return result;
  330. }
  331.  
  332.  
  333.  
  334. public static long printHours(Date startDate, Date endDate){
  335.  
  336. //milliseconds
  337. long different = endDate.getTime() - startDate.getTime();
  338.  
  339. System.out.println("startDate : " + startDate);
  340. System.out.println("endDate : "+ endDate);
  341. System.out.println("different : " + different);
  342.  
  343. long seconds = different / 1000;
  344. long minutes = seconds / 60;
  345. long hours = minutes / 60;
  346. long days = hours / 24;
  347.  
  348. return hours;
  349. }
  350.  
  351. public static void handleRetrofitError(int code) {
  352. switch (code) {
  353. case 404:
  354. Toast.makeText(AppController.getInstance().getApplicationContext(), "Not found", Toast.LENGTH_SHORT).show();
  355. break;
  356. case 500:
  357. Toast.makeText(AppController.getInstance().getApplicationContext(), "Server broken", Toast.LENGTH_SHORT).show();
  358. break;
  359. default:
  360. Toast.makeText(AppController.getInstance().getApplicationContext(), "Unknown error", Toast.LENGTH_SHORT).show();
  361. break;
  362. }
  363. }
  364. public static void handleRetrofitError(int code, TextView textView) {
  365. switch (code) {
  366. case 404:
  367. textView.setText("Not found");
  368. break;
  369. case 500:
  370. textView.setText("Server broken");
  371. break;
  372. default:
  373. textView.setText("Unknown error");
  374. break;
  375. }
  376. }
  377.  
  378. public static String formatDateTimeToDate(String date) {
  379.  
  380. // Date dt;
  381. // SimpleDateFormat sdf;
  382. // SimpleDateFormat sdfs;
  383. // String time1;
  384. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
  385. SimpleDateFormat toFormat = new SimpleDateFormat("yyyy MMMM dd", Locale.getDefault());
  386. Date dt1 = null;
  387. try {
  388. dt1 = simpleDateFormat.parse(date);
  389. } catch (ParseException e) {
  390. e.printStackTrace();
  391. }
  392.  
  393. if(dt1 !=null)
  394. return toFormat.format(dt1);
  395.  
  396. return date;
  397.  
  398. }
  399.  
  400. public static String formatDateToDate(String date) {
  401. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-yyyy", Locale.getDefault());
  402. SimpleDateFormat toFormat = new SimpleDateFormat(" MMMM dd yyyy", Locale.getDefault());
  403. Date dt1 = null;
  404. try {
  405. dt1 = simpleDateFormat.parse(date);
  406. } catch (ParseException e) {
  407. e.printStackTrace();
  408. }
  409.  
  410. if(dt1 !=null)
  411. return toFormat.format(dt1);
  412.  
  413. return date;
  414. }
  415.  
  416. public static String formatDateToDateDMY(String date) {
  417. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
  418. SimpleDateFormat toFormat = new SimpleDateFormat("dd MMMM yyyy", Locale.getDefault());
  419. Date dt1 = null;
  420. try {
  421. dt1 = simpleDateFormat.parse(date);
  422. } catch (ParseException e) {
  423. e.printStackTrace();
  424. }
  425.  
  426. if(dt1 !=null)
  427. return toFormat.format(dt1);
  428.  
  429. return date;
  430. }
  431. // public static String newformatDateTimeToDateYYYYMMDD(String date) {
  432. // SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
  433. // final SimpleDateFormat toFormat = new SimpleDateFormat("EEEE, dd MMMM yyyy HH:mm a" , Locale.getDefault());
  434. // Date dt1 = null;
  435. // try {
  436. // dt1 = simpleDateFormat.parse(date);
  437. // } catch (ParseException e) {
  438. // e.printStackTrace();
  439. // }
  440. //
  441. // if(dt1 !=null)
  442. // return toFormat.format(dt1);
  443. //
  444. // return date;
  445. // }
  446.  
  447. public static String formatDateTimeToTime(String date) {
  448. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
  449. SimpleDateFormat toFormat = new SimpleDateFormat("HH:mm", Locale.getDefault());
  450. Date dt1 = null;
  451. try {
  452. dt1 = simpleDateFormat.parse(date);
  453. } catch (ParseException e) {
  454. e.printStackTrace();
  455. }
  456.  
  457. if(dt1 !=null)
  458. return toFormat.format(dt1);
  459.  
  460. return date;
  461. }
  462.  
  463. public static String formatDateTimeToDayTime(String date) {
  464. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
  465. SimpleDateFormat toFormat = new SimpleDateFormat("EEEE, HH:mm", Locale.getDefault());
  466. Date dt1 = null;
  467. try {
  468. dt1 = simpleDateFormat.parse(date);
  469. } catch (ParseException e) {
  470. e.printStackTrace();
  471. }
  472.  
  473. if(dt1 !=null)
  474. return toFormat.format(dt1);
  475.  
  476. return date;
  477. }
  478.  
  479.  
  480. public static Date formatDateTimeToDateFormat(String date) {
  481. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
  482. SimpleDateFormat toFormat = new SimpleDateFormat("yyyy MMMM dd", Locale.getDefault());
  483. try {
  484. Date dt1 = simpleDateFormat.parse(date);
  485. String result = toFormat.format(dt1);
  486. return toFormat.parse(result);
  487. } catch (ParseException e) {
  488. e.printStackTrace();
  489. }
  490.  
  491. return null;
  492. }
  493.  
  494.  
  495. public static String formatDateToDateString(Date date) {
  496. SimpleDateFormat toFormat = new SimpleDateFormat("dd MMMM yyyy", Locale.getDefault());
  497. return toFormat.format(date);
  498. }
  499.  
  500. public static String formatDateTimeToDateYYYYMMDD(String date) {
  501. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
  502. final SimpleDateFormat toFormat = new SimpleDateFormat("EEEE, dd MMMM yyyy \nHH:mm a", Locale.getDefault());
  503. Date dt1 = null;
  504. try {
  505. dt1 = simpleDateFormat.parse(date);
  506. } catch (ParseException e) {
  507. e.printStackTrace();
  508. }
  509.  
  510. if(dt1 !=null)
  511. return toFormat.format(dt1);
  512.  
  513. return date;
  514. }
  515. public static String new1formatDateTimeToDateYYYYMMDD(String date) {
  516. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
  517. final SimpleDateFormat toFormat = new SimpleDateFormat("EEEE, dd MMMM yyyy", Locale.getDefault());
  518. Date dt1 = null;
  519. try {
  520. dt1 = simpleDateFormat.parse(date);
  521. } catch (ParseException e) {
  522. e.printStackTrace();
  523. }
  524.  
  525. if(dt1 !=null)
  526. return toFormat.format(dt1);
  527.  
  528. return date;
  529. }
  530. public static String newformatDateTimeToDateYYYYMMDD(String date) {
  531. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
  532. final SimpleDateFormat toFormat = new SimpleDateFormat("EEEE, dd MMMM yyyy HH:mm a" , Locale.getDefault());
  533. Date dt1 = null;
  534. try {
  535. dt1 = simpleDateFormat.parse(date);
  536. } catch (ParseException e) {
  537. e.printStackTrace();
  538. }
  539.  
  540. if(dt1 !=null)
  541. return toFormat.format(dt1);
  542.  
  543. return date;
  544. }
  545.  
  546. public static void changeLanguage(Context context, String val){
  547. final Locale newLocale = new Locale(val);
  548. Locale.setDefault(newLocale);
  549. final Configuration config = new Configuration();
  550. config.locale = newLocale;
  551.  
  552. final Resources res = context.getResources();
  553. res.updateConfiguration(config, res.getDisplayMetrics());
  554.  
  555. DataManager.getInstance().setLanguage(val);
  556. }
  557.  
  558. public static void setStatusColor(TextView textView, int color){
  559. Drawable drawable = ContextCompat.getDrawable(AppController.getInstance().getApplicationContext(), R.drawable.urgentsmall);
  560. drawable = DrawableCompat.wrap(drawable);
  561. DrawableCompat.setTint(drawable.mutate(), color);
  562.  
  563. drawable.setBounds( 0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
  564.  
  565. textView.setCompoundDrawables(drawable, null, null, null);
  566. textView.setTextColor(color);
  567. }
  568. public static void setStatusColorImage(ImageView img, int color){
  569. Drawable drawable = ContextCompat.getDrawable(AppController.getInstance().getApplicationContext(), R.drawable.urgentsmall);
  570. drawable = DrawableCompat.wrap(drawable);
  571. DrawableCompat.setTint(drawable.mutate(), color);
  572.  
  573. drawable.setBounds( 0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
  574.  
  575. img.setImageDrawable(drawable);
  576. }
  577.  
  578. public static boolean checkInternet(){
  579. ConnectivityManager cm =
  580. (ConnectivityManager)AppController.getInstance().getSystemService(Context.CONNECTIVITY_SERVICE);
  581.  
  582. NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
  583. boolean isConnected = activeNetwork != null &&
  584. activeNetwork.isConnectedOrConnecting();
  585.  
  586. MyLog.logE("isConnected : "+isConnected);
  587. return isConnected;
  588. }
  589. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement