OSDN Git Service

機能の実行部分をクラスとして独立。
[gokigen/A01c.git] / wear / src / main / java / jp / sfjp / gokigen / a01c / MainActivity.java
1 package jp.sfjp.gokigen.a01c;
2
3 import android.graphics.Color;
4 import android.os.Bundle;
5 import android.os.Vibrator;
6 import android.preference.PreferenceManager;
7 import android.support.wearable.activity.WearableActivity;
8 import android.util.Log;
9 import android.widget.ImageButton;
10 import android.widget.TextView;
11 import android.Manifest;
12 import android.content.pm.PackageManager;
13 import android.support.v4.app.ActivityCompat;
14 import android.support.v4.content.ContextCompat;
15
16 import jp.sfjp.gokigen.a01c.liveview.CameraLiveImageView;
17 import jp.sfjp.gokigen.a01c.liveview.CameraLiveViewListenerImpl;
18 import jp.sfjp.gokigen.a01c.liveview.FeatureDispatcher;
19 import jp.sfjp.gokigen.a01c.liveview.ICameraStatusReceiver;
20 import jp.sfjp.gokigen.a01c.liveview.IMessageDrawer;
21 import jp.sfjp.gokigen.a01c.liveview.OlyCameraLiveViewOnTouchListener;
22 import jp.sfjp.gokigen.a01c.olycamerawrapper.IOlyCameraCoordinator;
23 import jp.sfjp.gokigen.a01c.olycamerawrapper.OlyCameraCoordinator;
24 import jp.sfjp.gokigen.a01c.preference.ICameraPropertyAccessor;
25
26 /**
27  *   メインのActivity
28  *
29  */
30 public class MainActivity extends WearableActivity implements  IChangeScene, IShowInformation, ICameraStatusReceiver
31 {
32     private final String TAG = toString();
33     static final int REQUEST_NEED_PERMISSIONS = 1010;
34
35     private CameraLiveImageView liveView = null;
36     private IOlyCameraCoordinator coordinator = null;
37     private IMessageDrawer messageDrawer = null;
38     private OlyCameraLiveViewOnTouchListener listener = null;
39     private Vibrator vibrator = null;
40     /**
41      *
42      */
43     @Override
44     protected void onCreate(Bundle savedInstanceState)
45     {
46         super.onCreate(savedInstanceState);
47         Log.v(TAG, "onCreate()");
48
49         // Ambientモードを許してみる...
50         setAmbientEnabled();
51
52         //  画面全体の設定
53         setContentView(R.layout.activity_main);
54
55         // WiFIアクセス権のオプトイン
56         if ((ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_NETWORK_STATE) != PackageManager.PERMISSION_GRANTED)||
57                 (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_WIFI_STATE) != PackageManager.PERMISSION_GRANTED)||
58                 (ContextCompat.checkSelfPermission(this, Manifest.permission.CHANGE_WIFI_STATE) != PackageManager.PERMISSION_GRANTED)||
59                 (ContextCompat.checkSelfPermission(this, Manifest.permission.CHANGE_NETWORK_STATE) != PackageManager.PERMISSION_GRANTED)||
60                 (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_SETTINGS) != PackageManager.PERMISSION_GRANTED)||
61                 (ContextCompat.checkSelfPermission(this, Manifest.permission.WAKE_LOCK) != PackageManager.PERMISSION_GRANTED)||
62                 (ContextCompat.checkSelfPermission(this, Manifest.permission.VIBRATE) != PackageManager.PERMISSION_GRANTED)||
63                 (ContextCompat.checkSelfPermission(this, Manifest.permission.INTERNET) != PackageManager.PERMISSION_GRANTED))
64         {
65             ActivityCompat.requestPermissions(this,
66                     new String[]{
67                             Manifest.permission.ACCESS_NETWORK_STATE,
68                             Manifest.permission.ACCESS_WIFI_STATE,
69                             Manifest.permission.CHANGE_WIFI_STATE,
70                             Manifest.permission.CHANGE_NETWORK_STATE,
71                             Manifest.permission.WRITE_SETTINGS,
72                             Manifest.permission.WAKE_LOCK,
73                             Manifest.permission.INTERNET,
74                     },
75                     REQUEST_NEED_PERMISSIONS);
76         }
77
78         if (!hasGps())
79         {
80             // GPS機能が搭載されていない場合...
81             Log.d(TAG, "This hardware doesn't have GPS.");
82             // Fall back to functionality that does not use location or
83             // warn the user that location function is not available.
84         }
85
86         // バイブレータをつかまえる
87         vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
88
89         setupCameraCoordinator();
90         setupActionListener();
91     }
92
93     /**
94      *
95      */
96     @Override
97     protected void onResume()
98     {
99         super.onResume();
100         Log.v(TAG, "onResume()");
101     }
102
103     /**
104      *
105      */
106     @Override
107     protected void onPause()
108     {
109         super.onPause();
110         Log.v(TAG, "onPause()");
111
112
113     }
114
115     /**
116      *
117      *
118      */
119     @Override
120     public void onStart()
121     {
122         super.onStart();
123         Log.v(TAG, "onStart()");
124     }
125
126     /**
127      *
128      *
129      */
130     @Override
131     public void onStop()
132     {
133         super.onStop();
134         Log.v(TAG, "onStop()");
135         exitApplication();
136     }
137
138     /**
139      *
140      *
141      */
142      @Override
143      public void onEnterAmbient(Bundle ambientDetails)
144      {
145          super.onEnterAmbient(ambientDetails);
146          Log.v(TAG, "onEnterAmbient()");
147      }
148
149     /**
150      *
151      *
152      */
153     @Override
154     public void onExitAmbient()
155     {
156         super.onExitAmbient();
157         Log.v(TAG, "onExitAmbient()");
158     }
159
160     /**
161      *
162      *
163      */
164     @Override
165     public void onUpdateAmbient()
166     {
167         super.onUpdateAmbient();
168         Log.v(TAG, "onUpdateAmbient()");
169     }
170
171     /**
172      *   ボタンが押された、画面がタッチされた、、は、リスナクラスで処理するよう紐づける
173      *
174      */
175     private void setupActionListener()
176     {
177         final ImageButton btn1 = (ImageButton) findViewById(R.id.btn_1);
178         btn1.setOnClickListener(listener);
179         btn1.setOnLongClickListener(listener);
180
181         final ImageButton btn2 = (ImageButton) findViewById(R.id.btn_2);
182         btn2.setOnClickListener(listener);
183         btn2.setOnLongClickListener(listener);
184
185         final ImageButton btn3 = (ImageButton) findViewById(R.id.btn_3);
186         btn3.setOnClickListener(listener);
187         btn3.setOnLongClickListener(listener);
188
189         final ImageButton btn4 = (ImageButton) findViewById(R.id.btn_4);
190         btn4.setOnClickListener(listener);
191         btn4.setOnLongClickListener(listener);
192
193         final ImageButton btn5 = (ImageButton) findViewById(R.id.btn_5);
194         btn5.setOnClickListener(listener);
195         btn5.setOnLongClickListener(listener);
196
197         final ImageButton btn6 = (ImageButton) findViewById(R.id.btn_6);
198         btn6.setOnClickListener(listener);
199         btn6.setOnLongClickListener(listener);
200
201         final TextView textArea1 = (TextView) findViewById(R.id.text_1);
202         textArea1.setOnClickListener(listener);
203         textArea1.setOnLongClickListener(listener);
204
205         final TextView textArea2 = (TextView) findViewById(R.id.text_2);
206         textArea2.setOnClickListener(listener);
207         textArea2.setOnLongClickListener(listener);
208
209         final TextView textArea3 = (TextView) findViewById(R.id.text_3);
210         textArea3.setOnClickListener(listener);
211         textArea3.setOnLongClickListener(listener);
212
213         final TextView textArea4 = (TextView) findViewById(R.id.text_4);
214         textArea4.setOnClickListener(listener);
215         textArea4.setOnLongClickListener(listener);
216
217         if (liveView == null)
218         {
219             liveView = (CameraLiveImageView) findViewById(R.id.liveview);
220         }
221         liveView.setOnTouchListener(listener);
222         messageDrawer = liveView.getMessageDrawer();
223         messageDrawer.setLevelGauge(coordinator.getLevelGauge());
224     }
225
226     /**
227      *   Olympus Cameraクラスとのやりとりをするクラスを準備する
228      *   (カメラとの接続も、ここでスレッドを起こして開始する)
229      */
230     private void setupCameraCoordinator()
231     {
232         if (liveView == null)
233         {
234             liveView = (CameraLiveImageView) findViewById(R.id.liveview);
235         }
236         coordinator = null;
237         coordinator = new OlyCameraCoordinator(this, liveView, this, this);
238         coordinator.setLiveViewListener(new CameraLiveViewListenerImpl(liveView));
239         listener = new OlyCameraLiveViewOnTouchListener(this, new FeatureDispatcher(this, coordinator, liveView));
240
241         Thread thread = new Thread(new Runnable()
242         {
243             @Override
244             public void run()
245             {
246                 coordinator.getConnectionInterface().connect();
247             }
248         });
249         try
250         {
251             thread.start();
252         }
253         catch (Exception e)
254         {
255             e.printStackTrace();
256         }
257     }
258
259     /**
260      *   カメラの電源をOFFいして、アプリを抜ける処理
261      *
262      */
263     @Override
264     public void exitApplication()
265     {
266         Log.v(TAG, "exitApplication()");
267
268         // ライブビューを停止させる
269         coordinator.stopLiveView();
270
271         //  パラメータを確認し、カメラの電源を切る
272         if (PreferenceManager.getDefaultSharedPreferences(this).getBoolean(ICameraPropertyAccessor.EXIT_APPLICATION_WITH_DISCONNECT, true))
273         {
274             Log.v(TAG, "Shutdown camera...");
275
276             // カメラの電源をOFFにする
277             coordinator.getConnectionInterface().disconnect(true);
278         }
279         //finish();
280         //finishAndRemoveTask();
281         //android.os.Process.killProcess(android.os.Process.myPid());
282     }
283
284     /**
285      *
286      */
287     @Override
288     public void onStatusNotify(String message)
289     {
290         setMessage(IShowInformation.AREA_C, Color.WHITE, message);
291     }
292
293     /**
294      *
295      */
296     @Override
297     public void onCameraConnected()
298     {
299         Log.v(TAG, "onCameraConnected()");
300
301         // ライブビューの開始 & タッチ/ボタンの操作を可能にする
302         coordinator.startLiveView();
303         coordinator.setRecViewMode(false);
304         listener.setEnableOperation(true);
305         setMessage(IShowInformation.AREA_C, Color.WHITE, "");
306         coordinator.updateStatusAll();
307     }
308
309     /**
310      *   カメラとの接続が切れたとき...何もしない
311      *
312      */
313     @Override
314     public void onCameraDisconnected()
315     {
316         Log.v(TAG, "onCameraDisconnected()");
317         setMessage(IShowInformation.AREA_C, Color.YELLOW, getString(R.string.camera_disconnected));
318     }
319
320     /**
321      *  カメラに例外発生
322      */
323     @Override
324     public void onCameraOccursException(String message, Exception e)
325     {
326         setMessage(IShowInformation.AREA_C, Color.YELLOW, message);
327     }
328
329     /**s
330      *   メッセージの表示
331      *
332      * @param area    表示エリア (AREA_1 ~ AREA_6, AREA_C)
333      * @param color  表示色
334      * @param message 表示するメッセージ
335      */
336     @Override
337     public void setMessage(final int area, final int color, final String message)
338     {
339         int id = 0;
340         switch (area)
341         {
342             case IShowInformation.AREA_1:
343                 id = R.id.text_1;
344                 break;
345             case IShowInformation.AREA_2:
346                 id = R.id.text_2;
347                 break;
348             case IShowInformation.AREA_3:
349                 id = R.id.text_3;
350                 break;
351             case IShowInformation.AREA_4:
352                 id = R.id.text_4;
353                 break;
354             case IShowInformation.AREA_NONE:
355             default:
356                 // unknown
357                 break;
358         }
359         if (messageDrawer != null)
360         {
361             if (area == IShowInformation.AREA_C)
362             {
363                 messageDrawer.setMessageToShow(IMessageDrawer.MessageArea.CENTER, color, IMessageDrawer.SIZE_LARGE, message);
364                 return;
365             }
366             if (area == IShowInformation.AREA_5)
367             {
368                 messageDrawer.setMessageToShow(IMessageDrawer.MessageArea.UPLEFT, color, IMessageDrawer.SIZE_STD, message);
369                 return;
370             }
371             if (area == IShowInformation.AREA_6)
372             {
373                 messageDrawer.setMessageToShow(IMessageDrawer.MessageArea.LOWLEFT, color, IMessageDrawer.SIZE_STD, message);
374                 return;
375             }
376             if (area == IShowInformation.AREA_7)
377             {
378                 messageDrawer.setMessageToShow(IMessageDrawer.MessageArea.UPRIGHT, color, IMessageDrawer.SIZE_STD, message);
379                 return;
380             }
381             if (area == IShowInformation.AREA_8)
382             {
383                 messageDrawer.setMessageToShow(IMessageDrawer.MessageArea.LOWRIGHT, color, IMessageDrawer.SIZE_STD, message);
384                 return;
385             }
386             if (area == IShowInformation.AREA_9)
387             {
388                 messageDrawer.setMessageToShow(IMessageDrawer.MessageArea.UPCENTER, color, IMessageDrawer.SIZE_STD, message);
389                 return;
390             }
391             if (area == IShowInformation.AREA_A)
392             {
393                 messageDrawer.setMessageToShow(IMessageDrawer.MessageArea.LOWCENTER, color, IMessageDrawer.SIZE_STD, message);
394                 return;
395             }
396
397             if (id == 0)
398             {
399                 // 描画エリアが不定の場合...
400                 return;
401             }
402         }
403
404         final int areaId = id;
405         runOnUiThread(new Runnable()
406         {
407              @Override
408              public void run() {
409                  final TextView textArea = (TextView) findViewById(areaId);
410                  textArea.setTextColor(color);
411                  textArea.setText(message);
412                  textArea.invalidate();
413              }
414         });
415     }
416
417     /**
418      *   ボタンの表示イメージを変更する
419      *
420      * @param button  ボタンの場所
421      * @param labelId 変更する内容
422      */
423     @Override
424     public void setButtonDrawable(final int button, final int labelId)
425     {
426         int id;
427         switch (button)
428         {
429             case IShowInformation.BUTTON_1:
430                 id = R.id.btn_1;
431                 break;
432             case IShowInformation.BUTTON_2:
433                 id = R.id.btn_2;
434                 break;
435             case IShowInformation.BUTTON_3:
436                 id = R.id.btn_3;
437                 break;
438             case IShowInformation.BUTTON_4:
439                 id = R.id.btn_4;
440                 break;
441             case IShowInformation.BUTTON_5:
442                 id = R.id.btn_5;
443                 break;
444             case IShowInformation.BUTTON_6:
445             default:
446                 id = R.id.btn_6;
447                 break;
448         }
449
450         final int areaId = id;
451         runOnUiThread(new Runnable()
452         {
453             @Override
454             public void run() {
455                 final ImageButton button = (ImageButton) findViewById(areaId);
456                 button.setImageDrawable(getDrawable(labelId));
457                 button.invalidate();
458             }
459         });
460     }
461
462     /**
463      *
464      * @return true GPS搭載, false GPS非搭載
465      */
466     private boolean hasGps()
467     {
468         return (getPackageManager().hasSystemFeature(PackageManager.FEATURE_LOCATION_GPS));
469     }
470
471     /**
472      *
473      *
474      */
475     @Override
476     public void vibrate(int vibratePattern)
477     {
478         try
479         {
480             if ((vibrator == null)||(!vibrator.hasVibrator()))
481             {
482                 return;
483             }
484
485             switch (vibratePattern)
486             {
487                 case IShowInformation.VIBRATE_PATTERN_SIMPLE_LONGLONG:
488                     vibrator.vibrate(300);
489                     break;
490                 case IShowInformation.VIBRATE_PATTERN_SIMPLE_LONG:
491                     vibrator.vibrate(150);
492                     break;
493                 case IShowInformation.VIBRATE_PATTERN_SIMPLE_MIDDLE:
494                     vibrator.vibrate(75);
495                     break;
496                 case IShowInformation.VIBRATE_PATTERN_SIMPLE_SHORT:
497                     vibrator.vibrate(20);
498                     break;
499                 case IShowInformation.VIBRATE_PATTERN_SHORT_DOUBLE:
500                     long[] pattern = { 10, 25, 20, 25, 0 };
501                     vibrator.vibrate(pattern, -1);
502                     break;
503                 case IShowInformation.VIBRATE_PATTERN_NONE:
504                 default:
505                     // ぶるぶるしない
506                     break;
507             }
508         }
509         catch (Exception e)
510         {
511             e.printStackTrace();
512         }
513     }
514
515 }