Mujiburrohman

Utils

Jul 30th, 2020
1,791
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Utils {
  2.     private object Holder {
  3.         val INSTANCE = Utils()
  4.     }
  5.  
  6.     companion object {
  7.         val INSTANCE: Utils by lazy { Holder.INSTANCE }
  8.     }
  9.     private val TAG = "Utils"
  10.     var mCurrentPhotoPath = ""
  11.     var file: File? = null
  12.     var filePath: Uri? = null
  13.     private var dialog: Dialog? = null
  14.  
  15.     private var snackbarMessage: Snackbar? = null
  16.  
  17.     val bus: EventBus = EventBus.getDefault()
  18.  
  19.  
  20.     @SuppressLint("InlinedApi")
  21.     fun openFilePdf(fragment: Fragment, reqCode: Int) {
  22.         val intent = Intent(Intent.ACTION_OPEN_DOCUMENT).apply {
  23.             addCategory(Intent.CATEGORY_OPENABLE)
  24.             type = "application/pdf"
  25.  
  26.             // Optionally, specify a URI for the file that should appear in the
  27.             // system file picker when it loads.
  28.             putExtra(DocumentsContract.EXTRA_INITIAL_URI, filePath)
  29.         }
  30.  
  31.         fragment.startActivityForResult(intent, reqCode)
  32.     }
  33.  
  34.     fun ToastySuccess(context: Context, message: String){
  35.         Toasty.success(context, message, Toasty.LENGTH_LONG, true).show()
  36.     }
  37.     fun ToastyError(context: Context, message: String) {
  38.         Toasty.error(context, message, Toasty.LENGTH_LONG, true).show()
  39.     }
  40.     fun ToastyInfo(context: Context, message: String){
  41.         Toasty.info(context, message, Toasty.LENGTH_LONG, true).show()
  42.  
  43.     }
  44.  
  45.     fun snackBarMessage(rootview: View, message: String) {
  46.         if (!message.isBlank()) {
  47.             snackbarMessage = Snackbar.make(rootview, message, Snackbar.LENGTH_INDEFINITE)
  48.             snackbarMessage!!.setActionTextColor(Color.RED)
  49.             snackbarMessage!!.setAction("OK") {
  50.                 snackbarMessage!!.dismiss()
  51.                 bus.post(MessageEvent(message))
  52.             }
  53.             snackbarMessage!!.show()
  54.         } else {
  55.             snackbarMessage =
  56.                 Snackbar.make(rootview, "null error message", Snackbar.LENGTH_INDEFINITE)
  57.             snackbarMessage!!.setActionTextColor(Color.RED)
  58.             snackbarMessage!!.setAction("OK") {
  59.                 snackbarMessage!!.dismiss()
  60.                 bus.post(MessageEvent(message))
  61.             }
  62.             snackbarMessage!!.show()
  63.         }
  64.     }
  65.  
  66.  
  67.     var mLastClickTime=0L
  68.  
  69.     fun isOpenRecently():Boolean{
  70.         if (SystemClock.elapsedRealtime() - mLastClickTime < 1000){
  71.             return true
  72.         }
  73.         mLastClickTime = SystemClock.elapsedRealtime()
  74.         return false
  75.     }
  76.  
  77.  
  78.     @SuppressLint("LogNotTimber")
  79.     private fun takePictureFromCamera(fragment: Fragment, reqCode: Int) {
  80.         Intent(MediaStore.ACTION_IMAGE_CAPTURE).also { takePictureIntent ->
  81.             // Ensure that there's a camera activity to handle the intent
  82.             takePictureIntent.resolveActivity(fragment.requireContext().packageManager)?.also {
  83.                 // Create the File where the photo should go
  84.                 file = try {
  85.                     createImageFile(fragment.requireContext())
  86.                 } catch (ex: IOException) {
  87.                     // Error occurred while creating the File
  88.                     Log.e(TAG, "takePictureFromCamera: message = ${ex.message}")
  89.                     null
  90.                 }
  91.                 // Continue only if the File was successfully created
  92.                 file?.also {
  93.                     filePath = FileProvider.getUriForFile(
  94.                         fragment.requireContext(),
  95.                         "id.cikup.school",
  96.                         it
  97.                     )
  98.                     takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, filePath)
  99.                     fragment.startActivityForResult(takePictureIntent, reqCode)
  100.                 }
  101.             }
  102.         }
  103.     }
  104.  
  105.  
  106.     fun takePhotoFromGalery(fragment: Fragment, reqCode: Int) {
  107.         val takePictureIntent = Intent(Intent.ACTION_PICK)
  108.         takePictureIntent.type = "image/*"
  109.         takePictureIntent.putExtra(Intent.EXTRA_LOCAL_ONLY, true)
  110.         fragment.startActivityForResult(takePictureIntent, reqCode)
  111.     }
  112.  
  113.     @SuppressLint("SimpleDateFormat")
  114.     @Throws(IOException::class)
  115.     private fun createImageFile(context: Context): File {
  116.         // Create an image file name
  117.         val timeStamp: String = SimpleDateFormat("yyyyMMdd_HHmmss").format(Date())
  118.         val file = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES)
  119.         return File.createTempFile(
  120.             "JPEG_${timeStamp}_", /* prefix */
  121.             ".jpg", /* suffix */
  122.             file /* directory */
  123.         ).apply {
  124.             // Save a file: path for use with ACTION_VIEW intents
  125.             mCurrentPhotoPath = absolutePath
  126.         }
  127.     }
  128.  
  129.  
  130.     @SuppressLint("WrongViewCast")
  131.     fun showTakePictureChooser(fragment: Fragment) {
  132.         dialog = fragment.context?.let { Dialog(it) }
  133.         dialog!!.requestWindowFeature(Window.FEATURE_NO_TITLE)
  134.         dialog!!.setCancelable(true)
  135.         dialog!!.setContentView(R.layout.custom_image_chooser)
  136.         val width = (fragment.resources.displayMetrics.widthPixels * 0.90).toInt()
  137.         dialog!!.window?.setLayout(width, WindowManager.LayoutParams.WRAP_CONTENT)
  138.         dialog!!.window?.setGravity(Gravity.CENTER)
  139.  
  140.         val viaCameraButton = dialog!!.findViewById(R.id.cameraCustomUploadTV) as ImageView
  141.         viaCameraButton.setOnClickListener {
  142.             dialog!!.dismiss()
  143.             takePictureFromCamera(
  144.                 fragment,
  145.                 RequestCode.REQUEST_TAKE_PICTURE_CAMERA
  146.             )
  147.         }
  148.  
  149.         val viaGaleryButton = dialog!!.findViewById(R.id.galleryCustomUploafTV) as ImageView
  150.         viaGaleryButton.setOnClickListener {
  151.             dialog!!.dismiss()
  152.             takePhotoFromGalery(
  153.                 fragment,
  154.                 RequestCode.REQUEST_TAKE_PICTURE_GALERY
  155.             )
  156.         }
  157.         dialog!!.show()
  158.     }
  159.  
  160.     fun getBitmapFile(data: Intent, fragment: Fragment): File {
  161.         val selectedImage = data.data
  162.         val cursor = selectedImage?.let {
  163.             fragment.context?.contentResolver?.query(
  164.                 it,
  165.                 arrayOf<String>(android.provider.MediaStore.Images.ImageColumns.DATA),
  166.                 null,
  167.                 null,
  168.                 null
  169.             )
  170.         }
  171.         cursor?.moveToFirst()
  172.         val idx = cursor?.getColumnIndex(MediaStore.Images.ImageColumns.DATA)
  173.         val selectedImagePath = cursor?.getString(idx!!)
  174.         cursor?.close()
  175.         return File(selectedImagePath)
  176.     }
  177.  
RAW Paste Data