OSDN Git Service

とりあえず、1回だけ切り替えられるようになった。
[gokigen/Gr2Control.git] / app / src / main / java / net / osdn / gokigen / gr2control / camera / fuji_x / wrapper / playback / FujiXPlaybackControl.java
1 package net.osdn.gokigen.gr2control.camera.fuji_x.wrapper.playback;
2
3 import android.app.Activity;
4 import android.util.Log;
5 import android.util.SparseArray;
6
7 import androidx.annotation.NonNull;
8 import androidx.annotation.Nullable;
9
10 import net.osdn.gokigen.gr2control.camera.ICameraFileInfo;
11 import net.osdn.gokigen.gr2control.camera.ICameraStatus;
12 import net.osdn.gokigen.gr2control.camera.fuji_x.wrapper.FujiXInterfaceProvider;
13 import net.osdn.gokigen.gr2control.camera.fuji_x.wrapper.command.IFujiXCommandCallback;
14 import net.osdn.gokigen.gr2control.camera.fuji_x.wrapper.command.IFujiXCommandPublisher;
15 import net.osdn.gokigen.gr2control.camera.fuji_x.wrapper.command.messages.GetFullImage;
16 import net.osdn.gokigen.gr2control.camera.fuji_x.wrapper.command.messages.GetImageInfo;
17 import net.osdn.gokigen.gr2control.camera.fuji_x.wrapper.command.messages.GetThumbNail;
18 import net.osdn.gokigen.gr2control.camera.playback.ICameraContent;
19 import net.osdn.gokigen.gr2control.camera.playback.IContentInfoCallback;
20 import net.osdn.gokigen.gr2control.camera.playback.IDownloadContentCallback;
21 import net.osdn.gokigen.gr2control.camera.playback.ICameraContentListCallback;
22 import net.osdn.gokigen.gr2control.camera.playback.IDownloadThumbnailImageCallback;
23 import net.osdn.gokigen.gr2control.camera.playback.IPlaybackControl;
24
25 import java.util.ArrayList;
26 import java.util.List;
27
28 import static net.osdn.gokigen.gr2control.camera.fuji_x.wrapper.values.IFujiXCameraProperties.IMAGE_FILE_COUNT_STR_ID;
29
30 public class FujiXPlaybackControl implements IPlaybackControl, IFujiXCommandCallback
31 {
32     private final String TAG = toString();
33     private final Activity activity;
34     private final FujiXInterfaceProvider provider;
35     //private List<ICameraContent> imageInfo;
36     private SparseArray<FujiXImageContentInfo> imageContentInfo;
37
38     private int indexNumber = 0;
39     private ICameraContentListCallback finishedCallback = null;
40
41     public FujiXPlaybackControl(Activity activity, FujiXInterfaceProvider provider)
42     {
43         this.activity = activity;
44         this.provider = provider;
45         this.imageContentInfo = new SparseArray<>();
46     }
47
48     @Override
49     public String getRawFileSuffix() {
50         return (null);
51     }
52
53     @Override
54     public void downloadContentList(@NonNull final ICameraContentListCallback callback)
55     {
56         try
57         {
58             Thread thread = new Thread(new Runnable() {
59                 @Override
60                 public void run() {
61                     getCameraContents(callback);
62                 }
63             });
64             thread.start();
65         }
66         catch (Exception e)
67         {
68             e.printStackTrace();
69             callback.onErrorOccurred(e);
70         }
71     }
72
73     @Override
74     public void getContentInfo(@Nullable String path, @NonNull String name, @NonNull IContentInfoCallback callback)
75     {
76         // showFileInformation
77
78     }
79
80     @Override
81     public void updateCameraFileInfo(ICameraFileInfo info)
82     {
83         //  なにもしない
84         Log.v(TAG, " updateCameraFileInfo() : " + info.getDatetime());
85     }
86
87     @Override
88     public void downloadContentScreennail(@Nullable String path, @NonNull String name, @NonNull IDownloadThumbnailImageCallback callback)
89     {
90         // Thumbnail と同じ画像を表示する
91         downloadContentThumbnail(path, name, callback);
92     }
93
94     @Override
95     public void downloadContentThumbnail(@Nullable String path, @NonNull String name, @NonNull IDownloadThumbnailImageCallback callback)
96     {
97         try
98         {
99             int start = 0;
100             if (name.indexOf("/") == 0)
101             {
102                 start = 1;
103             }
104             Log.v(TAG, "downloadContentThumbnail() : " + path + " " + name);
105             int index = getIndexNumber(start, name);
106             if ((index > 0)&&(index <= imageContentInfo.size()))
107             {
108                 IFujiXCommandPublisher publisher = provider.getCommandPublisher();
109                 FujiXImageContentInfo contentInfo = imageContentInfo.get(index);
110                 if (!contentInfo.isReceived())
111                 {
112                     publisher.enqueueCommand(new GetImageInfo(index, index, contentInfo));
113                 }
114                 publisher.enqueueCommand(new GetThumbNail(index, new FujiXThumbnailImageReceiver(activity, callback)));
115             }
116         }
117         catch (Exception e)
118         {
119             e.printStackTrace();
120         }
121     }
122
123     @Override
124     public void downloadContent(@Nullable String path, @NonNull String name, boolean isSmallSize, @NonNull IDownloadContentCallback callback)
125     {
126         try
127         {
128             int start = 0;
129             if (name.indexOf("/") == 0)
130             {
131                 start = 1;
132             }
133             int index = getIndexNumber(start, name);
134             Log.v(TAG, "  FujiX::downloadContent() : " + path + " " + name + " " + index);
135             if ((index > 0)&&(index <= imageContentInfo.size()))
136             {
137                 IFujiXCommandPublisher publisher = provider.getCommandPublisher();
138                 publisher.enqueueCommand(new GetFullImage(index, new FujiXFullImageReceiver(callback)));
139             }
140         }
141         catch (Exception e)
142         {
143             e.printStackTrace();
144         }
145     }
146
147     private int getIndexNumber(int start, @NonNull String name)
148     {
149         String indexStr = name.substring(start, name.indexOf("."));
150         int indexNo = -1;
151         try
152         {
153             indexNo = Integer.parseInt(indexStr);
154         }
155         catch (Exception e)
156         {
157             //e.printStackTrace();
158         }
159         if (indexNo >= 0)
160         {
161             return (indexNo);
162         }
163         indexStr = name.substring(start);
164         int size = imageContentInfo.size();
165         for (int index = 0; index < size; index++)
166         {
167             FujiXImageContentInfo info = imageContentInfo.valueAt(index);
168             String contentName = info.getOriginalName();
169             if (indexStr.matches(contentName))
170             {
171                 return (info.getId());
172             }
173             Log.v(TAG, " contentName : " + contentName);
174         }
175         Log.v(TAG, "index is not found : " + name + " " + indexStr);
176         return (-1);
177     }
178
179     @Override
180     public void showPictureStarted()
181     {
182         try
183         {
184             Log.v(TAG, "   showPictureStarted() ");
185
186             IFujiXCommandPublisher publisher = provider.getCommandPublisher();
187             publisher.flushHoldQueue();
188             System.gc();
189         }
190         catch (Exception e)
191         {
192             e.printStackTrace();
193         }
194     }
195
196     @Override
197     public void showPictureFinished()
198     {
199         try
200         {
201             Log.v(TAG, "   showPictureFinished() ");
202
203             IFujiXCommandPublisher publisher = provider.getCommandPublisher();
204             publisher.flushHoldQueue();
205             System.gc();
206         }
207         catch (Exception e)
208         {
209             e.printStackTrace();
210         }
211     }
212
213     private void getCameraContents(ICameraContentListCallback callback)
214     {
215         int nofFiles = -1;
216         try {
217             finishedCallback = callback;
218             ICameraStatus statusListHolder = provider.getCameraStatusListHolder();
219             if (statusListHolder != null) {
220                 String count = statusListHolder.getStatus(IMAGE_FILE_COUNT_STR_ID);
221                 nofFiles = Integer.parseInt(count);
222                 Log.v(TAG, "getCameraContents() : " + nofFiles + " (" + count + ")");
223             }
224             Log.v(TAG, "getCameraContents() : DONE.");
225             if (nofFiles > 0)
226             {
227                 // 件数ベースで取得する(情報は、後追いで反映させる...この方式だと、キューに積みまくってるが、、、)
228                 checkImageFiles(nofFiles);
229             }
230             else
231             {
232                 // 件数が不明だったら、1件づつインデックスの情報を取得する
233                 checkImageFileAll();
234             }
235         }
236         catch (Exception e)
237         {
238             e.printStackTrace();
239             finishedCallback.onErrorOccurred(e);
240             finishedCallback = null;
241         }
242     }
243
244     /**
245      *   最初から取得可能なイメージ情報を(件数ベースで)取得する
246      *
247      */
248     private void checkImageFiles(int nofFiles)
249     {
250         try
251         {
252             imageContentInfo.clear();
253             //IFujiXCommandPublisher publisher = provider.getCommandPublisher();
254             //for (int index = nofFiles; index > 0; index--)
255             for (int index = 1; index <= nofFiles; index++)
256             {
257                 // ファイル数分、仮のデータを生成する
258                 imageContentInfo.append(index, new FujiXImageContentInfo(index, null));
259
260                 //ファイル名などを取得する (メッセージを積んでおく...でも遅くなるので、ここではやらない方がよいかな。)
261                 //publisher.enqueueCommand(new GetImageInfo(index, index, info));
262             }
263
264             // インデックスデータがなくなったことを検出...データがそろったとして応答する。
265             Log.v(TAG, "IMAGE LIST : " + imageContentInfo.size() + " (" + nofFiles + ")");
266             finishedCallback.onCompleted(getCameraFileInfoList());
267             finishedCallback = null;
268         }
269         catch (Exception e)
270         {
271             e.printStackTrace();
272         }
273     }
274
275     /**
276      *   最初から取得可能なイメージ情報をすべて取得する
277      *
278      */
279     private void checkImageFileAll()
280     {
281         try
282         {
283             imageContentInfo.clear();
284             indexNumber = 1;
285             IFujiXCommandPublisher publisher = provider.getCommandPublisher();
286             publisher.enqueueCommand(new GetImageInfo(indexNumber, indexNumber, this));
287         }
288         catch (Exception e)
289         {
290             e.printStackTrace();
291         }
292     }
293
294     @Override
295     public void onReceiveProgress(int currentBytes, int totalBytes, byte[] body)
296     {
297         Log.v(TAG, " " + currentBytes + "/" + totalBytes);
298     }
299
300     @Override
301     public boolean isReceiveMulti()
302     {
303         return (false);
304     }
305
306     @Override
307     public void receivedMessage(int id, byte[] rx_body)
308     {
309         // イメージ数の一覧が取得できなかった場合にここで作る。
310         if (rx_body.length < 16)
311         {
312             // インデックスデータがなくなったことを検出...データがそろったとして応答する。
313             Log.v(TAG, "IMAGE LIST : " + imageContentInfo.size());
314             finishedCallback.onCompleted(getCameraFileInfoList());
315             finishedCallback = null;
316             return;
317         }
318         try
319         {
320             Log.v(TAG, "RECEIVED IMAGE INFO : " + indexNumber);
321
322             // 受信データを保管しておく
323             imageContentInfo.append(indexNumber, new FujiXImageContentInfo(indexNumber, rx_body));
324
325             // 次のインデックスの情報を要求する
326             indexNumber++;
327             IFujiXCommandPublisher publisher = provider.getCommandPublisher();
328             publisher.enqueueCommand(new GetImageInfo(indexNumber, indexNumber, this));
329         }
330         catch (Exception e)
331         {
332             // エラーになったら、そこで終了にする
333             e.printStackTrace();
334             finishedCallback.onCompleted(getCameraFileInfoList());
335             finishedCallback = null;
336         }
337     }
338
339     private List<ICameraContent> getCameraContentList()
340     {
341         /// ダサいけど...コンテナクラスを詰め替えて応答する
342         List<ICameraContent> contentList = new ArrayList<>();
343         int listSize = imageContentInfo.size();
344         for(int index = 0; index < listSize; index++)
345         {
346             contentList.add(imageContentInfo.valueAt(index));
347         }
348         return (contentList);
349     }
350
351     private List<ICameraFileInfo> getCameraFileInfoList()
352     {
353         List<ICameraFileInfo> fileInfoList = new ArrayList<>();
354         try
355         {
356             int listSize = imageContentInfo.size();
357             for(int index = 0; index < listSize; index++)
358             {
359                 FujiXImageContentInfo info = imageContentInfo.valueAt(index);
360                 fileInfoList.add(info.getCameraFileInfo());
361             }
362         }
363         catch (Exception e)
364         {
365             e.printStackTrace();
366         }
367         return (fileInfoList);
368     }
369
370 }