OSDN Git Service

b85474ce008cae23f1d02607fd8a40ae1e9f4bea
[gokigen/PKRemote.git] / app / src / main / java / net / osdn / gokigen / pkremote / camera / vendor / olympuspen / wrapper / playback / OlympusPenPlaybackControl.java
1 package net.osdn.gokigen.pkremote.camera.vendor.olympuspen.wrapper.playback;
2 import android.app.Activity;
3 import android.content.SharedPreferences;
4 import android.graphics.Bitmap;
5 import android.util.Log;
6
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.IDownloadContentListCallback;
11 import net.osdn.gokigen.pkremote.camera.interfaces.playback.IDownloadContentCallback;
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.playback.ProgressEvent;
15 import net.osdn.gokigen.pkremote.camera.utils.SimpleHttpClient;
16 import net.osdn.gokigen.pkremote.preference.IPreferencePropertyAccessor;
17
18 import java.util.HashMap;
19 import java.util.Map;
20
21 import androidx.annotation.NonNull;
22 import androidx.preference.PreferenceManager;
23
24 /**
25  *
26  *
27  */
28 public class OlympusPenPlaybackControl implements IPlaybackControl
29 {
30     private final String TAG = toString();
31     private static final int DEFAULT_TIMEOUT = 3000;
32     private final Activity activity;
33     private final int timeoutValue;
34     private OlympusPenObjectDataHolder imageListHolder = new OlympusPenObjectDataHolder();
35
36     public OlympusPenPlaybackControl(@NonNull Activity activity, int timeoutMs)
37     {
38         Log.v(TAG, "OlympusPenPlaybackControl()");
39         this.activity = activity;
40         this.timeoutValue  = (timeoutMs < DEFAULT_TIMEOUT) ? DEFAULT_TIMEOUT : timeoutMs;
41     }
42
43     @Override
44     public String getRawFileSuffix()
45     {
46         return (".ORF");
47     }
48
49     @Override
50     public void downloadContentList(@NonNull IDownloadContentListCallback callback)
51     {
52         Log.v(TAG, " downloadContentList()");
53     }
54
55     @Override
56     public void updateCameraFileInfo(ICameraFileInfo info)
57     {
58         Log.v(TAG, " updateCameraFileInfo() : " + info.getFilename());
59     }
60
61     @Override
62     public void getContentInfo(@NonNull String path, @NonNull String name, @NonNull IContentInfoCallback callback)
63     {
64         Log.v(TAG, " getContentInfo() : " + path + " / " + name);
65
66         // 画像の情報を取得する
67     }
68
69     @Override
70     public void downloadContentScreennail(@NonNull String path, @NonNull IDownloadThumbnailImageCallback callback)
71     {
72         Log.v(TAG, "downloadContentScreennail() : " + path);
73         try
74         {
75             String url = "http://192.168.0.10/get_screennail.cgi?DIR=" + path;
76
77             Map<String, String> headerMap = new HashMap<>();
78             headerMap.put("User-Agent", "OlympusCameraKit"); // "OI.Share"
79             headerMap.put("X-Protocol", "OlympusCameraKit"); // "OI.Share"
80
81             Bitmap bmp = SimpleHttpClient.httpGetBitmap(url, headerMap, timeoutValue);
82             if (bmp != null)
83             {
84                 HashMap<String, Object> map = new HashMap<>();
85                 map.put("Orientation", 0);
86                 callback.onCompleted(bmp, map);
87                 return;
88             }
89
90             // screennail取得失敗時...リカバリする
91             try
92             {
93                 SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity);
94                 boolean smallSize = preferences.getBoolean(IPreferencePropertyAccessor.OLYMPUS_USE_SCREENNAIL_AS_SMALL, false);
95                 if (smallSize)
96                 {
97                     // 小さい画像をscreennailとして利用する
98                     url = "http://192.168.0.10/get_resizeimg.cgi?DIR=" + path + "&size=1024";
99                     bmp = SimpleHttpClient.httpGetBitmap(url, headerMap, timeoutValue);
100                     if (bmp != null)
101                     {
102                         HashMap<String, Object> map = new HashMap<>();
103                         map.put("Orientation", 0);
104                         callback.onCompleted(bmp, map);
105                         return;
106                     }
107                     // それでもダメな場合はサムネイル画像を使う...
108                 }
109             }
110             catch (Exception e)
111             {
112                 e.printStackTrace();
113             }
114
115
116             // サムネイルでscreennail表示...
117             downloadContentThumbnail(path, callback);
118         }
119         catch (Throwable e)
120         {
121             e.printStackTrace();
122             callback.onErrorOccurred(new NullPointerException());
123         }
124     }
125
126     @Override
127     public void downloadContentThumbnail(@NonNull String path, @NonNull IDownloadThumbnailImageCallback callback)
128     {
129         Log.v(TAG, "downloadContentThumbnail() : " + path);
130         try
131         {
132             String url = "http://192.168.0.10/get_thumbnail.cgi?DIR=" + path;
133
134             Map<String, String> headerMap = new HashMap<>();
135             headerMap.put("User-Agent", "OlympusCameraKit"); // "OI.Share"
136             headerMap.put("X-Protocol", "OlympusCameraKit"); // "OI.Share"
137             Bitmap bmp = SimpleHttpClient.httpGetBitmap(url, headerMap, timeoutValue);
138             HashMap<String, Object> map = new HashMap<>();
139             map.put("Orientation", 0);
140             callback.onCompleted(bmp, map);
141         }
142         catch (Throwable e)
143         {
144             e.printStackTrace();
145             callback.onErrorOccurred(new NullPointerException());
146         }
147    }
148
149     @Override
150     public void downloadContent(@NonNull String  path, boolean isSmallSize, @NonNull final IDownloadContentCallback callback)
151     {
152         Log.v(TAG, "downloadContent() : " + path + " (small :" + isSmallSize + ")");
153         try
154         {
155             String url;
156             if ((isSmallSize)&&(path.contains(".JPG")))
157             {
158                 String smallSize = "1600";
159                 try
160                 {
161                     SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity);
162                     smallSize = preferences.getString(IPreferencePropertyAccessor.PEN_SMALL_PICTURE_SIZE, IPreferencePropertyAccessor.PEN_SMALL_PICTURE_SIZE_DEFAULT_VALUE);
163                 }
164                 catch (Exception e)
165                 {
166                     e.printStackTrace();
167                 }
168                 // 縮小サイズで画像をとる
169                 url = "http://192.168.0.10/get_resizeimg.cgi?DIR=" + path + "&size=" + smallSize;
170             }
171             else
172             {
173                 url = "http://192.168.0.10/" + path;
174             }
175
176             Map<String, String> headerMap = new HashMap<>();
177             headerMap.put("User-Agent", "OlympusCameraKit"); // "OI.Share"
178             headerMap.put("X-Protocol", "OlympusCameraKit"); // "OI.Share"
179
180             SimpleHttpClient.httpGetBytes(url, headerMap, timeoutValue, new SimpleHttpClient.IReceivedMessageCallback() {
181                 @Override
182                 public void onCompleted() {
183                     callback.onCompleted();
184                 }
185
186                 @Override
187                 public void onErrorOccurred(Exception e) {
188                     callback.onErrorOccurred(e);
189                 }
190
191                 @Override
192                 public void onReceive(int readBytes, int length, int size, byte[] data) {
193                     float percent = (length == 0) ? 0.0f : ((float) readBytes / (float) length);
194                     //Log.v(TAG, " onReceive : " + readBytes + " " + length + " " + size);
195                     ProgressEvent event = new ProgressEvent(percent, null);
196                     callback.onProgress(data, size, event);
197                 }
198             });
199         }
200         catch (Throwable e)
201         {
202             e.printStackTrace();
203         }
204     }
205
206     /**
207      *   カメラ内画像ファイルの取得処理
208      *      - フォルダ一覧を取得してから、それぞれのフォルダ内に入っている画像一覧を取得する
209      */
210     @Override
211     public void getCameraContentList(ICameraContentListCallback callback)
212     {
213         String imageListTopLevelUrl = "http://192.168.0.10/get_imglist.cgi?DIR=/DCIM";
214         String contentInformation;
215         try
216         {
217             Map<String, String> headerMap = new HashMap<>();
218             headerMap.put("User-Agent", "OlympusCameraKit"); // "OI.Share"
219             headerMap.put("X-Protocol", "OlympusCameraKit"); // "OI.Share"
220
221             // フォルダー情報を取得する
222             contentInformation = SimpleHttpClient.httpGetWithHeader(imageListTopLevelUrl, headerMap, null, timeoutValue);
223             Log.v(TAG, " " + imageListTopLevelUrl + " " + contentInformation);
224             imageListHolder.clear();
225             for (OlympusPenCameraContent path : imageListHolder.parsePath(contentInformation))
226             {
227                 //  フォルダ内の画像を取得する
228                 String imageListPathUrl = "http://192.168.0.10/get_imglist.cgi?DIR=/DCIM/" + path.getContentName();
229                 String imgList = SimpleHttpClient.httpGetWithHeader(imageListPathUrl, headerMap, null, timeoutValue);
230                 if ((imgList != null)&&(imgList.length() > 0))
231                 {
232                     imageListHolder.parseImage(imgList);
233                 }
234             }
235             callback.onCompleted(imageListHolder.getImageList());
236         }
237         catch (Exception e)
238         {
239             // 例外をそのまま転送
240             callback.onErrorOccurred(e);
241         }
242     }
243
244     @Override
245     public void showPictureStarted()
246     {
247
248     }
249
250     @Override
251     public void showPictureFinished()
252     {
253
254     }
255 }