OSDN Git Service

Preferenceを階層化して整理。
[gokigen/Gr2Control.git] / app / src / main / java / net / osdn / gokigen / gr2control / scene / CameraSceneUpdater.java
1 package net.osdn.gokigen.gr2control.scene;
2
3 import android.util.Log;
4
5 import net.osdn.gokigen.gr2control.R;
6 import net.osdn.gokigen.gr2control.camera.ICameraConnection;
7 import net.osdn.gokigen.gr2control.camera.ICameraStatusReceiver;
8 import net.osdn.gokigen.gr2control.camera.IInterfaceProvider;
9 import net.osdn.gokigen.gr2control.liveview.IStatusViewDrawer;
10 import net.osdn.gokigen.gr2control.logcat.LogCatFragment;
11 import net.osdn.gokigen.gr2control.playback.ImageGridViewFragment;
12 import net.osdn.gokigen.gr2control.preference.Gr2ControlPreferenceFragment;
13 import net.osdn.gokigen.gr2control.preference.fuji_x.FujiXPreferenceFragment;
14 import net.osdn.gokigen.gr2control.preference.olympus.PreferenceFragment;
15 import net.osdn.gokigen.gr2control.preference.ricohgr2.RicohGr2PreferenceFragment;
16
17 import androidx.annotation.NonNull;
18 import androidx.appcompat.app.AppCompatActivity;
19 import androidx.fragment.app.FragmentTransaction;
20 import androidx.preference.PreferenceFragmentCompat;
21
22 /**
23  *
24  *
25  */
26 public class CameraSceneUpdater implements ICameraStatusReceiver, IChangeScene
27 {
28     private final String TAG = toString();
29     private final AppCompatActivity activity;
30     private IInterfaceProvider interfaceProvider;
31     private IStatusViewDrawer statusViewDrawer;
32
33     private PreferenceFragmentCompat preferenceFragment = null;
34     private PreferenceFragmentCompat preferenceFragmentOpc = null;
35     private PreferenceFragmentCompat preferenceFragmentRicoh = null;
36     private PreferenceFragmentCompat preferenceFragmentFujiX = null;
37
38     private LogCatFragment logCatFragment = null;
39     private ImageGridViewFragment imageGridViewFragment = null;
40
41     public static CameraSceneUpdater newInstance(@NonNull AppCompatActivity activity)
42     {
43         return (new CameraSceneUpdater(activity));
44     }
45
46     /**
47      *  コンストラクタ
48      *
49      */
50     private CameraSceneUpdater(@NonNull AppCompatActivity activity)
51     {
52         this.activity = activity;
53     }
54
55     //  CameraSceneUpdater
56     public void registerInterface(@NonNull IStatusViewDrawer statusViewDrawer, @NonNull IInterfaceProvider interfaceProvider)
57     {
58         Log.v(TAG, "registerInterface()");
59         this.statusViewDrawer = statusViewDrawer;
60         this.interfaceProvider = interfaceProvider;
61     }
62
63     // ICameraStatusReceiver
64     @Override
65     public void onStatusNotify(String message)
66     {
67         Log.v(TAG, " CONNECTION MESSAGE : " + message);
68         try
69         {
70             if (statusViewDrawer != null)
71             {
72                 statusViewDrawer.updateStatusView(message);
73                 ICameraConnection connection = getCameraConnection(interfaceProvider.getCammeraConnectionMethod());
74                 if (connection != null)
75                 {
76                     statusViewDrawer.updateConnectionStatus(connection.getConnectionStatus());
77                 }
78             }
79         }
80         catch (Exception e)
81         {
82             e.printStackTrace();
83         }
84     }
85
86     // ICameraStatusReceiver
87     @Override
88     public void onCameraConnected()
89     {
90         Log.v(TAG, "onCameraConnected()");
91
92         try
93         {
94             ICameraConnection connection = getCameraConnection(interfaceProvider.getCammeraConnectionMethod());
95             if (connection != null)
96             {
97                 connection.forceUpdateConnectionStatus(ICameraConnection.CameraConnectionStatus.CONNECTED);
98             }
99             if (statusViewDrawer != null)
100             {
101                 statusViewDrawer.updateConnectionStatus(ICameraConnection.CameraConnectionStatus.CONNECTED);
102
103                 // ライブビューの開始...
104                 statusViewDrawer.startLiveView();
105             }
106         }
107         catch (Exception e)
108         {
109             e.printStackTrace();
110         }
111     }
112
113     // ICameraStatusReceiver
114     @Override
115     public void onCameraDisconnected()
116     {
117         Log.v(TAG, "onCameraDisconnected()");
118         if (statusViewDrawer != null)
119         {
120             statusViewDrawer.updateStatusView(activity.getString(R.string.camera_disconnected));
121             statusViewDrawer.updateConnectionStatus(ICameraConnection.CameraConnectionStatus.DISCONNECTED);
122         }
123     }
124
125     // ICameraStatusReceiver
126     @Override
127     public void onCameraOccursException(String message, Exception e)
128     {
129         Log.v(TAG, "onCameraOccursException() " + message);
130         try
131         {
132             e.printStackTrace();
133             ICameraConnection connection = getCameraConnection(interfaceProvider.getCammeraConnectionMethod());
134             if (connection != null)
135             {
136                 connection.alertConnectingFailed(message + " " + e.getLocalizedMessage());
137             }
138             if (statusViewDrawer != null)
139             {
140                 statusViewDrawer.updateStatusView(message);
141                 if (connection != null)
142                 {
143                     statusViewDrawer.updateConnectionStatus(connection.getConnectionStatus());
144                 }
145             }
146         }
147         catch (Exception ee)
148         {
149             ee.printStackTrace();
150         }
151     }
152
153     //  IChangeScene
154     @Override
155     public void changeSceneToCameraPropertyList()
156     {
157 /*
158         try
159         {
160             ICameraConnection.CameraConnectionMethod method = interfaceProvider.getCammeraConnectionMethod();
161             ICameraConnection connection = getCameraConnection(method);
162             if (method == ICameraConnection.CameraConnectionMethod.RICOH_GR2)
163             {
164                 // OPCカメラでない場合には、「OPCカメラのみ有効です」表示をして画面遷移させない
165                 Toast.makeText(getApplicationContext(), getText(R.string.only_opc_feature), Toast.LENGTH_SHORT).show();
166             }
167             else if (method == ICameraConnection.CameraConnectionMethod.SONY)
168             {
169                 // OPCカメラでない場合には、「OPCカメラのみ有効です」表示をして画面遷移させない
170                 Toast.makeText(getApplicationContext(), getText(R.string.only_opc_feature), Toast.LENGTH_SHORT).show();
171             }
172             else
173             {
174                 // OPC カメラの場合...
175                 if (connection != null)
176                 {
177                     ICameraConnection.CameraConnectionStatus status = connection.getConnectionStatus();
178                     if (status == ICameraConnection.CameraConnectionStatus.CONNECTED)
179                     {
180                         if (propertyListFragment == null)
181                         {
182                             propertyListFragment = OlyCameraPropertyListFragment.newInstance(this, interfaceProvider.getOlympusInterface().getCameraPropertyProvider());
183                         }
184                         FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
185                         transaction.replace(R.id.fragment1, propertyListFragment);
186                         // backstackに追加
187                         transaction.addToBackStack(null);
188                         transaction.commit();
189                     }
190                 }
191             }
192         }
193         catch (Exception e)
194         {
195             e.printStackTrace();
196         }
197 */
198     }
199
200     //  IChangeScene
201     @Override
202     public void changeSceneToConfiguration(ICameraConnection.CameraConnectionMethod connectionMethod)
203     {
204         try
205         {
206             if (connectionMethod == ICameraConnection.CameraConnectionMethod.RICOH_GR2)
207             {
208                 changeSceneToRicohConfiguration();
209             }
210             else if (connectionMethod == ICameraConnection.CameraConnectionMethod.FUJI_X)
211             {
212                 changeSceneToFujiXConfiguration();
213             }
214             else if (connectionMethod == ICameraConnection.CameraConnectionMethod.OPC)
215             {
216                 changeSceneToOpcConfiguration();
217             }
218             else // if (connectionMethod == ICameraConnection.CameraConnectionMethod.UNKNOWN)
219             {
220                 changeSceneToSummaryConfiguration();
221             }
222         }
223         catch (Exception e)
224         {
225             e.printStackTrace();
226         }
227     }
228
229     private void changeSceneToSummaryConfiguration()
230     {
231         try
232         {
233             if (preferenceFragment == null)
234             {
235                 preferenceFragment = Gr2ControlPreferenceFragment.newInstance(activity, this);
236             }
237             FragmentTransaction transaction = activity.getSupportFragmentManager().beginTransaction();
238             transaction.replace(R.id.fragment1, preferenceFragment);
239             // backstackに追加
240             transaction.addToBackStack(null);
241             transaction.commit();
242         }
243         catch (Exception e)
244         {
245             e.printStackTrace();
246         }
247     }
248
249     private void changeSceneToOpcConfiguration()
250     {
251         try
252         {
253             if (preferenceFragmentOpc == null)
254             {
255                 preferenceFragmentOpc = PreferenceFragment.newInstance(activity, interfaceProvider, this);
256             }
257             FragmentTransaction transaction = activity.getSupportFragmentManager().beginTransaction();
258             transaction.replace(R.id.fragment1, preferenceFragmentOpc);
259             // backstackに追加
260             transaction.addToBackStack(null);
261             transaction.commit();
262         }
263         catch (Exception e)
264         {
265             e.printStackTrace();
266         }
267     }
268
269     private void changeSceneToRicohConfiguration()
270     {
271         try
272         {
273             if (preferenceFragmentRicoh == null)
274             {
275                 preferenceFragmentRicoh = RicohGr2PreferenceFragment.newInstance(activity, this);
276             }
277             FragmentTransaction transaction = activity.getSupportFragmentManager().beginTransaction();
278             transaction.replace(R.id.fragment1, preferenceFragmentRicoh);
279             // backstackに追加
280             transaction.addToBackStack(null);
281             transaction.commit();
282         }
283         catch (Exception e)
284         {
285             e.printStackTrace();
286         }
287     }
288
289     private void changeSceneToFujiXConfiguration()
290     {
291         try
292         {
293             if (preferenceFragmentFujiX == null)
294             {
295                 preferenceFragmentFujiX = FujiXPreferenceFragment.newInstance(activity, this);
296             }
297             FragmentTransaction transaction = activity.getSupportFragmentManager().beginTransaction();
298             transaction.replace(R.id.fragment1, preferenceFragmentFujiX);
299             // backstackに追加
300             transaction.addToBackStack(null);
301             transaction.commit();
302         }
303         catch (Exception e)
304         {
305             e.printStackTrace();
306         }
307     }
308
309     //  IChangeScene
310     @Override
311     public void changeCameraConnection()
312     {
313         if (interfaceProvider == null)
314         {
315             Log.v(TAG, "changeCameraConnection() : interfaceProvider is NULL");
316             return;
317         }
318         try
319         {
320             interfaceProvider.resetCameraConnectionMethod();
321             ICameraConnection connection = interfaceProvider.getCameraConnection();
322             if (connection != null)
323             {
324                 ICameraConnection.CameraConnectionStatus status = connection.getConnectionStatus();
325                 if (status == ICameraConnection.CameraConnectionStatus.CONNECTED)
326                 {
327                     // 接続中のときには切断する
328                     connection.disconnect(false);
329                     return;
330                 }
331                 // 接続中でない時は、接続中にする
332                 connection.startWatchWifiStatus(activity);
333             }
334         }
335         catch (Exception e)
336         {
337             e.printStackTrace();
338         }
339     }
340
341     //  IChangeScene
342     @Override
343     public void changeSceneToDebugInformation()
344     {
345         if (logCatFragment == null)
346         {
347             logCatFragment = LogCatFragment.newInstance();
348         }
349         FragmentTransaction transaction = activity.getSupportFragmentManager().beginTransaction();
350         transaction.replace(R.id.fragment1, logCatFragment);
351         // backstackに追加
352         transaction.addToBackStack(null);
353         transaction.commit();
354     }
355
356     //  IChangeScene
357     @Override
358     public void changeSceneToApiList()
359     {
360 /*
361         if (sonyApiListFragmentSony == null)
362         {
363             sonyApiListFragmentSony = SonyCameraApiListFragment.newInstance(interfaceProvider);
364         }
365         FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
366         transaction.replace(R.id.fragment1, sonyApiListFragmentSony);
367         // backstackに追加
368         transaction.addToBackStack(null);
369         transaction.commit();
370 */
371     }
372
373     /**
374      *   画像一覧画面を開く
375      *
376      */
377     //  IChangeScene
378     @Override
379     public void changeScenceToImageList()
380     {
381         Log.v(TAG, "changeScenceToImageList()");
382         try
383         {
384             if (imageGridViewFragment == null)
385             {
386                 imageGridViewFragment = ImageGridViewFragment.newInstance(interfaceProvider.getPlaybackControl(), interfaceProvider.getCameraRunMode());
387             }
388             FragmentTransaction transaction = activity.getSupportFragmentManager().beginTransaction();
389             transaction.replace(R.id.fragment1, imageGridViewFragment);
390             // backstackに追加
391             transaction.addToBackStack(null);
392             transaction.commit();
393         }
394         catch (Exception e)
395         {
396             e.printStackTrace();
397         }
398     }
399
400     //  IChangeScene
401     @Override
402     public void exitApplication()
403     {
404         Log.v(TAG, "exitApplication()");
405         try
406         {
407             ICameraConnection connection = getCameraConnection(interfaceProvider.getCammeraConnectionMethod());
408             if (connection != null)
409             {
410                 connection.disconnect(true);
411             }
412             activity.finish();
413         }
414         catch (Exception e)
415         {
416             e.printStackTrace();
417         }
418     }
419
420     private ICameraConnection getCameraConnection(ICameraConnection.CameraConnectionMethod method)
421     {
422         Log.v(TAG, "method : " + method);
423         return (interfaceProvider.getCameraConnection());
424     }
425 }