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.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;
29 public class MainActivity extends WearableActivity implements IChangeScene, IShowInformation, ICameraStatusReceiver
31 private final String TAG = toString();
32 static final int REQUEST_NEED_PERMISSIONS = 1010;
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;
43 protected void onCreate(Bundle savedInstanceState)
45 super.onCreate(savedInstanceState);
46 Log.v(TAG, "onCreate()");
49 setContentView(R.layout.activity_main);
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))
61 ActivityCompat.requestPermissions(this,
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,
71 REQUEST_NEED_PERMISSIONS);
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.
83 vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
85 listener = new OlyCameraLiveViewOnTouchListener(this);
87 setupCameraCoordinator();
88 setupActionListener();
95 protected void onResume()
98 Log.v(TAG, "onResume()");
105 protected void onPause()
108 Log.v(TAG, "onPause()");
118 public void onStart()
121 Log.v(TAG, "onStart()");
132 Log.v(TAG, "onStop()");
141 public void onEnterAmbient(Bundle ambientDetails)
143 super.onEnterAmbient(ambientDetails);
144 Log.v(TAG, "onEnterAmbient()");
152 public void onExitAmbient()
154 super.onExitAmbient();
155 Log.v(TAG, "onExitAmbient()");
163 public void onUpdateAmbient()
165 super.onUpdateAmbient();
166 Log.v(TAG, "onUpdateAmbient()");
170 * ボタンが押された、画面がタッチされた、、は、リスナクラスで処理するよう紐づける
173 private void setupActionListener()
175 final ImageButton btn1 = (ImageButton) findViewById(R.id.btn_1);
176 btn1.setOnClickListener(listener);
178 final ImageButton btn2 = (ImageButton) findViewById(R.id.btn_2);
179 btn2.setOnClickListener(listener);
181 final ImageButton btn3 = (ImageButton) findViewById(R.id.btn_3);
182 btn3.setOnClickListener(listener);
184 final ImageButton btn4 = (ImageButton) findViewById(R.id.btn_4);
185 btn4.setOnClickListener(listener);
187 final ImageButton btn5 = (ImageButton) findViewById(R.id.btn_5);
188 btn5.setOnClickListener(listener);
190 final ImageButton btn6 = (ImageButton) findViewById(R.id.btn_6);
191 btn6.setOnClickListener(listener);
193 final TextView textArea1 = (TextView) findViewById(R.id.text_1);
194 textArea1.setOnClickListener(listener);
196 final TextView textArea2 = (TextView) findViewById(R.id.text_2);
197 textArea2.setOnClickListener(listener);
199 final TextView textArea3 = (TextView) findViewById(R.id.text_3);
200 textArea3.setOnClickListener(listener);
202 final TextView textArea4 = (TextView) findViewById(R.id.text_4);
203 textArea4.setOnClickListener(listener);
205 if (liveView == null)
207 liveView = (CameraLiveImageView) findViewById(R.id.liveview);
209 liveView.setOnTouchListener(listener);
210 messageDrawer = liveView.getMessageDrawer();
211 listener.prepareInterfaces(coordinator, this, liveView);
215 * Olympus Cameraクラスとのやりとりをするクラスを準備する
216 * (カメラとの接続も、ここでスレッドを起こして開始する)
218 private void setupCameraCoordinator()
220 if (liveView == null)
222 liveView = (CameraLiveImageView) findViewById(R.id.liveview);
225 coordinator = new OlyCameraCoordinator(this, liveView, this, this);
226 coordinator.setLiveViewListener(new CameraLiveViewListenerImpl(liveView));
227 Thread thread = new Thread(new Runnable()
232 coordinator.getConnectionInterface().connect();
246 * カメラの電源をOFFいして、アプリを抜ける処理
250 public void exitApplication()
252 Log.v(TAG, "exitApplication()");
255 coordinator.stopLiveView();
257 // パラメータを確認し、カメラの電源を切る
258 if (PreferenceManager.getDefaultSharedPreferences(this).getBoolean(ICameraPropertyAccessor.EXIT_APPLICATION_WITH_DISCONNECT, true))
260 Log.v(TAG, "Shutdown camera...");
263 coordinator.getConnectionInterface().disconnect(true);
266 //finishAndRemoveTask();
267 //android.os.Process.killProcess(android.os.Process.myPid());
274 public void onStatusNotify(String message)
276 setMessage(IShowInformation.AREA_C, Color.WHITE, message);
283 public void onCameraConnected()
285 Log.v(TAG, "onCameraConnected()");
287 // ライブビューの開始 & タッチ/ボタンの操作を可能にする
288 coordinator.startLiveView();
289 coordinator.setRecViewMode(false);
290 listener.setEnableOperation(true);
291 setMessage(IShowInformation.AREA_C, Color.WHITE, "");
292 coordinator.updateStatusAll();
296 * カメラとの接続が切れたとき...何もしない
300 public void onCameraDisconnected()
302 Log.v(TAG, "onCameraDisconnected()");
303 setMessage(IShowInformation.AREA_C, Color.YELLOW, getString(R.string.camera_disconnected));
310 public void onCameraOccursException(String message, Exception e)
312 setMessage(IShowInformation.AREA_C, Color.YELLOW, message);
318 * @param area 表示エリア (AREA_1 ~ AREA_6, AREA_C)
320 * @param message 表示するメッセージ
323 public void setMessage(final int area, final int color, final String message)
328 case IShowInformation.AREA_1:
331 case IShowInformation.AREA_2:
334 case IShowInformation.AREA_3:
337 case IShowInformation.AREA_4:
340 case IShowInformation.AREA_NONE:
345 if (messageDrawer != null)
347 if (area == IShowInformation.AREA_C)
349 messageDrawer.setMessageToShow(IMessageDrawer.MessageArea.CENTER, color, IMessageDrawer.SIZE_LARGE, message);
352 if (area == IShowInformation.AREA_5)
354 messageDrawer.setMessageToShow(IMessageDrawer.MessageArea.UPLEFT, color, IMessageDrawer.SIZE_STD, message);
357 if (area == IShowInformation.AREA_6)
359 messageDrawer.setMessageToShow(IMessageDrawer.MessageArea.LOWLEFT, color, IMessageDrawer.SIZE_STD, message);
362 if (area == IShowInformation.AREA_7)
364 messageDrawer.setMessageToShow(IMessageDrawer.MessageArea.UPRIGHT, color, IMessageDrawer.SIZE_STD, message);
367 if (area == IShowInformation.AREA_8)
369 messageDrawer.setMessageToShow(IMessageDrawer.MessageArea.LOWRIGHT, color, IMessageDrawer.SIZE_STD, message);
379 final int areaId = id;
380 runOnUiThread(new Runnable()
384 final TextView textArea = (TextView) findViewById(areaId);
385 textArea.setTextColor(color);
386 textArea.setText(message);
387 textArea.invalidate();
395 * @param button ボタンの場所
396 * @param labelId 変更する内容
399 public void setButtonDrawable(final int button, final int labelId)
404 case IShowInformation.BUTTON_1:
407 case IShowInformation.BUTTON_2:
410 case IShowInformation.BUTTON_3:
413 case IShowInformation.BUTTON_4:
416 case IShowInformation.BUTTON_5:
419 case IShowInformation.BUTTON_6:
425 final int areaId = id;
426 runOnUiThread(new Runnable()
430 final ImageButton button = (ImageButton) findViewById(areaId);
431 button.setImageDrawable(getDrawable(labelId));
439 * @return true GPS搭載, false GPS非搭載
441 private boolean hasGps()
443 return (getPackageManager().hasSystemFeature(PackageManager.FEATURE_LOCATION_GPS));
451 public void vibrate(int vibratePattern)
455 if ((vibrator == null)||(!vibrator.hasVibrator()))
460 switch (vibratePattern)
462 case IShowInformation.VIBRATE_PATTERN_SIMPLE_LONGLONG:
463 vibrator.vibrate(300);
465 case IShowInformation.VIBRATE_PATTERN_SIMPLE_LONG:
466 vibrator.vibrate(150);
468 case IShowInformation.VIBRATE_PATTERN_SIMPLE_MIDDLE:
469 vibrator.vibrate(75);
471 case IShowInformation.VIBRATE_PATTERN_SIMPLE_SHORT:
472 vibrator.vibrate(20);
474 case IShowInformation.VIBRATE_PATTERN_NONE: