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 RAW_SUFFIX_4 = ".RAF";
47 private static final String MOVIE_SUFFIX = ".MOV";
48 private static final String JPEG_SUFFIX = ".JPG";
49 private ProgressDialog downloadDialog = null;
50 private FileOutputStream outputStream = null;
51 private String targetFileName = "";
52 private String filepath = "";
53 private String mimeType = "image/jpeg";
54 private boolean isDownloading = false;
60 public MyContentDownloader(@NonNull Activity activity, @NonNull final IPlaybackControl playbackControl)
62 this.activity = activity;
63 this.playbackControl = playbackControl;
70 public void startDownload(final ICameraFileInfo fileInfo, final String appendTitle, String replaceJpegSuffix, boolean isSmallSize)
74 Log.v(TAG, "startDownload() ICameraFileInfo is NULL...");
77 Log.v(TAG, "startDownload() " + fileInfo.getFilename());
79 // Download the image.
83 Calendar calendar = Calendar.getInstance();
84 String extendName = new SimpleDateFormat("yyyyMMdd-HHmmss", Locale.getDefault()).format(calendar.getTime());
85 targetFileName = fileInfo.getOriginalFilename().toUpperCase();
86 if (replaceJpegSuffix != null)
88 targetFileName = targetFileName.replace(JPEG_SUFFIX, replaceJpegSuffix);
90 if (targetFileName.toUpperCase().contains(RAW_SUFFIX_1))
92 mimeType = "image/x-adobe-dng";
95 else if (targetFileName.toUpperCase().contains(RAW_SUFFIX_2))
97 mimeType = "image/x-olympus-orf";
100 else if (targetFileName.toUpperCase().contains(RAW_SUFFIX_3))
102 mimeType = "image/x-pentax-pef";
105 else if (targetFileName.toUpperCase().contains(RAW_SUFFIX_4))
107 mimeType = "image/x-fuji-raf";
110 else if (targetFileName.toUpperCase().contains(MOVIE_SUFFIX))
112 mimeType = "video/mp4";
117 mimeType = "image/jpeg";
121 activity.runOnUiThread(new Runnable() {
124 downloadDialog = new ProgressDialog(activity);
125 downloadDialog.setTitle(activity.getString(R.string.dialog_download_file_title) + appendTitle);
126 downloadDialog.setMessage(activity.getString(R.string.dialog_download_message) + " " + targetFileName);
127 downloadDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
128 downloadDialog.setCancelable(false);
129 downloadDialog.show();
132 String fileName = fileInfo.getDirectoryPath() + "/" + fileInfo.getFilename();
134 final String directoryPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getPath() + "/" + activity.getString(R.string.app_name2) + "/";
135 String suffix = targetFileName.substring(targetFileName.indexOf("."));
136 String picName = targetFileName.substring(0, targetFileName.indexOf("."));
137 String outputFileName = picName + "_" + extendName + suffix;
138 filepath = new File(directoryPath.toLowerCase(), outputFileName.toLowerCase()).getPath();
141 final File directory = new File(directoryPath);
142 if (!directory.exists())
144 if (!directory.mkdirs())
146 Log.v(TAG, "MKDIR FAIL. : " + directoryPath);
149 outputStream = new FileOutputStream(filepath);
153 final String message = e.getMessage();
154 activity.runOnUiThread(new Runnable() {
157 if (downloadDialog != null) {
158 downloadDialog.dismiss();
160 downloadDialog = null;
161 isDownloading = false;
162 presentMessage(activity.getString(R.string.download_control_save_failed), message);
166 Log.v(TAG, "downloadContent : " + fileName + " (small: " + isSmallSize + ")");
167 playbackControl.downloadContent(null, fileName, isSmallSize, this);
171 ex.printStackTrace();
172 activity.runOnUiThread(new Runnable() {
177 if (downloadDialog != null) {
178 downloadDialog.dismiss();
185 downloadDialog = null;
186 isDownloading = false;
193 public void onProgress(byte[] bytes, int length, ProgressEvent progressEvent)
195 if (downloadDialog != null)
197 int percent = (int)(progressEvent.getProgress() * 100.0f);
198 downloadDialog.setProgress(percent);
199 //downloadDialog.setCancelable(progressEvent.isCancellable()); // キャンセルできるようにしないほうが良さそうなので
200 //Log.v(TAG, "DOWNLOAD (" + percent + "%) " + bytes.length);
204 if (outputStream != null)
206 outputStream.write(bytes, 0, length);
216 public void onCompleted()
220 if (outputStream != null)
222 outputStream.flush();
223 outputStream.close();
226 //if ((!targetFileName.toUpperCase().endsWith(RAW_SUFFIX_1))&&(!targetFileName.toUpperCase().endsWith(RAW_SUFFIX_2))&&(!targetFileName.toUpperCase().endsWith(RAW_SUFFIX_3)))
228 // ギャラリーに受信したファイルを登録する
229 long now = System.currentTimeMillis();
230 ContentValues values = new ContentValues();
231 ContentResolver resolver = activity.getContentResolver();
232 values.put(MediaStore.Images.Media.MIME_TYPE, mimeType);
233 values.put(MediaStore.Images.Media.DATA, filepath);
234 values.put(MediaStore.Images.Media.DATE_ADDED, now);
235 values.put(MediaStore.Images.Media.DATE_TAKEN, now);
236 values.put(MediaStore.Images.Media.DATE_MODIFIED, now);
237 Uri mediaValue = mimeType.contains("video") ? MediaStore.Video.Media.EXTERNAL_CONTENT_URI : MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
238 final Uri content = resolver.insert(mediaValue, values);
241 SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity);
242 if (preferences.getBoolean(IPreferencePropertyAccessor.SHARE_AFTER_SAVE, false))
244 activity.runOnUiThread(new Runnable()
249 shareContent(content, mimeType);
260 activity.runOnUiThread(new Runnable() {
264 if (downloadDialog != null)
266 downloadDialog.dismiss();
268 downloadDialog = null;
269 isDownloading = false;
270 View view = activity.findViewById(R.id.fragment1);
271 Snackbar.make(view, activity.getString(R.string.download_control_save_success) + " " + targetFileName, Snackbar.LENGTH_SHORT).show();
272 //Toast.makeText(activity, activity.getString(R.string.download_control_save_success) + " " + targetFileName, Toast.LENGTH_SHORT).show();
279 final String message = e.getMessage();
280 activity.runOnUiThread(new Runnable() {
283 if (downloadDialog != null)
285 downloadDialog.dismiss();
287 downloadDialog = null;
288 isDownloading = false;
289 presentMessage(activity.getString(R.string.download_control_save_failed), message);
297 public void onErrorOccurred(Exception e)
299 isDownloading = false;
300 final String message = e.getMessage();
303 if (outputStream != null)
305 outputStream.flush();
306 outputStream.close();
313 ex.printStackTrace();
315 activity.runOnUiThread(new Runnable()
320 if (downloadDialog != null)
322 downloadDialog.dismiss();
324 downloadDialog = null;
325 isDownloading = false;
326 presentMessage(activity.getString(R.string.download_control_download_failed), message);
333 public boolean isDownloading()
335 return (isDownloading);
341 * @param fileUri ファイルUri
343 private void shareContent(final Uri fileUri, final String contentType)
345 Intent intent = new Intent();
346 intent.setAction(Intent.ACTION_SEND);
349 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
350 intent.setType(contentType); // "video/mp4" or "image/jpeg" or "image/x-adobe-dng"
351 intent.putExtra(Intent.EXTRA_STREAM, fileUri);
352 activity.startActivityForResult(intent, 0);
360 private void presentMessage(final String title, final String message)
362 activity.runOnUiThread(new Runnable() {
365 AlertDialog.Builder builder = new AlertDialog.Builder(activity);
366 builder.setTitle(title).setMessage(message);