blob: aea337e3266e199fbc4cf4fa68070e5d66c5779a [file] [log] [blame]
Svetoslav Ganov152e9bb2012-10-12 20:15:29 -07001/*
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 =
Alan Viverette214fb682015-11-17 09:47:11 -050031 new SynchronizedPool<>(MAX_POOL_SIZE);
Svetoslav Ganov152e9bb2012-10-12 20:15:29 -070032
Alan Viverette214fb682015-11-17 09:47:11 -050033 /** The magnification scaling factor. */
Svetoslav Ganov152e9bb2012-10-12 20:15:29 -070034 public float scale = 1.0f;
Alan Viverette214fb682015-11-17 09:47:11 -050035
36 /**
37 * The X coordinate, in unscaled screen-relative pixels, around which
38 * magnification is focused.
39 */
Svetoslav Ganov152e9bb2012-10-12 20:15:29 -070040 public float offsetX;
Alan Viverette214fb682015-11-17 09:47:11 -050041
42 /**
43 * The Y coordinate, in unscaled screen-relative pixels, around which
44 * magnification is focused.
45 */
Svetoslav Ganov152e9bb2012-10-12 20:15:29 -070046 public float offsetY;
47
48 private MagnificationSpec() {
49 /* do nothing - reducing visibility */
50 }
51
52 public void initialize(float scale, float offsetX, float offsetY) {
Svetoslav Ganov545252f2012-12-10 18:29:24 -080053 if (scale < 1) {
54 throw new IllegalArgumentException("Scale must be greater than or equal to one!");
55 }
Svetoslav Ganov152e9bb2012-10-12 20:15:29 -070056 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
Alan Viverette214fb682015-11-17 09:47:11 -050089 public void setTo(MagnificationSpec other) {
90 scale = other.scale;
91 offsetX = other.offsetX;
92 offsetY = other.offsetY;
93 }
94
Svetoslav Ganov152e9bb2012-10-12 20:15:29 -070095 @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
Alan Viverette214fb682015-11-17 09:47:11 -0500109 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
Svetoslav Ganov152e9bb2012-10-12 20:15:29 -0700131 public String toString() {
132 StringBuilder builder = new StringBuilder();
133 builder.append("<scale:");
Svetoslav Ganov7d477d42016-04-15 14:01:42 -0700134 builder.append(Float.toString(scale));
Svetoslav Ganov152e9bb2012-10-12 20:15:29 -0700135 builder.append(",offsetX:");
Svetoslav Ganov7d477d42016-04-15 14:01:42 -0700136 builder.append(Float.toString(offsetX));
Svetoslav Ganov152e9bb2012-10-12 20:15:29 -0700137 builder.append(",offsetY:");
Svetoslav Ganov7d477d42016-04-15 14:01:42 -0700138 builder.append(Float.toString(offsetY));
Svetoslav Ganov152e9bb2012-10-12 20:15:29 -0700139 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
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -0700149 public static final @android.annotation.NonNull Creator<MagnificationSpec> CREATOR = new Creator<MagnificationSpec>() {
Svetoslav Ganov152e9bb2012-10-12 20:15:29 -0700150 @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}