PageRenderTime 66ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/app/src/main/java/com/gmail/victorkusov/postcrossinghelper/utils/Utils.java

https://bitbucket.org/KusovVV/postcrossinghelper
Java | 159 lines | 127 code | 32 blank | 0 comment | 15 complexity | 6da263f6acd875c4f445ae538a1a6d72 MD5 | raw file
  1. package com.gmail.victorkusov.postcrossinghelper.utils;
  2. import android.*;
  3. import android.app.Activity;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.content.pm.PackageManager;
  7. import android.graphics.Canvas;
  8. import android.graphics.Color;
  9. import android.graphics.Paint;
  10. import android.graphics.pdf.PdfDocument;
  11. import android.net.ConnectivityManager;
  12. import android.os.Environment;
  13. import android.support.v4.content.ContextCompat;
  14. import android.view.View;
  15. import android.view.inputmethod.InputMethodManager;
  16. import com.gmail.victorkusov.postcrossinghelper.model.InputData;
  17. import java.io.File;
  18. import java.io.FileOutputStream;
  19. import java.io.IOException;
  20. import java.util.List;
  21. import java.util.regex.Matcher;
  22. import java.util.regex.Pattern;
  23. public class Utils {
  24. private static final Pattern EMAIL_ADDRESS_REGEX = Pattern.compile("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,6}$", Pattern.CASE_INSENSITIVE);
  25. private static final Pattern TRACK_CODE_REGEX = Pattern.compile("^\\w{2}+\\d{9}+\\w{2}$");
  26. private static final String GEO_SERVICE_ACTION = "com.gmail.victorkusov.diffprocess.ServiceFromAnotherApp";
  27. private static final String GEO_SERVICE_PACKAGE = "com.gmail.victorkusov.diffprocess";
  28. private static final String GEO_SERVICE_PERMISSION = "com.gmail.victorkusov.diffprocess.START_INNER_SERVICE";
  29. private static final int REPORT_PAGE_WIDTH = 150;
  30. private static final float TITLE_TEXT_SIZE = 8;
  31. private static final float BODY_TEXT_SIZE = 6;
  32. private static final float TITLE_X_OFFSET = 30;
  33. private static final float TITLE_Y_OFFSET = 8;
  34. private static final float START_Y_POINTER = 12;
  35. private static final float START_X_OFFSET_LINE = 0;
  36. private static final float LINE_X_OFFSET = 10;
  37. private static final String REPORT_TITLE_TEXT = "Track events report";
  38. private static final String BODY_TEXT_DATE = "Date:";
  39. private static final String BODY_TEXT_EVENT = "Event:";
  40. private static final String BODY_TEXT_PLACE = "Place:";
  41. public static void hideKeyboard(View view) {
  42. InputMethodManager inputMethodManager = (InputMethodManager) view.getContext().getSystemService(Activity.INPUT_METHOD_SERVICE);
  43. if (inputMethodManager != null && inputMethodManager.isAcceptingText()) {
  44. inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
  45. }
  46. }
  47. public static boolean hasNetworkConnection(Context context) {
  48. boolean connected = true;
  49. ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
  50. if (mConnectivityManager != null) {
  51. connected = !(mConnectivityManager.getActiveNetworkInfo() == null || !mConnectivityManager.getActiveNetworkInfo().isConnectedOrConnecting());
  52. }
  53. return connected;
  54. }
  55. public static boolean isEmailAndPasswordValid(String email, String password) {
  56. if (email.isEmpty() || password.length() < 6) {
  57. return false;
  58. }
  59. Matcher matcher = EMAIL_ADDRESS_REGEX.matcher(email);
  60. return matcher.find();
  61. }
  62. public static boolean isGPSPermissonEnabled(Context context) {
  63. return ContextCompat.checkSelfPermission(context.getApplicationContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED;
  64. }
  65. public static boolean isNetworkPermissonEnabled(Context context) {
  66. return ContextCompat.checkSelfPermission(context.getApplicationContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED;
  67. }
  68. public static boolean isGeoServicePermissonEnabled(Context context) {
  69. return ContextCompat.checkSelfPermission(context.getApplicationContext(), GEO_SERVICE_PERMISSION) == PackageManager.PERMISSION_GRANTED;
  70. }
  71. public static void startGeoService(Context context) {
  72. Intent intent = new Intent(GEO_SERVICE_ACTION);
  73. intent.setPackage(GEO_SERVICE_PACKAGE);
  74. context.startService(intent);
  75. }
  76. public static boolean isFilesPermissonEnabled(Context context) {
  77. return ContextCompat.checkSelfPermission(context.getApplicationContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED &&
  78. ContextCompat.checkSelfPermission(context.getApplicationContext(), Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
  79. }
  80. public static boolean isTrackCodeValid(String trackCode) {
  81. Matcher matcher = TRACK_CODE_REGEX.matcher(trackCode);
  82. return matcher.find();
  83. }
  84. public static File makeFile(String fileName, List<InputData> dataList) {
  85. PdfDocument document = new PdfDocument();
  86. PdfDocument.PageInfo info = new PdfDocument.PageInfo.Builder(REPORT_PAGE_WIDTH, dataList.size() * 45, 1).create();
  87. PdfDocument.Page page = document.startPage(info);
  88. drawPage(page, dataList);
  89. document.finishPage(page);
  90. File root = new File(Environment.getExternalStorageDirectory(), Environment.DIRECTORY_DOWNLOADS);
  91. FileOutputStream stream;
  92. File outFile = new File(root.getPath(), fileName.concat(".pdf"));
  93. try {
  94. stream = new FileOutputStream(outFile);
  95. document.writeTo(stream);
  96. stream.close();
  97. } catch (IOException e) {
  98. e.printStackTrace();
  99. }
  100. return outFile;
  101. }
  102. private static void drawPage(PdfDocument.Page page, List<InputData> dataList) {
  103. Canvas canvas = page.getCanvas();
  104. float linePointer = LINE_X_OFFSET;
  105. Paint paint = new Paint();
  106. paint.setColor(Color.BLACK);
  107. paint.setTextSize(TITLE_TEXT_SIZE);
  108. canvas.drawText(REPORT_TITLE_TEXT, TITLE_X_OFFSET, TITLE_Y_OFFSET, paint);
  109. paint.setTextSize(BODY_TEXT_SIZE);
  110. for (InputData inputData : dataList) {
  111. canvas.drawText(BODY_TEXT_DATE, START_X_OFFSET_LINE, START_Y_POINTER, paint);
  112. linePointer += 6;
  113. canvas.drawText(inputData.getDateTime(), LINE_X_OFFSET, START_Y_POINTER, paint);
  114. linePointer += 6;
  115. canvas.drawText(BODY_TEXT_EVENT, START_X_OFFSET_LINE, START_Y_POINTER, paint);
  116. linePointer += 6;
  117. canvas.drawText(inputData.getEvent(), LINE_X_OFFSET, START_Y_POINTER, paint);
  118. linePointer += 6;
  119. canvas.drawText(BODY_TEXT_PLACE, START_X_OFFSET_LINE, START_Y_POINTER, paint);
  120. linePointer += 6;
  121. canvas.drawText(inputData.getPlace(), LINE_X_OFFSET, START_Y_POINTER, paint);
  122. linePointer += 10;
  123. }
  124. }
  125. public static void requesForFilePermission() {
  126. }
  127. }