1152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov/* 2152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov * Copyright (C) 2012 The Android Open Source Project 3152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov * 4152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov * Licensed under the Apache License, Version 2.0 (the "License"); 5152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov * you may not use this file except in compliance with the License. 6152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov * You may obtain a copy of the License at 7152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov * 8152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov * http://www.apache.org/licenses/LICENSE-2.0 9152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov * 10152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov * Unless required by applicable law or agreed to in writing, software 11152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov * distributed under the License is distributed on an "AS IS" BASIS, 12152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov * See the License for the specific language governing permissions and 14152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov * limitations under the License. 15152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov */ 16152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov 17152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganovpackage android.view; 18152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov 19152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganovimport android.os.Parcel; 20152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganovimport android.os.Parcelable; 21152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganovimport android.util.Pools.SynchronizedPool; 22152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov 23152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov/** 24152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov * This class represents spec for performing screen magnification. 25152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov * 26152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov * @hide 27152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov */ 28152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganovpublic class MagnificationSpec implements Parcelable { 29152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov private static final int MAX_POOL_SIZE = 20; 30152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov private static final SynchronizedPool<MagnificationSpec> sPool = 31214fb68767502f5fede643a062c1dc5975d75b27Alan Viverette new SynchronizedPool<>(MAX_POOL_SIZE); 32152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov 33214fb68767502f5fede643a062c1dc5975d75b27Alan Viverette /** The magnification scaling factor. */ 34152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov public float scale = 1.0f; 35214fb68767502f5fede643a062c1dc5975d75b27Alan Viverette 36214fb68767502f5fede643a062c1dc5975d75b27Alan Viverette /** 37214fb68767502f5fede643a062c1dc5975d75b27Alan Viverette * The X coordinate, in unscaled screen-relative pixels, around which 38214fb68767502f5fede643a062c1dc5975d75b27Alan Viverette * magnification is focused. 39214fb68767502f5fede643a062c1dc5975d75b27Alan Viverette */ 40152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov public float offsetX; 41214fb68767502f5fede643a062c1dc5975d75b27Alan Viverette 42214fb68767502f5fede643a062c1dc5975d75b27Alan Viverette /** 43214fb68767502f5fede643a062c1dc5975d75b27Alan Viverette * The Y coordinate, in unscaled screen-relative pixels, around which 44214fb68767502f5fede643a062c1dc5975d75b27Alan Viverette * magnification is focused. 45214fb68767502f5fede643a062c1dc5975d75b27Alan Viverette */ 46152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov public float offsetY; 47152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov 48152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov private MagnificationSpec() { 49152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov /* do nothing - reducing visibility */ 50152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov } 51152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov 52152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov public void initialize(float scale, float offsetX, float offsetY) { 53545252f4fde6fbb70b07e97a120c7d1405758017Svetoslav Ganov if (scale < 1) { 54545252f4fde6fbb70b07e97a120c7d1405758017Svetoslav Ganov throw new IllegalArgumentException("Scale must be greater than or equal to one!"); 55545252f4fde6fbb70b07e97a120c7d1405758017Svetoslav Ganov } 56152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov this.scale = scale; 57152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov this.offsetX = offsetX; 58152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov this.offsetY = offsetY; 59152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov } 60152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov 61152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov public boolean isNop() { 62152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov return scale == 1.0f && offsetX == 0 && offsetY == 0; 63152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov } 64152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov 65152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov public static MagnificationSpec obtain(MagnificationSpec other) { 66152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov MagnificationSpec info = obtain(); 67152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov info.scale = other.scale; 68152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov info.offsetX = other.offsetX; 69152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov info.offsetY = other.offsetY; 70152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov return info; 71152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov } 72152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov 73152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov public static MagnificationSpec obtain() { 74152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov MagnificationSpec spec = sPool.acquire(); 75152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov return (spec != null) ? spec : new MagnificationSpec(); 76152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov } 77152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov 78152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov public void recycle() { 79152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov clear(); 80152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov sPool.release(this); 81152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov } 82152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov 83152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov public void clear() { 84152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov scale = 1.0f; 85152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov offsetX = 0.0f; 86152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov offsetY = 0.0f; 87152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov } 88152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov 89214fb68767502f5fede643a062c1dc5975d75b27Alan Viverette public void setTo(MagnificationSpec other) { 90214fb68767502f5fede643a062c1dc5975d75b27Alan Viverette scale = other.scale; 91214fb68767502f5fede643a062c1dc5975d75b27Alan Viverette offsetX = other.offsetX; 92214fb68767502f5fede643a062c1dc5975d75b27Alan Viverette offsetY = other.offsetY; 93214fb68767502f5fede643a062c1dc5975d75b27Alan Viverette } 94214fb68767502f5fede643a062c1dc5975d75b27Alan Viverette 95152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov @Override 96152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov public int describeContents() { 97152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov return 0; 98152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov } 99152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov 100152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov @Override 101152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov public void writeToParcel(Parcel parcel, int flags) { 102152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov parcel.writeFloat(scale); 103152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov parcel.writeFloat(offsetX); 104152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov parcel.writeFloat(offsetY); 105152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov recycle(); 106152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov } 107152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov 108152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov @Override 109214fb68767502f5fede643a062c1dc5975d75b27Alan Viverette public boolean equals(Object other) { 110214fb68767502f5fede643a062c1dc5975d75b27Alan Viverette if (this == other) { 111214fb68767502f5fede643a062c1dc5975d75b27Alan Viverette return true; 112214fb68767502f5fede643a062c1dc5975d75b27Alan Viverette } 113214fb68767502f5fede643a062c1dc5975d75b27Alan Viverette 114214fb68767502f5fede643a062c1dc5975d75b27Alan Viverette if (other == null || getClass() != other.getClass()) { 115214fb68767502f5fede643a062c1dc5975d75b27Alan Viverette return false; 116214fb68767502f5fede643a062c1dc5975d75b27Alan Viverette } 117214fb68767502f5fede643a062c1dc5975d75b27Alan Viverette 118214fb68767502f5fede643a062c1dc5975d75b27Alan Viverette final MagnificationSpec s = (MagnificationSpec) other; 119214fb68767502f5fede643a062c1dc5975d75b27Alan Viverette return scale == s.scale && offsetX == s.offsetX && offsetY == s.offsetY; 120214fb68767502f5fede643a062c1dc5975d75b27Alan Viverette } 121214fb68767502f5fede643a062c1dc5975d75b27Alan Viverette 122214fb68767502f5fede643a062c1dc5975d75b27Alan Viverette @Override 123214fb68767502f5fede643a062c1dc5975d75b27Alan Viverette public int hashCode() { 124214fb68767502f5fede643a062c1dc5975d75b27Alan Viverette int result = (scale != +0.0f ? Float.floatToIntBits(scale) : 0); 125214fb68767502f5fede643a062c1dc5975d75b27Alan Viverette result = 31 * result + (offsetX != +0.0f ? Float.floatToIntBits(offsetX) : 0); 126214fb68767502f5fede643a062c1dc5975d75b27Alan Viverette result = 31 * result + (offsetY != +0.0f ? Float.floatToIntBits(offsetY) : 0); 127214fb68767502f5fede643a062c1dc5975d75b27Alan Viverette return result; 128214fb68767502f5fede643a062c1dc5975d75b27Alan Viverette } 129214fb68767502f5fede643a062c1dc5975d75b27Alan Viverette 130214fb68767502f5fede643a062c1dc5975d75b27Alan Viverette @Override 131152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov public String toString() { 132152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov StringBuilder builder = new StringBuilder(); 133152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov builder.append("<scale:"); 1347d477d4845e8518bd311a064fa307b4772bbb57eSvetoslav Ganov builder.append(Float.toString(scale)); 135152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov builder.append(",offsetX:"); 1367d477d4845e8518bd311a064fa307b4772bbb57eSvetoslav Ganov builder.append(Float.toString(offsetX)); 137152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov builder.append(",offsetY:"); 1387d477d4845e8518bd311a064fa307b4772bbb57eSvetoslav Ganov builder.append(Float.toString(offsetY)); 139152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov builder.append(">"); 140152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov return builder.toString(); 141152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov } 142152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov 143152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov private void initFromParcel(Parcel parcel) { 144152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov scale = parcel.readFloat(); 145152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov offsetX = parcel.readFloat(); 146152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov offsetY = parcel.readFloat(); 147152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov } 148152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov 149152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov public static final Creator<MagnificationSpec> CREATOR = new Creator<MagnificationSpec>() { 150152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov @Override 151152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov public MagnificationSpec[] newArray(int size) { 152152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov return new MagnificationSpec[size]; 153152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov } 154152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov 155152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov @Override 156152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov public MagnificationSpec createFromParcel(Parcel parcel) { 157152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov MagnificationSpec spec = MagnificationSpec.obtain(); 158152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov spec.initFromParcel(parcel); 159152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov return spec; 160152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov } 161152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov }; 162152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov} 163