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 =
31152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov            new SynchronizedPool<MagnificationSpec>(MAX_POOL_SIZE);
32152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov
33152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov    public float scale = 1.0f;
34152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov    public float offsetX;
35152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov    public float offsetY;
36152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov
37152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov    private MagnificationSpec() {
38152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov        /* do nothing - reducing visibility */
39152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov    }
40152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov
41152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov    public void initialize(float scale, float offsetX, float offsetY) {
42545252f4fde6fbb70b07e97a120c7d1405758017Svetoslav Ganov        if (scale < 1) {
43545252f4fde6fbb70b07e97a120c7d1405758017Svetoslav Ganov            throw new IllegalArgumentException("Scale must be greater than or equal to one!");
44545252f4fde6fbb70b07e97a120c7d1405758017Svetoslav Ganov        }
45152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov        this.scale = scale;
46152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov        this.offsetX = offsetX;
47152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov        this.offsetY = offsetY;
48152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov    }
49152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov
50152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov    public boolean isNop() {
51152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov        return scale == 1.0f && offsetX == 0 && offsetY == 0;
52152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov    }
53152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov
54152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov    public static MagnificationSpec obtain(MagnificationSpec other) {
55152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov        MagnificationSpec info = obtain();
56152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov        info.scale = other.scale;
57152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov        info.offsetX = other.offsetX;
58152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov        info.offsetY = other.offsetY;
59152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov        return info;
60152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov    }
61152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov
62152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov    public static MagnificationSpec obtain() {
63152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov        MagnificationSpec spec = sPool.acquire();
64152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov        return (spec != null) ? spec : new MagnificationSpec();
65152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov    }
66152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov
67152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov    public void recycle() {
68152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov        clear();
69152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov        sPool.release(this);
70152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov    }
71152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov
72152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov    public void clear() {
73152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov       scale = 1.0f;
74152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov       offsetX = 0.0f;
75152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov       offsetY = 0.0f;
76152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov    }
77152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov
78152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov    @Override
79152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov    public int describeContents() {
80152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov        return 0;
81152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov    }
82152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov
83152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov    @Override
84152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov    public void writeToParcel(Parcel parcel, int flags) {
85152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov        parcel.writeFloat(scale);
86152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov        parcel.writeFloat(offsetX);
87152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov        parcel.writeFloat(offsetY);
88152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov        recycle();
89152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov    }
90152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov
91152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov    @Override
92152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov    public String toString() {
93152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov        StringBuilder builder = new StringBuilder();
94152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov        builder.append("<scale:");
95152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov        builder.append(scale);
96152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov        builder.append(",offsetX:");
97152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov        builder.append(offsetX);
98152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov        builder.append(",offsetY:");
99152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov        builder.append(offsetY);
100152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov        builder.append(">");
101152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov        return builder.toString();
102152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov    }
103152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov
104152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov    private void initFromParcel(Parcel parcel) {
105152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov        scale = parcel.readFloat();
106152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov        offsetX = parcel.readFloat();
107152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov        offsetY = parcel.readFloat();
108152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov    }
109152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov
110152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov    public static final Creator<MagnificationSpec> CREATOR = new Creator<MagnificationSpec>() {
111152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov        @Override
112152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov        public MagnificationSpec[] newArray(int size) {
113152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov            return new MagnificationSpec[size];
114152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov        }
115152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov
116152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov        @Override
117152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov        public MagnificationSpec createFromParcel(Parcel parcel) {
118152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov            MagnificationSpec spec = MagnificationSpec.obtain();
119152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov            spec.initFromParcel(parcel);
120152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov            return spec;
121152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov        }
122152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov    };
123152e9bb81aa5b2ab4637f4b2dae04b3ce89fa891Svetoslav Ganov}
124