OSDN Git Service

最初に表示するフラグメントを変更した。
[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 = 5000;
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 IContentInfoCallback callback)
168     {
169         String url = getPhotoUrl + path + "/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         }
179         catch (Throwable e)
180         {
181             e.printStackTrace();
182         }
183     }
184
185     @Override
186     public void downloadContentScreennail(@NonNull String path, @NonNull IDownloadThumbnailImageCallback callback)
187     {
188         //Log.v(TAG, "downloadContentScreennail() : " + path);
189         String suffix = "?size=view";
190         String url = getPhotoUrl + path + suffix;
191         Log.v(TAG, "downloadContentScreennail() GET URL : " + url);
192         try
193         {
194             Bitmap bmp = SimpleHttpClient.httpGetBitmap(url, DEFAULT_TIMEOUT);
195             HashMap<String, Object> map = new HashMap<>();
196             map.put("Orientation", 0);
197             callback.onCompleted(bmp, map);
198         }
199         catch (Throwable e)
200         {
201             e.printStackTrace();
202         }
203     }
204
205     @Override
206     public void downloadContentThumbnail(@NonNull String path, @NonNull IDownloadThumbnailImageCallback callback)
207     {
208         //Log.v(TAG, "downloadContentThumbnail() : " + path);
209         String suffix = "?size=view";
210         if (path.contains(".JPG"))
211         {
212             suffix = "?size=thumb";
213         }
214         String url = getPhotoUrl + path + suffix;
215         Log.v(TAG, "downloadContentThumbnail() GET URL : " + url);
216         try
217         {
218             Bitmap bmp = SimpleHttpClient.httpGetBitmap(url, DEFAULT_TIMEOUT);
219             HashMap<String, Object> map = new HashMap<>();
220             map.put("Orientation", 0);
221             callback.onCompleted(bmp, map);
222         }
223         catch (Throwable e)
224         {
225             e.printStackTrace();
226             callback.onErrorOccurred(new NullPointerException());
227         }
228    }
229
230     @Override
231     public void downloadContent(@NonNull String  path, boolean isSmallSize, @NonNull final IDownloadContentCallback callback)
232     {
233         Log.v(TAG, "downloadContent() : " + path);
234         String suffix = "?size=full";
235         if (isSmallSize)
236         {
237             suffix = "?size=view";
238         }
239         String url = getPhotoUrl + path + suffix;
240         Log.v(TAG, "downloadContent() GET URL : " + url);
241         try
242         {
243             SimpleHttpClient.httpGetBytes(url, DEFAULT_TIMEOUT, new SimpleHttpClient.IReceivedMessageCallback() {
244                 @Override
245                 public void onCompleted() {
246                     callback.onCompleted();
247                 }
248
249                 @Override
250                 public void onErrorOccurred(Exception e) {
251                     callback.onErrorOccurred(e);
252                 }
253
254                 @Override
255                 public void onReceive(int readBytes, int length, int size, byte[] data) {
256                     float percent = (length == 0) ? 0.0f : ((float) readBytes / (float) length);
257                     ProgressEvent event = new ProgressEvent(percent, null);
258                     callback.onProgress(data, size, event);
259                 }
260             });
261         }
262         catch (Throwable e)
263         {
264             e.printStackTrace();
265         }
266     }
267
268     /**
269      *   撮影時刻は(個別に)取れるが、非常に遅い...
270      *
271      */
272     private Date getCameraContentDate(@NonNull ICameraContent cameraContent)
273     {
274 /**/
275         String fileInfo;
276         try
277         {
278             String imageInfoUrl = "http://192.168.0.1/v1/photos/" + cameraContent.getContentPath() + "/" + cameraContent.getContentName() + "/info?storage=" + cameraContent.getCardId();
279             Log.v(TAG, "getCameraContentDate() : " + imageInfoUrl);
280             fileInfo = SimpleHttpClient.httpGet(imageInfoUrl, DEFAULT_TIMEOUT);
281             if (fileInfo != null)
282             {
283                 String datetime = new JSONObject(fileInfo).getString("datetime");
284                 SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.US); // "yyyy-MM-dd'T'HH:mm:ssZ"
285                 dateFormatter.setCalendar(new GregorianCalendar());
286                 return (dateFormatter.parse(datetime));
287             }
288         }
289         catch (Exception e)
290         {
291             e.printStackTrace();
292         }
293 /**/
294         return (cameraContent.getCapturedDate());
295     }
296
297
298     /**
299      *   カメラ内画像ファイルの取得処理... GRコマンドが失敗したらPENTAXコマンドを使う。
300      *
301      */
302     @Override
303     public void getCameraContentList(ICameraContentListCallback callback)
304     {
305         try
306         {
307             if (useGrCommand)
308             {
309                 getGrCameraContentListImpl(callback);
310                 return;
311             }
312         }
313         catch (Exception e)
314         {
315             e.printStackTrace();
316         }
317         getCameraContentListImpl(callback);
318     }
319
320     /**
321      *   RICOH GR2用のカメラ内画像ファイル一覧取得処理
322      *   (エラー発生時には、通常のPENTAX用のカメラ内画像ファイル一覧取得処理を使う)
323      *
324      */
325     private void getGrCameraContentListImpl(ICameraContentListCallback callback)
326     {
327         List<ICameraContent> fileList = new ArrayList<>();
328         String imageListurl = "http://192.168.0.1/_gr/objs";
329         String contentList;
330
331         // try ~ catch でくくらない ... だめだったら PENTAXのシーケンスに入るようにしたいので
332         contentList = SimpleHttpClient.httpGet(imageListurl, DEFAULT_TIMEOUT);
333         if (contentList == null)
334         {
335             // ぬるぽ発行
336             throw (new NullPointerException());
337         }
338
339         try
340         {
341             String cameraId = statusChecker.getCameraId();
342             JSONArray dirsArray = new JSONObject(contentList).getJSONArray("dirs");
343             if (dirsArray != null)
344             {
345                 int size = dirsArray.length();
346                 for (int index = 0; index < size; index++)
347                 {
348                     JSONObject object = dirsArray.getJSONObject(index);
349                     String dirName = object.getString("name");
350                     JSONArray filesArray = object.getJSONArray("files");
351                     int nofFiles = filesArray.length();
352                     for (int fileIndex = 0; fileIndex < nofFiles; fileIndex++)
353                     {
354                         JSONObject fileObject = filesArray.getJSONObject(fileIndex);
355                         String fileName = fileObject.getString("n");
356                         String dateString = fileObject.getString("d");
357                         Date capturedDate = new Date();
358                         if (dateString != null)
359                         {
360                             SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.US); // "yyyy-MM-dd'T'HH:mm:ssZ"
361                             dateFormatter.setCalendar(new GregorianCalendar());
362                             capturedDate = dateFormatter.parse(dateString);
363                         }
364                         ICameraContent cameraContent = new CameraContentInfo(cameraId, "sd1", dirName, fileName, capturedDate);
365                         fileList.add(cameraContent);
366                     }
367                 }
368             }
369         }
370         catch (Exception e)
371         {
372             // ぬるぽ発行
373            throw  (new NullPointerException());
374         }
375         callback.onCompleted(fileList);
376     }
377
378     private void getCameraContentListImpl(ICameraContentListCallback callback)
379     {
380         List<ICameraContent> fileList = new ArrayList<>();
381         String imageListurl = "http://192.168.0.1/v1/photos?limit=3000";
382         String contentList;
383         try
384         {
385             contentList = SimpleHttpClient.httpGet(imageListurl, DEFAULT_TIMEOUT);
386             if (contentList == null)
387             {
388                 // ぬるぽ発行
389                 callback.onErrorOccurred(new NullPointerException());
390                 return;
391             }
392         }
393         catch (Exception e)
394         {
395             // 例外をそのまま転送
396             callback.onErrorOccurred(e);
397             return;
398         }
399         try
400         {
401             String cameraId = statusChecker.getCameraId();
402             JSONArray dirsArray = new JSONObject(contentList).getJSONArray("dirs");
403             if (dirsArray != null)
404             {
405                 int size = dirsArray.length();
406                 for (int index = 0; index < size; index++)
407                 {
408                     JSONObject object = dirsArray.getJSONObject(index);
409                     String dirName = object.getString("name");
410                     JSONArray filesArray = object.getJSONArray("files");
411                     int nofFiles = filesArray.length();
412                     for (int fileIndex = 0; fileIndex < nofFiles; fileIndex++)
413                     {
414                         String fileName = filesArray.getString(fileIndex);
415                         ICameraContent cameraContent = new CameraContentInfo(cameraId, "sd1", dirName, fileName, new Date());
416                         cameraContent.setCapturedDate(getCameraContentDate(cameraContent));
417                         fileList.add(cameraContent);
418                     }
419                 }
420             }
421         }
422         catch (Exception e)
423         {
424             //callback.onErrorOccurred(e);
425             //return;
426             e.printStackTrace();
427             try {
428                 fileList.clear();
429             }
430             catch (Exception ee)
431             {
432                 ee.printStackTrace();
433             }
434         }
435         callback.onCompleted(fileList);
436     }
437 }