OSDN Git Service

ファイルのカウントの情報をちょっと更新。
[gokigen/A01d.git] / app / src / main / java / net / osdn / gokigen / a01d / camera / fujix / cameraproperty / FujiXCameraCommandSendDialog.java
1 package net.osdn.gokigen.a01d.camera.fujix.cameraproperty;
2
3 import android.app.Activity;
4 import android.app.Dialog;
5 import android.content.DialogInterface;
6 import android.os.Bundle;
7 import android.util.Log;
8 import android.view.LayoutInflater;
9 import android.view.View;
10 import android.widget.AdapterView;
11 import android.widget.ArrayAdapter;
12 import android.widget.Button;
13 import android.widget.EditText;
14 import android.widget.Spinner;
15 import android.widget.TextView;
16
17 import androidx.annotation.NonNull;
18 import androidx.appcompat.app.AlertDialog;
19 import androidx.collection.SparseArrayCompat;
20 import androidx.fragment.app.DialogFragment;
21
22 import net.osdn.gokigen.a01d.R;
23 import net.osdn.gokigen.a01d.camera.fujix.IFujiXInterfaceProvider;
24 import net.osdn.gokigen.a01d.camera.fujix.wrapper.command.IFujiXCameraCommands;
25 import net.osdn.gokigen.a01d.camera.fujix.wrapper.command.IFujiXCommandPublisher;
26 import net.osdn.gokigen.a01d.camera.fujix.wrapper.command.messages.CommandGeneric;
27 import net.osdn.gokigen.a01d.camera.fujix.wrapper.command.messages.SetPropertyValue;
28 import net.osdn.gokigen.a01d.camera.fujix.wrapper.connection.FujiXCameraModeChangeToLiveView;
29 import net.osdn.gokigen.a01d.camera.fujix.wrapper.connection.FujiXCameraModeChangeToPlayback;
30 import net.osdn.gokigen.a01d.camera.fujix.wrapper.status.IFujiXCameraProperties;
31
32 public class FujiXCameraCommandSendDialog  extends DialogFragment
33 {
34     private final String TAG = toString();
35     private Dialog myDialog = null;
36     private IFujiXCommandPublisher commandPublisher = null;
37     private FujiXCameraCommandResponse responseReceiver = null;
38     private SparseArrayCompat<String> commandNameIndexArray;
39
40     private int selectedCommandIdPosition = 0;
41     private int selectedMessageTypePosition = 0;
42     private int selectedBodyLengthPosition = 0;
43
44     public static FujiXCameraCommandSendDialog newInstance(@NonNull IFujiXInterfaceProvider interfaceProvider)
45     {
46         FujiXCameraCommandSendDialog instance = new FujiXCameraCommandSendDialog();
47         instance.prepare(interfaceProvider);
48
49         // パラメータはBundleにまとめておく
50         Bundle arguments = new Bundle();
51         //arguments.putString("method", method);
52         //arguments.putString("message", message);
53         instance.setArguments(arguments);
54
55         return (instance);
56     }
57
58     private void prepare(@NonNull IFujiXInterfaceProvider interfaceProvider)
59     {
60         this.commandPublisher = interfaceProvider.getCommandPublisher();
61         this.commandNameIndexArray = new SparseArrayCompat<>();
62     }
63
64     @Override
65     public @NonNull Dialog onCreateDialog(Bundle savedInstanceState)
66     {
67         final Activity activity = getActivity();
68         final AlertDialog.Builder alertDialog = new AlertDialog.Builder(activity);
69
70         // Get the layout inflater
71         LayoutInflater inflater = activity.getLayoutInflater();
72         final View alertView = inflater.inflate(R.layout.fujix_request_command_layout, null, false);
73         alertDialog.setView(alertView);
74
75         alertDialog.setIcon(R.drawable.ic_linked_camera_black_24dp);
76         alertDialog.setTitle(getString(R.string.dialog_fujix_command_title_command));
77         try
78         {
79             final TextView commandResponse = alertView.findViewById(R.id.command_response_value);
80             final EditText edit_command_id = alertView.findViewById(R.id.edit_command_id);
81             final EditText edit_message_body1 = alertView.findViewById(R.id.edit_message_body1);
82             final EditText edit_message_body2 = alertView.findViewById(R.id.edit_message_body2);
83             final Spinner selection_command_id = alertView.findViewById(R.id.spinner_selection_command_id);
84             final Spinner selection_message_type = alertView.findViewById(R.id.spinner_selection_message_type);
85             final Spinner selection_message_body_length = alertView.findViewById(R.id.spinner_selection_message_body_length);
86             final Button sendButton = alertView.findViewById(R.id.send_message_button);
87             final Button liveViewButton = alertView.findViewById(R.id.change_to_liveview);
88             final Button playbackButton = alertView.findViewById(R.id.change_to_playback);
89
90             responseReceiver = new FujiXCameraCommandResponse(activity, commandResponse);
91
92             initializeCommandSelection(activity, selection_command_id, edit_command_id);
93             initializeMessageTypeSelection(activity, selection_message_type);
94             initializeBodyLengthSelection(activity, selection_message_body_length);
95
96             sendButton.setOnClickListener(new View.OnClickListener()
97             {
98                 @Override
99                 public void onClick(View v)
100                 {
101                     try
102                     {
103                         //Log.v(TAG, "SEND COMMAND");
104                         if (responseReceiver != null)
105                         {
106                             responseReceiver.clear();
107                             int id = parseInt(edit_command_id);
108                             int value1 = parseInt(edit_message_body1);
109                             int value2 = parseInt(edit_message_body2);
110                             if (selectedMessageTypePosition == 0)
111                             {
112                                 // single
113                                 if (selectedBodyLengthPosition == 0)
114                                 {
115                                     commandPublisher.enqueueCommand(new CommandGeneric(responseReceiver, id));
116                                 }
117                                 else if (selectedBodyLengthPosition == 3)
118                                 {
119                                     commandPublisher.enqueueCommand(new CommandGeneric(responseReceiver, id, 8, value1, value2));
120                                 }
121                                 else if (selectedBodyLengthPosition == 2)
122                                 {
123                                     commandPublisher.enqueueCommand(new CommandGeneric(responseReceiver, id, 4, value1));
124                                 }
125                                 else
126                                 {
127                                     commandPublisher.enqueueCommand(new CommandGeneric(responseReceiver, id, 2, value1));
128                                 }
129                             }
130                             else
131                             {
132                                 // multi
133                                 if (selectedBodyLengthPosition == 0)
134                                 {
135                                     commandPublisher.enqueueCommand(new SetPropertyValue(responseReceiver, id));
136                                 }
137                                 else if (selectedBodyLengthPosition == 3)
138                                 {
139                                     commandPublisher.enqueueCommand(new SetPropertyValue(responseReceiver, id, 8, value1, value2));
140                                 }
141                                 else if (selectedBodyLengthPosition == 2)
142                                 {
143                                     commandPublisher.enqueueCommand(new SetPropertyValue(responseReceiver, id, 4, value1));
144                                 }
145                                 else
146                                 {
147                                     commandPublisher.enqueueCommand(new SetPropertyValue(responseReceiver, id, 2, value1));
148                                 }
149                             }
150                         }
151                     }
152                     catch (Exception e)
153                     {
154                         e.printStackTrace();
155                     }
156                 }
157             });
158             if ((responseReceiver != null)&&(commandPublisher != null))
159             {
160                 liveViewButton.setOnClickListener(new FujiXCameraModeChangeToLiveView(commandPublisher, responseReceiver));
161                 playbackButton.setOnClickListener(new FujiXCameraModeChangeToPlayback(commandPublisher, responseReceiver));
162             }
163         }
164         catch (Exception e)
165         {
166             e.printStackTrace();
167         }
168         alertDialog.setCancelable(true);
169
170         // ボタンを設定する(実行ボタン)
171         alertDialog.setPositiveButton(activity.getString(R.string.dialog_positive_execute),
172                 new DialogInterface.OnClickListener() {
173                     public void onClick(DialogInterface dialog, int which)
174                     {
175                         //dialog.dismiss();
176                     }
177                 });
178
179         // ボタンを設定する (キャンセルボタン)
180         alertDialog.setNegativeButton(activity.getString(R.string.dialog_negative_cancel),
181                 new DialogInterface.OnClickListener() {
182                     public void onClick(DialogInterface dialog, int which) {
183                         dialog.cancel();
184                     }
185                 });
186
187         // 確認ダイアログを応答する
188         myDialog = alertDialog.create();
189         return (myDialog);
190     }
191
192     @Override
193     public void onPause()
194     {
195         super.onPause();
196         Log.v(TAG, "AlertDialog::onPause()");
197         if (myDialog != null)
198         {
199             myDialog.cancel();
200         }
201     }
202
203     private int parseInt(EditText area)
204     {
205         try
206         {
207             String value = (area.getText().toString()).toLowerCase();
208             int index =  value.indexOf("x");
209             if (index > 0)
210             {
211                 value = value.substring(index + 1);
212             }
213             if (value.length() < 1)
214             {
215                 // 未入力のときには0を返す
216                 return (0);
217             }
218             //int convertValue = (int)Long.parseLong(value, 16);
219             //Log.v(TAG, String.format(Locale.US, "PARSED VALUE : 0x%08x (%d)", convertValue, convertValue));
220             //return (convertValue);
221             return ((int)Long.parseLong(value, 16));
222         }
223         catch (Exception e)
224         {
225             e.printStackTrace();
226             Log.v(TAG, "[" + area.getText().toString() + "]");
227         }
228         return (-1);
229     }
230
231
232     private ArrayAdapter<String> prepareCommandAdapter(@NonNull final Activity activity)
233     {
234         int position = 0;
235         ArrayAdapter<String> adapter = new ArrayAdapter<>(activity, android.R.layout.simple_spinner_item);
236         commandNameIndexArray.clear();
237
238         // せっせとコマンドを入れていく...
239         adapter.add("(Direct Input)");
240         commandNameIndexArray.append(position++, "");
241
242         adapter.add(IFujiXCameraCommands.SHUTTER_STR + " (" + IFujiXCameraCommands.SHUTTER_STR_ID + ")");
243         commandNameIndexArray.append(position++, IFujiXCameraCommands.SHUTTER_STR_ID);
244
245         adapter.add(IFujiXCameraCommands.FOCUS_POINT_STR + " (" + IFujiXCameraCommands.FOCUS_POINT_STR_ID + ")");
246         commandNameIndexArray.append(position++, IFujiXCameraCommands.FOCUS_POINT_STR_ID);
247
248         adapter.add(IFujiXCameraCommands.FOCUS_UNLOCK_STR + " (" + IFujiXCameraCommands.FOCUS_UNLOCK_STR_ID + ")");
249         commandNameIndexArray.append(position++, IFujiXCameraCommands.FOCUS_UNLOCK_STR_ID);
250
251         adapter.add(IFujiXCameraCommands.SHUTTER_SPEED_STR + " (" + IFujiXCameraCommands.SHUTTER_SPEED_STR_ID + ")");
252         commandNameIndexArray.append(position++, IFujiXCameraCommands.SHUTTER_SPEED_STR_ID);
253
254         adapter.add(IFujiXCameraCommands.APERTURE_STR + " (" + IFujiXCameraCommands.APERTURE_STR_ID + ")");
255         commandNameIndexArray.append(position++, IFujiXCameraCommands.APERTURE_STR_ID);
256
257         adapter.add(IFujiXCameraCommands.EXPREV_STR + " (" + IFujiXCameraCommands.EXPREV_STR_ID + ")");
258         commandNameIndexArray.append(position++, IFujiXCameraCommands.EXPREV_STR_ID);
259
260         adapter.add(IFujiXCameraProperties.WHITE_BALANCE_STR + " (" + IFujiXCameraProperties.WHITE_BALANCE_STR_ID + ")");
261         commandNameIndexArray.append(position++, IFujiXCameraProperties.WHITE_BALANCE_STR_ID);
262
263         adapter.add(IFujiXCameraProperties.EXPOSURE_COMPENSATION_STR + " (" + IFujiXCameraProperties.EXPOSURE_COMPENSATION_STR_ID + ")");
264         commandNameIndexArray.append(position++, IFujiXCameraProperties.EXPOSURE_COMPENSATION_STR_ID);
265
266         adapter.add(IFujiXCameraProperties.APERTURE_STR + " (" + IFujiXCameraProperties.APERTURE_STR_ID + ")");
267         commandNameIndexArray.append(position++, IFujiXCameraProperties.APERTURE_STR_ID);
268
269         adapter.add(IFujiXCameraProperties.SHUTTER_SPEED_STR + " (" + IFujiXCameraProperties.SHUTTER_SPEED_STR_ID + ")");
270         commandNameIndexArray.append(position++, IFujiXCameraProperties.SHUTTER_SPEED_STR_ID);
271
272         adapter.add(IFujiXCameraProperties.SELF_TIMER_STR + " (" + IFujiXCameraProperties.SELF_TIMER_STR_ID + ")");
273         commandNameIndexArray.append(position++, IFujiXCameraProperties.SELF_TIMER_STR_ID);
274
275         adapter.add(IFujiXCameraProperties.FILM_SIMULATION_STR + " (" + IFujiXCameraProperties.FILM_SIMULATION_STR_ID + ")");
276         commandNameIndexArray.append(position++, IFujiXCameraProperties.FILM_SIMULATION_STR_ID);
277
278         adapter.add(IFujiXCameraProperties.ISO_STR + " (" + IFujiXCameraProperties.ISO_STR_ID + ")");
279         commandNameIndexArray.append(position++, IFujiXCameraProperties.ISO_STR_ID);
280
281         adapter.add(IFujiXCameraProperties.MOVIE_ISO_STR + " (" + IFujiXCameraProperties.MOVIE_ISO_STR_ID + ")");
282         commandNameIndexArray.append(position++, IFujiXCameraProperties.MOVIE_ISO_STR_ID);
283
284         adapter.add(IFujiXCameraProperties.IMAGE_FORMAT_STR + " (" + IFujiXCameraProperties.IMAGE_FORMAT_STR_ID + ")");
285         commandNameIndexArray.append(position++, IFujiXCameraProperties.IMAGE_FORMAT_STR_ID);
286
287         adapter.add(IFujiXCameraProperties.IMAGE_ASPECT_STR + " (" + IFujiXCameraProperties.IMAGE_ASPECT_STR_ID + ")");
288         commandNameIndexArray.append(position++, IFujiXCameraProperties.IMAGE_ASPECT_STR_ID);
289
290         adapter.add(IFujiXCameraProperties.FLASH_STR + " (" + IFujiXCameraProperties.FLASH_STR_ID + ")");
291         commandNameIndexArray.append(position++, IFujiXCameraProperties.FLASH_STR_ID);
292
293         adapter.add(IFujiXCameraProperties.F_SS_CONTROL_STR + " (" + IFujiXCameraProperties.F_SS_CONTROL_STR_ID + ")");
294         commandNameIndexArray.append(position++, IFujiXCameraProperties.F_SS_CONTROL_STR_ID);
295
296         adapter.add(IFujiXCameraProperties.RECMODE_ENABLE_STR + " (" + IFujiXCameraProperties.RECMODE_ENABLE_STR_ID + ")");
297         commandNameIndexArray.append(position++, IFujiXCameraProperties.RECMODE_ENABLE_STR_ID);
298
299         adapter.add(IFujiXCameraProperties.BATTERY_LEVEL_STR + " (" + IFujiXCameraProperties.BATTERY_LEVEL_STR_ID + ")");
300         commandNameIndexArray.append(position++, IFujiXCameraProperties.BATTERY_LEVEL_STR_ID);
301
302         adapter.add(IFujiXCameraProperties.BATTERY_LEVEL_2_STR + " (" + IFujiXCameraProperties.BATTERY_LEVEL_2_STR_ID + ")");
303         commandNameIndexArray.append(position++, IFujiXCameraProperties.BATTERY_LEVEL_2_STR_ID);
304
305         adapter.add(IFujiXCameraProperties.FOCUS_MODE_STR + " (" + IFujiXCameraProperties.FOCUS_MODE_STR_ID + ")");
306         commandNameIndexArray.append(position++, IFujiXCameraProperties.FOCUS_MODE_STR_ID);
307
308         adapter.add(IFujiXCameraProperties.SHOOTING_MODE_STR + " (" + IFujiXCameraProperties.SHOOTING_MODE_STR_ID + ")");
309         commandNameIndexArray.append(position++, IFujiXCameraProperties.SHOOTING_MODE_STR_ID);
310
311         adapter.add(IFujiXCameraProperties.FOCUS_POINT_STR + " (" + IFujiXCameraProperties.FOCUS_POINT_STR_ID + ")");
312         commandNameIndexArray.append(position++, IFujiXCameraProperties.FOCUS_POINT_STR_ID);
313
314         adapter.add(IFujiXCameraProperties.FOCUS_LOCK_STR + " (" + IFujiXCameraProperties.FOCUS_LOCK_STR_ID + ")");
315         commandNameIndexArray.append(position++, IFujiXCameraProperties.FOCUS_LOCK_STR_ID);
316
317         adapter.add(IFujiXCameraProperties.SDCARD_REMAIN_SIZE_STR + " (" + IFujiXCameraProperties.SDCARD_REMAIN_SIZE_STR_ID + ")");
318         commandNameIndexArray.append(position++, IFujiXCameraProperties.SDCARD_REMAIN_SIZE_STR_ID);
319
320         adapter.add(IFujiXCameraProperties.MOVIE_REMAINING_TIME_STR + " (" + IFujiXCameraProperties.MOVIE_REMAINING_TIME_STR_ID + ")");
321         commandNameIndexArray.append(position++, IFujiXCameraProperties.MOVIE_REMAINING_TIME_STR_ID);
322
323         adapter.add(IFujiXCameraProperties.DEVICE_ERROR_STR + " (" + IFujiXCameraProperties.DEVICE_ERROR_STR_ID + ")");
324         commandNameIndexArray.append(position++, IFujiXCameraProperties.DEVICE_ERROR_STR_ID);
325
326         adapter.add(IFujiXCameraProperties.IMAGE_FILE_COUNT_STR + " (" + IFujiXCameraProperties.IMAGE_FILE_COUNT_STR_ID + ")");
327         commandNameIndexArray.append(position++, IFujiXCameraProperties.IMAGE_FILE_COUNT_STR_ID);
328
329         adapter.add(IFujiXCameraCommands.CAMERA_CAPABILITIES_STR + " (" + IFujiXCameraCommands.CAMERA_CAPABILITIES_STR_ID + ")");
330         commandNameIndexArray.append(position++, IFujiXCameraCommands.CAMERA_CAPABILITIES_STR_ID);
331
332         adapter.add(IFujiXCameraCommands.SINGLE_REQUEST_STR + " (" + IFujiXCameraCommands.SINGLE_REQUEST_STR_ID + ")");
333         commandNameIndexArray.append(position++, IFujiXCameraCommands.SINGLE_REQUEST_STR_ID);
334
335         adapter.add(IFujiXCameraCommands.STOP_STR + " (" + IFujiXCameraCommands.STOP_STR_ID + ")");
336         commandNameIndexArray.append(position++, IFujiXCameraCommands.STOP_STR_ID);
337
338         adapter.add(IFujiXCameraCommands.IMAGE_INFO_STR + " (" + IFujiXCameraCommands.IMAGE_INFO_STR_ID + ")");
339         commandNameIndexArray.append(position++, IFujiXCameraCommands.IMAGE_INFO_STR_ID);
340
341         adapter.add(IFujiXCameraCommands.THUMBNAIL_INDEX_STR + " (" + IFujiXCameraCommands.THUMBNAIL_INDEX_STR_ID + ")");
342         commandNameIndexArray.append(position++, IFujiXCameraCommands.THUMBNAIL_INDEX_STR_ID);
343
344         adapter.add(IFujiXCameraCommands.FULL_IMAGE_STR + " (" + IFujiXCameraCommands.FULL_IMAGE_STR_ID + ")");
345         commandNameIndexArray.append(position++, IFujiXCameraCommands.FULL_IMAGE_STR_ID);
346
347         adapter.add(IFujiXCameraCommands.LAST_IMAGE_CAMERA_STR + " (" + IFujiXCameraCommands.LAST_IMAGE_CAMERA_STR_ID + ")");
348         commandNameIndexArray.append(position, IFujiXCameraCommands.LAST_IMAGE_CAMERA_STR_ID);
349
350         return (adapter);
351     }
352
353     private void initializeCommandSelection(@NonNull final Activity activity, final Spinner spinner, final EditText commandIdArea)
354     {
355         try
356         {
357             commandIdArea.setText("");
358             ArrayAdapter<String> adapter = prepareCommandAdapter(activity);
359             spinner.setAdapter(adapter);
360             spinner.setSelection(selectedCommandIdPosition);
361             spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
362             {
363                 @Override
364                 public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
365                 {
366                     Log.v(TAG, "onItemSelected : " + position + " (" + id + ")");
367                     try
368                     {
369                         selectedCommandIdPosition = position;
370                         commandIdArea.setText(commandNameIndexArray.get(position, ""));
371                     }
372                     catch (Exception e)
373                     {
374                         e.printStackTrace();
375                     }
376                 }
377
378                 @Override
379                 public void onNothingSelected(AdapterView<?> parent)
380                 {
381                     Log.v(TAG, "onNothingSelected");
382                 }
383             });
384         }
385         catch (Exception e)
386         {
387             e.printStackTrace();
388         }
389     }
390
391     private void initializeMessageTypeSelection(final Activity activity, final Spinner spinner)
392     {
393         try
394         {
395             ArrayAdapter<String> adapter = new ArrayAdapter<>(activity, android.R.layout.simple_spinner_item);
396             adapter.add("Command(Single)");
397             adapter.add("Property(Multi)");
398
399             spinner.setAdapter(adapter);
400             spinner.setSelection(selectedMessageTypePosition);
401             spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
402             {
403                 @Override
404                 public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
405                 {
406                     Log.v(TAG, "onItemSelected : " + position + " (" + id + ")");
407                     selectedMessageTypePosition = position;
408                 }
409
410                 @Override
411                 public void onNothingSelected(AdapterView<?> parent)
412                 {
413                     Log.v(TAG, "onNothingSelected");
414                 }
415             });
416         }
417         catch (Exception e)
418         {
419             e.printStackTrace();
420         }
421     }
422
423     private void initializeBodyLengthSelection(final Activity activity, final Spinner spinner)
424     {
425         try
426         {
427             ArrayAdapter<String> adapter = new ArrayAdapter<>(activity, android.R.layout.simple_spinner_item);
428             adapter.add("0");
429             adapter.add("2");
430             adapter.add("4");
431             adapter.add("8");
432
433             spinner.setAdapter(adapter);
434             spinner.setSelection(selectedBodyLengthPosition);
435             spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
436             {
437                 @Override
438                 public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
439                 {
440                     Log.v(TAG, "onItemSelected : " + position + " (" + id + ")");
441                     selectedBodyLengthPosition = position;
442                 }
443
444                 @Override
445                 public void onNothingSelected(AdapterView<?> parent)
446                 {
447                     Log.v(TAG, "onNothingSelected");
448                 }
449             });
450         }
451         catch (Exception e)
452         {
453             e.printStackTrace();
454         }
455     }
456 }