1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/widget/gonk/ProcessOrientation.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,110 @@ 1.4 +/* 1.5 + * Copyright (c) 2013, Linux Foundation. All rights reserved 1.6 + * 1.7 + * Copyright (C) 2008 The Android Open Source Project 1.8 + * 1.9 + * Licensed under the Apache License, Version 2.0 (the "License"); 1.10 + * you may not use this file except in compliance with the License. 1.11 + * You may obtain a copy of the License at 1.12 + * 1.13 + * http://www.apache.org/licenses/LICENSE-2.0 1.14 + * 1.15 + * Unless required by applicable law or agreed to in writing, software 1.16 + * distributed under the License is distributed on an "AS IS" BASIS, 1.17 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1.18 + * See the License for the specific language governing permissions and 1.19 + * limitations under the License. 1.20 + */ 1.21 + 1.22 +#ifndef ProcessOrientation_h 1.23 +#define ProcessOrientation_h 1.24 + 1.25 +#include "mozilla/Hal.h" 1.26 + 1.27 +namespace mozilla { 1.28 + 1.29 +// History of observed tilt angles. 1.30 +#define TILT_HISTORY_SIZE 40 1.31 + 1.32 +class ProcessOrientation { 1.33 +public: 1.34 + ProcessOrientation() {}; 1.35 + ~ProcessOrientation() {}; 1.36 + 1.37 + int OnSensorChanged(const mozilla::hal::SensorData& event, int deviceCurrentRotation); 1.38 + int Reset(); 1.39 + 1.40 +private: 1.41 + int GetProposedRotation(); 1.42 + 1.43 + // Returns true if the tilt angle is acceptable for a given predicted 1.44 + // rotation. 1.45 + bool IsTiltAngleAcceptable(int rotation, int tiltAngle); 1.46 + 1.47 + // Returns true if the orientation angle is acceptable for a given predicted 1.48 + // rotation. This function takes into account the gap between adjacent 1.49 + // orientations for hysteresis. 1.50 + bool IsOrientationAngleAcceptable(int rotation, int orientationAngle, 1.51 + int currentRotation); 1.52 + 1.53 + // Returns true if the predicted rotation is ready to be advertised as a 1.54 + // proposed rotation. 1.55 + bool IsPredictedRotationAcceptable(long now); 1.56 + 1.57 + void ClearPredictedRotation(); 1.58 + void UpdatePredictedRotation(long now, int rotation); 1.59 + bool IsAccelerating(float magnitude); 1.60 + void ClearTiltHistory(); 1.61 + void AddTiltHistoryEntry(long now, float tilt); 1.62 + bool IsFlat(long now); 1.63 + bool IsSwinging(long now, float tilt); 1.64 + int NextTiltHistoryIndex(int index); 1.65 + float RemainingMS(long now, long until); 1.66 + 1.67 + // The tilt angle range in degrees for each orientation. Beyond these tilt 1.68 + // angles, we don't even consider transitioning into the specified orientation. 1.69 + // We place more stringent requirements on unnatural orientations than natural 1.70 + // ones to make it less likely to accidentally transition into those states. 1.71 + // The first value of each pair is negative so it applies a limit when the 1.72 + // device is facing down (overhead reading in bed). The second value of each 1.73 + // pair is positive so it applies a limit when the device is facing up 1.74 + // (resting on a table). The ideal tilt angle is 0 (when the device is vertical) 1.75 + // so the limits establish how close to vertical the device must be in order 1.76 + // to change orientation. 1.77 + static const int tiltTolerance[][4]; 1.78 + 1.79 + // Timestamp and value of the last accelerometer sample. 1.80 + long mLastFilteredTimestampNanos; 1.81 + float mLastFilteredX, mLastFilteredY, mLastFilteredZ; 1.82 + 1.83 + // The last proposed rotation, -1 if unknown. 1.84 + int mProposedRotation; 1.85 + 1.86 + // Value of the current predicted rotation, -1 if unknown. 1.87 + int mPredictedRotation; 1.88 + 1.89 + // Timestamp of when the predicted rotation most recently changed. 1.90 + long mPredictedRotationTimestampNanos; 1.91 + 1.92 + // Timestamp when the device last appeared to be flat for sure (the flat delay 1.93 + // elapsed). 1.94 + long mFlatTimestampNanos; 1.95 + 1.96 + // Timestamp when the device last appeared to be swinging. 1.97 + long mSwingTimestampNanos; 1.98 + 1.99 + // Timestamp when the device last appeared to be undergoing external 1.100 + // acceleration. 1.101 + long mAccelerationTimestampNanos; 1.102 + 1.103 + struct { 1.104 + struct { 1.105 + float tiltAngle; 1.106 + long timestampNanos; 1.107 + } history[TILT_HISTORY_SIZE]; 1.108 + int index; 1.109 + } mTiltHistory; 1.110 +}; 1.111 + 1.112 +} // namespace mozilla 1.113 +#endif