1
2Android JumpingJack Sample
3===================================
4
5A basic sample showing how to use the Gravity sensor on the wearable device
6by counting how many jumping jacks you have performed.
7
8Introduction
9------------
10
11[SensorEventListener][1] offers you methods used for receiving notifications from the
12[SensorManager][2] when sensor values have changed.
13
14This example counts how many times Jumping Jacks are performed by detecting the value
15of the Gravity sensor by the following code:
16
17```java
18@Override
19public void onSensorChanged(SensorEvent event) {
20    detectJump(event.values[0], event.timestamp);
21}
22
23private void detectJump(float xValue, long timestamp) {
24    if ((Math.abs(xValue) > GRAVITY_THRESHOLD)) {
25        if(timestamp - mLastTime < TIME_THRESHOLD_NS && mUp != (xValue > 0)) {
26            onJumpDetected(!mUp);
27        }
28        mUp = xValue > 0;
29        mLastTime = timestamp;
30    }
31}
32```
33
34The detectJump method above assumes that when a person is wearing the watch, the x-component of gravity
35as measured by the Gravity Sensor is +9.8 when the hand is downward and -9.8 when the hand
36is upward (signs are reversed if the watch is worn on the right hand). Since the upward or
37downward may not be completely accurate, we leave some room and instead of 9.8, we use
38GRAVITY_THRESHOLD (7.0f). We also consider the up <-> down movement successful if it takes less than
39TIME_THRESHOLD_NS (2000000000 nanoseconds).
40
41[1]: http://developer.android.com/reference/android/hardware/SensorEventListener.html
42[2]: http://developer.android.com/reference/android/hardware/SensorManager.html
43
44Pre-requisites
45--------------
46
47- Android SDK 27
48- Android Build Tools v27.0.2
49- Android Support Repository
50
51Screenshots
52-------------
53
54<img src="screenshots/jumping_jack.gif" height="400" alt="Screenshot"/>
55
56Getting Started
57---------------
58
59This sample uses the Gradle build system. To build this project, use the
60"gradlew build" command or use "Import Project" in Android Studio.
61
62Support
63-------
64
65- Google+ Community: https://plus.google.com/communities/105153134372062985968
66- Stack Overflow: http://stackoverflow.com/questions/tagged/android
67
68If you've found an error in this sample, please file an issue:
69https://github.com/googlesamples/android-JumpingJack
70
71Patches are encouraged, and may be submitted by forking this project and
72submitting a pull request through GitHub. Please see CONTRIBUTING.md for more details.
73
74License
75-------
76
77Copyright 2017 The Android Open Source Project, Inc.
78
79Licensed to the Apache Software Foundation (ASF) under one or more contributor
80license agreements.  See the NOTICE file distributed with this work for
81additional information regarding copyright ownership.  The ASF licenses this
82file to you under the Apache License, Version 2.0 (the "License"); you may not
83use this file except in compliance with the License.  You may obtain a copy of
84the License at
85
86http://www.apache.org/licenses/LICENSE-2.0
87
88Unless required by applicable law or agreed to in writing, software
89distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
90WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
91License for the specific language governing permissions and limitations under
92the License.
93