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()
186 public void showPictureFinished()
191 private void getCameraContents(ICameraContentListCallback callback)
195 finishedCallback = callback;
196 ICameraStatus statusListHolder = provider.getCameraStatusListHolder();
197 if (statusListHolder != null) {
198 String count = statusListHolder.getStatus(IMAGE_FILE_COUNT_STR_ID);
199 nofFiles = Integer.parseInt(count);
200 Log.v(TAG, "getCameraContents() : " + nofFiles + " (" + count + ")");
202 Log.v(TAG, "getCameraContents() : DONE.");
205 // 件数ベースで取得する(情報は、後追いで反映させる...この方式だと、キューに積みまくってるが、、、)
206 checkImageFiles(nofFiles);
210 // 件数が不明だったら、1件づつインデックスの情報を取得する
217 finishedCallback.onErrorOccurred(e);
218 finishedCallback = null;
223 * 最初から取得可能なイメージ情報を(件数ベースで)取得する
226 private void checkImageFiles(int nofFiles)
230 imageContentInfo.clear();
231 //IFujiXCommandPublisher publisher = provider.getCommandPublisher();
232 //for (int index = nofFiles; index > 0; index--)
233 for (int index = 1; index <= nofFiles; index++)
236 imageContentInfo.append(index, new FujiXImageContentInfo(index, null));
238 //ファイル名などを取得する (メッセージを積んでおく...でも遅くなるので、ここではやらない方がよいかな。)
239 //publisher.enqueueCommand(new GetImageInfo(index, index, info));
242 // インデックスデータがなくなったことを検出...データがそろったとして応答する。
243 Log.v(TAG, "IMAGE LIST : " + imageContentInfo.size() + " (" + nofFiles + ")");
244 finishedCallback.onCompleted(getCameraFileInfoList());
245 finishedCallback = null;
254 * 最初から取得可能なイメージ情報をすべて取得する
257 private void checkImageFileAll()
261 imageContentInfo.clear();
263 IFujiXCommandPublisher publisher = provider.getCommandPublisher();
264 publisher.enqueueCommand(new GetImageInfo(indexNumber, indexNumber, this));
273 public void onReceiveProgress(int currentBytes, int totalBytes, byte[] body)
275 Log.v(TAG, " " + currentBytes + "/" + totalBytes);
279 public boolean isReceiveMulti()
285 public void receivedMessage(int id, byte[] rx_body)
287 // イメージ数の一覧が取得できなかった場合にここで作る。
288 if (rx_body.length < 16)
290 // インデックスデータがなくなったことを検出...データがそろったとして応答する。
291 Log.v(TAG, "IMAGE LIST : " + imageContentInfo.size());
292 finishedCallback.onCompleted(getCameraFileInfoList());
293 finishedCallback = null;
298 Log.v(TAG, "RECEIVED IMAGE INFO : " + indexNumber);
301 imageContentInfo.append(indexNumber, new FujiXImageContentInfo(indexNumber, rx_body));
305 IFujiXCommandPublisher publisher = provider.getCommandPublisher();
306 publisher.enqueueCommand(new GetImageInfo(indexNumber, indexNumber, this));
312 finishedCallback.onCompleted(getCameraFileInfoList());
313 finishedCallback = null;
317 private List<ICameraContent> getCameraContentList()
319 /// ダサいけど...コンテナクラスを詰め替えて応答する
320 List<ICameraContent> contentList = new ArrayList<>();
321 int listSize = imageContentInfo.size();
322 for(int index = 0; index < listSize; index++)
324 contentList.add(imageContentInfo.valueAt(index));
326 return (contentList);
329 private List<ICameraFileInfo> getCameraFileInfoList()
331 List<ICameraFileInfo> fileInfoList = new ArrayList<>();
334 int listSize = imageContentInfo.size();
335 for(int index = 0; index < listSize; index++)
337 FujiXImageContentInfo info = imageContentInfo.valueAt(index);
338 fileInfoList.add(info.getCameraFileInfo());
345 return (fileInfoList);