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.util.Log;
14 import android.view.View;
16 import com.google.android.material.snackbar.Snackbar;
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;
31 import androidx.annotation.NonNull;
32 import androidx.preference.PreferenceManager;
38 public class MyContentDownloader implements IDownloadContentCallback
40 private final String TAG = toString();
41 private final Activity activity;
42 private final IPlaybackControl playbackControl;
43 private static final String RAW_SUFFIX_1 = ".DNG";
44 private static final String RAW_SUFFIX_2 = ".ORF";
45 private static final String RAW_SUFFIX_3 = ".PEF";
46 private static final String MOVIE_SUFFIX = ".MOV";
47 private static final String JPEG_SUFFIX = ".JPG";
48 private ProgressDialog downloadDialog = null;
49 private FileOutputStream outputStream = null;
50 private String targetFileName = "";
51 private String filepath = "";
52 private String mimeType = "image/jpeg";
58 public MyContentDownloader(@NonNull Activity activity, @NonNull final IPlaybackControl playbackControl)
60 this.activity = activity;
61 this.playbackControl = playbackControl;
68 public void startDownload(final ICameraFileInfo fileInfo, final String appendTitle, String replaceJpegSuffix, boolean isSmallSize)
72 Log.v(TAG, "startDownload() ICameraFileInfo is NULL...");
75 Log.v(TAG, "startDownload() " + fileInfo.getFilename());
77 // Download the image.
80 Calendar calendar = Calendar.getInstance();
81 String extendName = new SimpleDateFormat("yyyyMMdd-HHmmss", Locale.getDefault()).format(calendar.getTime());
82 targetFileName = fileInfo.getFilename().toUpperCase();
83 if (replaceJpegSuffix != null)
85 targetFileName = targetFileName.replace(JPEG_SUFFIX, replaceJpegSuffix);
87 if (targetFileName.toUpperCase().contains(RAW_SUFFIX_1))
89 mimeType = "image/x-adobe-dng";
92 else if (targetFileName.toUpperCase().contains(RAW_SUFFIX_2))
94 mimeType = "image/x-olympus-orf";
97 else if (targetFileName.toUpperCase().contains(RAW_SUFFIX_3))
99 mimeType = "image/x-pentax-pef";
102 else if (targetFileName.toUpperCase().contains(MOVIE_SUFFIX))
104 mimeType = "video/mp4";
109 mimeType = "image/jpeg";
113 activity.runOnUiThread(new Runnable() {
116 downloadDialog = new ProgressDialog(activity);
117 downloadDialog.setTitle(activity.getString(R.string.dialog_download_file_title) + appendTitle);
118 downloadDialog.setMessage(activity.getString(R.string.dialog_download_message) + " " + targetFileName);
119 downloadDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
120 downloadDialog.setCancelable(false);
121 downloadDialog.show();
124 String path = fileInfo.getDirectoryPath() + "/" + targetFileName;
126 final String directoryPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getPath() + "/" + activity.getString(R.string.app_name2) + "/";
127 String suffix = targetFileName.substring(targetFileName.indexOf("."));
128 String picName = targetFileName.substring(0, targetFileName.indexOf("."));
129 String outputFileName = picName + "_" + extendName + suffix;
130 filepath = new File(directoryPath.toLowerCase(), outputFileName.toLowerCase()).getPath();
133 final File directory = new File(directoryPath);
134 if (!directory.exists())
136 if (!directory.mkdirs())
138 Log.v(TAG, "MKDIR FAIL. : " + directoryPath);
141 outputStream = new FileOutputStream(filepath);
145 final String message = e.getMessage();
146 activity.runOnUiThread(new Runnable() {
149 if (downloadDialog != null) {
150 downloadDialog.dismiss();
152 presentMessage(activity.getString(R.string.download_control_save_failed), message);
156 Log.v(TAG, "downloadContent : " + path + " (small: " + isSmallSize + ")");
157 playbackControl.downloadContent(null, path, isSmallSize, this);
161 ex.printStackTrace();
166 public void onProgress(byte[] bytes, int length, ProgressEvent progressEvent)
168 if (downloadDialog != null)
170 int percent = (int)(progressEvent.getProgress() * 100.0f);
171 downloadDialog.setProgress(percent);
172 //downloadDialog.setCancelable(progressEvent.isCancellable()); // キャンセルできるようにしないほうが良さそうなので
173 //Log.v(TAG, "DOWNLOAD (" + percent + "%) " + bytes.length);
177 if (outputStream != null)
179 outputStream.write(bytes, 0, length);
189 public void onCompleted()
193 if (outputStream != null)
195 outputStream.flush();
196 outputStream.close();
199 if ((!targetFileName.toUpperCase().endsWith(RAW_SUFFIX_1))&&(!targetFileName.toUpperCase().endsWith(RAW_SUFFIX_2))&&(!targetFileName.toUpperCase().endsWith(RAW_SUFFIX_3)))
201 // ギャラリーに受信したファイルを登録する
202 long now = System.currentTimeMillis();
203 ContentValues values = new ContentValues();
204 ContentResolver resolver = activity.getContentResolver();
205 values.put(MediaStore.Images.Media.MIME_TYPE, mimeType);
206 values.put(MediaStore.Images.Media.DATA, filepath);
207 values.put(MediaStore.Images.Media.DATE_ADDED, now);
208 values.put(MediaStore.Images.Media.DATE_TAKEN, now);
209 values.put(MediaStore.Images.Media.DATE_MODIFIED, now);
210 Uri mediaValue = mimeType.contains("video") ? MediaStore.Video.Media.EXTERNAL_CONTENT_URI : MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
211 final Uri content = resolver.insert(mediaValue, values);
214 SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity);
215 if (preferences.getBoolean(IPreferencePropertyAccessor.SHARE_AFTER_SAVE, false))
217 activity.runOnUiThread(new Runnable()
222 shareContent(content, mimeType);
233 activity.runOnUiThread(new Runnable() {
237 if (downloadDialog != null)
239 downloadDialog.dismiss();
241 View view = activity.findViewById(R.id.fragment1);
242 Snackbar.make(view, activity.getString(R.string.download_control_save_success) + " " + targetFileName, Snackbar.LENGTH_SHORT).show();
243 //Toast.makeText(activity, activity.getString(R.string.download_control_save_success) + " " + targetFileName, Toast.LENGTH_SHORT).show();
250 final String message = e.getMessage();
251 activity.runOnUiThread(new Runnable() {
254 if (downloadDialog != null)
256 downloadDialog.dismiss();
258 presentMessage(activity.getString(R.string.download_control_save_failed), message);
266 public void onErrorOccurred(Exception e)
268 final String message = e.getMessage();
271 if (outputStream != null)
273 outputStream.flush();
274 outputStream.close();
281 ex.printStackTrace();
283 activity.runOnUiThread(new Runnable()
288 if (downloadDialog != null)
290 downloadDialog.dismiss();
292 presentMessage(activity.getString(R.string.download_control_download_failed), message);
302 * @param fileUri ファイルUri
304 private void shareContent(final Uri fileUri, final String contentType)
306 Intent intent = new Intent();
307 intent.setAction(Intent.ACTION_SEND);
310 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
311 intent.setType(contentType); // "video/mp4" or "image/jpeg" or "image/x-adobe-dng"
312 intent.putExtra(Intent.EXTRA_STREAM, fileUri);
313 activity.startActivityForResult(intent, 0);
321 private void presentMessage(final String title, final String message)
323 activity.runOnUiThread(new Runnable() {
326 AlertDialog.Builder builder = new AlertDialog.Builder(activity);
327 builder.setTitle(title).setMessage(message);