1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.hardware.input;
18 
19 import android.annotation.Nullable;
20 import android.os.Parcel;
21 import android.os.Parcelable;
22 
23 /**
24  * Encapsulates calibration data for input devices.
25  *
26  * @hide
27  */
28 public class TouchCalibration implements Parcelable {
29 
30     public static final TouchCalibration IDENTITY = new TouchCalibration();
31 
32     public static final @android.annotation.NonNull Parcelable.Creator<TouchCalibration> CREATOR
33             = new Parcelable.Creator<TouchCalibration>() {
34         public TouchCalibration createFromParcel(Parcel in) {
35             return new TouchCalibration(in);
36         }
37 
38         public TouchCalibration[] newArray(int size) {
39             return new TouchCalibration[size];
40         }
41     };
42 
43     private final float mXScale, mXYMix, mXOffset;
44     private final float mYXMix, mYScale, mYOffset;
45 
46     /**
47      * Create a new TouchCalibration initialized to the identity transformation.
48      */
TouchCalibration()49     public TouchCalibration() {
50         this(1,0,0,0,1,0);
51     }
52 
53     /**
54      * Create a new TouchCalibration from affine transformation paramters.
55      * @param xScale   Influence of input x-axis value on output x-axis value.
56      * @param xyMix    Influence of input y-axis value on output x-axis value.
57      * @param xOffset  Constant offset to be applied to output x-axis value.
58      * @param yXMix    Influence of input x-axis value on output y-axis value.
59      * @param yScale   Influence of input y-axis value on output y-axis value.
60      * @param yOffset  Constant offset to be applied to output y-axis value.
61      */
TouchCalibration(float xScale, float xyMix, float xOffset, float yxMix, float yScale, float yOffset)62     public TouchCalibration(float xScale, float xyMix, float xOffset,
63             float yxMix, float yScale, float yOffset) {
64         mXScale  = xScale;
65         mXYMix   = xyMix;
66         mXOffset = xOffset;
67         mYXMix   = yxMix;
68         mYScale  = yScale;
69         mYOffset = yOffset;
70     }
71 
TouchCalibration(Parcel in)72     public TouchCalibration(Parcel in) {
73         mXScale  = in.readFloat();
74         mXYMix   = in.readFloat();
75         mXOffset = in.readFloat();
76         mYXMix   = in.readFloat();
77         mYScale  = in.readFloat();
78         mYOffset = in.readFloat();
79     }
80 
81     @Override
writeToParcel(Parcel dest, int flags)82     public void writeToParcel(Parcel dest, int flags) {
83         dest.writeFloat(mXScale);
84         dest.writeFloat(mXYMix);
85         dest.writeFloat(mXOffset);
86         dest.writeFloat(mYXMix);
87         dest.writeFloat(mYScale);
88         dest.writeFloat(mYOffset);
89     }
90 
91     @Override
describeContents()92     public int describeContents() {
93         return 0;
94     }
95 
getAffineTransform()96     public float[] getAffineTransform() {
97         return new float[] { mXScale, mXYMix, mXOffset, mYXMix, mYScale, mYOffset };
98     }
99 
100     @Override
equals(@ullable Object obj)101     public boolean equals(@Nullable Object obj) {
102         if (obj == this) {
103             return true;
104         } else if (obj instanceof TouchCalibration) {
105             TouchCalibration cal = (TouchCalibration)obj;
106 
107             return (cal.mXScale  == mXScale)  &&
108                    (cal.mXYMix   == mXYMix)   &&
109                    (cal.mXOffset == mXOffset) &&
110                    (cal.mYXMix   == mYXMix)   &&
111                    (cal.mYScale  == mYScale)  &&
112                    (cal.mYOffset == mYOffset);
113         } else {
114             return false;
115         }
116     }
117 
118     @Override
hashCode()119     public int hashCode() {
120         return Float.floatToIntBits(mXScale)  ^
121                Float.floatToIntBits(mXYMix)   ^
122                Float.floatToIntBits(mXOffset) ^
123                Float.floatToIntBits(mYXMix)   ^
124                Float.floatToIntBits(mYScale)  ^
125                Float.floatToIntBits(mYOffset);
126     }
127 }
128