OSDN Git Service

FUJIFILM機のビデオ撮影機能を追加。
[gokigen/Gr2Control.git] / app / src / main / java / net / osdn / gokigen / gr2control / liveview / LiveViewFujiXKeyPanelClickListener.java
1 package net.osdn.gokigen.gr2control.liveview;
2
3 import android.content.DialogInterface;
4 import android.os.Vibrator;
5 import android.util.Log;
6 import android.view.View;
7 import android.widget.ImageView;
8
9 import androidx.annotation.NonNull;
10 import androidx.annotation.Nullable;
11 import androidx.appcompat.app.AlertDialog;
12 import androidx.core.content.ContextCompat;
13 import androidx.fragment.app.FragmentActivity;
14
15 import net.osdn.gokigen.gr2control.R;
16 import net.osdn.gokigen.gr2control.camera.ICameraStatus;
17 import net.osdn.gokigen.gr2control.camera.IInterfaceProvider;
18 import net.osdn.gokigen.gr2control.camera.fuji_x.wrapper.command.IFujiXCommandCallback;
19 import net.osdn.gokigen.gr2control.camera.fuji_x.wrapper.command.IFujiXCommandPublisher;
20 import net.osdn.gokigen.gr2control.camera.fuji_x.wrapper.command.messages.CommandGeneric;
21 import net.osdn.gokigen.gr2control.camera.fuji_x.wrapper.command.messages.FinishRecordingMovie;
22 import net.osdn.gokigen.gr2control.camera.fuji_x.wrapper.command.messages.IFujiXCameraCommands;
23 import net.osdn.gokigen.gr2control.camera.fuji_x.wrapper.command.messages.StartRecordingMovie;
24 import net.osdn.gokigen.gr2control.camera.utils.SimpleLogDumper;
25
26 import java.util.List;
27
28 public class LiveViewFujiXKeyPanelClickListener implements View.OnClickListener, View.OnLongClickListener, IFujiXCommandCallback
29 {
30     private final String TAG = toString();
31     private final FragmentActivity activity;
32     private static final boolean isDumpLog = false;
33     private final IInterfaceProvider interfaceProvider;
34     private final Vibrator vibrator;
35     private boolean isRecordingVideoMovie = false;
36     private boolean commandIssued = false;
37     private int startMovieSequenceNumber = 0;
38
39     LiveViewFujiXKeyPanelClickListener(@NonNull FragmentActivity activity, @NonNull IInterfaceProvider interfaceProvider, @Nullable Vibrator vibrator)
40     {
41         this.activity = activity;
42         this.interfaceProvider = interfaceProvider;
43         this.vibrator = vibrator;
44     }
45
46     @Override
47     public void onClick(View v)
48     {
49         boolean isVibrate = true;
50         try
51         {
52             int id = v.getId();
53             switch (id)
54             {
55                 case R.id.button_fuji_x_sv_minus:
56                     updateValue(IFujiXCameraCommands.SHUTTER_SPEED, 0);
57                     break;
58                 case R.id.button_fuji_x_sv_plus:
59                     updateValue(IFujiXCameraCommands.SHUTTER_SPEED, 1);
60                     break;
61                 case R.id.button_fuji_x_tv_minus:
62                     updateValue(IFujiXCameraCommands.APERTURE, 0);
63                     break;
64                 case R.id.button_fuji_x_tv_plus:
65                     updateValue(IFujiXCameraCommands.APERTURE, 1);
66                     break;
67                 case R.id.button_fuji_x_xv_minus:
68                     updateValue(IFujiXCameraCommands.EXPREV, 0);
69                     break;
70                 case R.id.button_fuji_x_xv_plus:
71                     updateValue(IFujiXCameraCommands.EXPREV, 1);
72                     break;
73                 case R.id.button_fuji_x_flash:
74                     updateSelection(ICameraStatus.FLASH_XV);
75                     isVibrate = false;
76                     break;
77                 case R.id.button_fuji_x_timer:
78                     updateSelection(ICameraStatus.SELF_TIMER);
79                     isVibrate = false;
80                     break;
81                 case R.id.button_fuji_x_video_on_off:
82                     startFinishMovie();
83                     break;
84                 default:
85                     isVibrate = false;
86                     break;
87             }
88             vibrate(isVibrate);
89         }
90         catch (Exception e)
91         {
92             e.printStackTrace();
93         }
94     }
95
96     @Override
97     public boolean onLongClick(View v)
98     {
99         boolean isVibrate = false;
100         boolean ret = false;
101         try
102         {
103             int id = v.getId();
104             switch (id)
105             {
106                 case R.id.button_fuji_x_sv_minus:
107                     break;
108                 case R.id.button_fuji_x_sv_plus:
109                     break;
110                 case R.id.button_fuji_x_tv_minus:
111                     break;
112                 case R.id.button_fuji_x_tv_plus:
113                     break;
114                 case R.id.button_fuji_x_xv_minus:
115                     break;
116                 case R.id.button_fuji_x_xv_plus:
117                     break;
118                 case R.id.button_fuji_x_flash:
119                     break;
120                 case R.id.button_fuji_x_timer:
121                     break;
122                 default:
123                     break;
124             }
125             vibrate(isVibrate);
126         }
127         catch (Exception e)
128         {
129             e.printStackTrace();
130         }
131         return (ret);
132     }
133
134     /**
135      *  値を更新する
136      *
137      */
138     private void updateValue(int id, int value)
139     {
140         try
141         {
142             IFujiXCommandPublisher publisher = interfaceProvider.getFujiXInterfaceProvider().getCommandPublisher();
143             publisher.enqueueCommand(new CommandGeneric(this, id, 4, value));
144         }
145         catch (Exception e)
146         {
147             e.printStackTrace();
148         }
149     }
150
151     /**
152      *  選択肢を更新する
153      *
154      */
155     private void updateSelection(@NonNull final String key)
156     {
157         try
158         {
159             Log.v(TAG, "updateSelection() : " + key);
160             final ICameraStatus statusList = interfaceProvider.getCameraStatusListHolder();
161             if (statusList == null)
162             {
163                 // ステータスリストの保持クラスが取れなかった...
164                 Log.w(TAG, "ICameraStatus is NULL...");
165                 return;
166             }
167             final String current = statusList.getStatus(key);
168             final List<String> itemList = statusList.getStatusList(key);
169             if (itemList.size() <= 0)
170             {
171                 // アイテム(選択肢)が登録されていなければ、何もしない
172                 return;
173             }
174
175             AlertDialog.Builder builder = new AlertDialog.Builder(activity);
176
177             // リストから配列に積み替え...
178             String[] items = new String[itemList.size()];
179             int index = 0;
180             for (String item : itemList)
181             {
182                 Log.v(TAG, " (" + index + ") " + item);
183                 items[index] = item;
184                 index++;
185             }
186             int position = itemList.indexOf(current);
187             Log.v(TAG, " updateSelection() : " + key + " " + itemList.size() + " " + position);
188             builder.setSingleChoiceItems(items, position, new DialogInterface.OnClickListener() {
189                 @Override
190                 public void onClick(DialogInterface dialogInterface, int i)
191                 {
192                     String choice = itemList.get(i);
193                     Log.v(TAG, key + " ITEM CHOICED : " + choice + "(CURRENT : " + current + ")");
194
195                     statusList.setStatus(key, choice);
196                     dialogInterface.dismiss();
197                 }
198             });
199             builder.show();
200         }
201         catch (Exception e)
202         {
203             e.printStackTrace();
204         }
205     }
206
207     /**
208      *   ビデオ撮影開始と終了
209      *
210      */
211     private void startFinishMovie()
212     {
213         try
214         {
215             if (commandIssued)
216             {
217                 // すでにコマンド発行中。コマンドの発行は抑止する
218                 Log.v(TAG, " COMMAND IS ALREADY ISSUED...");
219                 return;
220             }
221             commandIssued = true;
222
223             IFujiXCommandPublisher publisher = interfaceProvider.getFujiXInterfaceProvider().getCommandPublisher();
224             if (isRecordingVideoMovie)
225             {
226                 // 撮影中の場合には、撮影を止める
227                 publisher.enqueueCommand(new FinishRecordingMovie(new IFujiXCommandCallback() {
228                     @Override
229                     public void receivedMessage(int id, byte[] rx_body)
230                     {
231                         commandIssued = false;
232                         isRecordingVideoMovie = false;
233                         updateMovieRecordingIcon();
234                         SimpleLogDumper.dump_bytes(" STOP MOVIE REPLY (" + startMovieSequenceNumber + ") ", rx_body);
235                         startMovieSequenceNumber = 0;
236                     }
237
238                     @Override
239                     public void onReceiveProgress(int currentBytes, int totalBytes, byte[] rx_body)
240                     {
241                         // 何もしない
242                     }
243
244                     @Override
245                     public boolean isReceiveMulti()
246                     {
247                         return (false);
248                     }
249                 }, startMovieSequenceNumber));
250             }
251             else
252             {
253                 // 撮影を開始する
254                 publisher.enqueueCommand(new StartRecordingMovie(new IFujiXCommandCallback() {
255                     @Override
256                     public void receivedMessage(int id, byte[] rx_body)
257                     {
258                         commandIssued = false;
259                         if ((rx_body[7] == (byte) 0x20)&&(rx_body[6] == (byte) 0x01))
260                         {
261                             // 応答コード OKの場合... SequenceNumberを記憶する
262                             startMovieSequenceNumber = ((((int) rx_body[11]) & 0xff) << 24) + ((((int) rx_body[10]) & 0xff) << 16) + ((((int) rx_body[9]) & 0xff) << 8) + (((int) rx_body[8]) & 0xff);
263                             isRecordingVideoMovie = true;
264                         }
265                         updateMovieRecordingIcon();
266                         SimpleLogDumper.dump_bytes(" START MOVIE REPLY (" + startMovieSequenceNumber + ") ", rx_body);
267                     }
268
269                     @Override
270                     public void onReceiveProgress(int currentBytes, int totalBytes, byte[] rx_body)
271                     {
272                         // 何もしない
273                     }
274
275                     @Override
276                     public boolean isReceiveMulti()
277                     {
278                         return (false);
279                     }
280                 }));
281             }
282         }
283         catch (Exception e)
284         {
285             e.printStackTrace();
286             commandIssued = false;
287         }
288     }
289
290     private void updateMovieRecordingIcon()
291     {
292         try
293         {
294             activity.runOnUiThread(new Runnable() {
295                 @Override
296                 public void run() {
297                     ImageView imageView = activity.findViewById(R.id.button_fuji_x_video_on_off);
298                     if (isRecordingVideoMovie)
299                     {
300                         //imageView.setImageDrawable(ContextCompat.getDrawable(activity,R.drawable.ic_videocam_off_black_24dp));
301                         imageView.setImageDrawable(ContextCompat.getDrawable(activity,R.drawable.ic_stop_black_24dp));
302                     }
303                     else
304                     {
305                         imageView.setImageDrawable(ContextCompat.getDrawable(activity,R.drawable.ic_videocam_black__24dp));
306                     }
307                     imageView.invalidate();
308                 }
309             });
310         }
311         catch (Exception e)
312         {
313             e.printStackTrace();
314         }
315     }
316
317
318
319
320
321     /**
322      *   ぶるぶるさせる
323      *
324      */
325     private void vibrate(boolean isVibrate)
326     {
327         if ((vibrator != null)&&(isVibrate))
328         {
329             vibrator.vibrate(30);
330         }
331     }
332
333     @Override
334     public void receivedMessage(int id, byte[] rx_body)
335     {
336         if (isDumpLog)
337         {
338             SimpleLogDumper.dump_bytes("" + id, rx_body);
339         }
340     }
341
342     @Override
343     public void onReceiveProgress(int currentBytes, int totalBytes, byte[] rx_body)
344     {
345         Log.v(TAG, " onReceiveProgress() : " + currentBytes + "/" + totalBytes);
346     }
347
348     @Override
349     public boolean isReceiveMulti()
350     {
351         return (false);
352     }
353 }