OSDN Git Service

いリース前に使用ライブラリを更新。
[gokigen/PKRemote.git] / app / src / main / java / net / osdn / gokigen / pkremote / camera / vendor / visionkids / wrapper / playback / VisionKidsPlaybackControl.kt
1 package net.osdn.gokigen.pkremote.camera.vendor.visionkids.wrapper.playback
2
3 import android.util.Log
4 import androidx.appcompat.app.AppCompatActivity
5 import androidx.preference.PreferenceManager
6 import net.osdn.gokigen.pkremote.IInformationReceiver
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.ICameraStatusReceiver
15 import net.osdn.gokigen.pkremote.camera.playback.ProgressEvent
16 import net.osdn.gokigen.pkremote.camera.utils.SimpleHttpClient
17 import net.osdn.gokigen.pkremote.camera.utils.SimpleHttpClient.IReceivedMessageCallback
18 import net.osdn.gokigen.pkremote.camera.vendor.visionkids.wrapper.connection.IVisionKidsConnection
19 import java.util.Date
20
21 class VisionKidsPlaybackControl(activity: AppCompatActivity, private val provider: ICameraStatusReceiver, private val informationReceiver: IInformationReceiver, timeoutMs: Int = DEFAULT_TIMEOUT): IPlaybackControl, IVisionKidsConnection
22 {
23     private val contentProvider = VisionKidsCameraContentProvider(activity)
24     private val timeoutValue = Math.max(DEFAULT_TIMEOUT, timeoutMs)
25
26     override fun getRawFileSuffix(): String
27     {
28         return (".DNG")
29     }
30
31     override fun downloadContentList(callback: IDownloadContentListCallback?)
32     {
33         // 利用箇所なし
34     }
35
36     override fun getContentInfo(path: String?, name: String?, callback: IContentInfoCallback?)
37     {
38         Log.v(TAG, "getContentInfo($path, $name)")
39         try
40         {
41             if (name == null)
42             {
43                 callback?.onErrorOccurred(Exception())
44                 return
45             }
46             val contentInfo = contentProvider.getCameraContent(name)
47             if (contentInfo != null)
48             {
49                 callback?.onCompleted(VisionKidsCameraInfo(name, contentInfo.capturedDate))
50             }
51             else
52             {
53                 callback?.onCompleted(VisionKidsCameraInfo(name, Date()))
54             }
55         }
56         catch (e: Exception)
57         {
58             e.printStackTrace()
59         }
60         return
61     }
62
63     override fun updateCameraFileInfo(info: ICameraFileInfo?)
64     {
65         // 利用箇所なし
66     }
67
68     override fun downloadContentScreennail(path: String?, callback: IDownloadThumbnailImageCallback?)
69     {
70         // サムネイルしか取得できないので統合する
71         downloadContentThumbnail(path, callback)
72     }
73
74     override fun downloadContentThumbnail(path: String?, callback: IDownloadThumbnailImageCallback?)
75     {
76         try
77         {
78             val address = contentProvider.getHostAddress()
79             val urlToGet = "http://$address/DCIM/T/$path".replace("//","/")
80             Log.v(TAG, "downloadContentThumbnail($path) : $urlToGet")
81
82             val bmp = SimpleHttpClient.httpGetBitmap(urlToGet, HashMap(), timeoutValue)
83             val map = HashMap<String, Any>()
84             map["Orientation"] = 0
85             callback?.onCompleted(bmp, map)
86         }
87         catch (e: Exception)
88         {
89             e.printStackTrace()
90             callback?.onErrorOccurred(e)
91         }
92     }
93
94     override fun downloadContent(path: String?, isSmallSize: Boolean, callback: IDownloadContentCallback?)
95     {
96         try
97         {
98             // 取得先URLを特定する
99             val dataType = if (isSmallSize) { "T" } else { "O" }
100             val address = contentProvider.getHostAddress()
101             val urlToGet = "http://$address/DCIM/$dataType/$path".replace("//","/")
102             Log.v(TAG, "downloadContent($path, $isSmallSize) : $urlToGet")
103
104             // 画像データを取得する
105             try
106             {
107                 SimpleHttpClient.httpGetBytes(urlToGet, HashMap(), timeoutValue,
108                     object : IReceivedMessageCallback {
109                         override fun onCompleted() {
110                             callback?.onCompleted()
111                         }
112                         override fun onErrorOccurred(e: java.lang.Exception) {
113                             callback?.onErrorOccurred(e)
114                         }
115                         override fun onReceive(readBytes: Int, length: Int, size: Int, data: ByteArray) {
116                             val percent = if (length == 0) 0.0f else readBytes.toFloat() / length.toFloat()
117                             val event = ProgressEvent(percent, null)
118                             callback?.onProgress(data, size, event)
119                         }
120                     })
121             }
122             catch (t: Throwable)
123             {
124                 t.printStackTrace()
125                 callback?.onErrorOccurred(NullPointerException())
126             }
127         }
128         catch (e: Exception)
129         {
130             e.printStackTrace()
131         }
132     }
133
134     override fun getCameraContentList(callback: ICameraContentListCallback)
135     {
136         Log.v(TAG, "getCameraContentList()")
137         try
138         {
139             contentProvider.getContentList(callback)
140         }
141         catch (e: Exception)
142         {
143             e.printStackTrace()
144         }
145     }
146
147     override fun showPictureStarted()
148     {
149         Log.v(TAG, "showPictureStarted()")
150     }
151
152     override fun showPictureFinished()
153     {
154         Log.v(TAG, "showPictureFinished()")
155     }
156
157     override fun forceDisconnect()
158     {
159         contentProvider.forceDisconnect()
160     }
161     companion object
162     {
163         private val TAG = VisionKidsPlaybackControl::class.java.simpleName
164         private const val DEFAULT_TIMEOUT = 3000
165     }
166 }