1/*
2 * Copyright (C) 2012 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
17package android.view;
18
19import android.os.Parcel;
20import android.os.Parcelable;
21import android.util.Pools.SynchronizedPool;
22
23/**
24 * This class represents spec for performing screen magnification.
25 *
26 * @hide
27 */
28public class MagnificationSpec implements Parcelable {
29    private static final int MAX_POOL_SIZE = 20;
30    private static final SynchronizedPool<MagnificationSpec> sPool =
31            new SynchronizedPool<>(MAX_POOL_SIZE);
32
33    /** The magnification scaling factor. */
34    public float scale = 1.0f;
35
36    /**
37     * The X coordinate, in unscaled screen-relative pixels, around which
38     * magnification is focused.
39     */
40    public float offsetX;
41
42    /**
43     * The Y coordinate, in unscaled screen-relative pixels, around which
44     * magnification is focused.
45     */
46    public float offsetY;
47
48    private MagnificationSpec() {
49        /* do nothing - reducing visibility */
50    }
51
52    public void initialize(float scale, float offsetX, float offsetY) {
53        if (scale < 1) {
54            throw new IllegalArgumentException("Scale must be greater than or equal to one!");
55        }
56        this.scale = scale;
57        this.offsetX = offsetX;
58        this.offsetY = offsetY;
59    }
60
61    public boolean isNop() {
62        return scale == 1.0f && offsetX == 0 && offsetY == 0;
63    }
64
65    public static MagnificationSpec obtain(MagnificationSpec other) {
66        MagnificationSpec info = obtain();
67        info.scale = other.scale;
68        info.offsetX = other.offsetX;
69        info.offsetY = other.offsetY;
70        return info;
71    }
72
73    public static MagnificationSpec obtain() {
74        MagnificationSpec spec = sPool.acquire();
75        return (spec != null) ? spec : new MagnificationSpec();
76    }
77
78    public void recycle() {
79        clear();
80        sPool.release(this);
81    }
82
83    public void clear() {
84       scale = 1.0f;
85       offsetX = 0.0f;
86       offsetY = 0.0f;
87    }
88
89    public void setTo(MagnificationSpec other) {
90        scale = other.scale;
91        offsetX = other.offsetX;
92        offsetY = other.offsetY;
93    }
94
95    @Override
96    public int describeContents() {
97        return 0;
98    }
99
100    @Override
101    public void writeToParcel(Parcel parcel, int flags) {
102        parcel.writeFloat(scale);
103        parcel.writeFloat(offsetX);
104        parcel.writeFloat(offsetY);
105        recycle();
106    }
107
108    @Override
109    public boolean equals(Object other) {
110        if (this == other) {
111            return true;
112        }
113
114        if (other == null || getClass() != other.getClass()) {
115            return false;
116        }
117
118        final MagnificationSpec s = (MagnificationSpec) other;
119        return scale == s.scale && offsetX == s.offsetX && offsetY == s.offsetY;
120    }
121
122    @Override
123    public int hashCode() {
124        int result = (scale != +0.0f ? Float.floatToIntBits(scale) : 0);
125        result = 31 * result + (offsetX != +0.0f ? Float.floatToIntBits(offsetX) : 0);
126        result = 31 * result + (offsetY != +0.0f ? Float.floatToIntBits(offsetY) : 0);
127        return result;
128    }
129
130    @Override
131    public String toString() {
132        StringBuilder builder = new StringBuilder();
133        builder.append("<scale:");
134        builder.append(Float.toString(scale));
135        builder.append(",offsetX:");
136        builder.append(Float.toString(offsetX));
137        builder.append(",offsetY:");
138        builder.append(Float.toString(offsetY));
139        builder.append(">");
140        return builder.toString();
141    }
142
143    private void initFromParcel(Parcel parcel) {
144        scale = parcel.readFloat();
145        offsetX = parcel.readFloat();
146        offsetY = parcel.readFloat();
147    }
148
149    public static final Creator<MagnificationSpec> CREATOR = new Creator<MagnificationSpec>() {
150        @Override
151        public MagnificationSpec[] newArray(int size) {
152            return new MagnificationSpec[size];
153        }
154
155        @Override
156        public MagnificationSpec createFromParcel(Parcel parcel) {
157            MagnificationSpec spec = MagnificationSpec.obtain();
158            spec.initFromParcel(parcel);
159            return spec;
160        }
161    };
162}
163