1 package jp.sfjp.gokigen.a01c;
3 import android.content.Intent;
4 import android.content.SharedPreferences;
5 import android.graphics.Color;
6 import android.os.Bundle;
7 import android.os.Vibrator;
8 import android.preference.PreferenceManager;
9 import android.provider.Settings;
10 import android.support.wearable.activity.WearableActivity;
11 import android.util.Log;
12 import android.widget.ImageButton;
13 import android.widget.TextView;
14 import android.Manifest;
15 import android.content.pm.PackageManager;
16 import android.support.v4.app.ActivityCompat;
17 import android.support.v4.content.ContextCompat;
19 import jp.sfjp.gokigen.a01c.liveview.CameraLiveImageView;
20 import jp.sfjp.gokigen.a01c.liveview.CameraLiveViewListenerImpl;
21 import jp.sfjp.gokigen.a01c.olycamerawrapper.dispatcher.FeatureDispatcher;
22 import jp.sfjp.gokigen.a01c.liveview.ICameraStatusReceiver;
23 import jp.sfjp.gokigen.a01c.liveview.IMessageDrawer;
24 import jp.sfjp.gokigen.a01c.liveview.OlyCameraLiveViewOnTouchListener;
25 import jp.sfjp.gokigen.a01c.olycamerawrapper.IOlyCameraCoordinator;
26 import jp.sfjp.gokigen.a01c.olycamerawrapper.OlyCameraCoordinator;
27 import jp.sfjp.gokigen.a01c.preference.ICameraPropertyAccessor;
33 public class MainActivity extends WearableActivity implements IChangeScene, IShowInformation, ICameraStatusReceiver
35 private final String TAG = toString();
36 static final int REQUEST_NEED_PERMISSIONS = 1010;
38 private CameraLiveImageView liveView = null;
39 private IOlyCameraCoordinator coordinator = null;
40 private IMessageDrawer messageDrawer = null;
41 private OlyCameraLiveViewOnTouchListener listener = null;
42 private Vibrator vibrator = null;
43 private boolean cameraDisconnectedHappened = false;
44 private boolean ambientMode = false;
50 protected void onCreate(Bundle savedInstanceState)
52 super.onCreate(savedInstanceState);
53 Log.v(TAG, "onCreate()");
55 // Ambientモードを許してみる...
59 setContentView(R.layout.activity_main);
62 if ((ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_NETWORK_STATE) != PackageManager.PERMISSION_GRANTED)||
63 (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_WIFI_STATE) != PackageManager.PERMISSION_GRANTED)||
64 (ContextCompat.checkSelfPermission(this, Manifest.permission.CHANGE_WIFI_STATE) != PackageManager.PERMISSION_GRANTED)||
65 (ContextCompat.checkSelfPermission(this, Manifest.permission.CHANGE_NETWORK_STATE) != PackageManager.PERMISSION_GRANTED)||
66 (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_SETTINGS) != PackageManager.PERMISSION_GRANTED)||
67 (ContextCompat.checkSelfPermission(this, Manifest.permission.WAKE_LOCK) != PackageManager.PERMISSION_GRANTED)||
68 (ContextCompat.checkSelfPermission(this, Manifest.permission.VIBRATE) != PackageManager.PERMISSION_GRANTED)||
69 (ContextCompat.checkSelfPermission(this, Manifest.permission.INTERNET) != PackageManager.PERMISSION_GRANTED))
71 ActivityCompat.requestPermissions(this,
73 Manifest.permission.ACCESS_NETWORK_STATE,
74 Manifest.permission.ACCESS_WIFI_STATE,
75 Manifest.permission.CHANGE_WIFI_STATE,
76 Manifest.permission.CHANGE_NETWORK_STATE,
77 Manifest.permission.WRITE_SETTINGS,
78 Manifest.permission.WAKE_LOCK,
79 Manifest.permission.INTERNET,
81 REQUEST_NEED_PERMISSIONS);
86 // GPS機能が搭載されていない場合...
87 Log.d(TAG, "This hardware doesn't have GPS.");
88 // Fall back to functionality that does not use location or
89 // warn the user that location function is not available.
93 vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
95 setupCameraCoordinator();
96 setupInitialButtonIcons();
97 setupActionListener();
104 protected void onResume()
107 Log.v(TAG, "onResume()");
114 protected void onPause()
117 Log.v(TAG, "onPause()");
125 public void onStart()
128 Log.v(TAG, "onStart()");
139 Log.v(TAG, "onStop()");
148 public void onEnterAmbient(Bundle ambientDetails)
150 super.onEnterAmbient(ambientDetails);
151 Log.v(TAG, "onEnterAmbient()");
160 public void onExitAmbient()
162 super.onExitAmbient();
163 Log.v(TAG, "onExitAmbient()");
172 public void onUpdateAmbient()
174 super.onUpdateAmbient();
175 Log.v(TAG, "onUpdateAmbient()");
179 * ボタンが押された、画面がタッチされた、、は、リスナクラスで処理するよう紐づける
182 private void setupActionListener()
184 final ImageButton btn1 = (ImageButton) findViewById(R.id.btn_1);
185 btn1.setOnClickListener(listener);
186 btn1.setOnLongClickListener(listener);
188 final ImageButton btn2 = (ImageButton) findViewById(R.id.btn_2);
189 btn2.setOnClickListener(listener);
190 btn2.setOnLongClickListener(listener);
192 final ImageButton btn3 = (ImageButton) findViewById(R.id.btn_3);
193 btn3.setOnClickListener(listener);
194 btn3.setOnLongClickListener(listener);
196 final ImageButton btn4 = (ImageButton) findViewById(R.id.btn_4);
197 btn4.setOnClickListener(listener);
198 btn4.setOnLongClickListener(listener);
200 final ImageButton btn5 = (ImageButton) findViewById(R.id.btn_5);
201 btn5.setOnClickListener(listener);
202 btn5.setOnLongClickListener(listener);
204 final ImageButton btn6 = (ImageButton) findViewById(R.id.btn_6);
205 btn6.setOnClickListener(listener);
206 btn6.setOnLongClickListener(listener);
208 final TextView textArea1 = (TextView) findViewById(R.id.text_1);
209 textArea1.setOnClickListener(listener);
210 textArea1.setOnLongClickListener(listener);
212 final TextView textArea2 = (TextView) findViewById(R.id.text_2);
213 textArea2.setOnClickListener(listener);
214 textArea2.setOnLongClickListener(listener);
216 final TextView textArea3 = (TextView) findViewById(R.id.text_3);
217 textArea3.setOnClickListener(listener);
218 textArea3.setOnLongClickListener(listener);
220 final TextView textArea4 = (TextView) findViewById(R.id.text_4);
221 textArea4.setOnClickListener(listener);
222 textArea4.setOnLongClickListener(listener);
224 if (liveView == null)
226 liveView = (CameraLiveImageView) findViewById(R.id.liveview);
228 liveView.setOnTouchListener(listener);
229 messageDrawer = liveView.getMessageDrawer();
230 messageDrawer.setLevelGauge(coordinator.getLevelGauge());
237 private void setupInitialButtonIcons()
239 if (coordinator != null)
242 SharedPreferences preferences = android.support.v7.preference.PreferenceManager.getDefaultSharedPreferences(this);
243 if (preferences.getBoolean(ICameraPropertyAccessor.SHOW_GRID_STATUS, true))
245 // ボタンをGrid OFFアイコンにする
246 resId = R.drawable.btn_ic_grid_off;
250 // ボタンをGrid ONアイコンにする
251 resId = R.drawable.btn_ic_grid_on;
253 setButtonDrawable(IShowInformation.BUTTON_1, resId);
258 * Intentを使ってWiFi設定画面を開く
261 private boolean launchWifiSettingScreen()
265 // Wifi 設定画面を表示する... (SONY Smart Watch 3では開かないけど...)
266 startActivity(new Intent("com.google.android.clockwork.settings.connectivity.wifi.ADD_NETWORK_SETTINGS"));
269 catch (android.content.ActivityNotFoundException ex)
271 Log.v(TAG, "android.content.ActivityNotFoundException... " + "com.google.android.clockwork.settings.connectivity.wifi.ADD_NETWORK_SETTINGS");
274 // SONY Smart Watch 3で開く場合のIntent...
275 Intent intent = new Intent("com.google.android.clockwork.settings.connectivity.wifi.ADD_NETWORK_SETTINGS");
276 intent.setClassName("com.google.android.apps.wearable.settings", "com.google.android.clockwork.settings.wifi.WifiSettingsActivity");
277 startActivity(intent);
280 catch (android.content.ActivityNotFoundException ex2)
284 // Wifi 設定画面を表示する...普通のAndroidの場合
285 startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
290 ee.printStackTrace();
300 e2.printStackTrace();
306 * Olympus Cameraクラスとのやりとりをするクラスを準備する
307 * (カメラとの接続も、ここでスレッドを起こして開始する)
309 private void setupCameraCoordinator()
311 if (liveView == null)
313 liveView = (CameraLiveImageView) findViewById(R.id.liveview);
316 coordinator = new OlyCameraCoordinator(this, liveView, this, this);
317 coordinator.setLiveViewListener(new CameraLiveViewListenerImpl(liveView));
318 listener = new OlyCameraLiveViewOnTouchListener(this, new FeatureDispatcher(this, coordinator, liveView), this);
326 private void connectToCamera()
328 Thread thread = new Thread(new Runnable()
333 coordinator.getConnectionInterface().connect();
347 * カメラの電源をOFFいして、アプリを抜ける処理
351 public void exitApplication()
353 Log.v(TAG, "exitApplication()");
356 // アンビエントモードの時(≒自分でアプリを終了しなかったとき)は、何もしない
358 Log.v(TAG, "keep liveview.");
363 coordinator.stopLiveView();
365 // パラメータを確認し、カメラの電源を切る
366 if (PreferenceManager.getDefaultSharedPreferences(this).getBoolean(ICameraPropertyAccessor.EXIT_APPLICATION_WITH_DISCONNECT, true))
368 Log.v(TAG, "Shutdown camera...");
371 coordinator.getConnectionInterface().disconnect(true);
374 //finishAndRemoveTask();
375 //android.os.Process.killProcess(android.os.Process.myPid());
382 public boolean checkConnectionFeature(int id)
388 ret = launchWifiSettingScreen();
397 public boolean showConnectionStatus()
399 if ((!listener.isEnabledOperation())&&(cameraDisconnectedHappened))
401 // カメラが切断されたとき、再接続を指示する
403 cameraDisconnectedHappened = false;
413 public void onStatusNotify(String message)
415 setMessage(IShowInformation.AREA_C, Color.WHITE, message);
422 public void onCameraConnected()
424 Log.v(TAG, "onCameraConnected()");
426 // ライブビューの開始 & タッチ/ボタンの操作を可能にする
427 coordinator.startLiveView();
428 coordinator.setRecViewMode(false);
429 listener.setEnableOperation(true, false);
430 setMessage(IShowInformation.AREA_C, Color.WHITE, "");
431 coordinator.updateStatusAll();
435 * カメラとの接続が切れたとき...何もしない
439 public void onCameraDisconnected()
441 Log.v(TAG, "onCameraDisconnected()");
442 setMessage(IShowInformation.AREA_C, Color.YELLOW, getString(R.string.camera_disconnected));
443 listener.setEnableOperation(false, false);
444 cameraDisconnectedHappened = true;
451 public void onCameraOccursException(String message, Exception e)
453 Log.v(TAG, "onCameraOccursException()");
454 setMessage(IShowInformation.AREA_C, Color.YELLOW, message);
455 listener.setEnableOperation(false, false);
456 cameraDisconnectedHappened = true;
462 * @param area 表示エリア (AREA_1 ~ AREA_6, AREA_C)
464 * @param message 表示するメッセージ
467 public void setMessage(final int area, final int color, final String message)
472 case IShowInformation.AREA_1:
475 case IShowInformation.AREA_2:
478 case IShowInformation.AREA_3:
481 case IShowInformation.AREA_4:
484 case IShowInformation.AREA_NONE:
489 if (messageDrawer != null)
491 if (area == IShowInformation.AREA_C)
493 messageDrawer.setMessageToShow(IMessageDrawer.MessageArea.CENTER, color, IMessageDrawer.SIZE_LARGE, message);
496 if (area == IShowInformation.AREA_5)
498 messageDrawer.setMessageToShow(IMessageDrawer.MessageArea.UPLEFT, color, IMessageDrawer.SIZE_STD, message);
501 if (area == IShowInformation.AREA_6)
503 messageDrawer.setMessageToShow(IMessageDrawer.MessageArea.LOWLEFT, color, IMessageDrawer.SIZE_STD, message);
506 if (area == IShowInformation.AREA_7)
508 messageDrawer.setMessageToShow(IMessageDrawer.MessageArea.UPRIGHT, color, IMessageDrawer.SIZE_STD, message);
511 if (area == IShowInformation.AREA_8)
513 messageDrawer.setMessageToShow(IMessageDrawer.MessageArea.LOWRIGHT, color, IMessageDrawer.SIZE_STD, message);
516 if (area == IShowInformation.AREA_9)
518 messageDrawer.setMessageToShow(IMessageDrawer.MessageArea.UPCENTER, color, IMessageDrawer.SIZE_STD, message);
521 if (area == IShowInformation.AREA_A)
523 messageDrawer.setMessageToShow(IMessageDrawer.MessageArea.LOWCENTER, color, IMessageDrawer.SIZE_STD, message);
534 final int areaId = id;
535 runOnUiThread(new Runnable()
539 final TextView textArea = (TextView) findViewById(areaId);
540 textArea.setTextColor(color);
541 textArea.setText(message);
542 textArea.invalidate();
550 * @param button ボタンの場所
551 * @param labelId 変更する内容
554 public void setButtonDrawable(final int button, final int labelId)
559 case IShowInformation.BUTTON_1:
562 case IShowInformation.BUTTON_2:
565 case IShowInformation.BUTTON_3:
568 case IShowInformation.BUTTON_4:
571 case IShowInformation.BUTTON_5:
574 case IShowInformation.BUTTON_6:
580 final int areaId = id;
581 runOnUiThread(new Runnable()
585 final ImageButton button = (ImageButton) findViewById(areaId);
586 button.setImageDrawable(getDrawable(labelId));
594 * @return true GPS搭載, false GPS非搭載
596 private boolean hasGps()
598 return (getPackageManager().hasSystemFeature(PackageManager.FEATURE_LOCATION_GPS));
606 public void vibrate(final int vibratePattern)
610 if ((vibrator == null)||(!vibrator.hasVibrator()))
615 Thread thread = new Thread(new Runnable() {
618 switch (vibratePattern)
620 case IShowInformation.VIBRATE_PATTERN_SIMPLE_LONGLONG:
621 vibrator.vibrate(300);
623 case IShowInformation.VIBRATE_PATTERN_SIMPLE_LONG:
624 vibrator.vibrate(150);
626 case IShowInformation.VIBRATE_PATTERN_SIMPLE_MIDDLE:
627 vibrator.vibrate(75);
629 case IShowInformation.VIBRATE_PATTERN_SIMPLE_SHORT:
630 vibrator.vibrate(20);
632 case IShowInformation.VIBRATE_PATTERN_SHORT_DOUBLE:
633 long[] pattern = { 10, 25, 20, 25, 0 };
634 vibrator.vibrate(pattern, -1);
636 case IShowInformation.VIBRATE_PATTERN_NONE:
652 public void setEnabledOperation(boolean operation, boolean suppress)
654 if (listener != null)
656 listener.setEnableOperation(operation, suppress);