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.liveview.dialog.FavoriteSettingSelectionDialog;
22 import jp.sfjp.gokigen.a01c.liveview.dialog.IDialogDismissedNotifier;
23 import jp.sfjp.gokigen.a01c.liveview.dialog.IDialogDrawer;
24 import jp.sfjp.gokigen.a01c.olycamerawrapper.dispatcher.FeatureDispatcher;
25 import jp.sfjp.gokigen.a01c.liveview.ICameraStatusReceiver;
26 import jp.sfjp.gokigen.a01c.liveview.IMessageDrawer;
27 import jp.sfjp.gokigen.a01c.liveview.OlyCameraLiveViewOnTouchListener;
28 import jp.sfjp.gokigen.a01c.olycamerawrapper.IOlyCameraCoordinator;
29 import jp.sfjp.gokigen.a01c.olycamerawrapper.OlyCameraCoordinator;
30 import jp.sfjp.gokigen.a01c.preference.IPreferenceCameraPropertyAccessor;
36 public class MainActivity extends WearableActivity implements IChangeScene, IShowInformation, ICameraStatusReceiver, IDialogDismissedNotifier
38 private final String TAG = toString();
39 static final int REQUEST_NEED_PERMISSIONS = 1010;
40 static final int COMMAND_MY_PROPERTY = 0x00000100;
42 private CameraLiveImageView liveView = null;
43 private IOlyCameraCoordinator coordinator = null;
44 private IMessageDrawer messageDrawer = null;
45 private OlyCameraLiveViewOnTouchListener listener = null;
46 private FavoriteSettingSelectionDialog selectionDialog = null;
47 private Vibrator vibrator = null;
48 private boolean cameraDisconnectedHappened = false;
49 private boolean ambientMode = false;
55 protected void onCreate(Bundle savedInstanceState)
57 super.onCreate(savedInstanceState);
58 Log.v(TAG, "onCreate()");
60 // Ambientモードを許してみる...
64 setContentView(R.layout.activity_main);
67 if ((ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_NETWORK_STATE) != PackageManager.PERMISSION_GRANTED)||
68 (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_WIFI_STATE) != PackageManager.PERMISSION_GRANTED)||
69 (ContextCompat.checkSelfPermission(this, Manifest.permission.CHANGE_WIFI_STATE) != PackageManager.PERMISSION_GRANTED)||
70 (ContextCompat.checkSelfPermission(this, Manifest.permission.CHANGE_NETWORK_STATE) != PackageManager.PERMISSION_GRANTED)||
71 (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_SETTINGS) != PackageManager.PERMISSION_GRANTED)||
72 (ContextCompat.checkSelfPermission(this, Manifest.permission.WAKE_LOCK) != PackageManager.PERMISSION_GRANTED)||
73 (ContextCompat.checkSelfPermission(this, Manifest.permission.VIBRATE) != PackageManager.PERMISSION_GRANTED)||
74 (ContextCompat.checkSelfPermission(this, Manifest.permission.INTERNET) != PackageManager.PERMISSION_GRANTED))
76 ActivityCompat.requestPermissions(this,
78 Manifest.permission.ACCESS_NETWORK_STATE,
79 Manifest.permission.ACCESS_WIFI_STATE,
80 Manifest.permission.CHANGE_WIFI_STATE,
81 Manifest.permission.CHANGE_NETWORK_STATE,
82 Manifest.permission.WRITE_SETTINGS,
83 Manifest.permission.WAKE_LOCK,
84 Manifest.permission.INTERNET,
86 REQUEST_NEED_PERMISSIONS);
91 // GPS機能が搭載されていない場合...
92 Log.d(TAG, "This hardware doesn't have GPS.");
93 // Fall back to functionality that does not use location or
94 // warn the user that location function is not available.
98 vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
100 setupCameraCoordinator();
101 setupInitialButtonIcons();
102 setupActionListener();
109 protected void onResume()
112 Log.v(TAG, "onResume()");
119 protected void onPause()
122 Log.v(TAG, "onPause()");
130 public void onStart()
133 Log.v(TAG, "onStart()");
144 Log.v(TAG, "onStop()");
153 public void onEnterAmbient(Bundle ambientDetails)
155 super.onEnterAmbient(ambientDetails);
156 Log.v(TAG, "onEnterAmbient()");
165 public void onExitAmbient()
167 super.onExitAmbient();
168 Log.v(TAG, "onExitAmbient()");
177 public void onUpdateAmbient()
179 super.onUpdateAmbient();
180 Log.v(TAG, "onUpdateAmbient()");
184 * ボタンが押された、画面がタッチされた、、は、リスナクラスで処理するよう紐づける
187 private void setupActionListener()
189 final ImageButton btn1 = (ImageButton) findViewById(R.id.btn_1);
190 btn1.setOnClickListener(listener);
191 btn1.setOnLongClickListener(listener);
193 final ImageButton btn2 = (ImageButton) findViewById(R.id.btn_2);
194 btn2.setOnClickListener(listener);
195 btn2.setOnLongClickListener(listener);
197 final ImageButton btn3 = (ImageButton) findViewById(R.id.btn_3);
198 btn3.setOnClickListener(listener);
199 btn3.setOnLongClickListener(listener);
201 final ImageButton btn4 = (ImageButton) findViewById(R.id.btn_4);
202 btn4.setOnClickListener(listener);
203 btn4.setOnLongClickListener(listener);
205 final ImageButton btn5 = (ImageButton) findViewById(R.id.btn_5);
206 btn5.setOnClickListener(listener);
207 btn5.setOnLongClickListener(listener);
209 final ImageButton btn6 = (ImageButton) findViewById(R.id.btn_6);
210 btn6.setOnClickListener(listener);
211 btn6.setOnLongClickListener(listener);
213 final TextView textArea1 = (TextView) findViewById(R.id.text_1);
214 textArea1.setOnClickListener(listener);
215 textArea1.setOnLongClickListener(listener);
217 final TextView textArea2 = (TextView) findViewById(R.id.text_2);
218 textArea2.setOnClickListener(listener);
219 textArea2.setOnLongClickListener(listener);
221 final TextView textArea3 = (TextView) findViewById(R.id.text_3);
222 textArea3.setOnClickListener(listener);
223 textArea3.setOnLongClickListener(listener);
225 final TextView textArea4 = (TextView) findViewById(R.id.text_4);
226 textArea4.setOnClickListener(listener);
227 textArea4.setOnLongClickListener(listener);
229 if (liveView == null)
231 liveView = (CameraLiveImageView) findViewById(R.id.liveview);
233 liveView.setOnTouchListener(listener);
234 messageDrawer = liveView.getMessageDrawer();
235 messageDrawer.setLevelGauge(coordinator.getLevelGauge());
242 private void setupInitialButtonIcons()
244 if (coordinator != null)
247 SharedPreferences preferences = android.support.v7.preference.PreferenceManager.getDefaultSharedPreferences(this);
248 if (preferences.getBoolean(IPreferenceCameraPropertyAccessor.SHOW_GRID_STATUS, true))
250 // ボタンをGrid OFFアイコンにする
251 resId = R.drawable.btn_ic_grid_off;
255 // ボタンをGrid ONアイコンにする
256 resId = R.drawable.btn_ic_grid_on;
258 setButtonDrawable(IShowInformation.BUTTON_1, resId);
263 * Intentを使ってWiFi設定画面を開く
266 private boolean launchWifiSettingScreen()
270 // Wifi 設定画面を表示する... (SONY Smart Watch 3では開かないけど...)
271 startActivity(new Intent("com.google.android.clockwork.settings.connectivity.wifi.ADD_NETWORK_SETTINGS"));
274 catch (android.content.ActivityNotFoundException ex)
276 Log.v(TAG, "android.content.ActivityNotFoundException... " + "com.google.android.clockwork.settings.connectivity.wifi.ADD_NETWORK_SETTINGS");
279 // SONY Smart Watch 3で開く場合のIntent...
280 Intent intent = new Intent("com.google.android.clockwork.settings.connectivity.wifi.ADD_NETWORK_SETTINGS");
281 intent.setClassName("com.google.android.apps.wearable.settings", "com.google.android.clockwork.settings.wifi.WifiSettingsActivity");
282 startActivity(intent);
285 catch (android.content.ActivityNotFoundException ex2)
289 // Wifi 設定画面を表示する...普通のAndroidの場合
290 startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
295 ee.printStackTrace();
305 e2.printStackTrace();
311 * Olympus Cameraクラスとのやりとりをするクラスを準備する
312 * (カメラとの接続も、ここでスレッドを起こして開始する)
314 private void setupCameraCoordinator()
316 if (liveView == null)
318 liveView = (CameraLiveImageView) findViewById(R.id.liveview);
321 coordinator = new OlyCameraCoordinator(this, liveView, this, this);
322 coordinator.setLiveViewListener(new CameraLiveViewListenerImpl(liveView));
323 listener = new OlyCameraLiveViewOnTouchListener(this, new FeatureDispatcher(this, coordinator, liveView), this);
324 selectionDialog = new FavoriteSettingSelectionDialog(coordinator.getCameraPropertyLoadSaveOperations(), this);
332 private void connectToCamera()
334 Thread thread = new Thread(new Runnable()
339 coordinator.getConnectionInterface().connect();
353 * カメラの電源をOFFいして、アプリを抜ける処理
357 public void exitApplication()
359 Log.v(TAG, "exitApplication()");
362 // アンビエントモードの時(≒自分でアプリを終了しなかったとき)は、何もしない
364 Log.v(TAG, "keep liveview.");
369 coordinator.stopLiveView();
371 // パラメータを確認し、カメラの電源を切る
372 if (PreferenceManager.getDefaultSharedPreferences(this).getBoolean(IPreferenceCameraPropertyAccessor.EXIT_APPLICATION_WITH_DISCONNECT, true))
374 Log.v(TAG, "Shutdown camera...");
377 coordinator.getConnectionInterface().disconnect(true);
380 //finishAndRemoveTask();
381 //android.os.Process.killProcess(android.os.Process.myPid());
388 public boolean checkConnectionFeature(int id)
394 ret = launchWifiSettingScreen();
402 * @param posX X座標位置 (0.0f - 1.0f)
403 * @param posY Y座標位置 (0.0f - 1.0f)
404 * @return true / false
407 public boolean touchedPosition(float posX, float posY)
409 Log.v(TAG, "touchedPosition (" + posX + ", " + posY);
410 return ((liveView != null)&&(liveView.touchedPosition(posX, posY)));
417 public boolean showConnectionStatus()
419 if ((listener.isEnabledOperation() == IShowInformation.operation.ONLY_CONNECT)&&(cameraDisconnectedHappened))
421 // カメラが切断されたとき、再接続を指示する
423 cameraDisconnectedHappened = false;
433 public void onStatusNotify(String message)
435 setMessage(IShowInformation.AREA_C, Color.WHITE, message);
442 public void onCameraConnected()
444 Log.v(TAG, "onCameraConnected()");
446 // ライブビューの開始 & タッチ/ボタンの操作を可能にする
447 coordinator.startLiveView();
448 coordinator.setRecViewMode(false);
449 listener.setEnableOperation(operation.ENABLE);
450 setMessage(IShowInformation.AREA_C, Color.WHITE, "");
451 coordinator.updateStatusAll();
455 * カメラとの接続が切れたとき...何もしない
459 public void onCameraDisconnected()
461 Log.v(TAG, "onCameraDisconnected()");
462 setMessage(IShowInformation.AREA_C, Color.YELLOW, getString(R.string.camera_disconnected));
463 listener.setEnableOperation(operation.ONLY_CONNECT);
464 cameraDisconnectedHappened = true;
471 public void onCameraOccursException(String message, Exception e)
473 Log.v(TAG, "onCameraOccursException()");
474 setMessage(IShowInformation.AREA_C, Color.YELLOW, message);
475 listener.setEnableOperation(operation.ONLY_CONNECT);
476 cameraDisconnectedHappened = true;
482 * @param area 表示エリア (AREA_1 ~ AREA_6, AREA_C)
484 * @param message 表示するメッセージ
487 public void setMessage(final int area, final int color, final String message)
492 case IShowInformation.AREA_1:
495 case IShowInformation.AREA_2:
498 case IShowInformation.AREA_3:
501 case IShowInformation.AREA_4:
504 case IShowInformation.AREA_NONE:
509 if (messageDrawer != null)
511 if (area == IShowInformation.AREA_C)
513 messageDrawer.setMessageToShow(IMessageDrawer.MessageArea.CENTER, color, IMessageDrawer.SIZE_LARGE, message);
516 if (area == IShowInformation.AREA_5)
518 messageDrawer.setMessageToShow(IMessageDrawer.MessageArea.UPLEFT, color, IMessageDrawer.SIZE_STD, message);
521 if (area == IShowInformation.AREA_6)
523 messageDrawer.setMessageToShow(IMessageDrawer.MessageArea.LOWLEFT, color, IMessageDrawer.SIZE_STD, message);
526 if (area == IShowInformation.AREA_7)
528 messageDrawer.setMessageToShow(IMessageDrawer.MessageArea.UPRIGHT, color, IMessageDrawer.SIZE_STD, message);
531 if (area == IShowInformation.AREA_8)
533 messageDrawer.setMessageToShow(IMessageDrawer.MessageArea.LOWRIGHT, color, IMessageDrawer.SIZE_STD, message);
536 if (area == IShowInformation.AREA_9)
538 messageDrawer.setMessageToShow(IMessageDrawer.MessageArea.UPCENTER, color, IMessageDrawer.SIZE_STD, message);
541 if (area == IShowInformation.AREA_A)
543 messageDrawer.setMessageToShow(IMessageDrawer.MessageArea.LOWCENTER, color, IMessageDrawer.SIZE_STD, message);
554 final int areaId = id;
555 runOnUiThread(new Runnable()
559 final TextView textArea = (TextView) findViewById(areaId);
560 textArea.setTextColor(color);
561 textArea.setText(message);
562 textArea.invalidate();
570 * @param button ボタンの場所
571 * @param labelId 変更する内容
574 public void setButtonDrawable(final int button, final int labelId)
579 case IShowInformation.BUTTON_1:
582 case IShowInformation.BUTTON_2:
585 case IShowInformation.BUTTON_3:
588 case IShowInformation.BUTTON_4:
591 case IShowInformation.BUTTON_5:
594 case IShowInformation.BUTTON_6:
600 final int areaId = id;
601 runOnUiThread(new Runnable()
605 final ImageButton button = (ImageButton) findViewById(areaId);
606 button.setImageDrawable(getDrawable(labelId));
614 * @return true GPS搭載, false GPS非搭載
616 private boolean hasGps()
618 return (getPackageManager().hasSystemFeature(PackageManager.FEATURE_LOCATION_GPS));
626 public void vibrate(final int vibratePattern)
630 if ((vibrator == null)||(!vibrator.hasVibrator()))
635 Thread thread = new Thread(new Runnable() {
638 switch (vibratePattern)
640 case IShowInformation.VIBRATE_PATTERN_SIMPLE_LONGLONG:
641 vibrator.vibrate(300);
643 case IShowInformation.VIBRATE_PATTERN_SIMPLE_LONG:
644 vibrator.vibrate(150);
646 case IShowInformation.VIBRATE_PATTERN_SIMPLE_MIDDLE:
647 vibrator.vibrate(75);
649 case IShowInformation.VIBRATE_PATTERN_SIMPLE_SHORT:
650 vibrator.vibrate(20);
652 case IShowInformation.VIBRATE_PATTERN_SHORT_DOUBLE:
653 long[] pattern = { 10, 25, 20, 25, 0 };
654 vibrator.vibrate(pattern, -1);
656 case IShowInformation.VIBRATE_PATTERN_NONE:
672 public void setEnabledOperation(IShowInformation.operation operation)
674 if (listener != null)
676 listener.setEnableOperation(operation);
685 public void showFavoriteSettingsDialog()
689 LoadSaveMyCameraPropertyDialog dialog = new LoadSaveMyCameraPropertyDialog();
690 dialog.setTargetFragment(this, COMMAND_MY_PROPERTY);
691 dialog.setPropertyOperationsHolder(coordinator.getCameraPropertyLoadSaveOperations());
692 dialog.show(this, "my_dialog");
695 // コマンドの実行確認ダイアログ... 動かん。。
696 ConfirmationDialog dialog = new ConfirmationDialog(this);
697 dialog.show(R.string.title_my_settings, R.string.message_none, new ConfirmationDialog.Callback()
700 public void confirm()
702 vibrate(IShowInformation.VIBRATE_PATTERN_SIMPLE_LONGLONG);
706 if ((liveView != null)&&(listener != null)&&(listener.isEnabledOperation() != operation.ONLY_CONNECT))
708 listener.setEnableOperation(operation.ENABLE_ONLY_TOUCHED_POSITION);
709 liveView.showDialog(selectionDialog);
718 public void dialogDismissed(boolean isExecuted)
720 if ((liveView != null)&&(listener != null))
722 liveView.hideDialog();
723 listener.setEnableOperation(operation.ENABLE);