PageRenderTime 81ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/app/src/main/java/ua/pp/garik/gifcam/asynctask/CreateGif.java

https://gitlab.com/Potter/GifCam
Java | 177 lines | 134 code | 38 blank | 5 comment | 15 complexity | a778370c98e45ab7ffc282afddd25a02 MD5 | raw file
  1. package ua.pp.garik.gifcam.asynctask;
  2. import android.app.Activity;
  3. import android.app.Notification;
  4. import android.app.NotificationManager;
  5. import android.app.PendingIntent;
  6. import android.content.Context;
  7. import android.content.Intent;
  8. import android.content.SharedPreferences;
  9. import android.graphics.Bitmap;
  10. import android.graphics.BitmapFactory;
  11. import android.os.AsyncTask;
  12. import android.os.Environment;
  13. import android.preference.PreferenceManager;
  14. import android.widget.Button;
  15. import java.io.ByteArrayOutputStream;
  16. import java.io.File;
  17. import java.io.FileOutputStream;
  18. import java.io.OutputStream;
  19. import java.text.SimpleDateFormat;
  20. import java.util.Date;
  21. import ua.pp.garik.gifcam.R;
  22. import ua.pp.garik.gifcam.activity.MainActivity;
  23. import ua.pp.garik.gifcam.entity.Video;
  24. import ua.pp.garik.gifcam.utils.GifEncoder;
  25. import ua.pp.garik.gifcam.utils.Utils;
  26. /**
  27. * Created by Gary on 14.03.2015.
  28. */
  29. public class CreateGif extends AsyncTask<Object, Integer, String[]> {
  30. private Context context;
  31. private int framerate;
  32. private int frameDelay = 1000;
  33. private boolean upload = false;
  34. private boolean kill_source = true;
  35. private Video video;
  36. public CreateGif(Context contextin) {
  37. context = contextin;
  38. SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
  39. framerate = preferences.getInt("framerate", 2);
  40. frameDelay = Math.round(1000 / framerate);
  41. }
  42. @Override
  43. protected void onPostExecute(String[] strings) {
  44. super.onPostExecute(strings);
  45. Button btn_save = (Button)((Activity)this.context).findViewById(R.id.btn_save);
  46. if (btn_save != null) {
  47. btn_save.setText("Saved");
  48. }
  49. Intent intent = new Intent(context, MainActivity.class);
  50. PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, 0);
  51. Notification notification = new Notification.Builder(context)
  52. .setContentTitle("Job complete")
  53. .setContentText("GifCam - creating GIF")
  54. .setSmallIcon(R.mipmap.ic_launcher)
  55. .setContentIntent(pIntent)
  56. .setAutoCancel(true)
  57. .build();
  58. NotificationManager notificationManager =
  59. (NotificationManager) context.getSystemService(Activity.NOTIFICATION_SERVICE);
  60. notificationManager.notify(0, notification);
  61. Utils utils = new Utils();
  62. File outputFolder = utils.prepareOutputDirectory();
  63. String extension = "";
  64. if (this instanceof CreateGif) {
  65. extension = "gif";
  66. } else {
  67. extension = "mp4";
  68. }
  69. if (outputFolder != null) {
  70. File mediaStorageDir = utils.getMediaStorageDir();
  71. if (mediaStorageDir != null) {
  72. String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
  73. File inputFile = new File(mediaStorageDir.getPath() + File.separator + "movie" + "." + extension);
  74. //костиль, ага
  75. while (!inputFile.exists()) {
  76. }
  77. File outputFile = new File(outputFolder.getPath() + File.separator + "movie_" + timeStamp + "." + extension);
  78. inputFile.renameTo(outputFile);
  79. while (!outputFile.exists()) {
  80. }
  81. if (upload) {
  82. utils.saveResult(context, outputFile, kill_source, video, framerate);
  83. } else {
  84. if (kill_source) {
  85. utils.killSources(mediaStorageDir);
  86. }
  87. }
  88. }
  89. }
  90. }
  91. protected void onProgressUpdate(Integer... progress) {
  92. Notification notification = new Notification.Builder(context)
  93. .setContentTitle("Photo " + progress[0])
  94. .setProgress(progress[1], progress[0], false)
  95. .setContentText("GifCam - creating GIF")
  96. .setSmallIcon(R.mipmap.ic_launcher)
  97. .setAutoCancel(true)
  98. .build();
  99. NotificationManager notificationManager =
  100. (NotificationManager) context.getSystemService(Activity.NOTIFICATION_SERVICE);
  101. notificationManager.notify(0, notification);
  102. }
  103. @Override
  104. protected String[] doInBackground(Object... params) {
  105. File mediaStorageDir = (File) params[0];
  106. video = (Video) params[1];
  107. kill_source = (boolean) params[2];
  108. upload = (boolean) params[3];
  109. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  110. GifEncoder encoder = new GifEncoder();
  111. encoder.start(bos);
  112. if (mediaStorageDir.isDirectory()) {
  113. String[] children = mediaStorageDir.list();
  114. int filesCount = mediaStorageDir.list().length;
  115. int count = 0;
  116. for (String aChildren : children) {
  117. BitmapFactory.Options options = new BitmapFactory.Options();
  118. options.inPreferredConfig = Bitmap.Config.ARGB_8888;
  119. Bitmap bitmap = BitmapFactory.decodeFile(mediaStorageDir + "/" + aChildren, options);
  120. encoder.setDelay(frameDelay);
  121. encoder.addFrame(bitmap);
  122. publishProgress(count++, filesCount);
  123. // new File(mediaStorageDir, aChildren).delete();
  124. }
  125. }
  126. encoder.finish();
  127. bos.toByteArray();
  128. OutputStream outputStream = null;
  129. try {
  130. outputStream = new FileOutputStream(new File(Environment.getExternalStoragePublicDirectory(
  131. Environment.DIRECTORY_PICTURES), Utils.FOLDER_NAME) + "/movie.gif");
  132. bos.writeTo(outputStream);
  133. bos.close();
  134. } catch (Exception e) {
  135. e.printStackTrace();
  136. }
  137. return null;
  138. }
  139. }