OSDN Git Service

cd0ea5e7667c85112ebeb101133a305a76b9572b
[gokigen/PKRemote.git] / app / src / main / java / net / osdn / gokigen / pkremote / camera / vendor / ptpip / wrapper / playback / PtpIpScreennailImageReceiver.java
1 package net.osdn.gokigen.pkremote.camera.vendor.ptpip.wrapper.playback;
2
3 import android.app.Activity;
4 import android.graphics.BitmapFactory;
5 import android.os.Environment;
6 import android.util.Log;
7
8 import net.osdn.gokigen.pkremote.R;
9 import net.osdn.gokigen.pkremote.camera.interfaces.playback.IDownloadThumbnailImageCallback;
10 import net.osdn.gokigen.pkremote.camera.vendor.ptpip.wrapper.command.IPtpIpCommandCallback;
11 import net.osdn.gokigen.pkremote.camera.vendor.ptpip.wrapper.command.IPtpIpCommandPublisher;
12 import net.osdn.gokigen.pkremote.camera.vendor.ptpip.wrapper.command.messages.PtpIpCommandGeneric;
13 import net.osdn.gokigen.pkremote.camera.vendor.ptpip.wrapper.command.messages.specific.CanonRequestInnerDevelopEnd;
14
15 import java.io.ByteArrayInputStream;
16 import java.io.File;
17 import java.io.FileOutputStream;
18 import java.text.SimpleDateFormat;
19 import java.util.Calendar;
20 import java.util.Locale;
21
22 import static net.osdn.gokigen.pkremote.camera.utils.SimpleLogDumper.binaryOutputToFile;
23
24
25 public class PtpIpScreennailImageReceiver  implements IPtpIpCommandCallback
26 {
27     private static final String TAG = PtpIpScreennailImageReceiver.class.getSimpleName();
28     private final Activity activity;
29     private final IDownloadThumbnailImageCallback callback;
30     private final IPtpIpCommandPublisher publisher;
31     private final int objectId;
32
33     PtpIpScreennailImageReceiver(Activity activity, int objectId, IPtpIpCommandPublisher publisher, IDownloadThumbnailImageCallback callback)
34     {
35         this.activity = activity;
36         this.callback = callback;
37         this.publisher = publisher;
38         this.objectId = objectId;
39         Log.v(TAG, "PtpIpScreennailImageReceiver CREATED : " + objectId);
40
41     }
42
43     @Override
44     public void receivedMessage(int id, byte[] rx_body)
45     {
46         try
47         {
48             if (rx_body != null)
49             {
50                 Log.v(TAG, "  receivedMessage() : " + id +  " " + rx_body.length + " bytes.");
51             }
52             else
53             {
54                 Log.v(TAG, "  receivedMessage() : " + id + " NULL.");
55             }
56             if (id == objectId)
57             {
58                 requestGetPartialObject();
59             }
60             else if (id == objectId + 1)
61             {
62                 getPartialObject(rx_body);
63             }
64             else if (id == objectId + 2)
65             {
66                 requestInnerDevelopEnd();
67             }
68             else if (id == objectId + 3)
69             {
70                 finishedGetScreeennail();
71             }
72             else if (id == objectId + 4)
73             {
74                 Log.v(TAG, " RECEIVED  : " + id);
75             }
76             else
77             {
78                 Log.v(TAG, " RECEIVED UNKNOWN ID : " + id);
79             }
80         }
81         catch (Exception e)
82         {
83             e.printStackTrace();
84             {
85                 callback.onErrorOccurred(e);
86             }
87         }
88     }
89
90     @Override
91     public void onReceiveProgress(int currentBytes, int totalBytes, byte[] rx_body)
92     {
93         int length = (rx_body == null) ? 0 : rx_body.length;
94         Log.v(TAG, " onReceiveProgress() " + currentBytes + "/" + totalBytes + " (" + length + " bytes.)");
95     }
96
97     @Override
98     public boolean isReceiveMulti()
99     {
100         return (false);
101     }
102
103     private void requestGetPartialObject()
104     {
105         Log.v(TAG, " requestGetPartialObject() : " + objectId);
106         //publisher.enqueueCommand(new PtpIpCommandGeneric(this, false, (objectId + 1), 0x9107, 12, 0x01, 0x00, 0x00200000));
107     }
108
109     private void getPartialObject(byte[] rx_body)
110     {
111         Log.v(TAG, " getPartialObject(), id : " + objectId + " size: " + rx_body.length);
112         callback.onCompleted(BitmapFactory.decodeStream(new ByteArrayInputStream(rx_body)), null);
113         //publisher.enqueueCommand(new PtpIpCommandGeneric(this, false, (objectId + 2), 0x9117, 4,0x01));
114
115         // ファイルにバイナリデータをダンプする
116         //binaryOutputToFile(activity, objectId + "_", rx_body);
117     }
118
119     private void requestInnerDevelopEnd()
120     {
121         Log.v(TAG, " requestInnerDevelopEnd() : " + objectId);
122         //publisher.enqueueCommand(new CanonRequestInnerDevelopEnd(this, true, (objectId + 3)));
123     }
124
125     private void finishedGetScreeennail()
126     {
127         Log.v(TAG, "  --- SCREENNAIL RECV FINISHED. : " + objectId + " --- ");
128
129         // リセットコマンドを送ってみる
130         publisher.enqueueCommand(new PtpIpCommandGeneric(this, false, (objectId + 4), 0x902f));
131     }
132
133 }