OSDN Git Service

OPCで一括ダウンロードできるようにする。Smallサイズ取得も可能にする。
[gokigen/PKRemote.git] / app / src / main / java / net / osdn / gokigen / pkremote / camera / vendor / ricoh / wrapper / RicohGr2PlaybackControl.java
1 package net.osdn.gokigen.pkremote.camera.vendor.ricoh.wrapper;
2 import android.graphics.Bitmap;
3 import android.util.Log;
4
5 import net.osdn.gokigen.pkremote.camera.interfaces.playback.ICameraContent;
6 import net.osdn.gokigen.pkremote.camera.interfaces.playback.ICameraContentListCallback;
7 import net.osdn.gokigen.pkremote.camera.interfaces.playback.ICameraFileInfo;
8 import net.osdn.gokigen.pkremote.camera.interfaces.playback.IContentInfoCallback;
9 import net.osdn.gokigen.pkremote.camera.interfaces.playback.IDownloadContentListCallback;
10 import net.osdn.gokigen.pkremote.camera.interfaces.playback.IDownloadContentCallback;
11 import net.osdn.gokigen.pkremote.camera.interfaces.playback.IDownloadThumbnailImageCallback;
12 import net.osdn.gokigen.pkremote.camera.interfaces.playback.IPlaybackControl;
13 import net.osdn.gokigen.pkremote.camera.playback.CameraContentInfo;
14 import net.osdn.gokigen.pkremote.camera.playback.CameraFileInfo;
15 import net.osdn.gokigen.pkremote.camera.playback.ProgressEvent;
16 import net.osdn.gokigen.pkremote.camera.utils.SimpleHttpClient;
17
18 import org.json.JSONArray;
19 import org.json.JSONObject;
20
21 import java.text.SimpleDateFormat;
22 import java.util.ArrayList;
23 import java.util.Date;
24 import java.util.GregorianCalendar;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Locale;
28
29 import androidx.annotation.NonNull;
30
31 /**
32  *
33  *
34  */
35 public class RicohGr2PlaybackControl implements IPlaybackControl
36 {
37     private final String TAG = toString();
38     private final String getPhotoUrl = "http://192.168.0.1/v1/photos/";
39     private final RicohGr2StatusChecker statusChecker;
40     private static final int DEFAULT_TIMEOUT = 7700;
41     private final boolean useGrCommand;
42
43
44     /*****
45          [操作メモ]
46             画像の一覧をとる            : http://192.168.0.1/v1/photos?limit=3000
47             画像の情報をとる            : http://192.168.0.1/v1/photos/yyyRICOH/R0000xxx.DNG/info
48             サムネール画像をとる(JPEG)  : http://192.168.0.1/v1/photos/yyyRICOH/R0000xxx.JPG?size=thumb
49             サムネール画像をとる(DNG)   : http://192.168.0.1/v1/photos/yyyRICOH/R0000xxx.DNG?size=view
50             サムネール画像をとる(MOV)   : http://192.168.0.1/v1/photos/yyyRICOH/R0000xxx.MOV?size=view
51             デバイス表示用画像をとる     :  http://192.168.0.1/v1/photos/yyyRICOH/R0000xxx.JPG?size=view
52             画像(JPEG)をダウンロードする : http://192.168.0.1/v1/photos/yyyRICOH/R0000xxx.JPG?size=full
53             画像(DNG)をダウンロードする  : http://192.168.0.1/v1/photos/yyyRICOH/R0000xxx.DNG?size=full
54             動画をダウンロードする      : http://192.168.0.1/v1/photos/yyyRICOH/R0000xxx.MOV?size=full
55      *****/
56
57     RicohGr2PlaybackControl(RicohGr2StatusChecker statusChecker, boolean useGrCommand)
58     {
59         this.statusChecker = statusChecker;
60         this.useGrCommand = useGrCommand;
61     }
62
63     @Override
64     public String getRawFileSuffix()
65     {
66         return (".DNG");
67     }
68
69     @Override
70     public void downloadContentList(@NonNull IDownloadContentListCallback callback)
71     {
72         List<ICameraFileInfo> fileList = new ArrayList<>();
73         String imageListurl = "http://192.168.0.1/v1/photos?limit=3000";
74         String contentList;
75         try
76         {
77             contentList = SimpleHttpClient.httpGet(imageListurl, DEFAULT_TIMEOUT);
78             if (contentList == null)
79             {
80                 // ぬるぽ発行
81                 callback.onErrorOccurred(new NullPointerException());
82                 return;
83             }
84         }
85         catch (Exception e)
86         {
87             // 例外をそのまま転送
88             callback.onErrorOccurred(e);
89             return;
90         }
91         try
92         {
93             JSONArray dirsArray = new JSONObject(contentList).getJSONArray("dirs");
94             if (dirsArray != null)
95             {
96                 int size = dirsArray.length();
97                 for (int index = 0; index < size; index++)
98                 {
99                     JSONObject object = dirsArray.getJSONObject(index);
100                     String dirName = object.getString("name");
101                     JSONArray filesArray = object.getJSONArray("files");
102                     int nofFiles = filesArray.length();
103                     for (int fileIndex = 0; fileIndex < nofFiles; fileIndex++)
104                     {
105                         String fileName = filesArray.getString(fileIndex);
106                         fileList.add(new CameraFileInfo(dirName, fileName));
107                     }
108                 }
109             }
110         }
111         catch (Exception e)
112         {
113             callback.onErrorOccurred(e);
114             return;
115         }
116         callback.onCompleted(fileList);
117     }
118
119     @Override
120     public void updateCameraFileInfo(ICameraFileInfo info)
121     {
122         String url = getPhotoUrl + info.getDirectoryPath() + "/" + info.getFilename() + "/info";
123         Log.v(TAG, "updateCameraFileInfo() GET URL : " + url);
124         try
125         {
126             String response = SimpleHttpClient.httpGet(url, DEFAULT_TIMEOUT);
127             if ((response == null)||(response.length() < 1))
128             {
129                 return;
130             }
131             JSONObject object = new JSONObject(response);
132
133             // データを突っ込む
134             boolean captured = object.getBoolean("captured");
135             String av = getJSONString(object, "av");
136             String tv = getJSONString(object, "tv");
137             String sv = getJSONString(object,"sv");
138             String xv = getJSONString(object,"xv");
139             int orientation = object.getInt("orientation");
140             String aspectRatio = getJSONString(object,"aspectRatio");
141             String cameraModel = getJSONString(object,"cameraModel");
142             String latLng = getJSONString(object,"latlng");
143             String dateTime = object.getString("datetime");
144             info.updateValues(dateTime, av, tv, sv, xv, orientation, aspectRatio, cameraModel, latLng, captured);
145         }
146         catch (Throwable e)
147         {
148             e.printStackTrace();
149         }
150     }
151
152     private String getJSONString(JSONObject object, String key)
153     {
154         String value = "";
155         try
156         {
157             value = object.getString(key);
158         }
159         catch (Exception e)
160         {
161             e.printStackTrace();
162         }
163         return (value);
164     }
165
166     @Override
167     public void getContentInfo(@NonNull String path, @NonNull String name, @NonNull IContentInfoCallback callback)
168     {
169         String url = getPhotoUrl + path + "/" + name + "/info";
170         Log.v(TAG, "getContentInfo() GET URL : " + url);
171         try
172         {
173             String response = SimpleHttpClient.httpGet(url, DEFAULT_TIMEOUT);
174             if ((response == null)||(response.length() < 1))
175             {
176                 callback.onErrorOccurred(new NullPointerException());
177             }
178             CameraFileInfo fileInfo = new CameraFileInfo(path, name);
179
180             JSONObject object = new JSONObject(response);
181
182             boolean captured = object.getBoolean("captured");
183             String av = getJSONString(object, "av");
184             String tv = getJSONString(object, "tv");
185             String sv = getJSONString(object,"sv");
186             String xv = getJSONString(object,"xv");
187             int orientation = object.getInt("orientation");
188             String aspectRatio = getJSONString(object,"aspectRatio");
189             String cameraModel = getJSONString(object,"cameraModel");
190             String latLng = getJSONString(object,"latlng");
191             String dateTime = object.getString("datetime");
192             fileInfo.updateValues(dateTime, av, tv, sv, xv, orientation, aspectRatio, cameraModel, latLng, captured);
193
194             callback.onCompleted(fileInfo);
195         }
196         catch (Throwable e)
197         {
198             e.printStackTrace();
199         }
200     }
201
202     @Override
203     public void downloadContentScreennail(@NonNull String path, @NonNull IDownloadThumbnailImageCallback callback)
204     {
205         //Log.v(TAG, "downloadContentScreennail() : " + path);
206         String suffix = "?size=view";
207         String url = getPhotoUrl + path + suffix;
208         Log.v(TAG, "downloadContentScreennail() GET URL : " + url);
209         try
210         {
211             Bitmap bmp = SimpleHttpClient.httpGetBitmap(url, DEFAULT_TIMEOUT);
212             HashMap<String, Object> map = new HashMap<>();
213             map.put("Orientation", 0);
214             callback.onCompleted(bmp, map);
215         }
216         catch (Throwable e)
217         {
218             e.printStackTrace();
219         }
220     }
221
222     @Override
223     public void downloadContentThumbnail(@NonNull String path, @NonNull IDownloadThumbnailImageCallback callback)
224     {
225         //Log.v(TAG, "downloadContentThumbnail() : " + path);
226         String suffix = "?size=view";
227         if (path.contains(".JPG"))
228         {
229             suffix = "?size=thumb";
230         }
231         String url = getPhotoUrl + path + suffix;
232         Log.v(TAG, "downloadContentThumbnail() GET URL : " + url);
233         try
234         {
235             Bitmap bmp = SimpleHttpClient.httpGetBitmap(url, DEFAULT_TIMEOUT);
236             HashMap<String, Object> map = new HashMap<>();
237             map.put("Orientation", 0);
238             callback.onCompleted(bmp, map);
239         }
240         catch (Throwable e)
241         {
242             e.printStackTrace();
243             callback.onErrorOccurred(new NullPointerException());
244         }
245    }
246
247     @Override
248     public void downloadContent(@NonNull String  path, boolean isSmallSize, @NonNull final IDownloadContentCallback callback)
249     {
250         Log.v(TAG, "downloadContent() : " + path);
251         String suffix = "?size=full";
252         if (isSmallSize)
253         {
254             suffix = "?size=view";
255         }
256         String url = getPhotoUrl + path + suffix;
257         Log.v(TAG, "downloadContent() GET URL : " + url);
258         try
259         {
260             SimpleHttpClient.httpGetBytes(url, DEFAULT_TIMEOUT, new SimpleHttpClient.IReceivedMessageCallback() {
261                 @Override
262                 public void onCompleted() {
263                     callback.onCompleted();
264                 }
265
266                 @Override
267                 public void onErrorOccurred(Exception e) {
268                     callback.onErrorOccurred(e);
269                 }
270
271                 @Override
272                 public void onReceive(int readBytes, int length, int size, byte[] data) {
273                     float percent = (length == 0) ? 0.0f : ((float) readBytes / (float) length);
274                     ProgressEvent event = new ProgressEvent(percent, null);
275                     callback.onProgress(data, size, event);
276                 }
277             });
278         }
279         catch (Throwable e)
280         {
281             e.printStackTrace();
282         }
283     }
284
285     /**
286      *   撮影時刻は(個別に)取れるが、非常に遅い...
287      *
288      */
289     private Date getCameraContentDate(@NonNull ICameraContent cameraContent)
290     {
291         // 各ファイルを個別に撮影時刻をとると、反応が悪すぎるので処理を抑止。
292 /*
293         String fileInfo;
294         try
295         {
296             String imageInfoUrl = "http://192.168.0.1/v1/photos/" + cameraContent.getContentPath() + "/" + cameraContent.getContentName() + "/info?storage=" + cameraContent.getCardId();
297             //Log.v(TAG, "getCameraContentDate() : " + imageInfoUrl);
298             fileInfo = SimpleHttpClient.httpGet(imageInfoUrl, DEFAULT_TIMEOUT);
299             if (fileInfo != null)
300             {
301                 String datetime = new JSONObject(fileInfo).getString("datetime");
302                 SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.US); // "yyyy-MM-dd'T'HH:mm:ssZ"
303                 dateFormatter.setCalendar(new GregorianCalendar());
304                 return (dateFormatter.parse(datetime));
305             }
306         }
307         catch (Exception e)
308         {
309             e.printStackTrace();
310         }
311 */
312         return (cameraContent.getCapturedDate());
313     }
314
315
316     /**
317      *   カメラ内画像ファイルの取得処理... GRコマンドが失敗したらPENTAXコマンドを使う。
318      *
319      */
320     @Override
321     public void getCameraContentList(ICameraContentListCallback callback)
322     {
323         try
324         {
325             if (useGrCommand)
326             {
327                 getGrCameraContentListImpl(callback);
328                 return;
329             }
330         }
331         catch (Exception e)
332         {
333             e.printStackTrace();
334         }
335         getCameraContentListImpl(callback);
336     }
337
338     /**
339      *   RICOH GR2用のカメラ内画像ファイル一覧取得処理
340      *   (エラー発生時には、通常のPENTAX用のカメラ内画像ファイル一覧取得処理を使う)
341      *
342      */
343     private void getGrCameraContentListImpl(ICameraContentListCallback callback)
344     {
345         List<ICameraContent> fileList = new ArrayList<>();
346         String imageListurl = "http://192.168.0.1/_gr/objs";
347         String contentList;
348
349         // try ~ catch でくくらない ... だめだったら PENTAXのシーケンスに入るようにしたいので
350         contentList = SimpleHttpClient.httpGet(imageListurl, DEFAULT_TIMEOUT);
351         if (contentList == null)
352         {
353             // ぬるぽ発行
354             throw (new NullPointerException());
355         }
356
357         try
358         {
359             String cameraId = statusChecker.getCameraId();
360             JSONArray dirsArray = new JSONObject(contentList).getJSONArray("dirs");
361             if (dirsArray != null)
362             {
363                 int size = dirsArray.length();
364                 for (int index = 0; index < size; index++)
365                 {
366                     JSONObject object = dirsArray.getJSONObject(index);
367                     String dirName = object.getString("name");
368                     JSONArray filesArray = object.getJSONArray("files");
369                     int nofFiles = filesArray.length();
370                     for (int fileIndex = 0; fileIndex < nofFiles; fileIndex++)
371                     {
372                         JSONObject fileObject = filesArray.getJSONObject(fileIndex);
373                         String fileName = fileObject.getString("n");
374                         String dateString = fileObject.getString("d");
375                         Date capturedDate = new Date(2001, 1, 1);
376                         if (dateString != null)
377                         {
378                             SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.US); // "yyyy-MM-dd'T'HH:mm:ssZ"
379                             dateFormatter.setCalendar(new GregorianCalendar());
380                             capturedDate = dateFormatter.parse(dateString);
381                         }
382                         ICameraContent cameraContent = new CameraContentInfo(cameraId, "sd1", dirName, fileName, capturedDate);
383                         fileList.add(cameraContent);
384                     }
385                 }
386             }
387         }
388         catch (Exception e)
389         {
390             // ぬるぽ発行
391            throw  (new NullPointerException());
392         }
393         callback.onCompleted(fileList);
394     }
395
396     private void getCameraContentListImpl(ICameraContentListCallback callback)
397     {
398         List<ICameraContent> fileList = new ArrayList<>();
399         String imageListurl = "http://192.168.0.1/v1/photos?limit=3000";
400         String contentList;
401         try
402         {
403             contentList = SimpleHttpClient.httpGet(imageListurl, DEFAULT_TIMEOUT);
404             if (contentList == null)
405             {
406                 // ぬるぽ発行
407                 callback.onErrorOccurred(new NullPointerException());
408                 return;
409             }
410         }
411         catch (Exception e)
412         {
413             // 例外をそのまま転送
414             callback.onErrorOccurred(e);
415             return;
416         }
417         try
418         {
419             Log.v(TAG, "PHOTO LIST RECV: [" + contentList.length() + "]");
420             String cameraId = statusChecker.getCameraId();
421             JSONArray dirsArray = new JSONObject(contentList).getJSONArray("dirs");
422             if (dirsArray != null)
423             {
424                 int size = dirsArray.length();
425                 Log.v(TAG, "DIRECTORIES : " + size);
426                 for (int index = 0; index < size; index++)
427                 {
428                     JSONObject object = dirsArray.getJSONObject(index);
429                     String dirName = object.getString("name");
430                     JSONArray filesArray = object.getJSONArray("files");
431                     int nofFiles = filesArray.length();
432                     Log.v(TAG, "FILES : [" + dirName + "] " + nofFiles);
433                     for (int fileIndex = 0; fileIndex < nofFiles; fileIndex++)
434                     {
435                         String fileName = filesArray.getString(fileIndex);
436                         Log.v(TAG, "FILE : " + fileName);
437                         ICameraContent cameraContent = new CameraContentInfo(cameraId, "sd1", dirName, fileName, new Date());
438                         cameraContent.setCapturedDate(getCameraContentDate(cameraContent));
439                         fileList.add(cameraContent);
440                     }
441                 }
442             }
443             else
444             {
445                 Log.v(TAG, "NOT FOUND dirs array.");
446             }
447         }
448         catch (Exception e)
449         {
450             e.printStackTrace();
451             try {
452                 fileList.clear();
453             }
454             catch (Exception ee)
455             {
456                 ee.printStackTrace();
457             }
458         }
459         callback.onCompleted(fileList);
460     }
461 }