OSDN Git Service

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