1 package jp.sfjp.gokigen.a01c;
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;
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;
30 public class MainActivity extends WearableActivity implements IChangeScene, IShowInformation, ICameraStatusReceiver
32 private final String TAG = toString();
33 static final int REQUEST_NEED_PERMISSIONS = 1010;
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;
44 protected void onCreate(Bundle savedInstanceState)
46 super.onCreate(savedInstanceState);
47 Log.v(TAG, "onCreate()");
49 // Ambientモードを許してみる...
53 setContentView(R.layout.activity_main);
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))
65 ActivityCompat.requestPermissions(this,
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,
75 REQUEST_NEED_PERMISSIONS);
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.
87 vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
89 setupCameraCoordinator();
90 setupActionListener();
97 protected void onResume()
100 Log.v(TAG, "onResume()");
107 protected void onPause()
110 Log.v(TAG, "onPause()");
120 public void onStart()
123 Log.v(TAG, "onStart()");
134 Log.v(TAG, "onStop()");
143 public void onEnterAmbient(Bundle ambientDetails)
145 super.onEnterAmbient(ambientDetails);
146 Log.v(TAG, "onEnterAmbient()");
154 public void onExitAmbient()
156 super.onExitAmbient();
157 Log.v(TAG, "onExitAmbient()");
165 public void onUpdateAmbient()
167 super.onUpdateAmbient();
168 Log.v(TAG, "onUpdateAmbient()");
172 * ボタンが押された、画面がタッチされた、、は、リスナクラスで処理するよう紐づける
175 private void setupActionListener()
177 final ImageButton btn1 = (ImageButton) findViewById(R.id.btn_1);
178 btn1.setOnClickListener(listener);
179 btn1.setOnLongClickListener(listener);
181 final ImageButton btn2 = (ImageButton) findViewById(R.id.btn_2);
182 btn2.setOnClickListener(listener);
183 btn2.setOnLongClickListener(listener);
185 final ImageButton btn3 = (ImageButton) findViewById(R.id.btn_3);
186 btn3.setOnClickListener(listener);
187 btn3.setOnLongClickListener(listener);
189 final ImageButton btn4 = (ImageButton) findViewById(R.id.btn_4);
190 btn4.setOnClickListener(listener);
191 btn4.setOnLongClickListener(listener);
193 final ImageButton btn5 = (ImageButton) findViewById(R.id.btn_5);
194 btn5.setOnClickListener(listener);
195 btn5.setOnLongClickListener(listener);
197 final ImageButton btn6 = (ImageButton) findViewById(R.id.btn_6);
198 btn6.setOnClickListener(listener);
199 btn6.setOnLongClickListener(listener);
201 final TextView textArea1 = (TextView) findViewById(R.id.text_1);
202 textArea1.setOnClickListener(listener);
203 textArea1.setOnLongClickListener(listener);
205 final TextView textArea2 = (TextView) findViewById(R.id.text_2);
206 textArea2.setOnClickListener(listener);
207 textArea2.setOnLongClickListener(listener);
209 final TextView textArea3 = (TextView) findViewById(R.id.text_3);
210 textArea3.setOnClickListener(listener);
211 textArea3.setOnLongClickListener(listener);
213 final TextView textArea4 = (TextView) findViewById(R.id.text_4);
214 textArea4.setOnClickListener(listener);
215 textArea4.setOnLongClickListener(listener);
217 if (liveView == null)
219 liveView = (CameraLiveImageView) findViewById(R.id.liveview);
221 liveView.setOnTouchListener(listener);
222 messageDrawer = liveView.getMessageDrawer();
223 messageDrawer.setLevelGauge(coordinator.getLevelGauge());
227 * Olympus Cameraクラスとのやりとりをするクラスを準備する
228 * (カメラとの接続も、ここでスレッドを起こして開始する)
230 private void setupCameraCoordinator()
232 if (liveView == null)
234 liveView = (CameraLiveImageView) findViewById(R.id.liveview);
237 coordinator = new OlyCameraCoordinator(this, liveView, this, this);
238 coordinator.setLiveViewListener(new CameraLiveViewListenerImpl(liveView));
239 listener = new OlyCameraLiveViewOnTouchListener(this, new FeatureDispatcher(this, coordinator, liveView));
241 Thread thread = new Thread(new Runnable()
246 coordinator.getConnectionInterface().connect();
260 * カメラの電源をOFFいして、アプリを抜ける処理
264 public void exitApplication()
266 Log.v(TAG, "exitApplication()");
269 coordinator.stopLiveView();
271 // パラメータを確認し、カメラの電源を切る
272 if (PreferenceManager.getDefaultSharedPreferences(this).getBoolean(ICameraPropertyAccessor.EXIT_APPLICATION_WITH_DISCONNECT, true))
274 Log.v(TAG, "Shutdown camera...");
277 coordinator.getConnectionInterface().disconnect(true);
280 //finishAndRemoveTask();
281 //android.os.Process.killProcess(android.os.Process.myPid());
288 public void onStatusNotify(String message)
290 setMessage(IShowInformation.AREA_C, Color.WHITE, message);
297 public void onCameraConnected()
299 Log.v(TAG, "onCameraConnected()");
301 // ライブビューの開始 & タッチ/ボタンの操作を可能にする
302 coordinator.startLiveView();
303 coordinator.setRecViewMode(false);
304 listener.setEnableOperation(true);
305 setMessage(IShowInformation.AREA_C, Color.WHITE, "");
306 coordinator.updateStatusAll();
310 * カメラとの接続が切れたとき...何もしない
314 public void onCameraDisconnected()
316 Log.v(TAG, "onCameraDisconnected()");
317 setMessage(IShowInformation.AREA_C, Color.YELLOW, getString(R.string.camera_disconnected));
324 public void onCameraOccursException(String message, Exception e)
326 setMessage(IShowInformation.AREA_C, Color.YELLOW, message);
332 * @param area 表示エリア (AREA_1 ~ AREA_6, AREA_C)
334 * @param message 表示するメッセージ
337 public void setMessage(final int area, final int color, final String message)
342 case IShowInformation.AREA_1:
345 case IShowInformation.AREA_2:
348 case IShowInformation.AREA_3:
351 case IShowInformation.AREA_4:
354 case IShowInformation.AREA_NONE:
359 if (messageDrawer != null)
361 if (area == IShowInformation.AREA_C)
363 messageDrawer.setMessageToShow(IMessageDrawer.MessageArea.CENTER, color, IMessageDrawer.SIZE_LARGE, message);
366 if (area == IShowInformation.AREA_5)
368 messageDrawer.setMessageToShow(IMessageDrawer.MessageArea.UPLEFT, color, IMessageDrawer.SIZE_STD, message);
371 if (area == IShowInformation.AREA_6)
373 messageDrawer.setMessageToShow(IMessageDrawer.MessageArea.LOWLEFT, color, IMessageDrawer.SIZE_STD, message);
376 if (area == IShowInformation.AREA_7)
378 messageDrawer.setMessageToShow(IMessageDrawer.MessageArea.UPRIGHT, color, IMessageDrawer.SIZE_STD, message);
381 if (area == IShowInformation.AREA_8)
383 messageDrawer.setMessageToShow(IMessageDrawer.MessageArea.LOWRIGHT, color, IMessageDrawer.SIZE_STD, message);
386 if (area == IShowInformation.AREA_9)
388 messageDrawer.setMessageToShow(IMessageDrawer.MessageArea.UPCENTER, color, IMessageDrawer.SIZE_STD, message);
391 if (area == IShowInformation.AREA_A)
393 messageDrawer.setMessageToShow(IMessageDrawer.MessageArea.LOWCENTER, color, IMessageDrawer.SIZE_STD, message);
404 final int areaId = id;
405 runOnUiThread(new Runnable()
409 final TextView textArea = (TextView) findViewById(areaId);
410 textArea.setTextColor(color);
411 textArea.setText(message);
412 textArea.invalidate();
420 * @param button ボタンの場所
421 * @param labelId 変更する内容
424 public void setButtonDrawable(final int button, final int labelId)
429 case IShowInformation.BUTTON_1:
432 case IShowInformation.BUTTON_2:
435 case IShowInformation.BUTTON_3:
438 case IShowInformation.BUTTON_4:
441 case IShowInformation.BUTTON_5:
444 case IShowInformation.BUTTON_6:
450 final int areaId = id;
451 runOnUiThread(new Runnable()
455 final ImageButton button = (ImageButton) findViewById(areaId);
456 button.setImageDrawable(getDrawable(labelId));
464 * @return true GPS搭載, false GPS非搭載
466 private boolean hasGps()
468 return (getPackageManager().hasSystemFeature(PackageManager.FEATURE_LOCATION_GPS));
476 public void vibrate(int vibratePattern)
480 if ((vibrator == null)||(!vibrator.hasVibrator()))
485 switch (vibratePattern)
487 case IShowInformation.VIBRATE_PATTERN_SIMPLE_LONGLONG:
488 vibrator.vibrate(300);
490 case IShowInformation.VIBRATE_PATTERN_SIMPLE_LONG:
491 vibrator.vibrate(150);
493 case IShowInformation.VIBRATE_PATTERN_SIMPLE_MIDDLE:
494 vibrator.vibrate(75);
496 case IShowInformation.VIBRATE_PATTERN_SIMPLE_SHORT:
497 vibrator.vibrate(20);
499 case IShowInformation.VIBRATE_PATTERN_SHORT_DOUBLE:
500 long[] pattern = { 10, 25, 20, 25, 0 };
501 vibrator.vibrate(pattern, -1);
503 case IShowInformation.VIBRATE_PATTERN_NONE: