OSDN Git Service

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