OSDN Git Service

Olympusの送信ダイアログをhttp用のダイアログに役割拡大。
[gokigen/A01d.git] / app / src / main / java / net / osdn / gokigen / a01d / A01dMain.java
1 package net.osdn.gokigen.a01d;
2
3 import android.Manifest;
4 import android.content.SharedPreferences;
5 import android.content.pm.PackageManager;
6 import android.os.Bundle;
7 import android.util.Log;
8 import android.view.KeyEvent;
9 import android.view.WindowManager;
10
11 import net.osdn.gokigen.a01d.camera.CameraInterfaceProvider;
12 import net.osdn.gokigen.a01d.camera.IInterfaceProvider;
13 import net.osdn.gokigen.a01d.camera.fujix.cameraproperty.FujiXCameraCommandSendDialog;
14 import net.osdn.gokigen.a01d.camera.olympus.cameraproperty.OlyCameraPropertyListFragment;
15 import net.osdn.gokigen.a01d.camera.ICameraStatusReceiver;
16 import net.osdn.gokigen.a01d.camera.ICameraConnection;
17 import net.osdn.gokigen.a01d.camera.olympus.wrapper.connection.ble.ICameraPowerOn;
18 import net.osdn.gokigen.a01d.camera.utils.SimpleHttpSendCommandDialog;
19 import net.osdn.gokigen.a01d.camera.panasonic.operation.PanasonicSendCommandDialog;
20 import net.osdn.gokigen.a01d.camera.ricohgr2.operation.RicohGr2SendCommandDialog;
21 import net.osdn.gokigen.a01d.camera.sony.cameraproperty.SonyCameraApiListFragment;
22 import net.osdn.gokigen.a01d.liveview.IStatusViewDrawer;
23 import net.osdn.gokigen.a01d.liveview.LiveViewFragment;
24 import net.osdn.gokigen.a01d.logcat.LogCatFragment;
25 import net.osdn.gokigen.a01d.preference.IPreferencePropertyAccessor;
26 import net.osdn.gokigen.a01d.preference.fujix.FujiXPreferenceFragment;
27 import net.osdn.gokigen.a01d.preference.olympus.PreferenceFragment;
28 import net.osdn.gokigen.a01d.preference.panasonic.PanasonicPreferenceFragment;
29 import net.osdn.gokigen.a01d.preference.olympuspen.OlympusPreferenceFragment;
30 import net.osdn.gokigen.a01d.preference.ricohgr2.RicohGr2PreferenceFragment;
31 import net.osdn.gokigen.a01d.preference.sony.SonyPreferenceFragment;
32
33 import androidx.annotation.NonNull;
34 import androidx.appcompat.app.ActionBar;
35 import androidx.appcompat.app.AppCompatActivity;
36 import androidx.core.app.ActivityCompat;
37 import androidx.core.content.ContextCompat;
38 import androidx.fragment.app.FragmentTransaction;
39 import androidx.preference.PreferenceFragmentCompat;
40 import androidx.preference.PreferenceManager;
41
42 import java.util.HashMap;
43 import java.util.Map;
44
45 /**
46  *   A01d ;
47  *
48  */
49 public class A01dMain extends AppCompatActivity implements ICameraStatusReceiver, IChangeScene, ICameraPowerOn.PowerOnCameraCallback
50 {
51     private final String TAG = toString();
52     private IInterfaceProvider interfaceProvider = null;
53     private IStatusViewDrawer statusViewDrawer = null;
54
55     private PreferenceFragmentCompat preferenceFragment = null;
56     private OlyCameraPropertyListFragment propertyListFragment = null;
57     private SonyCameraApiListFragment sonyApiListFragmentSony = null;
58     private LogCatFragment logCatFragment = null;
59     private LiveViewFragment liveViewFragment = null;
60
61     @Override
62     protected void onCreate(Bundle savedInstanceState)
63     {
64         final int REQUEST_NEED_PERMISSIONS = 1010;
65
66         super.onCreate(savedInstanceState);
67 /*
68         try {
69             // 全画面表示...
70             if (Build.VERSION.SDK_INT >= 19)
71             {
72                 View decor = this.getWindow().getDecorView();
73                 decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
74             } else {
75                 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
76             }
77         }
78         catch (Exception e)
79         {
80             e.printStackTrace();
81         }
82 */
83         setContentView(R.layout.activity_a01d_main);
84
85         ActionBar bar = getSupportActionBar();
86         if (bar != null) {
87             // タイトルバーは表示しない
88             bar.hide();
89         }
90         getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
91
92         // 外部メモリアクセス権のオプトイン
93         if ((ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) ||
94                 (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) ||
95                 (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_MEDIA_LOCATION) != PackageManager.PERMISSION_GRANTED) ||
96                 (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_NETWORK_STATE) != PackageManager.PERMISSION_GRANTED) ||
97                 (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_WIFI_STATE) != PackageManager.PERMISSION_GRANTED) ||
98                 (ContextCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH) != PackageManager.PERMISSION_GRANTED) ||
99                 (ContextCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_ADMIN) != PackageManager.PERMISSION_GRANTED) ||
100                 (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) ||
101                 (ContextCompat.checkSelfPermission(this, Manifest.permission.INTERNET) != PackageManager.PERMISSION_GRANTED)) {
102             ActivityCompat.requestPermissions(this,
103                     new String[]{
104                             Manifest.permission.WRITE_EXTERNAL_STORAGE,
105                             Manifest.permission.READ_EXTERNAL_STORAGE,
106                             Manifest.permission.ACCESS_MEDIA_LOCATION,
107                             Manifest.permission.ACCESS_NETWORK_STATE,
108                             Manifest.permission.ACCESS_WIFI_STATE,
109                             Manifest.permission.BLUETOOTH,
110                             Manifest.permission.BLUETOOTH_ADMIN,
111                             Manifest.permission.ACCESS_COARSE_LOCATION,
112                             Manifest.permission.INTERNET,
113                     },
114                     REQUEST_NEED_PERMISSIONS);
115         }
116         initializeClass();
117         initializeFragment();
118         onReadyClass();
119     }
120
121     /**
122      *   なぜか、onReadyClass() が有効ではなさそうなので...
123      *
124      */
125     @Override
126     public void onRequestPermissionsResult(int requestCode, @NonNull String[]  permissions, @NonNull int[] grantResults)
127     {
128         super.onRequestPermissionsResult(requestCode, permissions, grantResults);
129         onReadyClass();
130     }
131
132     /**
133      * クラスの初期化
134      */
135     private void initializeClass()
136     {
137         try
138         {
139             interfaceProvider = new CameraInterfaceProvider(this, this);
140         }
141         catch (Exception e)
142         {
143             e.printStackTrace();
144         }
145     }
146
147     /**
148      * 初期化終了時の処理
149      */
150     private void onReadyClass()
151     {
152         if (isBlePowerOn())
153         {
154             // BLEでPower ONは、OPCのみ対応
155             if (interfaceProvider.getCammeraConnectionMethod() == ICameraConnection.CameraConnectionMethod.OPC)
156             {
157                 // BLEでカメラの電源をONにする設定だった時
158                 try
159                 {
160                     // カメラの電源ONクラスを呼び出しておく (電源ONができたら、コールバックをもらう)
161                     interfaceProvider.getOlympusInterface().getCameraPowerOn().wakeup(this);
162                 }
163                 catch (Exception e)
164                 {
165                     e.printStackTrace();
166                 }
167             }
168         }
169         else if (isAutoConnectCamera())
170         {
171             // 自動接続の指示があったとき
172             changeCameraConnection();
173         }
174     }
175
176     /**
177      * フラグメントの初期化
178      */
179     private void initializeFragment()
180     {
181         try
182         {
183             //if (liveViewFragment == null)
184             {
185                 liveViewFragment = LiveViewFragment.newInstance(this, interfaceProvider);
186             }
187             statusViewDrawer = liveViewFragment;
188             liveViewFragment.setRetainInstance(true);
189             FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
190             transaction.replace(R.id.fragment1, liveViewFragment);
191             transaction.commitAllowingStateLoss();
192         }
193         catch (Exception e)
194         {
195             e.printStackTrace();
196         }
197     }
198
199     /**
200      *
201      */
202     @Override
203     protected void onPause()
204     {
205         super.onPause();
206         try
207         {
208             ICameraConnection.CameraConnectionMethod method = interfaceProvider.getCammeraConnectionMethod();
209             ICameraConnection connection = getCameraConnection(method);
210             if (connection != null)
211             {
212                 connection.stopWatchWifiStatus(this);
213             }
214         }
215         catch (Exception e)
216         {
217             e.printStackTrace();
218         }
219     }
220
221     /**
222      * カメラのプロパティ一覧画面を開く
223      * (カメラと接続中のときのみ、接続方式が Olympusのときのみ)
224      */
225     @Override
226     public void changeSceneToCameraPropertyList()
227     {
228         try
229         {
230             ICameraConnection.CameraConnectionMethod method = interfaceProvider.getCammeraConnectionMethod();
231             ICameraConnection connection = getCameraConnection(method);
232             if (method == ICameraConnection.CameraConnectionMethod.RICOH_GR2)
233             {
234                 try
235                 {
236                     // Ricohの場合は、コマンド送信ダイアログを表示する
237                     RicohGr2SendCommandDialog.newInstance().show(getSupportFragmentManager(), "RicohGr2SendCommandDialog");
238                 }
239                 catch (Exception e)
240                 {
241                     e.printStackTrace();
242                 }
243             }
244             else if (method == ICameraConnection.CameraConnectionMethod.SONY)
245             {
246                 // SONYの場合は、API一覧画面へ遷移させる
247                 changeSceneToApiList();
248             }
249             else if (method == ICameraConnection.CameraConnectionMethod.PANASONIC)
250             {
251                 try
252                 {
253                     // Panasonicの場合は、コマンド送信ダイアログを表示する
254                     PanasonicSendCommandDialog.newInstance(interfaceProvider.getPanasonicInterface()).show(getSupportFragmentManager(), "panasonicSendCommandDialog");
255                 }
256                 catch (Exception e)
257                 {
258                     e.printStackTrace();
259                 }
260             }
261             else if (method == ICameraConnection.CameraConnectionMethod.FUJI_X)
262             {
263                 try
264                 {
265                     // FUJI X Seriesの場合は、コマンド送信ダイアログを表示する
266                     FujiXCameraCommandSendDialog.newInstance(interfaceProvider.getFujiXInterface()).show(getSupportFragmentManager(), "sendCommandDialog");
267
268                 }
269                 catch (Exception e)
270                 {
271                     e.printStackTrace();
272                 }
273             }
274             else if (method == ICameraConnection.CameraConnectionMethod.OLYMPUS)
275             {
276                 try
277                 {
278                     Map<String, String> headerMap = new HashMap<>();
279                     headerMap.put("User-Agent", "OlympusCameraKit"); // "OI.Share"
280                     headerMap.put("X-Protocol", "OlympusCameraKit"); // "OI.Share"
281
282                     // Olympus Penの場合は、コマンド送信ダイアログを表示する
283                     SimpleHttpSendCommandDialog.newInstance("http://192.168.0.10/", interfaceProvider.getOlympusPenInterface().getLiveViewControl(), headerMap).show(getSupportFragmentManager(), "olympusPenSendCommandDialog");
284                 }
285                 catch (Exception e)
286                 {
287                     e.printStackTrace();
288                 }
289             }
290             else
291             {
292                 // OPC カメラの場合...
293                 if (connection != null)
294                 {
295                     ICameraConnection.CameraConnectionStatus status = connection.getConnectionStatus();
296                     if (status == ICameraConnection.CameraConnectionStatus.CONNECTED)
297                     {
298                         if (propertyListFragment == null)
299                         {
300                             propertyListFragment = OlyCameraPropertyListFragment.newInstance(this, interfaceProvider.getOlympusInterface().getCameraPropertyProvider());
301                         }
302                         FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
303                         transaction.replace(R.id.fragment1, propertyListFragment);
304                         // backstackに追加
305                         transaction.addToBackStack(null);
306                         transaction.commit();
307                     }
308                 }
309             }
310         }
311         catch (Exception e)
312         {
313             e.printStackTrace();
314         }
315     }
316
317     /**
318      *   設定画面を開く
319      *
320      */
321     @Override
322     public void changeSceneToConfiguration()
323     {
324         try
325         {
326             if (preferenceFragment == null)
327             {
328                 try
329                 {
330                     ICameraConnection.CameraConnectionMethod connectionMethod = interfaceProvider.getCammeraConnectionMethod();
331                     if (connectionMethod == ICameraConnection.CameraConnectionMethod.RICOH_GR2) {
332                         preferenceFragment = RicohGr2PreferenceFragment.newInstance(this, this);
333                     } else if (connectionMethod == ICameraConnection.CameraConnectionMethod.SONY) {
334                         preferenceFragment = SonyPreferenceFragment.newInstance(this, this);
335                     } else if (connectionMethod == ICameraConnection.CameraConnectionMethod.PANASONIC) {
336                         preferenceFragment = PanasonicPreferenceFragment.newInstance(this, this);
337                     } else if (connectionMethod == ICameraConnection.CameraConnectionMethod.OLYMPUS) {
338                         preferenceFragment = OlympusPreferenceFragment.newInstance(this, this);
339                     } else if (connectionMethod == ICameraConnection.CameraConnectionMethod.FUJI_X) {
340                         preferenceFragment = FujiXPreferenceFragment.newInstance(this, this);
341                     } else //  if (connectionMethod == ICameraConnection.CameraConnectionMethod.OPC)
342                     {
343                         preferenceFragment = PreferenceFragment.newInstance(this, interfaceProvider, this);
344                     }
345                 }
346                 catch (Exception e)
347                 {
348                     e.printStackTrace();
349                     preferenceFragment = SonyPreferenceFragment.newInstance(this, this);
350                 }
351             }
352
353             FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
354             transaction.replace(R.id.fragment1, preferenceFragment);
355             // backstackに追加
356             transaction.addToBackStack(null);
357             transaction.commit();
358         }
359         catch (Exception e)
360         {
361             e.printStackTrace();
362         }
363     }
364
365     /**
366      *   デバッグ情報画面を開く
367      *
368      */
369     @Override
370     public void changeSceneToDebugInformation()
371     {
372         if (logCatFragment == null)
373         {
374             logCatFragment = LogCatFragment.newInstance();
375         }
376         FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
377         transaction.replace(R.id.fragment1, logCatFragment);
378         // backstackに追加
379         transaction.addToBackStack(null);
380         transaction.commit();
381     }
382
383     /**
384      *   SonyのAPI List画面を開く
385      *
386      */
387     @Override
388     public void changeSceneToApiList()
389     {
390         if (sonyApiListFragmentSony == null)
391         {
392             sonyApiListFragmentSony = SonyCameraApiListFragment.newInstance(interfaceProvider);
393         }
394         FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
395         transaction.replace(R.id.fragment1, sonyApiListFragmentSony);
396         // backstackに追加
397         transaction.addToBackStack(null);
398         transaction.commit();
399     }
400
401     /**
402      *   カメラとの接続・切断のシーケンス
403      */
404     @Override
405     public void changeCameraConnection()
406     {
407         if (interfaceProvider == null)
408         {
409             Log.v(TAG, "changeCameraConnection() : interfaceProvider is NULL");
410             return;
411         }
412         try
413         {
414             ICameraConnection connection = getCameraConnection(interfaceProvider.getCammeraConnectionMethod());
415             if (connection != null)
416             {
417                 ICameraConnection.CameraConnectionStatus status = connection.getConnectionStatus();
418                 if (status == ICameraConnection.CameraConnectionStatus.CONNECTED)
419                 {
420                     // 接続中のときには切断する
421                     connection.disconnect(false);
422                     return;
423                 }
424                 // 接続中でない時は、接続中にする
425                 connection.startWatchWifiStatus(this);
426             }
427         }
428         catch (Exception e)
429         {
430             e.printStackTrace();
431         }
432     }
433
434     /**
435      * アプリを抜ける
436      */
437     @Override
438     public void exitApplication()
439     {
440         Log.v(TAG, "exitApplication()");
441         try
442         {
443             ICameraConnection connection = getCameraConnection(interfaceProvider.getCammeraConnectionMethod());
444             if (connection != null)
445             {
446                 connection.disconnect(true);
447             }
448             finish();
449         }
450         catch (Exception e)
451         {
452             e.printStackTrace();
453         }
454     }
455
456     /**
457      *
458      *
459      */
460     @Override
461     public void onStatusNotify(String message)
462     {
463         Log.v(TAG, " CONNECTION MESSAGE : " + message);
464         try
465         {
466             if (statusViewDrawer != null)
467             {
468                 statusViewDrawer.updateStatusView(message);
469                 ICameraConnection connection = getCameraConnection(interfaceProvider.getCammeraConnectionMethod());
470                 if (connection != null)
471                 {
472                     statusViewDrawer.updateConnectionStatus(connection.getConnectionStatus());
473                 }
474             }
475         }
476         catch (Exception e)
477         {
478             e.printStackTrace();
479         }
480     }
481
482     /**
483      *
484      *
485      */
486     @Override
487     public void onCameraConnected()
488     {
489         Log.v(TAG, "onCameraConnected()");
490
491         try
492         {
493             ICameraConnection connection = getCameraConnection(interfaceProvider.getCammeraConnectionMethod());
494             if (connection != null)
495             {
496                 // クラス構造をミスった...のでこんなところで、無理やりステータスを更新する
497                 connection.forceUpdateConnectionStatus(ICameraConnection.CameraConnectionStatus.CONNECTED);
498             }
499             if (statusViewDrawer != null)
500             {
501                 statusViewDrawer.updateConnectionStatus(ICameraConnection.CameraConnectionStatus.CONNECTED);
502
503                 // ライブビューの開始...
504                 statusViewDrawer.startLiveView();
505             }
506         }
507         catch (Exception e)
508         {
509             e.printStackTrace();
510         }
511     }
512
513     /**
514      *
515      *
516      */
517     @Override
518     public void onCameraDisconnected()
519     {
520         Log.v(TAG, "onCameraDisconnected()");
521         if (statusViewDrawer != null)
522         {
523             statusViewDrawer.updateStatusView(getString(R.string.camera_disconnected));
524             statusViewDrawer.updateConnectionStatus(ICameraConnection.CameraConnectionStatus.DISCONNECTED);
525         }
526     }
527
528     /**
529      *
530      *
531      */
532     @Override
533     public void onCameraOccursException(String message, Exception e)
534     {
535         Log.v(TAG, "onCameraOccursException() " + message);
536         try
537         {
538             e.printStackTrace();
539             ICameraConnection connection = getCameraConnection(interfaceProvider.getCammeraConnectionMethod());
540             if (connection != null)
541             {
542                 connection.alertConnectingFailed(message + " " + e.getLocalizedMessage());
543             }
544             if (statusViewDrawer != null)
545             {
546                 statusViewDrawer.updateStatusView(message);
547                 if (connection != null)
548                 {
549                     statusViewDrawer.updateConnectionStatus(connection.getConnectionStatus());
550                 }
551             }
552         }
553         catch (Exception ee)
554         {
555             ee.printStackTrace();
556         }
557     }
558
559     /**
560      *   BLE経由でカメラの電源を入れるかどうか
561      *
562      */
563     private boolean isBlePowerOn()
564     {
565         boolean ret = false;
566         try
567         {
568             if (interfaceProvider.getCammeraConnectionMethod() == ICameraConnection.CameraConnectionMethod.OPC)
569             {
570                 SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
571                 ret = preferences.getBoolean(IPreferencePropertyAccessor.BLE_POWER_ON, false);
572                 // Log.v(TAG, "isBlePowerOn() : " + ret);
573             }
574         }
575         catch (Exception e)
576         {
577             e.printStackTrace();
578         }
579         return (ret);
580     }
581
582     /**
583      *    カメラへの自動接続を行うかどうか
584      *
585      */
586     private boolean isAutoConnectCamera()
587     {
588         boolean ret = true;
589         try
590         {
591             SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
592             ret = preferences.getBoolean(IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA, true);
593             // Log.v(TAG, "isAutoConnectCamera() : " + ret);
594         }
595         catch (Exception e)
596         {
597             e.printStackTrace();
598         }
599         return (ret);
600     }
601
602     /**
603      *
604      *
605      *
606      */
607     private ICameraConnection getCameraConnection(ICameraConnection.CameraConnectionMethod connectionMethod)
608     {
609         ICameraConnection connection;
610         if (connectionMethod == ICameraConnection.CameraConnectionMethod.RICOH_GR2)
611         {
612             connection = interfaceProvider.getRicohGr2Infterface().getRicohGr2CameraConnection();
613         }
614         else if  (connectionMethod == ICameraConnection.CameraConnectionMethod.SONY)
615         {
616             connection = interfaceProvider.getSonyInterface().getSonyCameraConnection();
617         }
618         else if  (connectionMethod == ICameraConnection.CameraConnectionMethod.PANASONIC)
619         {
620             connection = interfaceProvider.getPanasonicInterface().getPanasonicCameraConnection();
621         }
622         else if  (connectionMethod == ICameraConnection.CameraConnectionMethod.FUJI_X)
623         {
624             connection = interfaceProvider.getFujiXInterface().getFujiXCameraConnection();
625         }
626         else if  (connectionMethod == ICameraConnection.CameraConnectionMethod.OLYMPUS)
627         {
628             connection = interfaceProvider.getOlympusPenInterface().getOlyCameraConnection();
629         }
630         else // if (connectionMethod == ICameraConnection.CameraConnectionMethod.OPC)
631         {
632             connection = interfaceProvider.getOlympusInterface().getOlyCameraConnection();
633         }
634         return (connection);
635     }
636
637     /**
638      *   カメラへのBLE接続指示が完了したとき
639      *
640      * @param isExecuted  true : BLEで起動した, false : 起動していない、その他
641      */
642     @Override
643     public void wakeupExecuted(boolean isExecuted)
644     {
645         Log.v(TAG, "wakeupExecuted() : " + isExecuted);
646         if (isAutoConnectCamera())
647         {
648             // カメラへ自動接続する設定だった場合、カメラへWiFi接続する (BLEで起動しなくても)
649             changeCameraConnection();
650         }
651     }
652
653
654     @Override
655     public boolean onKeyDown(int keyCode, KeyEvent event)
656     {
657         Log.v(TAG, "onKeyDown()" + " " + keyCode);
658         try
659         {
660             if ((event.getAction() == KeyEvent.ACTION_DOWN)&&
661                     ((keyCode == KeyEvent.KEYCODE_VOLUME_UP)||(keyCode == KeyEvent.KEYCODE_CAMERA)))
662             {
663                 if (liveViewFragment != null)
664                 {
665                     return (liveViewFragment.handleKeyDown(keyCode, event));
666                 }
667             }
668         }
669         catch (Exception e)
670         {
671             e.printStackTrace();
672         }
673         return (super.onKeyDown(keyCode, event));
674     }
675 }