OSDN Git Service

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