PageRenderTime 36ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/app/src/main/java/com/ecyber/ivivaanywhere/smartfm/popup/AttachmentSendDialog.java

https://bitbucket.org/Fidenz-kelumw/ec_ivivaanyware_android
Java | 695 lines | 512 code | 131 blank | 52 comment | 55 complexity | 7c8775324bb65cdfad242514984220fa MD5 | raw file
  1. package com.ecyber.ivivaanywhere.smartfm.popup;
  2. import android.app.Activity;
  3. import android.app.Dialog;
  4. import android.app.Fragment;
  5. import android.app.ProgressDialog;
  6. import android.content.Intent;
  7. import android.database.Cursor;
  8. import android.graphics.Bitmap;
  9. import android.graphics.BitmapFactory;
  10. import android.graphics.drawable.ColorDrawable;
  11. import android.media.MediaPlayer;
  12. import android.media.ThumbnailUtils;
  13. import android.net.Uri;
  14. import android.os.AsyncTask;
  15. import android.os.Bundle;
  16. import android.os.Environment;
  17. import android.os.ParcelFileDescriptor;
  18. import android.os.SystemClock;
  19. import android.provider.MediaStore;
  20. import android.support.annotation.Nullable;
  21. import android.support.design.widget.Snackbar;
  22. import android.util.Log;
  23. import android.view.LayoutInflater;
  24. import android.view.View;
  25. import android.view.ViewGroup;
  26. import android.widget.Chronometer;
  27. import android.widget.ImageButton;
  28. import android.widget.ImageView;
  29. import android.widget.LinearLayout;
  30. import android.widget.RelativeLayout;
  31. import android.widget.Toast;
  32. import com.activeandroid.query.Select;
  33. import com.ecyber.ivivaanywhere.smartfm.R;
  34. import com.ecyber.ivivaanywhere.smartfm.activities.SketchActivity;
  35. import com.ecyber.ivivaanywhere.smartfm.helper.utilities.AccountPermission;
  36. import com.ecyber.ivivaanywhere.smartfm.helper.utilities.BitMaptoBase64Converter;
  37. import com.ecyber.ivivaanywhere.smartfm.helper.utilities.ColoredSnackbar;
  38. import com.ecyber.ivivaanywhere.smartfm.customviews.CustomButton;
  39. import com.ecyber.ivivaanywhere.smartfm.customviews.CustomEditTextView;
  40. import com.ecyber.ivivaanywhere.smartfm.helper.utilities.SmartConstants;
  41. import com.ecyber.ivivaanywhere.smartfm.helper.utilities.Utility;
  42. import com.ecyber.ivivaanywhere.smartfm.models.AttachmentTypesMaster;
  43. import com.ecyber.ivivaanywhere.smartfm.models.InputMode;
  44. import java.io.File;
  45. import java.io.FileDescriptor;
  46. import java.io.IOException;
  47. import java.text.SimpleDateFormat;
  48. import java.util.Date;
  49. import java.util.List;
  50. import java.util.UUID;
  51. /**
  52. * Created by Lakmal on 6/1/2016.
  53. */
  54. public class AttachmentSendDialog implements AttachmentTypeDialog.AttachmentTypeListCallback, SignatureDialog.SignatureCallback, AudioRecordDialog.AudioRecordCallback {
  55. private static final String TAG = "AttachmentSendDialog";
  56. private ColoredSnackbar snackbar;
  57. private Activity mActivity;
  58. private AttachmentSendCallback mDelegate;
  59. private ImageView mAttachedImage;
  60. private ImageView mAttType;
  61. private Uri mAttachedVideoPath;
  62. private CustomEditTextView mTitleDialog;
  63. private CustomButton mBtnSaveFile;
  64. private CustomButton mBtnTypeSelect;
  65. private Bitmap mBitmapFile;
  66. private Uri mUri = null;
  67. private Fragment mFragment;
  68. private String mObjectKey;
  69. private Dialog mDialog;
  70. private RelativeLayout mAudioPlaybackContainer;
  71. private LinearLayout mAttachmentTypeContainer;
  72. private LinearLayout mTypePhoto;
  73. private LinearLayout mTypeAudio;
  74. private LinearLayout mTypeSig;
  75. private LinearLayout mTypeVideo;
  76. private LinearLayout mTypeFile;
  77. private ImageButton mBtnPhoto;
  78. private ImageButton mBtnFile;
  79. private ImageButton mBtnSig;
  80. private ImageButton mBtnVideo;
  81. private ImageButton mBtnAudio;
  82. private ImageButton mBtnPlayPause;
  83. private ImageButton mBtnStop;
  84. private MediaPlayer mPlayer = null;
  85. private Chronometer mPlaybackTime;
  86. private boolean mIsPlaying = false;
  87. private long mTimeWhenStopped = 0;
  88. private int mContinuePos = 0;
  89. // Attachment Details
  90. private String mCurrentAttachmentCode = null;
  91. private String mFileName = null;
  92. private String mFilePath = null;
  93. private boolean isAttachmentSet = false;
  94. //region Public Methods
  95. public AttachmentSendDialog(Activity activity, AttachmentSendCallback delegate, Fragment fragment, String objectKey) {
  96. mActivity = activity;
  97. mDelegate = delegate;
  98. mFragment = fragment;
  99. mObjectKey = objectKey;
  100. snackbar = new ColoredSnackbar(activity);
  101. }
  102. public Dialog show() {
  103. final Dialog dialogAttachment = new Dialog(mActivity, R.style.CustomDialog);
  104. LayoutInflater i = LayoutInflater.from(mActivity);
  105. View viewDialog = i.inflate(R.layout.view_add_attachment, null);
  106. // mLayoutButtonHolder = (LinearLayout) viewDialog.findViewById(R.id.lay_lin_add_attachment_button_holder);
  107. mAttachedImage = (ImageView) viewDialog.findViewById(R.id.img_preview_att);
  108. mAttType = (ImageView) viewDialog.findViewById(R.id.img_type);
  109. mBtnSaveFile = (CustomButton) viewDialog.findViewById(R.id.btn_save_file);
  110. mBtnTypeSelect = (CustomButton) viewDialog.findViewById(R.id.btn_type_select);
  111. mAttachmentTypeContainer = (LinearLayout) viewDialog.findViewById(R.id.lin_lay_att_type_cont);
  112. mAudioPlaybackContainer = (RelativeLayout) viewDialog.findViewById(R.id.rel_lay_audio_playback);
  113. mTypePhoto = (LinearLayout) viewDialog.findViewById(R.id.lin_lay_att_photo);
  114. mTypeAudio = (LinearLayout) viewDialog.findViewById(R.id.lin_lay_att_audio);
  115. mTypeVideo = (LinearLayout) viewDialog.findViewById(R.id.lin_lay_att_video);
  116. mTypeSig = (LinearLayout) viewDialog.findViewById(R.id.lin_lay_att_sig);
  117. mTypeFile = (LinearLayout) viewDialog.findViewById(R.id.lin_lay_att_file);
  118. mBtnPhoto = (ImageButton) viewDialog.findViewById(R.id.img_btn_photo);
  119. mBtnVideo = (ImageButton) viewDialog.findViewById(R.id.img_btn_video);
  120. mBtnAudio = (ImageButton) viewDialog.findViewById(R.id.img_btn_audio);
  121. mBtnSig = (ImageButton) viewDialog.findViewById(R.id.img_btn_sig);
  122. mBtnFile = (ImageButton) viewDialog.findViewById(R.id.img_btn_file);
  123. mBtnPlayPause = (ImageButton) viewDialog.findViewById(R.id.btn_play_pause);
  124. mBtnStop = (ImageButton) viewDialog.findViewById(R.id.btn_stop);
  125. mPlaybackTime = (Chronometer) viewDialog.findViewById(R.id.lbl_audio_time);
  126. mPlaybackTime.setText("00:00:00");
  127. mPlaybackTime.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener(){
  128. @Override
  129. public void onChronometerTick(Chronometer cArg) {
  130. long time = SystemClock.elapsedRealtime() - cArg.getBase();
  131. int h = (int)(time /3600000);
  132. int m = (int)(time - h*3600000)/60000;
  133. int s= (int)(time - h*3600000- m*60000)/1000 ;
  134. String hh = h < 10 ? "0"+h: h+"";
  135. String mm = m < 10 ? "0"+m: m+"";
  136. String ss = s < 10 ? "0"+s: s+"";
  137. cArg.setText(hh+":"+mm+":"+ss);
  138. }
  139. });
  140. registerCallbacks();
  141. registerAudioPlayerCallbacks();
  142. viewDialog.post(new Runnable() {
  143. @Override
  144. public void run() {
  145. int itemWidth = mAttachmentTypeContainer.getWidth() / 5;
  146. LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(itemWidth, LinearLayout.LayoutParams.WRAP_CONTENT);
  147. lp.height = itemWidth;
  148. mTypePhoto.setLayoutParams(lp);
  149. mTypeAudio.setLayoutParams(lp);
  150. mTypeVideo.setLayoutParams(lp);
  151. mTypeSig.setLayoutParams(lp);
  152. mTypeFile.setLayoutParams(lp);
  153. mTypePhoto.setVisibility(View.GONE);
  154. mTypeAudio.setVisibility(View.GONE);
  155. mTypeVideo.setVisibility(View.GONE);
  156. mTypeSig.setVisibility(View.GONE);
  157. mTypeFile.setVisibility(View.GONE);
  158. List<InputMode> inputModes = InputMode.getInputModesForObject(mObjectKey);
  159. if(inputModes != null) {
  160. for(InputMode im : inputModes) {
  161. String mode = im.getMode();
  162. if(mode.equals("PHOTO")) {
  163. mTypePhoto.setVisibility(View.VISIBLE);
  164. } else if(mode.equals("AUDIO")) {
  165. mTypeAudio.setVisibility(View.VISIBLE);
  166. } else if(mode.equals("VIDEO")) {
  167. mTypeVideo.setVisibility(View.VISIBLE);
  168. } else if(mode.equals("SIG")) {
  169. mTypeSig.setVisibility(View.VISIBLE);
  170. } else if(mode.equals("FILE")) {
  171. mTypeFile.setVisibility(View.VISIBLE);
  172. }
  173. }
  174. }
  175. }
  176. });
  177. List<AttachmentTypesMaster> attachmentTypes = new Select().from(AttachmentTypesMaster.class).execute();
  178. if (attachmentTypes != null) {
  179. if (attachmentTypes.size() != 0) {
  180. if (attachmentTypes.get(0).getTypeName() != null) {
  181. mBtnTypeSelect.setText(attachmentTypes.get(0).getTypeName());
  182. }
  183. }
  184. }
  185. //mBtnSaveFile.setEnabled(false);
  186. isAttachmentSet = false;
  187. mTitleDialog = (CustomEditTextView) viewDialog.findViewById(R.id.editText_attachmnet_titile);
  188. dialogAttachment.setContentView(viewDialog);
  189. ImageButton dClose = (ImageButton) viewDialog.findViewById(R.id.imageButton_attachment_close);
  190. dClose.setOnClickListener(new View.OnClickListener() {
  191. @Override
  192. public void onClick(View view) {
  193. dialogAttachment.dismiss();
  194. mAttachedImage = null;
  195. }
  196. });
  197. mBtnTypeSelect.setOnClickListener(v -> showAttachmentTypeSelectDialog());
  198. mBtnSaveFile.setOnClickListener(view -> {
  199. if (isAttachmentSet) {
  200. String Title = mTitleDialog.getText().toString();
  201. mDialog.dismiss();
  202. snackbar.showSnackBar("Updating...", ColoredSnackbar.TYPE_UPDATE, Snackbar.LENGTH_INDEFINITE);
  203. new ConvertAsync(Title).execute("");
  204. } else {
  205. Toast.makeText(mActivity, "Please select an attachment to continue", Toast.LENGTH_LONG).show();
  206. }
  207. });
  208. dialogAttachment.show();
  209. dialogAttachment.setCanceledOnTouchOutside(false);
  210. dialogAttachment.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
  211. dialogAttachment.getWindow().setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
  212. return dialogAttachment;
  213. }
  214. public void setVideoToView(Uri videoPath) {
  215. mAttType.setImageResource(R.drawable.att_video);
  216. mAttType.setVisibility(View.VISIBLE);
  217. mCurrentAttachmentCode = "VID";
  218. //mBtnSaveFile.setEnabled(true);
  219. isAttachmentSet = true;
  220. if(videoPath != null) {
  221. mAttachedVideoPath = videoPath;
  222. mAttachmentTypeContainer.setVisibility(View.GONE);
  223. mFilePath = getRealPathFromURI(videoPath);
  224. //mFilePath = videoPath.getPath();
  225. Bitmap thumb = ThumbnailUtils.createVideoThumbnail(mFilePath, MediaStore.Images.Thumbnails.MINI_KIND);
  226. mAttachedImage.setImageBitmap(thumb);
  227. mAttachedImage.setVisibility(View.VISIBLE);
  228. mAttachedImage.setOnClickListener(view -> {
  229. Intent intent = new Intent(Intent.ACTION_VIEW);
  230. intent.setDataAndType(mAttachedVideoPath, "video/*");
  231. mFragment.startActivity(intent);
  232. });
  233. }
  234. }
  235. public void imageSetToView(String imagePath, boolean isSignature, Uri uri) {
  236. mCurrentAttachmentCode = "IMG";
  237. mAttachedImage.setVisibility(View.VISIBLE);
  238. try {
  239. mFilePath = imagePath;
  240. if (Utility.IsEmpty(mFilePath)) {
  241. String tempPath = Utility.IsEmpty(mFilePath) ? Utility.createNewPath() : mFilePath;
  242. mBitmapFile = MediaStore.Images.Media.getBitmap(mActivity.getContentResolver(), uri);
  243. if (Utility.createdTempImageFile(mBitmapFile, tempPath) != null)
  244. mFilePath = tempPath;
  245. mBitmapFile = Bitmap.createBitmap(BitmapFactory.decodeFile(mFilePath));
  246. } else {
  247. mBitmapFile = Bitmap.createBitmap(BitmapFactory.decodeFile(mFilePath));
  248. }
  249. // mBitmapFile = new Utility().getResizedBitmap(mBitmapFile);
  250. // String tempPath = Utility.createNewPath();
  251. // Utility.createdTempImageFile(mBitmapFile, tempPath);
  252. // mFilePath = tempPath;
  253. if (!Utility.IsEmpty(mFilePath))
  254. mBitmapFile = Utility.rotateImage(mBitmapFile, mFilePath, mActivity.getBaseContext());
  255. Bitmap TempViewBitmap = Bitmap.createScaledBitmap(mBitmapFile, mBitmapFile.getWidth() / 2, mBitmapFile.getHeight() / 2, false);
  256. mAttachedImage.setImageBitmap(TempViewBitmap);
  257. //mBtnSaveFile.setEnabled(true);
  258. isAttachmentSet = true;
  259. if (isSignature) {
  260. //mBtnSignature.setVisibility(View.GONE);
  261. }
  262. //mLayoutButtonHolder.setVisibility(View.GONE);
  263. mAttachmentTypeContainer.setVisibility(View.GONE);
  264. mAttachedImage.setOnClickListener(view -> {
  265. Intent intent = new Intent(mActivity, SketchActivity.class);
  266. intent.putExtra(SmartConstants.SKETCH_IMAGE, mFilePath);
  267. mFragment.startActivityForResult(intent, SmartConstants.RESULT_SKETCH);
  268. mActivity.overridePendingTransition(R.anim.slide_bottomlayer_display, R.anim.keep_active);
  269. });
  270. } catch (Exception e) {
  271. Toast.makeText(mActivity, "Unable to load image", Toast.LENGTH_SHORT).show();
  272. //mBtnSaveFile.setEnabled(false);
  273. isAttachmentSet = false;
  274. mAttachmentTypeContainer.setVisibility(View.GONE);
  275. //mLayoutButtonHolder.setVisibility(View.GONE);
  276. return;
  277. }
  278. }
  279. private void setAudioToPlayer(String fileName) {
  280. mCurrentAttachmentCode = "AUD";
  281. mFileName = fileName;
  282. mFilePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + mFileName;
  283. mAttachmentTypeContainer.setVisibility(View.GONE);
  284. mAudioPlaybackContainer.setVisibility(View.VISIBLE);
  285. }
  286. //endregion
  287. //region Private Methods
  288. private void registerCallbacks() {
  289. mBtnPhoto.setOnClickListener(view -> {
  290. try {
  291. final int requestId = 1;
  292. Uri fileUri = Uri.fromFile(getOutputMediaFile());
  293. Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  294. cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
  295. mDelegate.onCapturePath(fileUri.getPath());
  296. Bundle bundle = new Bundle();
  297. bundle.putString("imgPath", fileUri.getPath());
  298. cameraIntent.putExtras(bundle);
  299. mFragment.startActivityForResult(cameraIntent, SmartConstants.RESULT_IMAGE_CAMERA);
  300. } catch (IllegalStateException e) {
  301. Log.e(TAG, "onClick: " + e.getStackTrace().toString());
  302. } catch (Exception e) {
  303. Log.e(TAG, "onClick: " + e.getStackTrace().toString());
  304. }
  305. });
  306. mBtnFile.setOnClickListener(view -> {
  307. Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
  308. mFragment.startActivityForResult(i, SmartConstants.RESULT_IMAGE_GALLERY);
  309. });
  310. mBtnAudio.setOnClickListener(view -> openAudioRecordDialog());
  311. mBtnSig.setOnClickListener(view -> openSignatureDialog());
  312. mBtnVideo.setOnClickListener(view -> {
  313. Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
  314. //File mediaFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + makeGUIDForType(".mp4"));
  315. //takeVideoIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mediaFile));
  316. if (takeVideoIntent.resolveActivity(mActivity.getPackageManager()) != null) {
  317. mFragment.startActivityForResult(takeVideoIntent, SmartConstants.RESULT_VIDEO_CAPTURE);
  318. }
  319. });
  320. }
  321. void registerAudioPlayerCallbacks() {
  322. mBtnPlayPause.setOnClickListener(view -> {
  323. if(mIsPlaying) {
  324. pausePlayback();
  325. } else {
  326. startPlayback();
  327. }
  328. });
  329. mBtnStop.setOnClickListener(view -> {
  330. if(mIsPlaying) {
  331. stopPlayback();
  332. }
  333. });
  334. }
  335. //endregion
  336. //region Audio Player
  337. void startPlayback() {
  338. mPlaybackTime.setBase(SystemClock.elapsedRealtime() + mTimeWhenStopped);
  339. mPlaybackTime.start();
  340. if(mPlayer != null) {
  341. mPlayer.seekTo(mContinuePos);
  342. mPlayer.start();
  343. mBtnPlayPause.setImageResource(R.drawable.pause);
  344. } else {
  345. mIsPlaying = true;
  346. mBtnPlayPause.setImageResource(R.drawable.pause);
  347. mPlayer = new MediaPlayer();
  348. try {
  349. mPlayer.setDataSource(Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + mFileName);
  350. mPlayer.setOnCompletionListener(mp -> {
  351. stopPlayback();
  352. mBtnPlayPause.setImageResource(R.drawable.play);
  353. });
  354. mPlayer.prepare();
  355. mPlayer.start();
  356. } catch (IOException e) {
  357. }
  358. }
  359. }
  360. void stopPlayback() {
  361. mPlayer.release();
  362. mPlayer = null;
  363. mTimeWhenStopped = 0;
  364. mPlaybackTime.setBase(SystemClock.elapsedRealtime());
  365. mPlaybackTime.stop();
  366. mPlaybackTime.setText("00:00:00");
  367. mBtnPlayPause.setImageResource(R.drawable.play);
  368. mIsPlaying = false;
  369. }
  370. void pausePlayback() {
  371. mPlayer.pause();
  372. mContinuePos = mPlayer.getCurrentPosition();
  373. mTimeWhenStopped = mPlaybackTime.getBase() - SystemClock.elapsedRealtime();
  374. mPlaybackTime.stop();
  375. mBtnPlayPause.setImageResource(R.drawable.play);
  376. mIsPlaying = false;
  377. }
  378. //endregion
  379. //region Utility
  380. public String getRealPathFromURI(Uri contentUri) {
  381. String[] proj = { MediaStore.Images.Media.DATA };
  382. Cursor cursor = mActivity.managedQuery(contentUri, proj, null, null, null);
  383. int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
  384. cursor.moveToFirst();
  385. return cursor.getString(column_index);
  386. }
  387. public String converterImage() {
  388. BitMaptoBase64Converter conv = new BitMaptoBase64Converter();
  389. String base64String = "";
  390. Bitmap bitmap;
  391. try {
  392. if (Utility.IsEmpty(mFilePath)) {
  393. bitmap = MediaStore.Images.Media.getBitmap(mActivity.getContentResolver(), mUri);
  394. } else {
  395. bitmap = BitmapFactory.decodeFile(mFilePath);
  396. }
  397. bitmap = new Utility().getResizedBitmap(bitmap);
  398. String tempPath = Utility.IsEmpty(mFilePath) ? Utility.createNewPath() : mFilePath;
  399. if (Utility.IsEmpty(mFilePath))
  400. Utility.createdTempImageFile(bitmap, tempPath);
  401. if (!Utility.IsEmpty(tempPath))
  402. bitmap = Utility.rotateImage(bitmap, tempPath, mActivity);
  403. File file = new File(tempPath);
  404. long length = file.length();// TODO: 20/10/2016 change this function for the recusive func
  405. long mega_bytes = (length / (1024 * 1024));
  406. int reduce = 80;
  407. if (mega_bytes > 1) {
  408. reduce = 50;
  409. }
  410. base64String = conv.converterBase64(bitmap, reduce);
  411. bitmap.recycle();
  412. } catch (Exception e) {
  413. }
  414. return base64String;
  415. }
  416. private void showMsgFailedImageSelect() {
  417. Toast.makeText(mActivity, "Failed image select.Please try again", Toast.LENGTH_SHORT).show();
  418. }
  419. @Nullable
  420. private static File getOutputMediaFile() {
  421. // To be safe, you should check that the SDCard is mounted
  422. // using Environment.getExternalStorageState() before doing this.
  423. File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
  424. Environment.DIRECTORY_PICTURES), SmartConstants.FILE_SAVE_LOCATION);
  425. // This location works best if you want the created images to be shared
  426. // between applications and persist after your app has been uninstalled.
  427. // Create the storage directory if it does not exist
  428. if (!mediaStorageDir.exists()) {
  429. if (!mediaStorageDir.mkdirs()) {
  430. Log.d(SmartConstants.FILE_SAVE_LOCATION, "failed to create directory");
  431. return null;
  432. }
  433. }
  434. // Create a media file name
  435. String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
  436. File mediaFile;
  437. mediaFile = new File(mediaStorageDir.getPath() + File.separator +
  438. "IMG_" + timeStamp + ".jpg");
  439. return mediaFile;
  440. }
  441. public void setDialogInstance(Dialog thisDialog) {
  442. mDialog = thisDialog;
  443. }
  444. private void displayMsg() {
  445. Toast.makeText(mActivity, "Please select attachment type", Toast.LENGTH_SHORT).show();
  446. }
  447. public String makeGUIDForType(String ext) {
  448. String guid = UUID.randomUUID().toString();
  449. guid = new AccountPermission().getAccountInfo().get(2) + mActivity.getString(R.string.underscore) + guid + mActivity.getString(R.string.underscore) + new Date().getTime();
  450. return guid + ext;
  451. }
  452. public String makeGUID() {
  453. String guid = UUID.randomUUID().toString();
  454. guid = new AccountPermission().getAccountInfo().get(2) + mActivity.getString(R.string.underscore) + guid + mActivity.getString(R.string.underscore) + new Date().getTime();
  455. String extension = "";
  456. if(mCurrentAttachmentCode.equals("IMG")) {
  457. extension = ".jpg";
  458. } else if(mCurrentAttachmentCode.equals("AUD")) {
  459. extension = ".mp4";
  460. } else if(mCurrentAttachmentCode.equals("VID")) {
  461. extension = ".mov";
  462. }
  463. return guid + extension;
  464. }
  465. //endregion
  466. //region Dialog Methods
  467. private void openSignatureDialog() {
  468. new SignatureDialog(mActivity, this).show();
  469. }
  470. private void openAudioRecordDialog() {
  471. new AudioRecordDialog(mActivity ,mActivity, this).show();
  472. }
  473. private void showAttachmentTypeSelectDialog() {
  474. new AttachmentTypeDialog(mActivity, this).makeDialog();
  475. }
  476. //endregion
  477. //region Callbacks Implementations
  478. @Override
  479. public void onSaveAudio(String fileName) {
  480. setAudioToPlayer(fileName);
  481. //mBtnSaveFile.setEnabled(true);
  482. isAttachmentSet = true;
  483. }
  484. @Override
  485. public void onSaveSignature(String picturePath) {
  486. imageSetToView(picturePath, true, null);
  487. }
  488. private Bitmap getBitmapFromUri(Uri uri) throws IOException {
  489. ParcelFileDescriptor parcelFileDescriptor = mActivity.getContentResolver().openFileDescriptor(uri, "r");
  490. FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
  491. Bitmap image = BitmapFactory.decodeFileDescriptor(fileDescriptor);
  492. parcelFileDescriptor.close();
  493. return image;
  494. }
  495. @Override
  496. public void onTypeItemClick(AttachmentTypesMaster type, Dialog dialogInstance) {
  497. mBtnTypeSelect.setText("" + type.getTypeName());
  498. mBtnTypeSelect.setTag("" + type.getSignature());
  499. dialogInstance.dismiss();
  500. //mBtnSignature.setVisibility(View.GONE);
  501. if (type.getSignature() == 1) {
  502. //mBtnSignature.setVisibility(View.VISIBLE);
  503. //mLayoutButtonHolder.setVisibility(View.GONE);
  504. mAttachmentTypeContainer.setVisibility(View.GONE);
  505. mAttachedImage.setImageResource(R.drawable.rounded_white_box);
  506. //mBtnSaveFile.setEnabled(false);
  507. isAttachmentSet = false;
  508. } else {
  509. mAttachedImage.setImageResource(R.drawable.rounded_white_box);
  510. //mBtnSaveFile.setEnabled(false);
  511. isAttachmentSet = false;
  512. //mLayoutButtonHolder.setVisibility(View.VISIBLE);
  513. mAttachedImage.setOnClickListener(null);
  514. mAttachedImage.setVisibility(View.INVISIBLE);
  515. mAttachmentTypeContainer.setVisibility(View.VISIBLE);
  516. }
  517. }
  518. //endregion
  519. //region Callback Interfaces
  520. public interface AttachmentSendCallback {
  521. void onAttachmentSend(String attachmentType, String title, String base64String, Bitmap bitmap);
  522. void onAttachmentSend(String attachmentCode, String attachmentType, String title, String filePatch, String fileName);
  523. void onCapturePath(String path);
  524. }
  525. //endregion
  526. //region Inner Classes
  527. private class ConvertAsync extends AsyncTask<String, Integer, String> {
  528. private ProgressDialog dialog;
  529. private String mTitle;
  530. public ConvertAsync(String title) {
  531. this.mTitle = title;
  532. this.dialog = new ProgressDialog(mActivity);
  533. dialog.setCancelable(false);
  534. dialog.setCanceledOnTouchOutside(false);
  535. dialog.setIndeterminate(true);
  536. }
  537. protected void onPreExecute() {
  538. this.dialog.setMessage("Please wait...");
  539. //this.dialog.show();
  540. }
  541. //public void onAttachmentSend(String attachmentCode, String attachmentType, String title, String filePath, String fileName){
  542. @Override
  543. protected void onPostExecute(String base64String) {
  544. //super.onPostExecute(s);
  545. if(mCurrentAttachmentCode == "IMG") {
  546. mBitmapFile = new Utility().getResizedBitmap(mBitmapFile);
  547. String tempPath = Utility.createNewPath();
  548. Utility.createdTempImageFile(mBitmapFile, tempPath);
  549. mFilePath = tempPath;
  550. }
  551. mDelegate.onAttachmentSend(mCurrentAttachmentCode, mBtnTypeSelect.getText().toString(), mTitleDialog.getText().toString(), mFilePath, makeGUID());
  552. if (mBitmapFile != null) {
  553. mBitmapFile.recycle();
  554. }
  555. //dialog.dismiss();
  556. }
  557. @Override
  558. protected String doInBackground(String... strings) {
  559. String base64String = converterImage();
  560. return base64String;
  561. }
  562. }
  563. //endregion
  564. }