OSDN Git Service

ファイルのダウンロードのロジックまで。(ただまともに取得できない。。。)
[gokigen/Gr2Control.git] / app / src / main / java / net / osdn / gokigen / gr2control / playback / detail / MyContentDownloader.java
1 package net.osdn.gokigen.gr2control.playback.detail;
2
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;
17
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;
24
25 import java.io.File;
26 import java.io.FileOutputStream;
27
28 /**
29  *   コンテントのダウンロード
30  *
31  */
32 class MyContentDownloader implements IDownloadContentCallback
33 {
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";
45
46     /**
47      *   コンストラクタ
48      *
49      */
50     MyContentDownloader(@NonNull Activity activity, @NonNull final IPlaybackControl playbackControl)
51     {
52         this.activity = activity;
53         this.playbackControl = playbackControl;
54     }
55
56     /**
57      *   ダウンロードの開始
58      *
59      */
60     void startDownload(final ICameraFileInfo fileInfo, String replaceJpegSuffix, boolean isSmallSize)
61     {
62         if (fileInfo == null)
63         {
64             Log.v(TAG, "startDownload() ICameraFileInfo is NULL...");
65             return;
66         }
67         Log.v(TAG, "startDownload() " + fileInfo.getFilename());
68
69         // Download the image.
70         try
71         {
72             ////// ダイアログの表示
73             activity.runOnUiThread(new Runnable() {
74                 @Override
75                 public void run() {
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();
82                 }
83             });
84
85             targetFileName = fileInfo.getFilename().toUpperCase();
86             if (replaceJpegSuffix != null)
87             {
88                 targetFileName = targetFileName.replace(JPEG_SUFFIX, replaceJpegSuffix);
89             }
90             if (targetFileName.contains(RAW_SUFFIX))
91             {
92                 mimeType = "image/x-adobe-dng";
93             }
94             else if (targetFileName.contains(MOVIE_SUFFIX))
95             {
96                 mimeType =  "video/mp4";
97             }
98
99             String path = fileInfo.getDirectoryPath() + "/" + targetFileName;
100
101
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();
104             try
105             {
106                 final File directory = new File(directoryPath);
107                 if (!directory.exists())
108                 {
109                     if (!directory.mkdirs())
110                     {
111                         Log.v(TAG, "MKDIR FAIL. : " + directoryPath);
112                     }
113                 }
114                 outputStream = new FileOutputStream(filepath);
115             }
116             catch (Exception e)
117             {
118                 final String message = e.getMessage();
119                 activity.runOnUiThread(new Runnable() {
120                     @Override
121                     public void run() {
122                         if (downloadDialog != null) {
123                             downloadDialog.dismiss();
124                         }
125                         presentMessage(activity.getString(R.string.download_control_save_failed), message);
126                     }
127                 });
128             }
129             Log.v(TAG, "downloadContent : " + path + " (small: " + isSmallSize + ")");
130             playbackControl.downloadContent(path, isSmallSize, this);
131         }
132         catch (Exception ex)
133         {
134             ex.printStackTrace();
135         }
136     }
137
138     @Override
139     public void onProgress(byte[] bytes, ProgressEvent progressEvent)
140     {
141         if (downloadDialog != null)
142         {
143             int percent = (int)(progressEvent.getProgress() * 100.0f);
144             downloadDialog.setProgress(percent);
145             //downloadDialog.setCancelable(progressEvent.isCancellable()); // キャンセルできるようにしないほうが良さそうなので
146         }
147         try
148         {
149             if (outputStream != null)
150             {
151                 outputStream.write(bytes);
152             }
153         }
154         catch (Exception e)
155         {
156             e.printStackTrace();
157         }
158     }
159
160     @Override
161     public void onCompleted()
162     {
163         try
164         {
165             if (outputStream != null)
166             {
167                 outputStream.flush();
168                 outputStream.close();
169                 outputStream = null;
170             }
171             if (!targetFileName.endsWith(RAW_SUFFIX))
172             {
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);
183                     try
184                     {
185                         SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity);
186                         if (preferences.getBoolean(IPreferencePropertyAccessor.SHARE_AFTER_SAVE, false))
187                         {
188                             activity.runOnUiThread(new Runnable()
189                             {
190                                 @Override
191                                 public void run()
192                                 {
193                                     shareContent(content, mimeType);
194                                 }
195                             });
196                         }
197                     }
198                     catch (Exception e)
199                     {
200                         e.printStackTrace();
201                     }
202             }
203             activity.runOnUiThread(new Runnable() {
204                 @Override
205                 public void run()
206                 {
207                     if (downloadDialog != null)
208                     {
209                         downloadDialog.dismiss();
210                     }
211                     Toast.makeText(activity, activity.getString(R.string.download_control_save_success) + " " + targetFileName, Toast.LENGTH_SHORT).show();
212                     System.gc();
213                 }
214             });
215         }
216         catch (Exception e)
217         {
218             final String message = e.getMessage();
219             activity.runOnUiThread(new Runnable() {
220                 @Override
221                 public void run() {
222                     if (downloadDialog != null)
223                     {
224                         downloadDialog.dismiss();
225                     }
226                     presentMessage(activity.getString(R.string.download_control_save_failed), message);
227                 }
228             });
229         }
230         System.gc();
231     }
232
233     @Override
234     public void onErrorOccurred(Exception e)
235     {
236         final String message = e.getMessage();
237         try
238         {
239             if (outputStream != null)
240             {
241                 outputStream.flush();
242                 outputStream.close();
243                 outputStream = null;
244             }
245         }
246         catch (Exception ex)
247         {
248             e.printStackTrace();
249             ex.printStackTrace();
250         }
251         activity.runOnUiThread(new Runnable()
252         {
253             @Override
254             public void run()
255             {
256                 if (downloadDialog != null)
257                 {
258                     downloadDialog.dismiss();
259                 }
260                 presentMessage(activity.getString(R.string.download_control_download_failed), message);
261                 System.gc();
262             }
263         });
264         System.gc();
265     }
266
267     /**
268      *   共有の呼び出し
269      *
270      * @param fileUri  ファイルUri
271      */
272     private void shareContent(final Uri fileUri, final String contentType)
273     {
274         Intent intent = new Intent();
275         intent.setAction(Intent.ACTION_SEND);
276         try
277         {
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);
282         }
283         catch (Exception e)
284         {
285             e.printStackTrace();
286         }
287     }
288
289     private void presentMessage(final String title, final String message)
290     {
291         activity.runOnUiThread(new Runnable() {
292             @Override
293             public void run() {
294                 AlertDialog.Builder builder = new AlertDialog.Builder(activity);
295                 builder.setTitle(title).setMessage(message);
296                 builder.show();
297             }
298         });
299     }
300 }