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;
32 class MyContentDownloader implements IDownloadContentCallback
34 private final String TAG = toString();
35 private final Activity activity;
36 private final IPlaybackControl playbackControl;
37 private static final String RAW_SUFFIX = ".DNG";
38 private static final String MOVIE_SUFFIX = ".MOV";
39 private static final String JPEG_SUFFIX = ".JPG";
40 private ProgressDialog downloadDialog = null;
41 private FileOutputStream outputStream = null;
42 private String targetFileName = "";
43 private String filepath = "";
44 private String mimeType = "image/jpeg";
50 MyContentDownloader(@NonNull Activity activity, @NonNull final IPlaybackControl playbackControl)
52 this.activity = activity;
53 this.playbackControl = playbackControl;
60 void startDownload(final ICameraFileInfo fileInfo, String replaceJpegSuffix, boolean isSmallSize)
64 Log.v(TAG, "startDownload() ICameraFileInfo is NULL...");
67 Log.v(TAG, "startDownload() " + fileInfo.getFilename());
69 // Download the image.
73 activity.runOnUiThread(new Runnable() {
76 downloadDialog = new ProgressDialog(activity);
77 downloadDialog.setTitle(activity.getString(R.string.dialog_download_file_title));
78 downloadDialog.setMessage(activity.getString(R.string.dialog_download_message) + " " + fileInfo.getFilename());
79 downloadDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
80 downloadDialog.setCancelable(false);
81 downloadDialog.show();
85 targetFileName = fileInfo.getFilename().toUpperCase();
86 if (replaceJpegSuffix != null)
88 targetFileName = targetFileName.replace(JPEG_SUFFIX, replaceJpegSuffix);
90 if (targetFileName.contains(RAW_SUFFIX))
92 mimeType = "image/x-adobe-dng";
94 else if (targetFileName.contains(MOVIE_SUFFIX))
96 mimeType = "video/mp4";
99 String path = fileInfo.getDirectoryPath() + "/" + targetFileName;
102 final String directoryPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getPath() + "/" + activity.getString(R.string.app_name2) + "/";
103 filepath = new File(directoryPath.toLowerCase(), targetFileName.toLowerCase()).getPath();
106 final File directory = new File(directoryPath);
107 if (!directory.exists())
109 if (!directory.mkdirs())
111 Log.v(TAG, "MKDIR FAIL. : " + directoryPath);
114 outputStream = new FileOutputStream(filepath);
118 final String message = e.getMessage();
119 activity.runOnUiThread(new Runnable() {
122 if (downloadDialog != null) {
123 downloadDialog.dismiss();
125 presentMessage(activity.getString(R.string.download_control_save_failed), message);
129 Log.v(TAG, "downloadContent : " + path + " (small: " + isSmallSize + ")");
130 playbackControl.downloadContent(path, isSmallSize, this);
134 ex.printStackTrace();
139 public void onProgress(byte[] bytes, ProgressEvent progressEvent)
141 if (downloadDialog != null)
143 int percent = (int)(progressEvent.getProgress() * 100.0f);
144 downloadDialog.setProgress(percent);
145 //downloadDialog.setCancelable(progressEvent.isCancellable()); // キャンセルできるようにしないほうが良さそうなので
149 if (outputStream != null)
151 outputStream.write(bytes);
161 public void onCompleted()
165 if (outputStream != null)
167 outputStream.flush();
168 outputStream.close();
171 if (!targetFileName.endsWith(RAW_SUFFIX))
173 // ギャラリーに受信したファイルを登録する
174 long now = System.currentTimeMillis();
175 ContentValues values = new ContentValues();
176 ContentResolver resolver = activity.getContentResolver();
177 values.put(MediaStore.Images.Media.MIME_TYPE, mimeType);
178 values.put(MediaStore.Images.Media.DATA, filepath);
179 values.put(MediaStore.Images.Media.DATE_ADDED, now);
180 values.put(MediaStore.Images.Media.DATE_TAKEN, now);
181 values.put(MediaStore.Images.Media.DATE_MODIFIED, now);
182 final Uri content = resolver.insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, values);
185 SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity);
186 if (preferences.getBoolean(IPreferencePropertyAccessor.SHARE_AFTER_SAVE, false))
188 activity.runOnUiThread(new Runnable()
193 shareContent(content, mimeType);
203 activity.runOnUiThread(new Runnable() {
207 if (downloadDialog != null)
209 downloadDialog.dismiss();
211 Toast.makeText(activity, activity.getString(R.string.download_control_save_success) + " " + targetFileName, Toast.LENGTH_SHORT).show();
218 final String message = e.getMessage();
219 activity.runOnUiThread(new Runnable() {
222 if (downloadDialog != null)
224 downloadDialog.dismiss();
226 presentMessage(activity.getString(R.string.download_control_save_failed), message);
234 public void onErrorOccurred(Exception e)
236 final String message = e.getMessage();
239 if (outputStream != null)
241 outputStream.flush();
242 outputStream.close();
249 ex.printStackTrace();
251 activity.runOnUiThread(new Runnable()
256 if (downloadDialog != null)
258 downloadDialog.dismiss();
260 presentMessage(activity.getString(R.string.download_control_download_failed), message);
270 * @param fileUri ファイルUri
272 private void shareContent(final Uri fileUri, final String contentType)
274 Intent intent = new Intent();
275 intent.setAction(Intent.ACTION_SEND);
278 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
279 intent.setType(contentType); // "video/mp4" or "image/jpeg" or "image/x-adobe-dng"
280 intent.putExtra(Intent.EXTRA_STREAM, fileUri);
281 activity.startActivityForResult(intent, 0);
289 private void presentMessage(final String title, final String message)
291 activity.runOnUiThread(new Runnable() {
294 AlertDialog.Builder builder = new AlertDialog.Builder(activity);
295 builder.setTitle(title).setMessage(message);