1 /* 2 * Copyright (C) 2010 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 com.android.gallery3d.ui; 18 19 import com.android.gallery3d.app.GalleryActivity; 20 import com.android.gallery3d.common.Utils; 21 22 import java.util.HashMap; 23 import java.util.WeakHashMap; 24 25 public class PositionRepository { 26 private static final WeakHashMap<GalleryActivity, PositionRepository> 27 sMap = new WeakHashMap<GalleryActivity, PositionRepository>(); 28 29 public static class Position implements Cloneable { 30 public float x; 31 public float y; 32 public float z; 33 public float theta; 34 public float alpha; 35 36 public Position() { 37 } 38 39 public Position(float x, float y, float z) { 40 this(x, y, z, 0f, 1f); 41 } 42 43 public Position(float x, float y, float z, float ftheta, float alpha) { 44 this.x = x; 45 this.y = y; 46 this.z = z; 47 this.theta = ftheta; 48 this.alpha = alpha; 49 } 50 51 @Override 52 public Position clone() { 53 try { 54 return (Position) super.clone(); 55 } catch (CloneNotSupportedException e) { 56 throw new AssertionError(); // we do support clone. 57 } 58 } 59 60 public void set(Position another) { 61 x = another.x; 62 y = another.y; 63 z = another.z; 64 theta = another.theta; 65 alpha = another.alpha; 66 } 67 68 public void set(float x, float y, float z, float ftheta, float alpha) { 69 this.x = x; 70 this.y = y; 71 this.z = z; 72 this.theta = ftheta; 73 this.alpha = alpha; 74 } 75 76 @Override 77 public boolean equals(Object object) { 78 if (!(object instanceof Position)) return false; 79 Position position = (Position) object; 80 return x == position.x && y == position.y && z == position.z 81 && theta == position.theta 82 && alpha == position.alpha; 83 } 84 85 public static void interpolate( 86 Position source, Position target, Position output, float progress) { 87 if (progress < 1f) { 88 output.set( 89 Utils.interpolateScale(source.x, target.x, progress), 90 Utils.interpolateScale(source.y, target.y, progress), 91 Utils.interpolateScale(source.z, target.z, progress), 92 Utils.interpolateAngle(source.theta, target.theta, progress), 93 Utils.interpolateScale(source.alpha, target.alpha, progress)); 94 } else { 95 output.set(target); 96 } 97 } 98 } 99 100 public static PositionRepository getInstance(GalleryActivity activity) { 101 PositionRepository repository = sMap.get(activity); 102 if (repository == null) { 103 repository = new PositionRepository(); 104 sMap.put(activity, repository); 105 } 106 return repository; 107 } 108 109 private HashMap<Long, Position> mData = new HashMap<Long, Position>(); 110 private int mOffsetX; 111 private int mOffsetY; 112 private Position mTempPosition = new Position(); 113 114 public Position get(Long identity) { 115 Position position = mData.get(identity); 116 if (position == null) return null; 117 mTempPosition.set(position); 118 position = mTempPosition; 119 position.x -= mOffsetX; 120 position.y -= mOffsetY; 121 return position; 122 } 123 124 public void setOffset(int offsetX, int offsetY) { 125 mOffsetX = offsetX; 126 mOffsetY = offsetY; 127 } 128 129 public void putPosition(Long identity, Position position) { 130 Position clone = position.clone(); 131 clone.x += mOffsetX; 132 clone.y += mOffsetY; 133 mData.put(identity, clone); 134 } 135 136 public void clear() { 137 mData.clear(); 138 } 139 } 140