1 package net.osdn.gokigen.gr2control.camera.fuji_x.wrapper.playback;
3 import android.app.Activity;
4 import android.util.Log;
5 import android.util.SparseArray;
7 import androidx.annotation.NonNull;
8 import androidx.annotation.Nullable;
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;
25 import java.util.ArrayList;
26 import java.util.List;
28 import static net.osdn.gokigen.gr2control.camera.fuji_x.wrapper.values.IFujiXCameraProperties.IMAGE_FILE_COUNT_STR_ID;
30 public class FujiXPlaybackControl implements IPlaybackControl, IFujiXCommandCallback
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;
38 private int indexNumber = 0;
39 private ICameraContentListCallback finishedCallback = null;
41 public FujiXPlaybackControl(Activity activity, FujiXInterfaceProvider provider)
43 this.activity = activity;
44 this.provider = provider;
45 this.imageContentInfo = new SparseArray<>();
49 public String getRawFileSuffix() {
54 public void downloadContentList(@NonNull final ICameraContentListCallback callback)
58 Thread thread = new Thread(new Runnable() {
61 getCameraContents(callback);
69 callback.onErrorOccurred(e);
74 public void getContentInfo(@Nullable String path, @NonNull String name, @NonNull IContentInfoCallback callback)
76 // showFileInformation
81 public void updateCameraFileInfo(ICameraFileInfo info)
84 Log.v(TAG, " updateCameraFileInfo() : " + info.getDatetime());
88 public void downloadContentScreennail(@Nullable String path, @NonNull String name, @NonNull IDownloadThumbnailImageCallback callback)
90 // Thumbnail と同じ画像を表示する
91 downloadContentThumbnail(path, name, callback);
95 public void downloadContentThumbnail(@Nullable String path, @NonNull String name, @NonNull IDownloadThumbnailImageCallback callback)
100 if (name.indexOf("/") == 0)
104 Log.v(TAG, "downloadContentThumbnail() : " + path + " " + name);
105 int index = getIndexNumber(start, name);
106 if ((index > 0)&&(index <= imageContentInfo.size()))
108 IFujiXCommandPublisher publisher = provider.getCommandPublisher();
109 FujiXImageContentInfo contentInfo = imageContentInfo.get(index);
110 if (!contentInfo.isReceived())
112 publisher.enqueueCommand(new GetImageInfo(index, index, contentInfo));
114 publisher.enqueueCommand(new GetThumbNail(index, new FujiXThumbnailImageReceiver(activity, callback)));
124 public void downloadContent(@Nullable String path, @NonNull String name, boolean isSmallSize, @NonNull IDownloadContentCallback callback)
129 if (name.indexOf("/") == 0)
133 int index = getIndexNumber(start, name);
134 Log.v(TAG, " FujiX::downloadContent() : " + path + " " + name + " " + index);
135 if ((index > 0)&&(index <= imageContentInfo.size()))
137 IFujiXCommandPublisher publisher = provider.getCommandPublisher();
138 publisher.enqueueCommand(new GetFullImage(index, new FujiXFullImageReceiver(callback)));
147 private int getIndexNumber(int start, @NonNull String name)
149 String indexStr = name.substring(start, name.indexOf("."));
153 indexNo = Integer.parseInt(indexStr);
157 //e.printStackTrace();
163 indexStr = name.substring(start);
164 int size = imageContentInfo.size();
165 for (int index = 0; index < size; index++)
167 FujiXImageContentInfo info = imageContentInfo.valueAt(index);
168 String contentName = info.getOriginalName();
169 if (indexStr.matches(contentName))
171 return (info.getId());
173 Log.v(TAG, " contentName : " + contentName);
175 Log.v(TAG, "index is not found : " + name + " " + indexStr);
180 public void showPictureStarted()
184 Log.v(TAG, " showPictureStarted() ");
186 IFujiXCommandPublisher publisher = provider.getCommandPublisher();
187 publisher.flushHoldQueue();
197 public void showPictureFinished()
201 Log.v(TAG, " showPictureFinished() ");
203 IFujiXCommandPublisher publisher = provider.getCommandPublisher();
204 publisher.flushHoldQueue();
213 private void getCameraContents(ICameraContentListCallback callback)
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 + ")");
224 Log.v(TAG, "getCameraContents() : DONE.");
227 // 件数ベースで取得する(情報は、後追いで反映させる...この方式だと、キューに積みまくってるが、、、)
228 checkImageFiles(nofFiles);
232 // 件数が不明だったら、1件づつインデックスの情報を取得する
239 finishedCallback.onErrorOccurred(e);
240 finishedCallback = null;
245 * 最初から取得可能なイメージ情報を(件数ベースで)取得する
248 private void checkImageFiles(int nofFiles)
252 imageContentInfo.clear();
253 //IFujiXCommandPublisher publisher = provider.getCommandPublisher();
254 //for (int index = nofFiles; index > 0; index--)
255 for (int index = 1; index <= nofFiles; index++)
258 imageContentInfo.append(index, new FujiXImageContentInfo(index, null));
260 //ファイル名などを取得する (メッセージを積んでおく...でも遅くなるので、ここではやらない方がよいかな。)
261 //publisher.enqueueCommand(new GetImageInfo(index, index, info));
264 // インデックスデータがなくなったことを検出...データがそろったとして応答する。
265 Log.v(TAG, "IMAGE LIST : " + imageContentInfo.size() + " (" + nofFiles + ")");
266 finishedCallback.onCompleted(getCameraFileInfoList());
267 finishedCallback = null;
276 * 最初から取得可能なイメージ情報をすべて取得する
279 private void checkImageFileAll()
283 imageContentInfo.clear();
285 IFujiXCommandPublisher publisher = provider.getCommandPublisher();
286 publisher.enqueueCommand(new GetImageInfo(indexNumber, indexNumber, this));
295 public void onReceiveProgress(int currentBytes, int totalBytes, byte[] body)
297 Log.v(TAG, " " + currentBytes + "/" + totalBytes);
301 public boolean isReceiveMulti()
307 public void receivedMessage(int id, byte[] rx_body)
309 // イメージ数の一覧が取得できなかった場合にここで作る。
310 if (rx_body.length < 16)
312 // インデックスデータがなくなったことを検出...データがそろったとして応答する。
313 Log.v(TAG, "IMAGE LIST : " + imageContentInfo.size());
314 finishedCallback.onCompleted(getCameraFileInfoList());
315 finishedCallback = null;
320 Log.v(TAG, "RECEIVED IMAGE INFO : " + indexNumber);
323 imageContentInfo.append(indexNumber, new FujiXImageContentInfo(indexNumber, rx_body));
327 IFujiXCommandPublisher publisher = provider.getCommandPublisher();
328 publisher.enqueueCommand(new GetImageInfo(indexNumber, indexNumber, this));
334 finishedCallback.onCompleted(getCameraFileInfoList());
335 finishedCallback = null;
339 private List<ICameraContent> getCameraContentList()
341 /// ダサいけど...コンテナクラスを詰め替えて応答する
342 List<ICameraContent> contentList = new ArrayList<>();
343 int listSize = imageContentInfo.size();
344 for(int index = 0; index < listSize; index++)
346 contentList.add(imageContentInfo.valueAt(index));
348 return (contentList);
351 private List<ICameraFileInfo> getCameraFileInfoList()
353 List<ICameraFileInfo> fileInfoList = new ArrayList<>();
356 int listSize = imageContentInfo.size();
357 for(int index = 0; index < listSize; index++)
359 FujiXImageContentInfo info = imageContentInfo.valueAt(index);
360 fileInfoList.add(info.getCameraFileInfo());
367 return (fileInfoList);