OSDN Git Service

FujiXのJPEGダウンロード機能を追加。
[gokigen/PKRemote.git] / app / src / main / java / net / osdn / gokigen / pkremote / camera / vendor / fujix / wrapper / FujiXPlaybackControl.java
1 package net.osdn.gokigen.pkremote.camera.vendor.fujix.wrapper;
2
3 import android.app.Activity;
4 import android.util.Log;
5
6 import net.osdn.gokigen.pkremote.camera.interfaces.playback.ICameraContent;
7 import net.osdn.gokigen.pkremote.camera.interfaces.playback.ICameraContentListCallback;
8 import net.osdn.gokigen.pkremote.camera.interfaces.playback.ICameraFileInfo;
9 import net.osdn.gokigen.pkremote.camera.interfaces.playback.IContentInfoCallback;
10 import net.osdn.gokigen.pkremote.camera.interfaces.playback.IDownloadContentCallback;
11 import net.osdn.gokigen.pkremote.camera.interfaces.playback.IDownloadContentListCallback;
12 import net.osdn.gokigen.pkremote.camera.interfaces.playback.IDownloadThumbnailImageCallback;
13 import net.osdn.gokigen.pkremote.camera.interfaces.playback.IPlaybackControl;
14 import net.osdn.gokigen.pkremote.camera.interfaces.status.ICameraStatus;
15 import net.osdn.gokigen.pkremote.camera.vendor.fujix.wrapper.command.IFujiXCommandCallback;
16 import net.osdn.gokigen.pkremote.camera.vendor.fujix.wrapper.command.IFujiXCommandPublisher;
17 import net.osdn.gokigen.pkremote.camera.vendor.fujix.wrapper.command.messages.GetFullImage;
18 import net.osdn.gokigen.pkremote.camera.vendor.fujix.wrapper.command.messages.GetImageInfo;
19 import net.osdn.gokigen.pkremote.camera.vendor.fujix.wrapper.command.messages.GetThumbNail;
20
21 import java.util.ArrayList;
22 import java.util.List;
23
24 import static net.osdn.gokigen.pkremote.camera.vendor.fujix.wrapper.status.IFujiXCameraProperties.IMAGE_FILE_COUNT_STR_ID;
25
26 public class FujiXPlaybackControl implements IPlaybackControl, IFujiXCommandCallback
27 {
28     private final String TAG = toString();
29     private final Activity activity;
30     private final FujiXInterfaceProvider provider;
31     private List<ICameraContent> imageInfo;
32     private int indexNumber = 0;
33     private ICameraContentListCallback finishedCallback = null;
34
35     FujiXPlaybackControl(Activity activity, FujiXInterfaceProvider provider)
36     {
37         this.activity = activity;
38         this.provider = provider;
39         this.imageInfo = new ArrayList<>();
40     }
41
42
43     @Override
44     public String getRawFileSuffix() {
45         return (null);
46     }
47
48     @Override
49     public void downloadContentList(IDownloadContentListCallback callback)
50     {
51
52     }
53
54     @Override
55     public void getContentInfo(String path, String name, IContentInfoCallback callback)
56     {
57         // showFileInformation
58
59     }
60
61     @Override
62     public void updateCameraFileInfo(ICameraFileInfo info)
63     {
64
65     }
66
67     @Override
68     public void downloadContentScreennail(String path, IDownloadThumbnailImageCallback callback)
69     {
70         // Thumbnail と同じ画像を表示する
71         downloadContentThumbnail(path, callback);
72     }
73
74     @Override
75     public void downloadContentThumbnail(String path, IDownloadThumbnailImageCallback callback)
76     {
77         try
78         {
79             int start = 0;
80             if (path.indexOf("/") == 0)
81             {
82                 start = 1;
83             }
84             String indexStr = path.substring(start, path.indexOf("."));
85             Log.v(TAG, "downloadContentThumbnail() : " + path + " " + indexStr);
86             int index = Integer.parseInt(indexStr);
87             if ((index > 0)&&(index <= imageInfo.size()))
88             {
89                 IFujiXCommandPublisher publisher = provider.getCommandPublisher();
90                 ICameraContent contentInfo = imageInfo.get(index - 1);
91                 publisher.enqueueCommand(new GetImageInfo(indexNumber, indexNumber, (FujiXImageContentInfo) contentInfo));
92                 publisher.enqueueCommand(new GetThumbNail(index, new FujiXThumbnailImageReceiver(activity, callback)));
93             }
94         }
95         catch (Exception e)
96         {
97             e.printStackTrace();
98         }
99     }
100
101     @Override
102     public void downloadContent(String path, boolean isSmallSize, IDownloadContentCallback callback)
103     {
104         try
105         {
106             int start = 0;
107             if (path.indexOf("/") == 0)
108             {
109                 start = 1;
110             }
111             String indexStr = path.substring(start, path.indexOf("."));
112             Log.v(TAG, "FujiX::downloadContent() : " + path + " " + indexStr);
113             int index = Integer.parseInt(indexStr);
114             if ((index > 0)&&(index <= imageInfo.size()))
115             {
116                 IFujiXCommandPublisher publisher = provider.getCommandPublisher();
117                 publisher.enqueueCommand(new GetFullImage(index, new FujiXFullImageReceiver(callback)));
118             }
119         }
120         catch (Exception e)
121         {
122             e.printStackTrace();
123         }
124     }
125
126     @Override
127     public void getCameraContentList(final ICameraContentListCallback callback)
128     {
129         if (callback == null) {
130             return;
131         }
132         try {
133             Thread thread = new Thread(new Runnable() {
134                 @Override
135                 public void run() {
136                     getCameraContents(callback);
137                 }
138             });
139             thread.start();
140         } catch (Exception e) {
141             e.printStackTrace();
142             callback.onErrorOccurred(e);
143         }
144     }
145
146     private void getCameraContents(ICameraContentListCallback callback)
147     {
148         int nofFiles = -1;
149         try {
150             finishedCallback = callback;
151             ICameraStatus statusListHolder = provider.getCameraStatusListHolder();
152             if (statusListHolder != null) {
153                 String count = statusListHolder.getStatus(IMAGE_FILE_COUNT_STR_ID);
154                 nofFiles = Integer.parseInt(count);
155                 Log.v(TAG, "getCameraContents() : " + nofFiles + " (" + count + ")");
156             }
157             Log.v(TAG, "getCameraContents() : DONE.");
158             if (nofFiles > 0)
159             {
160                 // 件数ベースで取得する(情報は、後追いで反映させる...この方式だと、キューに積みまくってるが、、、)
161                 checkImageFiles(nofFiles);
162             }
163             else
164             {
165                 checkImageFileAll();
166             }
167         }
168         catch (Exception e)
169         {
170             e.printStackTrace();
171             finishedCallback.onErrorOccurred(e);
172             finishedCallback = null;
173         }
174     }
175
176     /**
177      *   最初から取得可能なイメージ情報を(件数ベースで)取得する
178      *
179      */
180     private void checkImageFiles(int nofFiles)
181     {
182         try
183         {
184             imageInfo.clear();
185             //IFujiXCommandPublisher publisher = provider.getCommandPublisher();
186             for (int index = 1; index <= nofFiles; index++)
187             {
188                 FujiXImageContentInfo info = new FujiXImageContentInfo(index, null);
189                 //ファイル名などを取得する
190                 //publisher.enqueueCommand(new GetImageInfo(index, index, info));
191                 imageInfo.add(info);
192             }
193
194             // インデックスデータがなくなったことを検出...データがそろったとして応答する。
195             Log.v(TAG, "IMAGE LIST : " + imageInfo.size() + " (" + nofFiles + ")");
196             finishedCallback.onCompleted(imageInfo);
197             finishedCallback = null;
198         }
199         catch (Exception e)
200         {
201             e.printStackTrace();
202         }
203     }
204
205     /**
206      *   最初から取得可能なイメージ情報をすべて取得する
207      *
208      */
209     private void checkImageFileAll()
210     {
211         try
212         {
213             imageInfo.clear();
214             indexNumber = 1;
215             IFujiXCommandPublisher publisher = provider.getCommandPublisher();
216             publisher.enqueueCommand(new GetImageInfo(indexNumber, indexNumber, this));
217         }
218         catch (Exception e)
219         {
220             e.printStackTrace();
221         }
222     }
223
224     @Override
225     public void onReceiveProgress(int currentBytes, int totalBytes, byte[] body)
226     {
227         Log.v(TAG, " " + currentBytes + "/" + totalBytes);
228     }
229
230     @Override
231     public boolean isReceiveMulti()
232     {
233         return (false);
234     }
235
236     @Override
237     public void receivedMessage(int id, byte[] rx_body)
238     {
239         // イメージ数の一覧が取得できなかった場合にここで作る。
240         if (rx_body.length < 16)
241         {
242             // インデックスデータがなくなったことを検出...データがそろったとして応答する。
243             Log.v(TAG, "IMAGE LIST : " + imageInfo.size());
244             finishedCallback.onCompleted(imageInfo);
245             finishedCallback = null;
246             return;
247         }
248         try
249         {
250             Log.v(TAG, "RECEIVED IMAGE INFO : " + indexNumber);
251
252             // 受信データを保管しておく
253             imageInfo.add(new FujiXImageContentInfo(indexNumber, rx_body));
254
255             // 次のインデックスの情報を要求する
256             indexNumber++;
257             IFujiXCommandPublisher publisher = provider.getCommandPublisher();
258             publisher.enqueueCommand(new GetImageInfo(indexNumber, indexNumber, this));
259         }
260         catch (Exception e)
261         {
262             // エラーになったら、そこで終了にする
263             e.printStackTrace();
264             finishedCallback.onCompleted(imageInfo);
265             finishedCallback = null;
266         }
267     }
268 }