OSDN Git Service

一時保管。
[gokigen/PKRemote.git] / app / src / main / java / net / osdn / gokigen / pkremote / camera / vendor / ptpip / wrapper / playback / PtpIpPlaybackControl.java
1 package net.osdn.gokigen.pkremote.camera.vendor.ptpip.wrapper.playback;
2
3 import android.app.Activity;
4 import android.util.Log;
5 import android.util.SparseArray;
6
7 import net.osdn.gokigen.pkremote.camera.interfaces.playback.ICameraContent;
8 import net.osdn.gokigen.pkremote.camera.interfaces.playback.ICameraContentListCallback;
9 import net.osdn.gokigen.pkremote.camera.interfaces.playback.ICameraFileInfo;
10 import net.osdn.gokigen.pkremote.camera.interfaces.playback.IContentInfoCallback;
11 import net.osdn.gokigen.pkremote.camera.interfaces.playback.IDownloadContentCallback;
12 import net.osdn.gokigen.pkremote.camera.interfaces.playback.IDownloadContentListCallback;
13 import net.osdn.gokigen.pkremote.camera.interfaces.playback.IDownloadThumbnailImageCallback;
14 import net.osdn.gokigen.pkremote.camera.interfaces.playback.IPlaybackControl;
15 import net.osdn.gokigen.pkremote.camera.vendor.ptpip.wrapper.PtpIpInterfaceProvider;
16 import net.osdn.gokigen.pkremote.camera.vendor.ptpip.wrapper.command.IPtpIpCommandCallback;
17
18
19 import java.util.ArrayList;
20 import java.util.List;
21
22 public class PtpIpPlaybackControl implements IPlaybackControl, IPtpIpCommandCallback
23 {
24     private final String TAG = toString();
25     private final Activity activity;
26     private final PtpIpInterfaceProvider provider;
27     //private List<ICameraContent> imageInfo;
28     private SparseArray<PtpIpImageContentInfo> imageContentInfo;
29     private int indexNumber = 0;
30     private ICameraContentListCallback finishedCallback = null;
31     private CanonImageObjectReceiver canonImageObjectReceiver;
32
33     public PtpIpPlaybackControl(Activity activity, PtpIpInterfaceProvider provider)
34     {
35         this.activity = activity;
36         this.provider = provider;
37         canonImageObjectReceiver = new CanonImageObjectReceiver(activity, provider);
38         this.imageContentInfo = new SparseArray<>();
39     }
40
41     @Override
42     public String getRawFileSuffix() {
43         return (null);
44     }
45
46     @Override
47     public void downloadContentList(IDownloadContentListCallback callback)
48     {
49         // なにもしない。(未使用)
50     }
51
52     @Override
53     public void getContentInfo(String path, String name, IContentInfoCallback callback)
54     {
55         // showFileInformation
56
57     }
58
59     @Override
60     public void updateCameraFileInfo(ICameraFileInfo info)
61     {
62         //  なにもしない
63     }
64
65     @Override
66     public void downloadContentScreennail(String path, IDownloadThumbnailImageCallback callback)
67     {
68         // Thumbnail と同じ画像を表示する
69         downloadContentThumbnail(path, callback);
70     }
71
72     @Override
73     public void downloadContentThumbnail(String path, IDownloadThumbnailImageCallback callback)
74     {
75         try
76         {
77 /*
78             int start = 0;
79             if (path.indexOf("/") == 0)
80             {
81                 start = 1;
82             }
83             String indexStr = path.substring(start, path.indexOf("."));
84             Log.v(TAG, "downloadContentThumbnail() : " + path + " " + indexStr);
85             int index = Integer.parseInt(indexStr);
86             if ((index > 0)&&(index <= imageContentInfo.size()))
87             {
88                 IPtpIpCommandPublisher publisher = provider.getCommandPublisher();
89                 PtpIpImageContentInfo contentInfo = imageContentInfo.get(index);
90                 if (!contentInfo.isReceived())
91                 {
92                     publisher.enqueueCommand(new GetImageInfo(index, index, contentInfo));
93                 }
94                 publisher.enqueueCommand(new GetThumbNail(index, new PtpIpThumbnailImageReceiver(activity, callback)));
95             }
96 */
97         }
98         catch (Exception e)
99         {
100             e.printStackTrace();
101         }
102     }
103
104     @Override
105     public void downloadContent(String path, boolean isSmallSize, IDownloadContentCallback callback)
106     {
107         try
108         {
109 /*
110             int start = 0;
111             if (path.indexOf("/") == 0)
112             {
113                 start = 1;
114             }
115             String indexStr = path.substring(start, path.indexOf("."));
116             Log.v(TAG, "FujiX::downloadContent() : " + path + " " + indexStr);
117             int index = Integer.parseInt(indexStr);
118             //PtpIpImageContentInfo contentInfo = imageContentInfo.get(index);   // 特にデータを更新しないから大丈夫か?
119             if ((index > 0)&&(index <= imageContentInfo.size()))
120             {
121                 IPtpIpCommandPublisher publisher = provider.getCommandPublisher();
122                 publisher.enqueueCommand(new GetFullImage(index, new PtpIpFullImageReceiver(callback)));
123             }
124 */
125         }
126         catch (Exception e)
127         {
128             e.printStackTrace();
129         }
130     }
131
132     @Override
133     public void getCameraContentList(final ICameraContentListCallback callback)
134     {
135         if (callback == null)
136         {
137             return;
138         }
139
140         try
141         {
142             Thread thread = new Thread(new Runnable() {
143                 @Override
144                 public void run() {
145                     canonImageObjectReceiver.getCameraContents(imageContentInfo, callback);
146                 }
147             });
148             thread.start();
149         }
150         catch (Exception e)
151         {
152             e.printStackTrace();
153             callback.onErrorOccurred(e);
154         }
155     }
156
157
158     @Override
159     public void onReceiveProgress(int currentBytes, int totalBytes, byte[] body)
160     {
161         Log.v(TAG, " " + currentBytes + "/" + totalBytes);
162     }
163
164     @Override
165     public boolean isReceiveMulti()
166     {
167         return (false);
168     }
169
170     @Override
171     public void receivedMessage(int id, byte[] rx_body)
172     {
173         // イメージ数の一覧が取得できなかった場合にここで作る。
174         if (rx_body.length < 16)
175         {
176             // インデックスデータがなくなったことを検出...データがそろったとして応答する。
177             Log.v(TAG, "IMAGE LIST : " + imageContentInfo.size());
178             finishedCallback.onCompleted(getCameraContentList());
179             finishedCallback = null;
180             return;
181         }
182         try
183         {
184 /*
185             Log.v(TAG, "RECEIVED IMAGE INFO : " + indexNumber);
186
187             // 受信データを保管しておく
188             imageContentInfo.append(indexNumber, new PtpIpImageContentInfo(indexNumber, rx_body));
189
190             // 次のインデックスの情報を要求する
191             indexNumber++;
192             IPtpIpCommandPublisher publisher = provider.getCommandPublisher();
193             publisher.enqueueCommand(new GetImageInfo(indexNumber, indexNumber, this));
194 */
195         }
196         catch (Exception e)
197         {
198             // エラーになったら、そこで終了にする
199             e.printStackTrace();
200             finishedCallback.onCompleted(getCameraContentList());
201             finishedCallback = null;
202         }
203     }
204
205     private List<ICameraContent> getCameraContentList()
206     {
207         /// ダサいけど...コンテナクラスを詰め替えて応答する
208         List<ICameraContent> contentList = new ArrayList<>();
209         int listSize = imageContentInfo.size();
210         for(int index = 0; index < listSize; index++)
211         {
212             contentList.add(imageContentInfo.valueAt(index));
213         }
214         return (contentList);
215     }
216
217 }