OSDN Git Service

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