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)
87 public void downloadContentScreennail(@Nullable String path, @NonNull String name, @NonNull IDownloadThumbnailImageCallback callback)
89 // Thumbnail と同じ画像を表示する
90 downloadContentThumbnail(path, name, callback);
94 public void downloadContentThumbnail(@Nullable String path, @NonNull String name, @NonNull IDownloadThumbnailImageCallback callback)
99 if (name.indexOf("/") == 0)
103 Log.v(TAG, "downloadContentThumbnail() : " + path + " " + name);
104 int index = getIndexNumber(start, name);
105 if ((index > 0)&&(index <= imageContentInfo.size()))
107 IFujiXCommandPublisher publisher = provider.getCommandPublisher();
108 FujiXImageContentInfo contentInfo = imageContentInfo.get(index);
109 if (!contentInfo.isReceived())
111 publisher.enqueueCommand(new GetImageInfo(index, index, contentInfo));
113 publisher.enqueueCommand(new GetThumbNail(index, new FujiXThumbnailImageReceiver(activity, callback)));
123 public void downloadContent(@Nullable String path, @NonNull String name, boolean isSmallSize, @NonNull IDownloadContentCallback callback)
128 if (name.indexOf("/") == 0)
132 String indexStr = name.substring(start, name.indexOf("."));
133 Log.v(TAG, "FujiX::downloadContent() : " + path + " " + name + " " + indexStr);
134 int index = Integer.parseInt(indexStr);
135 //FujiXImageContentInfo contentInfo = imageContentInfo.get(index); // 特にデータを更新しないから大丈夫か?
136 if ((index > 0)&&(index <= imageContentInfo.size()))
138 IFujiXCommandPublisher publisher = provider.getCommandPublisher();
139 publisher.enqueueCommand(new GetFullImage(index, new FujiXFullImageReceiver(callback)));
148 private int getIndexNumber(int start, @NonNull String name)
150 String indexStr = name.substring(start, name.indexOf("."));
154 indexNo = Integer.parseInt(indexStr);
158 //e.printStackTrace();
164 indexStr = name.substring(start);
165 int size = imageContentInfo.size();
166 for (int index = 0; index < size; index++)
168 FujiXImageContentInfo info = imageContentInfo.valueAt(index);
169 String contentName = info.getOriginalName();
170 if (indexStr.matches(contentName))
172 return (info.getId());
174 Log.v(TAG, " contentName : " + contentName);
176 Log.v(TAG, "index is not found : " + name + " " + indexStr);
181 public void showPictureStarted()
187 public void showPictureFinished()
192 private void getCameraContents(ICameraContentListCallback callback)
196 finishedCallback = callback;
197 ICameraStatus statusListHolder = provider.getCameraStatusListHolder();
198 if (statusListHolder != null) {
199 String count = statusListHolder.getStatus(IMAGE_FILE_COUNT_STR_ID);
200 nofFiles = Integer.parseInt(count);
201 Log.v(TAG, "getCameraContents() : " + nofFiles + " (" + count + ")");
203 Log.v(TAG, "getCameraContents() : DONE.");
206 // 件数ベースで取得する(情報は、後追いで反映させる...この方式だと、キューに積みまくってるが、、、)
207 checkImageFiles(nofFiles);
211 // 件数が不明だったら、1件づつインデックスの情報を取得する
218 finishedCallback.onErrorOccurred(e);
219 finishedCallback = null;
224 * 最初から取得可能なイメージ情報を(件数ベースで)取得する
227 private void checkImageFiles(int nofFiles)
231 imageContentInfo.clear();
232 //IFujiXCommandPublisher publisher = provider.getCommandPublisher();
233 //for (int index = nofFiles; index > 0; index--)
234 for (int index = 1; index <= nofFiles; index++)
237 imageContentInfo.append(index, new FujiXImageContentInfo(index, null));
239 //ファイル名などを取得する (メッセージを積んでおく...でも遅くなるので、ここではやらない方がよいかな。)
240 //publisher.enqueueCommand(new GetImageInfo(index, index, info));
243 // インデックスデータがなくなったことを検出...データがそろったとして応答する。
244 Log.v(TAG, "IMAGE LIST : " + imageContentInfo.size() + " (" + nofFiles + ")");
245 finishedCallback.onCompleted(getCameraFileInfoList());
246 finishedCallback = null;
255 * 最初から取得可能なイメージ情報をすべて取得する
258 private void checkImageFileAll()
262 imageContentInfo.clear();
264 IFujiXCommandPublisher publisher = provider.getCommandPublisher();
265 publisher.enqueueCommand(new GetImageInfo(indexNumber, indexNumber, this));
274 public void onReceiveProgress(int currentBytes, int totalBytes, byte[] body)
276 Log.v(TAG, " " + currentBytes + "/" + totalBytes);
280 public boolean isReceiveMulti()
286 public void receivedMessage(int id, byte[] rx_body)
288 // イメージ数の一覧が取得できなかった場合にここで作る。
289 if (rx_body.length < 16)
291 // インデックスデータがなくなったことを検出...データがそろったとして応答する。
292 Log.v(TAG, "IMAGE LIST : " + imageContentInfo.size());
293 finishedCallback.onCompleted(getCameraFileInfoList());
294 finishedCallback = null;
299 Log.v(TAG, "RECEIVED IMAGE INFO : " + indexNumber);
302 imageContentInfo.append(indexNumber, new FujiXImageContentInfo(indexNumber, rx_body));
306 IFujiXCommandPublisher publisher = provider.getCommandPublisher();
307 publisher.enqueueCommand(new GetImageInfo(indexNumber, indexNumber, this));
313 finishedCallback.onCompleted(getCameraFileInfoList());
314 finishedCallback = null;
318 private List<ICameraContent> getCameraContentList()
320 /// ダサいけど...コンテナクラスを詰め替えて応答する
321 List<ICameraContent> contentList = new ArrayList<>();
322 int listSize = imageContentInfo.size();
323 for(int index = 0; index < listSize; index++)
325 contentList.add(imageContentInfo.valueAt(index));
327 return (contentList);
330 private List<ICameraFileInfo> getCameraFileInfoList()
332 List<ICameraFileInfo> fileInfoList = new ArrayList<>();
335 int listSize = imageContentInfo.size();
336 for(int index = 0; index < listSize; index++)
338 FujiXImageContentInfo info = imageContentInfo.valueAt(index);
339 fileInfoList.add(info.getCameraFileInfo());
346 return (fileInfoList);