Advertisement
Guest User

Untitled

a guest
Jul 24th, 2014
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.05 KB | None | 0 0
  1. /*
  2. Copyright (c) 2011, Sony Ericsson Mobile Communications AB
  3. Copyright (c) 2011-2013, Sony Mobile Communications AB
  4.  
  5. All rights reserved.
  6.  
  7. Redistribution and use in source and binary forms, with or without
  8. modification, are permitted provided that the following conditions are met:
  9.  
  10. * Redistributions of source code must retain the above copyright notice, this
  11. list of conditions and the following disclaimer.
  12.  
  13. * Redistributions in binary form must reproduce the above copyright notice,
  14. this list of conditions and the following disclaimer in the documentation
  15. and/or other materials provided with the distribution.
  16.  
  17. * Neither the name of the Sony Ericsson Mobile Communications AB nor the names
  18. of its contributors may be used to endorse or promote products derived from
  19. this software without specific prior written permission.
  20.  
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  22. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  23. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  24. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  25. FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  27. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  28. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  29. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32.  
  33. package com.example.sonymobile.smartextension.hellosensors;
  34.  
  35. import android.annotation.SuppressLint;
  36. import android.content.Context;
  37. import android.graphics.Bitmap;
  38. import android.graphics.BitmapFactory;
  39. import android.graphics.Canvas;
  40. import android.util.DisplayMetrics;
  41. import android.util.Log;
  42. import android.view.Gravity;
  43. import android.view.LayoutInflater;
  44. import android.view.ViewGroup;
  45. import android.view.ViewGroup.LayoutParams;
  46. import android.widget.LinearLayout;
  47. import android.widget.TextView;
  48.  
  49. import com.sonyericsson.extras.liveware.aef.control.Control;
  50. import com.sonyericsson.extras.liveware.aef.registration.Registration;
  51. import com.sonyericsson.extras.liveware.aef.registration.Registration.SensorTypeValue;
  52. import com.sonyericsson.extras.liveware.aef.sensor.Sensor;
  53. import com.sonyericsson.extras.liveware.aef.sensor.Sensor.SensorAccuracy;
  54. import com.sonyericsson.extras.liveware.extension.util.control.ControlExtension;
  55. import com.sonyericsson.extras.liveware.extension.util.control.ControlTouchEvent;
  56. import com.sonyericsson.extras.liveware.extension.util.registration.DeviceInfoHelper;
  57. import com.sonyericsson.extras.liveware.extension.util.sensor.AccessorySensor;
  58. import com.sonyericsson.extras.liveware.extension.util.sensor.AccessorySensorEvent;
  59. import com.sonyericsson.extras.liveware.extension.util.sensor.AccessorySensorEventListener;
  60. import com.sonyericsson.extras.liveware.extension.util.sensor.AccessorySensorException;
  61. import com.sonyericsson.extras.liveware.extension.util.sensor.AccessorySensorManager;
  62.  
  63. import java.util.ArrayList;
  64. import java.util.List;
  65.  
  66. /**
  67. * This demonstrates how to collect and display data from two different sensors,
  68. * accelerometer and light.
  69. */
  70. public class HelloSensorsControl extends ControlExtension {
  71.  
  72. private static final Bitmap.Config BITMAP_CONFIG = Bitmap.Config.RGB_565;
  73.  
  74. private int mWidth = 220;
  75.  
  76. private int mHeight = 176;
  77.  
  78. private int mCurrentSensor = 0;
  79.  
  80. private List<AccessorySensor> mSensors = new ArrayList<AccessorySensor>();
  81.  
  82.  
  83.  
  84. private final AccessorySensorEventListener mListener = new AccessorySensorEventListener() {
  85.  
  86. @Override
  87. public void onSensorEvent(AccessorySensorEvent sensorEvent) {
  88. Log.d(HelloSensorsExtensionService.LOG_TAG,
  89. "Listener: OnSensorEvent");
  90. updateCurrentDisplay(sensorEvent);
  91. }
  92. };
  93.  
  94. /**
  95. * Creates a control extension.
  96. *
  97. * @param hostAppPackageName
  98. * Package name of host application.
  99. * @param context
  100. * The context.
  101. */
  102. public HelloSensorsControl( final String hostAppPackageName,
  103. final Context context) {
  104.  
  105. super(context, hostAppPackageName);
  106.  
  107. AccessorySensorManager manager = new AccessorySensorManager(context,
  108. hostAppPackageName);
  109.  
  110. // Add accelerometer, if supported by the host application.
  111. /*
  112. * if (DeviceInfoHelper.isSensorSupported(context, hostAppPackageName,
  113. * SensorTypeValue.ACCELEROMETER)) {
  114. * mSensors.add(manager.getSensor(SensorTypeValue.ACCELEROMETER)); }
  115. */
  116.  
  117. // Add magnetic field sensor, if supported by the host application.
  118. if (DeviceInfoHelper.isSensorSupported(context, hostAppPackageName,
  119. SensorTypeValue.MAGNETIC_FIELD)) {
  120. mSensors.add(manager.getSensor(SensorTypeValue.MAGNETIC_FIELD));
  121. }
  122.  
  123. // Add light sensor, if supported by the host application.
  124. if (DeviceInfoHelper.isSensorSupported(context, hostAppPackageName,
  125. SensorTypeValue.LIGHT)) {
  126. mSensors.add(manager.getSensor(SensorTypeValue.LIGHT));
  127. }
  128.  
  129. // Determine host application screen size.
  130. determineSize(context, hostAppPackageName);
  131. }
  132.  
  133. @Override
  134. public void onResume() {
  135. Log.d(HelloSensorsExtensionService.LOG_TAG, "Starting control");
  136.  
  137. // Note: Setting the screen to be always on will drain the accessory
  138. // battery. It is done here solely for demonstration purposes.
  139. setScreenState(Control.Intents.SCREEN_STATE_ON);
  140.  
  141. // Start listening for sensor updates.
  142. register();
  143.  
  144. updateCurrentDisplay(null);
  145. }
  146.  
  147. @Override
  148. public void onPause() {
  149. // Stop sensor.
  150. unregister();
  151. }
  152.  
  153. @Override
  154. public void onDestroy() {
  155. // Stop sensor.
  156. unregisterAndDestroy();
  157. }
  158.  
  159. /**
  160. * Checks if the control extension supports the given width.
  161. *
  162. * @param context
  163. * The context.
  164. * @param int width The width.
  165. * @return True if the control extension supports the given width.
  166. */
  167. public static boolean isWidthSupported(Context context, int width) {
  168. return width == context.getResources().getDimensionPixelSize(
  169. R.dimen.smart_watch_2_control_width)
  170. || width == context.getResources().getDimensionPixelSize(
  171. R.dimen.smart_watch_control_width);
  172. }
  173.  
  174. /**
  175. * Checks if the control extension supports the given height.
  176. *
  177. * @param context
  178. * The context.
  179. * @param int height The height.
  180. * @return True if the control extension supports the given height.
  181. */
  182. public static boolean isHeightSupported(Context context, int height) {
  183. return height == context.getResources().getDimensionPixelSize(
  184. R.dimen.smart_watch_2_control_height)
  185. || height == context.getResources().getDimensionPixelSize(
  186. R.dimen.smart_watch_control_height);
  187. }
  188.  
  189. @Override
  190. public void onTouch(ControlTouchEvent event) {
  191. super.onTouch(event);
  192. if (event.getAction() == Control.Intents.TOUCH_ACTION_RELEASE) {
  193. toggleSensor();
  194. }
  195. }
  196.  
  197. /**
  198. * Determines the width and height in pixels of a given host application.
  199. *
  200. * @param context
  201. * The context.
  202. * @param hostAppPackageName
  203. * The host application.
  204. */
  205. private void determineSize(Context context, String hostAppPackageName) {
  206. Log.d(HelloSensorsExtensionService.LOG_TAG,
  207. "Now determine screen size.");
  208.  
  209. boolean smartWatch2Supported = DeviceInfoHelper
  210. .isSmartWatch2ApiAndScreenDetected(context, hostAppPackageName);
  211. if (smartWatch2Supported) {
  212. mWidth = context.getResources().getDimensionPixelSize(
  213. R.dimen.smart_watch_2_control_width);
  214. mHeight = context.getResources().getDimensionPixelSize(
  215. R.dimen.smart_watch_2_control_height);
  216. } else {
  217. mWidth = context.getResources().getDimensionPixelSize(
  218. R.dimen.smart_watch_control_width);
  219. mHeight = context.getResources().getDimensionPixelSize(
  220. R.dimen.smart_watch_control_height);
  221. }
  222. }
  223.  
  224. /**
  225. * Returns the sensor currently being used.
  226. *
  227. * @return The sensor.
  228. */
  229. private AccessorySensor getCurrentSensor() {
  230. return mSensors.get(mCurrentSensor);
  231. }
  232.  
  233. /**
  234. * Checks if the sensor currently being used supports interrupt mode and
  235. * registers an interrupt listener if it does. If not, a fixed rate listener
  236. * will be registered instead.
  237. */
  238. private void register() {
  239. Log.d(HelloSensorsExtensionService.LOG_TAG, "Register listener");
  240.  
  241. AccessorySensor sensor = getCurrentSensor();
  242. if (sensor != null) {
  243. try {
  244. if (sensor.isInterruptModeSupported()) {
  245. sensor.registerInterruptListener(mListener);
  246. } else {
  247. sensor.registerFixedRateListener(mListener,
  248. Sensor.SensorRates.SENSOR_DELAY_UI);
  249. }
  250. } catch (AccessorySensorException e) {
  251. Log.d(HelloSensorsExtensionService.LOG_TAG,
  252. "Failed to register listener", e);
  253. }
  254. }
  255. }
  256.  
  257. /**
  258. * Unregisters any sensor event listeners connected to the sensor currently
  259. * being used.
  260. */
  261. private void unregister() {
  262. AccessorySensor sensor = getCurrentSensor();
  263. if (sensor != null) {
  264. sensor.unregisterListener();
  265. }
  266. }
  267.  
  268. /**
  269. * Unregisters any sensor event listeners and unsets the sensor currently
  270. * being used.
  271. */
  272. private void unregisterAndDestroy() {
  273. unregister();
  274. mSensors.clear();
  275. mSensors = null;
  276. }
  277.  
  278. /**
  279. * Cycles between currently available sensors and updates the display with
  280. * new data.
  281. */
  282. private void toggleSensor() {
  283. // Unregister the current sensor.
  284. unregister();
  285.  
  286. // Toggle sensor type.
  287. nextSensor();
  288.  
  289. // Register the new sensor.
  290. register();
  291.  
  292. // Update the screen.
  293. updateCurrentDisplay(null);
  294. }
  295.  
  296. /**
  297. * Cycles between sensors to be used.
  298. */
  299. private void nextSensor() {
  300. if (mCurrentSensor == (mSensors.size() - 1)) {
  301. mCurrentSensor = 0;
  302. } else {
  303. mCurrentSensor++;
  304. }
  305. }
  306.  
  307. /**
  308. * Determines what sensor is currently being used and updates the display
  309. * with new data.
  310. *
  311. * @param sensorEvent
  312. */
  313. private void updateCurrentDisplay(AccessorySensorEvent sensorEvent) {
  314. AccessorySensor sensor = getCurrentSensor();
  315. if (sensor.getType().getName()
  316. .equals(Registration.SensorTypeValue.ACCELEROMETER)
  317. || sensor.getType().getName()
  318. .equals(Registration.SensorTypeValue.MAGNETIC_FIELD)) {
  319. updateGenericSensorDisplay(sensorEvent, sensor.getType().getName());
  320. } else {
  321. updateLightSensorDisplay(sensorEvent);
  322. }
  323. }
  324.  
  325. /**
  326. * Updates the display with new accelerometer data.
  327. *
  328. * @param sensorEvent
  329. * The sensor event.
  330. */
  331. private void updateGenericSensorDisplay(AccessorySensorEvent sensorEvent,
  332. String sensorType) {
  333. // Create bitmap to draw in.
  334. Bitmap bitmap = Bitmap.createBitmap(mWidth, mHeight, BITMAP_CONFIG);
  335.  
  336. // Set default density to avoid scaling.
  337. bitmap.setDensity(DisplayMetrics.DENSITY_DEFAULT);
  338.  
  339. LinearLayout root = new LinearLayout(mContext);
  340. root.setLayoutParams(new ViewGroup.LayoutParams(mWidth, mHeight));
  341. root.setGravity(Gravity.CENTER);
  342.  
  343. LayoutInflater inflater = (LayoutInflater) mContext
  344. .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  345. LinearLayout sensorLayout = (LinearLayout) inflater.inflate(
  346. R.layout.generic_sensor_values, root, true);
  347.  
  348. TextView title = (TextView) sensorLayout
  349. .findViewById(R.id.sensor_title);
  350. title.setText(sensorType);
  351.  
  352. // Update the values.
  353. if (sensorEvent != null) {
  354. float[] values = sensorEvent.getSensorValues();
  355.  
  356. if (values != null && values.length == 3) {
  357. TextView xView = (TextView) sensorLayout
  358. .findViewById(R.id.sensor_value_x);
  359. TextView yView = (TextView) sensorLayout
  360. .findViewById(R.id.sensor_value_y);
  361. TextView zView = (TextView) sensorLayout
  362. .findViewById(R.id.sensor_value_z);
  363.  
  364. // Show values with one decimal.
  365. xView.setText(String.format("%.1f", values[0]));
  366. yView.setText(String.format("%.1f", values[1]));
  367. zView.setText(String.format("%.1f", values[2]));
  368. }
  369.  
  370. // Show time stamp in milliseconds. (Reading is in nanoseconds.)
  371. TextView timeStampView = (TextView) sensorLayout
  372. .findViewById(R.id.sensor_value_timestamp);
  373. timeStampView.setText(String.format("%d",
  374. (long) (sensorEvent.getTimestamp() / 1e9)));
  375.  
  376. // Show sensor accuracy.
  377. TextView accuracyView = (TextView) sensorLayout
  378. .findViewById(R.id.sensor_value_accuracy);
  379. accuracyView.setText(getAccuracyText(sensorEvent.getAccuracy()));
  380. }
  381.  
  382. root.measure(mWidth, mHeight);
  383. root.layout(0, 0, mWidth, mHeight);
  384.  
  385. Canvas canvas = new Canvas(bitmap);
  386. sensorLayout.draw(canvas);
  387.  
  388. showBitmap(bitmap);
  389. }
  390.  
  391. /**
  392. * Updates the display with new light sensor data.
  393. *
  394. * @param sensorEvent
  395. * The sensor event.
  396. */
  397.  
  398. public boolean sensorCheck(AccessorySensorEvent sensorEvent){
  399. StopWatch s1 = new StopWatch();
  400. if (sensorEvent != null) {
  401. float[] values = sensorEvent.getSensorValues();
  402.  
  403. if (values != null && values.length == 1) {
  404.  
  405.  
  406. // Show values with one decimal. if (values[0]<=20.0){
  407. s1.start();
  408. long measure = 0;
  409. while (s1.getElapsedTimeSecs() <= 3) {
  410.  
  411. if ((int) sensorEvent.getSensorValues()[0] >= 20) {
  412. measure = s1.getElapsedTimeSecs();
  413. s1.stop();
  414. return false;
  415. } else
  416. measure = 5;
  417. }
  418.  
  419. if (measure > 3) {
  420. // skip
  421. s1.stop();
  422. return true;
  423.  
  424. }
  425. }
  426. }
  427. return true;
  428. }
  429. private void updateLightSensorDisplay(AccessorySensorEvent sensorEvent) {
  430. // Create bitmap to draw in.
  431. Bitmap bitmap = Bitmap.createBitmap(mWidth, mHeight, BITMAP_CONFIG);
  432. StopWatch s1 = new StopWatch();
  433. // Set default density to avoid scaling.
  434. bitmap.setDensity(DisplayMetrics.DENSITY_DEFAULT);
  435.  
  436. LinearLayout root = new LinearLayout(mContext);
  437. root.setLayoutParams(new LayoutParams(mWidth, mHeight));
  438.  
  439. LinearLayout sensorLayout = (LinearLayout) LinearLayout.inflate(
  440. mContext, R.layout.lightsensor_values, root);
  441.  
  442. // Update the values.
  443. if (sensorEvent != null) {
  444. float[] values = sensorEvent.getSensorValues();
  445.  
  446. if (values != null && values.length == 1) {
  447. TextView xView = (TextView) sensorLayout
  448. .findViewById(R.id.light_value);
  449.  
  450. // Show values with one decimal. if (values[0]<=20.0){
  451. s1.start();
  452. long measure = 0;
  453. while (s1.getElapsedTimeSecs() <= 3) {
  454.  
  455. if ((int) sensorEvent.getSensorValues()[0] >= 20) {
  456. measure = s1.getElapsedTimeSecs();
  457. s1.stop();
  458. break;
  459. } else
  460. measure = 5;
  461. }
  462.  
  463. if (measure > 3) {
  464. // skip
  465. xView.setText(String.format("%.1f", 999.0));
  466. try {
  467. Thread.sleep(3000);
  468. } catch (Exception e) {
  469. }
  470. s1.stop();
  471. }
  472. }
  473. }
  474. {
  475. /*
  476. * if (values[0] <= 20.0) { toggleNightMode(); }
  477. */
  478. /*
  479. * if (values[0]<=20.0){ s1.start();
  480. * while(s1.getElapsedTimeSecs()<=10){ if(s1.getElapsedTimeSecs()>=3
  481. * && s1.getElapsedTimeSecs()<=10){ if(values[0]>=20){
  482. * xView.setText(String.format("%.1f", 999.0)); try {
  483. * Thread.sleep(3000); // three second } catch (Exception e) {}
  484. * s1.stop(); break;} } s1.stop(); } if(values[0]<=20.0){
  485. * toggleNightMode(); } } // Next Bitmap nextBitmap =
  486. * BitmapFactory.decodeResource(mContext.getResources(),
  487. * R.drawable.music_next_icn, mBitmapOptions); Bitmap
  488. * nextPressedBitmap =
  489. * BitmapFactory.decodeResource(mContext.getResources(),
  490. * R.drawable.music_next_pressed_icn, mBitmapOptions); ControlButton
  491. * nextButton = new ControlButton(WIDTH - nextBitmap.getWidth(),
  492. * HEIGHT - nextBitmap.getHeight(), nextBitmap, nextPressedBitmap) {
  493. *
  494. * @Override public void onClick() { mMediaPlayerAdapter.next(); }
  495. * }; mButtons.add(nextButton);
  496. */
  497.  
  498. // Show time stamp in milliseconds. (Reading is in nanoseconds.)
  499. TextView timeStampView = (TextView) sensorLayout
  500. .findViewById(R.id.light_value_timestamp);
  501. timeStampView.setText(String.format("%d",
  502. (long) (sensorEvent.getTimestamp() / 1e9)));
  503.  
  504. // Show sensor accuracy.
  505. TextView accuracyView = (TextView) sensorLayout
  506. .findViewById(R.id.light_value_accuracy);
  507. accuracyView.setText(getAccuracyText(sensorEvent.getAccuracy()));
  508. }
  509.  
  510. sensorLayout.measure(mWidth, mHeight);
  511. sensorLayout.layout(0, 0, sensorLayout.getMeasuredWidth(),
  512. sensorLayout.getMeasuredHeight());
  513.  
  514. Canvas canvas = new Canvas(bitmap);
  515. sensorLayout.draw(canvas);
  516.  
  517. showBitmap(bitmap);
  518. }
  519.  
  520. /**
  521. * Converts an accuracy value to a string.
  522. *
  523. * @param accuracy
  524. * The accuracy value.
  525. * @return The text.
  526. */
  527. @SuppressLint("DefaultLocale")
  528. private String getAccuracyText(int accuracy) {
  529.  
  530. switch (accuracy) {
  531. case SensorAccuracy.SENSOR_STATUS_UNRELIABLE:
  532. return mContext.getString(R.string.accuracy_unreliable);
  533. case SensorAccuracy.SENSOR_STATUS_ACCURACY_LOW:
  534. return mContext.getString(R.string.accuracy_low);
  535. case SensorAccuracy.SENSOR_STATUS_ACCURACY_MEDIUM:
  536. return mContext.getString(R.string.accuracy_medium);
  537. case SensorAccuracy.SENSOR_STATUS_ACCURACY_HIGH:
  538. return mContext.getString(R.string.accuracy_high);
  539. default:
  540. return String.format("%d", accuracy);
  541. }
  542.  
  543. }
  544.  
  545. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement