1 package net.osdn.gokigen.gr2control.camera.fuji_x.wrapper.playback;
3 import android.app.Activity;
4 import android.content.SharedPreferences;
5 import android.util.Log;
6 import android.util.SparseArray;
8 import androidx.annotation.NonNull;
9 import androidx.annotation.Nullable;
10 import androidx.preference.PreferenceManager;
12 import net.osdn.gokigen.gr2control.camera.ICameraFileInfo;
13 import net.osdn.gokigen.gr2control.camera.ICameraStatus;
14 import net.osdn.gokigen.gr2control.camera.fuji_x.wrapper.FujiXInterfaceProvider;
15 import net.osdn.gokigen.gr2control.camera.fuji_x.wrapper.command.IFujiXCommandCallback;
16 import net.osdn.gokigen.gr2control.camera.fuji_x.wrapper.command.IFujiXCommandPublisher;
17 import net.osdn.gokigen.gr2control.camera.fuji_x.wrapper.command.messages.GetFullImage;
18 import net.osdn.gokigen.gr2control.camera.fuji_x.wrapper.command.messages.GetImageInfo;
19 import net.osdn.gokigen.gr2control.camera.fuji_x.wrapper.command.messages.GetScreenNail;
20 import net.osdn.gokigen.gr2control.camera.fuji_x.wrapper.command.messages.GetThumbNail;
21 import net.osdn.gokigen.gr2control.camera.fuji_x.wrapper.command.messages.SetPropertyValue;
22 import net.osdn.gokigen.gr2control.camera.playback.ICameraContent;
23 import net.osdn.gokigen.gr2control.camera.playback.IContentInfoCallback;
24 import net.osdn.gokigen.gr2control.camera.playback.IDownloadContentCallback;
25 import net.osdn.gokigen.gr2control.camera.playback.ICameraContentListCallback;
26 import net.osdn.gokigen.gr2control.camera.playback.IDownloadThumbnailImageCallback;
27 import net.osdn.gokigen.gr2control.camera.playback.IPlaybackControl;
28 import net.osdn.gokigen.gr2control.preference.IPreferencePropertyAccessor;
30 import java.util.ArrayList;
31 import java.util.List;
33 import static net.osdn.gokigen.gr2control.camera.fuji_x.wrapper.values.IFujiXCameraProperties.IMAGE_FILE_COUNT_STR_ID;
35 public class FujiXPlaybackControl implements IPlaybackControl, IFujiXCommandCallback
37 private final String TAG = toString();
38 private final Activity activity;
39 private final FujiXInterfaceProvider provider;
40 //private List<ICameraContent> imageInfo;
41 private SparseArray<FujiXImageContentInfo> imageContentInfo;
43 private int indexNumber = 0;
44 private ICameraContentListCallback finishedCallback = null;
46 public FujiXPlaybackControl(Activity activity, FujiXInterfaceProvider provider)
48 this.activity = activity;
49 this.provider = provider;
50 this.imageContentInfo = new SparseArray<>();
54 public String getRawFileSuffix() {
59 public void downloadContentList(@NonNull final ICameraContentListCallback callback)
63 Thread thread = new Thread(new Runnable() {
66 getCameraContents(callback);
74 callback.onErrorOccurred(e);
79 public void getContentInfo(@Nullable String path, @NonNull String name, @NonNull IContentInfoCallback callback)
81 // showFileInformation
86 public void updateCameraFileInfo(ICameraFileInfo info)
89 Log.v(TAG, " updateCameraFileInfo() : " + info.getDatetime());
93 public void downloadContentScreennail(@Nullable String path, @NonNull String name, @NonNull IDownloadThumbnailImageCallback callback)
95 SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity);
96 boolean useSmallImage = preferences.getBoolean(IPreferencePropertyAccessor.FUJI_X_GET_SCREENNAIL_AS_SMALL_PICTURE, false);
100 downloadContentScreennailImpl(path, name, callback);
104 // Thumbnail と同じ画像を表示する
105 downloadContentThumbnail(path, name, callback);
109 private void downloadContentScreennailImpl(@Nullable String path, @NonNull String name, @NonNull IDownloadThumbnailImageCallback callback)
113 Log.v(TAG, " ----- downloadContentScreennailImpl() ");
115 if (name.indexOf("/") == 0)
119 Log.v(TAG, " downloadContentThumbnail() : " + path + " " + name);
120 int index = getIndexNumber(start, name);
121 if ((index > 0)&&(index <= imageContentInfo.size()))
123 IFujiXCommandPublisher publisher = provider.getCommandPublisher();
124 FujiXImageContentInfo contentInfo = imageContentInfo.get(index);
125 if (contentInfo.isReceived())
127 if (!contentInfo.isMovie())
129 // スモール画像を取得する (たぶんこのシーケンスでいけるはず...)
130 publisher.enqueueCommand(new SetPropertyValue(new FujiXReplyReceiver(), 0xd226, 2, 0x0001));
131 publisher.enqueueCommand(new SetPropertyValue(new FujiXReplyReceiver(), 0xd227, 2, 0x0001));
132 publisher.enqueueCommand(new GetScreenNail(index, 0x00800000, new FujiXThumbnailImageReceiver(activity, callback)));
133 publisher.enqueueCommand(new SetPropertyValue(new FujiXReplyReceiver(), 0xd226, 2, 0x0000));
134 publisher.enqueueCommand(new SetPropertyValue(new FujiXReplyReceiver(), 0xd227, 2, 0x0001));
138 // movieの時は、Small画像を使えないのでThumbnailで代用する。
139 publisher.enqueueCommand(new GetThumbNail(index, new FujiXThumbnailImageReceiver(activity, callback)));
144 // まだ、ファイル情報を受信していない場合は、サムネイルの情報を流用する
145 publisher.enqueueCommand(new GetImageInfo(index, index, contentInfo));
146 publisher.enqueueCommand(new GetThumbNail(index, new FujiXThumbnailImageReceiver(activity, callback)));
157 public void downloadContentThumbnail(@Nullable String path, @NonNull String name, @NonNull IDownloadThumbnailImageCallback callback)
162 if (name.indexOf("/") == 0)
166 Log.v(TAG, " downloadContentThumbnail() : " + path + " " + name);
167 int index = getIndexNumber(start, name);
168 if ((index > 0)&&(index <= imageContentInfo.size()))
170 IFujiXCommandPublisher publisher = provider.getCommandPublisher();
171 FujiXImageContentInfo contentInfo = imageContentInfo.get(index);
172 if (!contentInfo.isReceived())
174 publisher.enqueueCommand(new GetImageInfo(index, index, contentInfo));
176 publisher.enqueueCommand(new GetThumbNail(index, new FujiXThumbnailImageReceiver(activity, callback)));
186 public void downloadContent(@Nullable String path, @NonNull String name, boolean isSmallSize, @NonNull IDownloadContentCallback callback)
191 if (name.indexOf("/") == 0)
195 int index = getIndexNumber(start, name);
196 Log.v(TAG, " FujiX::downloadContent() : " + path + " " + name + " " + index);
197 if ((index > 0)&&(index <= imageContentInfo.size()))
199 IFujiXCommandPublisher publisher = provider.getCommandPublisher();
200 publisher.enqueueCommand(new GetFullImage(index, new FujiXFullImageReceiver(callback)));
209 private int getIndexNumber(int start, @NonNull String name)
211 String indexStr = name.substring(start, name.indexOf("."));
215 indexNo = Integer.parseInt(indexStr);
219 //e.printStackTrace();
225 indexStr = name.substring(start);
226 int size = imageContentInfo.size();
227 for (int index = 0; index < size; index++)
229 FujiXImageContentInfo info = imageContentInfo.valueAt(index);
230 String contentName = info.getOriginalName();
231 if (indexStr.matches(contentName))
233 return (info.getId());
235 Log.v(TAG, " contentName : " + contentName);
237 Log.v(TAG, "index is not found : " + name + " " + indexStr);
242 public void showPictureStarted()
246 Log.v(TAG, " showPictureStarted() ");
248 IFujiXCommandPublisher publisher = provider.getCommandPublisher();
249 publisher.flushHoldQueue();
259 public void showPictureFinished()
263 Log.v(TAG, " showPictureFinished() ");
265 IFujiXCommandPublisher publisher = provider.getCommandPublisher();
266 publisher.flushHoldQueue();
275 private void getCameraContents(ICameraContentListCallback callback)
279 finishedCallback = callback;
280 ICameraStatus statusListHolder = provider.getCameraStatusListHolder();
281 if (statusListHolder != null) {
282 String count = statusListHolder.getStatus(IMAGE_FILE_COUNT_STR_ID);
283 nofFiles = Integer.parseInt(count);
284 Log.v(TAG, "getCameraContents() : " + nofFiles + " (" + count + ")");
286 Log.v(TAG, "getCameraContents() : DONE.");
289 // 件数ベースで取得する(情報は、後追いで反映させる...この方式だと、キューに積みまくってるが、、、)
290 checkImageFiles(nofFiles);
294 // 件数が不明だったら、1件づつインデックスの情報を取得する
301 finishedCallback.onErrorOccurred(e);
302 finishedCallback = null;
307 * 最初から取得可能なイメージ情報を(件数ベースで)取得する
310 private void checkImageFiles(int nofFiles)
314 imageContentInfo.clear();
315 //IFujiXCommandPublisher publisher = provider.getCommandPublisher();
316 //for (int index = nofFiles; index > 0; index--)
317 for (int index = 1; index <= nofFiles; index++)
320 imageContentInfo.append(index, new FujiXImageContentInfo(index, null));
322 //ファイル名などを取得する (メッセージを積んでおく...でも遅くなるので、ここではやらない方がよいかな。)
323 //publisher.enqueueCommand(new GetImageInfo(index, index, info));
326 // インデックスデータがなくなったことを検出...データがそろったとして応答する。
327 Log.v(TAG, "IMAGE LIST : " + imageContentInfo.size() + " (" + nofFiles + ")");
328 finishedCallback.onCompleted(getCameraFileInfoList());
329 finishedCallback = null;
338 * 最初から取得可能なイメージ情報をすべて取得する
341 private void checkImageFileAll()
345 imageContentInfo.clear();
347 IFujiXCommandPublisher publisher = provider.getCommandPublisher();
348 publisher.enqueueCommand(new GetImageInfo(indexNumber, indexNumber, this));
357 public void onReceiveProgress(int currentBytes, int totalBytes, byte[] body)
359 Log.v(TAG, " " + currentBytes + "/" + totalBytes);
363 public boolean isReceiveMulti()
369 public void receivedMessage(int id, byte[] rx_body)
371 // イメージ数の一覧が取得できなかった場合にここで作る。
372 if (rx_body.length < 16)
374 // インデックスデータがなくなったことを検出...データがそろったとして応答する。
375 Log.v(TAG, "IMAGE LIST : " + imageContentInfo.size());
376 finishedCallback.onCompleted(getCameraFileInfoList());
377 finishedCallback = null;
382 Log.v(TAG, "RECEIVED IMAGE INFO : " + indexNumber);
385 imageContentInfo.append(indexNumber, new FujiXImageContentInfo(indexNumber, rx_body));
389 IFujiXCommandPublisher publisher = provider.getCommandPublisher();
390 publisher.enqueueCommand(new GetImageInfo(indexNumber, indexNumber, this));
396 finishedCallback.onCompleted(getCameraFileInfoList());
397 finishedCallback = null;
401 private List<ICameraContent> getCameraContentList()
403 /// ダサいけど...コンテナクラスを詰め替えて応答する
404 List<ICameraContent> contentList = new ArrayList<>();
405 int listSize = imageContentInfo.size();
406 for(int index = 0; index < listSize; index++)
408 contentList.add(imageContentInfo.valueAt(index));
410 return (contentList);
413 private List<ICameraFileInfo> getCameraFileInfoList()
415 Log.v(TAG, " FujiXPlaybackControl::getCameraFileInfoList() ");
416 List<ICameraFileInfo> fileInfoList = new ArrayList<>();
419 int listSize = imageContentInfo.size();
420 for(int index = 0; index < listSize; index++)
422 FujiXImageContentInfo info = imageContentInfo.valueAt(index);
423 fileInfoList.add(info);
430 return (fileInfoList);