OSDN Git Service

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