1 package net.osdn.gokigen.gr2control.playback.detail;
3 import android.app.Activity;
4 import android.app.AlertDialog;
5 import android.app.ProgressDialog;
6 import android.content.ContentResolver;
7 import android.content.ContentValues;
8 import android.content.Intent;
9 import android.content.SharedPreferences;
10 import android.net.Uri;
11 import android.os.Environment;
12 import android.provider.MediaStore;
13 import android.support.annotation.NonNull;
14 import android.support.v7.preference.PreferenceManager;
15 import android.util.Log;
16 import android.widget.Toast;
18 import net.osdn.gokigen.gr2control.R;
19 import net.osdn.gokigen.gr2control.camera.ICameraFileInfo;
20 import net.osdn.gokigen.gr2control.camera.playback.IDownloadContentCallback;
21 import net.osdn.gokigen.gr2control.camera.playback.IPlaybackControl;
22 import net.osdn.gokigen.gr2control.camera.playback.ProgressEvent;
23 import net.osdn.gokigen.gr2control.preference.IPreferencePropertyAccessor;
26 import java.io.FileOutputStream;
27 import java.text.SimpleDateFormat;
28 import java.util.Calendar;
29 import java.util.Locale;
35 class MyContentDownloader implements IDownloadContentCallback
37 private final String TAG = toString();
38 private final Activity activity;
39 private final IPlaybackControl playbackControl;
40 private static final String RAW_SUFFIX = ".DNG";
41 private static final String MOVIE_SUFFIX = ".MOV";
42 private static final String JPEG_SUFFIX = ".JPG";
43 private ProgressDialog downloadDialog = null;
44 private FileOutputStream outputStream = null;
45 private String targetFileName = "";
46 private String filepath = "";
47 private String mimeType = "image/jpeg";
53 MyContentDownloader(@NonNull Activity activity, @NonNull final IPlaybackControl playbackControl)
55 this.activity = activity;
56 this.playbackControl = playbackControl;
63 void startDownload(final ICameraFileInfo fileInfo, String replaceJpegSuffix, boolean isSmallSize)
67 Log.v(TAG, "startDownload() ICameraFileInfo is NULL...");
70 Log.v(TAG, "startDownload() " + fileInfo.getFilename());
72 // Download the image.
75 Calendar calendar = Calendar.getInstance();
76 String extendName = new SimpleDateFormat("yyyyMMdd-HHmmss", Locale.getDefault()).format(calendar.getTime());
77 targetFileName = fileInfo.getFilename().toUpperCase();
78 if (replaceJpegSuffix != null)
80 targetFileName = targetFileName.replace(JPEG_SUFFIX, replaceJpegSuffix);
82 if (targetFileName.toUpperCase().contains(RAW_SUFFIX))
84 mimeType = "image/x-adobe-dng";
86 else if (targetFileName.toUpperCase().contains(MOVIE_SUFFIX))
88 mimeType = "video/mp4";
92 activity.runOnUiThread(new Runnable() {
95 downloadDialog = new ProgressDialog(activity);
96 downloadDialog.setTitle(activity.getString(R.string.dialog_download_file_title));
97 downloadDialog.setMessage(activity.getString(R.string.dialog_download_message) + " " + targetFileName);
98 downloadDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
99 downloadDialog.setCancelable(false);
100 downloadDialog.show();
103 String path = fileInfo.getDirectoryPath() + "/" + targetFileName;
105 final String directoryPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getPath() + "/" + activity.getString(R.string.app_name2) + "/";
106 String outputFileName = extendName + "_" + targetFileName;
107 filepath = new File(directoryPath.toLowerCase(), outputFileName.toLowerCase()).getPath();
110 final File directory = new File(directoryPath);
111 if (!directory.exists())
113 if (!directory.mkdirs())
115 Log.v(TAG, "MKDIR FAIL. : " + directoryPath);
118 outputStream = new FileOutputStream(filepath);
122 final String message = e.getMessage();
123 activity.runOnUiThread(new Runnable() {
126 if (downloadDialog != null) {
127 downloadDialog.dismiss();
129 presentMessage(activity.getString(R.string.download_control_save_failed), message);
133 Log.v(TAG, "downloadContent : " + path + " (small: " + isSmallSize + ")");
134 playbackControl.downloadContent(path, isSmallSize, this);
138 ex.printStackTrace();
143 public void onProgress(byte[] bytes, int length, ProgressEvent progressEvent)
145 if (downloadDialog != null)
147 int percent = (int)(progressEvent.getProgress() * 100.0f);
148 downloadDialog.setProgress(percent);
149 //downloadDialog.setCancelable(progressEvent.isCancellable()); // キャンセルできるようにしないほうが良さそうなので
150 //Log.v(TAG, "DOWNLOAD (" + percent + "%) " + bytes.length);
154 if (outputStream != null)
156 outputStream.write(bytes, 0, length);
166 public void onCompleted()
170 if (outputStream != null)
172 outputStream.flush();
173 outputStream.close();
176 if (!targetFileName.toUpperCase().endsWith(RAW_SUFFIX))
178 // ギャラリーに受信したファイルを登録する
179 long now = System.currentTimeMillis();
180 ContentValues values = new ContentValues();
181 ContentResolver resolver = activity.getContentResolver();
182 values.put(MediaStore.Images.Media.MIME_TYPE, mimeType);
183 values.put(MediaStore.Images.Media.DATA, filepath);
184 values.put(MediaStore.Images.Media.DATE_ADDED, now);
185 values.put(MediaStore.Images.Media.DATE_TAKEN, now);
186 values.put(MediaStore.Images.Media.DATE_MODIFIED, now);
187 Uri mediaValue = mimeType.contains("video") ? MediaStore.Video.Media.EXTERNAL_CONTENT_URI : MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
188 final Uri content = resolver.insert(mediaValue, values);
191 SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity);
192 if (preferences.getBoolean(IPreferencePropertyAccessor.SHARE_AFTER_SAVE, false))
194 activity.runOnUiThread(new Runnable()
199 shareContent(content, mimeType);
210 activity.runOnUiThread(new Runnable() {
214 if (downloadDialog != null)
216 downloadDialog.dismiss();
218 Toast.makeText(activity, activity.getString(R.string.download_control_save_success) + " " + targetFileName, Toast.LENGTH_SHORT).show();
225 final String message = e.getMessage();
226 activity.runOnUiThread(new Runnable() {
229 if (downloadDialog != null)
231 downloadDialog.dismiss();
233 presentMessage(activity.getString(R.string.download_control_save_failed), message);
241 public void onErrorOccurred(Exception e)
243 final String message = e.getMessage();
246 if (outputStream != null)
248 outputStream.flush();
249 outputStream.close();
256 ex.printStackTrace();
258 activity.runOnUiThread(new Runnable()
263 if (downloadDialog != null)
265 downloadDialog.dismiss();
267 presentMessage(activity.getString(R.string.download_control_download_failed), message);
277 * @param fileUri ファイルUri
279 private void shareContent(final Uri fileUri, final String contentType)
281 Intent intent = new Intent();
282 intent.setAction(Intent.ACTION_SEND);
285 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
286 intent.setType(contentType); // "video/mp4" or "image/jpeg" or "image/x-adobe-dng"
287 intent.putExtra(Intent.EXTRA_STREAM, fileUri);
288 activity.startActivityForResult(intent, 0);
296 private void presentMessage(final String title, final String message)
298 activity.runOnUiThread(new Runnable() {
301 AlertDialog.Builder builder = new AlertDialog.Builder(activity);
302 builder.setTitle(title).setMessage(message);