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_1 = ".DNG";
41 private static final String RAW_SUFFIX_2 = ".ORF";
42 private static final String MOVIE_SUFFIX = ".MOV";
43 private static final String JPEG_SUFFIX = ".JPG";
44 private ProgressDialog downloadDialog = null;
45 private FileOutputStream outputStream = null;
46 private String targetFileName = "";
47 private String filepath = "";
48 private String mimeType = "image/jpeg";
54 MyContentDownloader(@NonNull Activity activity, @NonNull final IPlaybackControl playbackControl)
56 this.activity = activity;
57 this.playbackControl = playbackControl;
64 void startDownload(final ICameraFileInfo fileInfo, String replaceJpegSuffix, boolean isSmallSize)
68 Log.v(TAG, "startDownload() ICameraFileInfo is NULL...");
71 Log.v(TAG, "startDownload() " + fileInfo.getFilename());
73 // Download the image.
76 Calendar calendar = Calendar.getInstance();
77 String extendName = new SimpleDateFormat("yyyyMMdd-HHmmss", Locale.getDefault()).format(calendar.getTime());
78 targetFileName = fileInfo.getFilename().toUpperCase();
79 if (replaceJpegSuffix != null)
81 targetFileName = targetFileName.replace(JPEG_SUFFIX, replaceJpegSuffix);
83 if (targetFileName.toUpperCase().contains(RAW_SUFFIX_1))
85 mimeType = "image/x-adobe-dng";
87 else if (targetFileName.toUpperCase().contains(RAW_SUFFIX_2))
89 mimeType = "image/image/x-olympus-orf";
91 else if (targetFileName.toUpperCase().contains(MOVIE_SUFFIX))
93 mimeType = "video/mp4";
97 activity.runOnUiThread(new Runnable() {
100 downloadDialog = new ProgressDialog(activity);
101 downloadDialog.setTitle(activity.getString(R.string.dialog_download_file_title));
102 downloadDialog.setMessage(activity.getString(R.string.dialog_download_message) + " " + targetFileName);
103 downloadDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
104 downloadDialog.setCancelable(false);
105 downloadDialog.show();
108 String path = fileInfo.getDirectoryPath() + "/" + targetFileName;
110 final String directoryPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getPath() + "/" + activity.getString(R.string.app_name2) + "/";
111 String outputFileName = extendName + "_" + targetFileName;
112 filepath = new File(directoryPath.toLowerCase(), outputFileName.toLowerCase()).getPath();
115 final File directory = new File(directoryPath);
116 if (!directory.exists())
118 if (!directory.mkdirs())
120 Log.v(TAG, "MKDIR FAIL. : " + directoryPath);
123 outputStream = new FileOutputStream(filepath);
127 final String message = e.getMessage();
128 activity.runOnUiThread(new Runnable() {
131 if (downloadDialog != null) {
132 downloadDialog.dismiss();
134 presentMessage(activity.getString(R.string.download_control_save_failed), message);
138 Log.v(TAG, "downloadContent : " + path + " (small: " + isSmallSize + ")");
139 playbackControl.downloadContent(path, isSmallSize, this);
143 ex.printStackTrace();
148 public void onProgress(byte[] bytes, int length, ProgressEvent progressEvent)
150 if (downloadDialog != null)
152 int percent = (int)(progressEvent.getProgress() * 100.0f);
153 downloadDialog.setProgress(percent);
154 //downloadDialog.setCancelable(progressEvent.isCancellable()); // キャンセルできるようにしないほうが良さそうなので
155 //Log.v(TAG, "DOWNLOAD (" + percent + "%) " + bytes.length);
159 if (outputStream != null)
161 outputStream.write(bytes, 0, length);
171 public void onCompleted()
175 if (outputStream != null)
177 outputStream.flush();
178 outputStream.close();
181 if ((!targetFileName.toUpperCase().endsWith(RAW_SUFFIX_1))&&(!targetFileName.toUpperCase().endsWith(RAW_SUFFIX_2)))
183 // ギャラリーに受信したファイルを登録する
184 long now = System.currentTimeMillis();
185 ContentValues values = new ContentValues();
186 ContentResolver resolver = activity.getContentResolver();
187 values.put(MediaStore.Images.Media.MIME_TYPE, mimeType);
188 values.put(MediaStore.Images.Media.DATA, filepath);
189 values.put(MediaStore.Images.Media.DATE_ADDED, now);
190 values.put(MediaStore.Images.Media.DATE_TAKEN, now);
191 values.put(MediaStore.Images.Media.DATE_MODIFIED, now);
192 Uri mediaValue = mimeType.contains("video") ? MediaStore.Video.Media.EXTERNAL_CONTENT_URI : MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
193 final Uri content = resolver.insert(mediaValue, values);
196 SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity);
197 if (preferences.getBoolean(IPreferencePropertyAccessor.SHARE_AFTER_SAVE, false))
199 activity.runOnUiThread(new Runnable()
204 shareContent(content, mimeType);
215 activity.runOnUiThread(new Runnable() {
219 if (downloadDialog != null)
221 downloadDialog.dismiss();
223 Toast.makeText(activity, activity.getString(R.string.download_control_save_success) + " " + targetFileName, Toast.LENGTH_SHORT).show();
230 final String message = e.getMessage();
231 activity.runOnUiThread(new Runnable() {
234 if (downloadDialog != null)
236 downloadDialog.dismiss();
238 presentMessage(activity.getString(R.string.download_control_save_failed), message);
246 public void onErrorOccurred(Exception e)
248 final String message = e.getMessage();
251 if (outputStream != null)
253 outputStream.flush();
254 outputStream.close();
261 ex.printStackTrace();
263 activity.runOnUiThread(new Runnable()
268 if (downloadDialog != null)
270 downloadDialog.dismiss();
272 presentMessage(activity.getString(R.string.download_control_download_failed), message);
282 * @param fileUri ファイルUri
284 private void shareContent(final Uri fileUri, final String contentType)
286 Intent intent = new Intent();
287 intent.setAction(Intent.ACTION_SEND);
290 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
291 intent.setType(contentType); // "video/mp4" or "image/jpeg" or "image/x-adobe-dng"
292 intent.putExtra(Intent.EXTRA_STREAM, fileUri);
293 activity.startActivityForResult(intent, 0);
301 private void presentMessage(final String title, final String message)
303 activity.runOnUiThread(new Runnable() {
306 AlertDialog.Builder builder = new AlertDialog.Builder(activity);
307 builder.setTitle(title).setMessage(message);