Mitsuru Oshima | 9189cab | 2009-06-03 11:19:12 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 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 android.content.res; |
| 18 | |
| 19 | import android.content.pm.ApplicationInfo; |
Mitsuru Oshima | 64f5934 | 2009-06-21 00:03:11 -0700 | [diff] [blame] | 20 | import android.graphics.Canvas; |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 21 | import android.graphics.PointF; |
Mitsuru Oshima | 64f5934 | 2009-06-21 00:03:11 -0700 | [diff] [blame] | 22 | import android.graphics.Rect; |
| 23 | import android.graphics.Region; |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 24 | import android.os.Parcel; |
| 25 | import android.os.Parcelable; |
Mitsuru Oshima | 9189cab | 2009-06-03 11:19:12 -0700 | [diff] [blame] | 26 | import android.util.DisplayMetrics; |
Mitsuru Oshima | 64f5934 | 2009-06-21 00:03:11 -0700 | [diff] [blame] | 27 | import android.view.MotionEvent; |
| 28 | import android.view.WindowManager; |
| 29 | import android.view.WindowManager.LayoutParams; |
Mitsuru Oshima | 9189cab | 2009-06-03 11:19:12 -0700 | [diff] [blame] | 30 | |
| 31 | /** |
| 32 | * CompatibilityInfo class keeps the information about compatibility mode that the application is |
| 33 | * running under. |
| 34 | * |
| 35 | * {@hide} |
| 36 | */ |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 37 | public class CompatibilityInfo implements Parcelable { |
Mitsuru Oshima | 9189cab | 2009-06-03 11:19:12 -0700 | [diff] [blame] | 38 | /** default compatibility info object for compatible applications */ |
Mitsuru Oshima | 5a2b91d | 2009-07-16 16:30:02 -0700 | [diff] [blame] | 39 | public static final CompatibilityInfo DEFAULT_COMPATIBILITY_INFO = new CompatibilityInfo() { |
Mitsuru Oshima | 5a2b91d | 2009-07-16 16:30:02 -0700 | [diff] [blame] | 40 | }; |
Mitsuru Oshima | 9189cab | 2009-06-03 11:19:12 -0700 | [diff] [blame] | 41 | |
| 42 | /** |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 43 | * This is the number of pixels we would like to have along the |
| 44 | * short axis of an app that needs to run on a normal size screen. |
Mitsuru Oshima | 9189cab | 2009-06-03 11:19:12 -0700 | [diff] [blame] | 45 | */ |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 46 | public static final int DEFAULT_NORMAL_SHORT_DIMENSION = 320; |
Mitsuru Oshima | 9189cab | 2009-06-03 11:19:12 -0700 | [diff] [blame] | 47 | |
| 48 | /** |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 49 | * This is the maximum aspect ratio we will allow while keeping |
| 50 | * applications in a compatible screen size. |
| 51 | */ |
| 52 | public static final float MAXIMUM_ASPECT_RATIO = (854f/480f); |
Mitsuru Oshima | 9189cab | 2009-06-03 11:19:12 -0700 | [diff] [blame] | 53 | |
| 54 | /** |
Mitsuru Oshima | 64f5934 | 2009-06-21 00:03:11 -0700 | [diff] [blame] | 55 | * A compatibility flags |
| 56 | */ |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 57 | private final int mCompatibilityFlags; |
Mitsuru Oshima | 64f5934 | 2009-06-21 00:03:11 -0700 | [diff] [blame] | 58 | |
| 59 | /** |
| 60 | * A flag mask to tell if the application needs scaling (when mApplicationScale != 1.0f) |
| 61 | * {@see compatibilityFlag} |
| 62 | */ |
| 63 | private static final int SCALING_REQUIRED = 1; |
| 64 | |
| 65 | /** |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 66 | * Has the application said that its UI is expandable? Based on the |
| 67 | * <supports-screen> android:expandible in the manifest. |
Mitsuru Oshima | 64f5934 | 2009-06-21 00:03:11 -0700 | [diff] [blame] | 68 | */ |
| 69 | private static final int EXPANDABLE = 2; |
| 70 | |
| 71 | /** |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 72 | * Has the application said that its UI supports large screens? Based on the |
| 73 | * <supports-screen> android:largeScreens in the manifest. |
Dianne Hackborn | c4db95c | 2009-07-21 17:46:02 -0700 | [diff] [blame] | 74 | */ |
| 75 | private static final int LARGE_SCREENS = 8; |
| 76 | |
| 77 | /** |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 78 | * Has the application said that its UI supports xlarge screens? Based on the |
| 79 | * <supports-screen> android:xlargeScreens in the manifest. |
Dianne Hackborn | 14cee9f | 2010-04-23 17:51:26 -0700 | [diff] [blame] | 80 | */ |
| 81 | private static final int XLARGE_SCREENS = 32; |
| 82 | |
| 83 | /** |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 84 | * Application must always run in compatibility mode? |
| 85 | */ |
| 86 | private static final int ALWAYS_COMPAT = 64; |
| 87 | |
| 88 | /** |
| 89 | * Application never should run in compatibility mode? |
| 90 | */ |
| 91 | private static final int NEVER_COMPAT = 128; |
| 92 | |
| 93 | /** |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 94 | * Set if the application needs to run in screen size compatibility mode. |
Dianne Hackborn | 14cee9f | 2010-04-23 17:51:26 -0700 | [diff] [blame] | 95 | */ |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 96 | private static final int NEEDS_SCREEN_COMPAT = 256; |
Mitsuru Oshima | 64f5934 | 2009-06-21 00:03:11 -0700 | [diff] [blame] | 97 | |
| 98 | /** |
Dianne Hackborn | 2784ff0 | 2009-07-18 17:13:29 -0700 | [diff] [blame] | 99 | * The effective screen density we have selected for this application. |
| 100 | */ |
| 101 | public final int applicationDensity; |
| 102 | |
| 103 | /** |
Mitsuru Oshima | 9189cab | 2009-06-03 11:19:12 -0700 | [diff] [blame] | 104 | * Application's scale. |
| 105 | */ |
Mitsuru Oshima | 64f5934 | 2009-06-21 00:03:11 -0700 | [diff] [blame] | 106 | public final float applicationScale; |
Mitsuru Oshima | 9189cab | 2009-06-03 11:19:12 -0700 | [diff] [blame] | 107 | |
| 108 | /** |
| 109 | * Application's inverted scale. |
| 110 | */ |
Mitsuru Oshima | 64f5934 | 2009-06-21 00:03:11 -0700 | [diff] [blame] | 111 | public final float applicationInvertedScale; |
| 112 | |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 113 | public CompatibilityInfo(ApplicationInfo appInfo, int screenLayout, boolean forceCompat) { |
| 114 | int compatFlags = 0; |
| 115 | |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 116 | // We can't rely on the application always setting |
| 117 | // FLAG_RESIZEABLE_FOR_SCREENS so will compute it based on various input. |
| 118 | boolean anyResizeable = false; |
| 119 | |
Mitsuru Oshima | 64f5934 | 2009-06-21 00:03:11 -0700 | [diff] [blame] | 120 | if ((appInfo.flags & ApplicationInfo.FLAG_SUPPORTS_LARGE_SCREENS) != 0) { |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 121 | compatFlags |= LARGE_SCREENS; |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 122 | anyResizeable = true; |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 123 | if (!forceCompat) { |
| 124 | // If we aren't forcing the app into compatibility mode, then |
| 125 | // assume if it supports large screens that we should allow it |
| 126 | // to use the full space of an xlarge screen as well. |
| 127 | compatFlags |= XLARGE_SCREENS | EXPANDABLE; |
| 128 | } |
Dianne Hackborn | c4db95c | 2009-07-21 17:46:02 -0700 | [diff] [blame] | 129 | } |
Dianne Hackborn | 14cee9f | 2010-04-23 17:51:26 -0700 | [diff] [blame] | 130 | if ((appInfo.flags & ApplicationInfo.FLAG_SUPPORTS_XLARGE_SCREENS) != 0) { |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 131 | anyResizeable = true; |
| 132 | if (!forceCompat) { |
| 133 | compatFlags |= XLARGE_SCREENS | EXPANDABLE; |
| 134 | } |
Dianne Hackborn | 14cee9f | 2010-04-23 17:51:26 -0700 | [diff] [blame] | 135 | } |
Dianne Hackborn | 5be8de3 | 2011-05-24 18:11:57 -0700 | [diff] [blame] | 136 | if ((appInfo.flags & ApplicationInfo.FLAG_RESIZEABLE_FOR_SCREENS) != 0) { |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 137 | anyResizeable = true; |
Dianne Hackborn | 5be8de3 | 2011-05-24 18:11:57 -0700 | [diff] [blame] | 138 | compatFlags |= EXPANDABLE; |
| 139 | } |
| 140 | |
| 141 | if (forceCompat) { |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 142 | // If we are forcing compatibility mode, then ignore an app that |
| 143 | // just says it is resizable for screens. We'll only have it fill |
| 144 | // the screen if it explicitly says it supports the screen size we |
| 145 | // are running in. |
Dianne Hackborn | 5be8de3 | 2011-05-24 18:11:57 -0700 | [diff] [blame] | 146 | compatFlags &= ~EXPANDABLE; |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | boolean supportsScreen = false; |
| 150 | switch (screenLayout&Configuration.SCREENLAYOUT_SIZE_MASK) { |
| 151 | case Configuration.SCREENLAYOUT_SIZE_XLARGE: |
| 152 | if ((compatFlags&XLARGE_SCREENS) != 0) { |
| 153 | supportsScreen = true; |
| 154 | } |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 155 | if ((appInfo.flags & ApplicationInfo.FLAG_SUPPORTS_XLARGE_SCREENS) != 0) { |
| 156 | compatFlags |= NEVER_COMPAT; |
| 157 | } |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 158 | break; |
| 159 | case Configuration.SCREENLAYOUT_SIZE_LARGE: |
| 160 | if ((compatFlags&LARGE_SCREENS) != 0) { |
| 161 | supportsScreen = true; |
| 162 | } |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 163 | if ((appInfo.flags & ApplicationInfo.FLAG_SUPPORTS_LARGE_SCREENS) != 0) { |
| 164 | compatFlags |= NEVER_COMPAT; |
| 165 | } |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 166 | break; |
| 167 | } |
| 168 | |
Dianne Hackborn | 5be8de3 | 2011-05-24 18:11:57 -0700 | [diff] [blame] | 169 | if ((screenLayout&Configuration.SCREENLAYOUT_COMPAT_NEEDED) != 0) { |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 170 | if ((compatFlags&EXPANDABLE) != 0) { |
| 171 | supportsScreen = true; |
Dianne Hackborn | 36cd41f | 2011-05-25 21:00:46 -0700 | [diff] [blame] | 172 | } else if (!anyResizeable) { |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 173 | compatFlags |= ALWAYS_COMPAT; |
| 174 | } |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | if (supportsScreen) { |
| 178 | compatFlags &= ~NEEDS_SCREEN_COMPAT; |
| 179 | } else { |
| 180 | compatFlags |= NEEDS_SCREEN_COMPAT; |
Mitsuru Oshima | 64f5934 | 2009-06-21 00:03:11 -0700 | [diff] [blame] | 181 | } |
Mitsuru Oshima | e5fb328 | 2009-06-09 21:16:08 -0700 | [diff] [blame] | 182 | |
Dianne Hackborn | 11b822d | 2009-07-21 20:03:02 -0700 | [diff] [blame] | 183 | if ((appInfo.flags & ApplicationInfo.FLAG_SUPPORTS_SCREEN_DENSITIES) != 0) { |
| 184 | applicationDensity = DisplayMetrics.DENSITY_DEVICE; |
| 185 | applicationScale = 1.0f; |
| 186 | applicationInvertedScale = 1.0f; |
Mitsuru Oshima | 9189cab | 2009-06-03 11:19:12 -0700 | [diff] [blame] | 187 | } else { |
Dianne Hackborn | 2784ff0 | 2009-07-18 17:13:29 -0700 | [diff] [blame] | 188 | applicationDensity = DisplayMetrics.DENSITY_DEFAULT; |
Dianne Hackborn | 11b822d | 2009-07-21 20:03:02 -0700 | [diff] [blame] | 189 | applicationScale = DisplayMetrics.DENSITY_DEVICE |
| 190 | / (float) DisplayMetrics.DENSITY_DEFAULT; |
| 191 | applicationInvertedScale = 1.0f / applicationScale; |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 192 | compatFlags |= SCALING_REQUIRED; |
Mitsuru Oshima | 64f5934 | 2009-06-21 00:03:11 -0700 | [diff] [blame] | 193 | } |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 194 | |
| 195 | mCompatibilityFlags = compatFlags; |
Mitsuru Oshima | 9189cab | 2009-06-03 11:19:12 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 198 | private CompatibilityInfo(int compFlags, |
Dianne Hackborn | 2784ff0 | 2009-07-18 17:13:29 -0700 | [diff] [blame] | 199 | int dens, float scale, float invertedScale) { |
Mitsuru Oshima | 569076c | 2009-07-02 20:06:08 -0700 | [diff] [blame] | 200 | mCompatibilityFlags = compFlags; |
Dianne Hackborn | 2784ff0 | 2009-07-18 17:13:29 -0700 | [diff] [blame] | 201 | applicationDensity = dens; |
Mitsuru Oshima | 569076c | 2009-07-02 20:06:08 -0700 | [diff] [blame] | 202 | applicationScale = scale; |
| 203 | applicationInvertedScale = invertedScale; |
Mitsuru Oshima | 9189cab | 2009-06-03 11:19:12 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Mitsuru Oshima | 569076c | 2009-07-02 20:06:08 -0700 | [diff] [blame] | 206 | private CompatibilityInfo() { |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 207 | this(XLARGE_SCREENS | LARGE_SCREENS | EXPANDABLE, |
Dianne Hackborn | 2784ff0 | 2009-07-18 17:13:29 -0700 | [diff] [blame] | 208 | DisplayMetrics.DENSITY_DEVICE, |
Mitsuru Oshima | 569076c | 2009-07-02 20:06:08 -0700 | [diff] [blame] | 209 | 1.0f, |
| 210 | 1.0f); |
| 211 | } |
| 212 | |
| 213 | /** |
Mitsuru Oshima | 64f5934 | 2009-06-21 00:03:11 -0700 | [diff] [blame] | 214 | * @return true if the scaling is required |
| 215 | */ |
| 216 | public boolean isScalingRequired() { |
Dianne Hackborn | 723738c | 2009-06-25 19:48:04 -0700 | [diff] [blame] | 217 | return (mCompatibilityFlags & SCALING_REQUIRED) != 0; |
Mitsuru Oshima | 64f5934 | 2009-06-21 00:03:11 -0700 | [diff] [blame] | 218 | } |
| 219 | |
Mitsuru Oshima | 1ecf5d2 | 2009-07-06 17:20:38 -0700 | [diff] [blame] | 220 | public boolean supportsScreen() { |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 221 | return (mCompatibilityFlags&NEEDS_SCREEN_COMPAT) == 0; |
Mitsuru Oshima | 1ecf5d2 | 2009-07-06 17:20:38 -0700 | [diff] [blame] | 222 | } |
| 223 | |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 224 | public boolean neverSupportsScreen() { |
| 225 | return (mCompatibilityFlags&NEVER_COMPAT) != 0; |
| 226 | } |
| 227 | |
| 228 | public boolean alwaysSupportsScreen() { |
| 229 | return (mCompatibilityFlags&ALWAYS_COMPAT) != 0; |
| 230 | } |
| 231 | |
Mitsuru Oshima | 9189cab | 2009-06-03 11:19:12 -0700 | [diff] [blame] | 232 | @Override |
| 233 | public String toString() { |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 234 | return "CompatibilityInfo{scale=" + applicationScale + "}"; |
Mitsuru Oshima | 9189cab | 2009-06-03 11:19:12 -0700 | [diff] [blame] | 235 | } |
Mitsuru Oshima | 64f5934 | 2009-06-21 00:03:11 -0700 | [diff] [blame] | 236 | |
| 237 | /** |
Mitsuru Oshima | 589cebe | 2009-07-22 20:38:58 -0700 | [diff] [blame] | 238 | * Returns the translator which translates the coordinates in compatibility mode. |
Mitsuru Oshima | 64f5934 | 2009-06-21 00:03:11 -0700 | [diff] [blame] | 239 | * @param params the window's parameter |
| 240 | */ |
Mitsuru Oshima | 589cebe | 2009-07-22 20:38:58 -0700 | [diff] [blame] | 241 | public Translator getTranslator() { |
| 242 | return isScalingRequired() ? new Translator() : null; |
Mitsuru Oshima | 64f5934 | 2009-06-21 00:03:11 -0700 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | /** |
| 246 | * A helper object to translate the screen and window coordinates back and forth. |
| 247 | * @hide |
| 248 | */ |
| 249 | public class Translator { |
Mitsuru Oshima | 64f5934 | 2009-06-21 00:03:11 -0700 | [diff] [blame] | 250 | final public float applicationScale; |
| 251 | final public float applicationInvertedScale; |
| 252 | |
| 253 | private Rect mContentInsetsBuffer = null; |
Mitsuru Oshima | 1ecf5d2 | 2009-07-06 17:20:38 -0700 | [diff] [blame] | 254 | private Rect mVisibleInsetsBuffer = null; |
Jeff Brown | fbf0977 | 2011-01-16 14:06:57 -0800 | [diff] [blame] | 255 | private Region mTouchableAreaBuffer = null; |
Mitsuru Oshima | 64f5934 | 2009-06-21 00:03:11 -0700 | [diff] [blame] | 256 | |
Mitsuru Oshima | 1ecf5d2 | 2009-07-06 17:20:38 -0700 | [diff] [blame] | 257 | Translator(float applicationScale, float applicationInvertedScale) { |
Mitsuru Oshima | 64f5934 | 2009-06-21 00:03:11 -0700 | [diff] [blame] | 258 | this.applicationScale = applicationScale; |
| 259 | this.applicationInvertedScale = applicationInvertedScale; |
| 260 | } |
| 261 | |
Mitsuru Oshima | 64f5934 | 2009-06-21 00:03:11 -0700 | [diff] [blame] | 262 | Translator() { |
Mitsuru Oshima | 1ecf5d2 | 2009-07-06 17:20:38 -0700 | [diff] [blame] | 263 | this(CompatibilityInfo.this.applicationScale, |
| 264 | CompatibilityInfo.this.applicationInvertedScale); |
Mitsuru Oshima | 64f5934 | 2009-06-21 00:03:11 -0700 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | /** |
| 268 | * Translate the screen rect to the application frame. |
| 269 | */ |
| 270 | public void translateRectInScreenToAppWinFrame(Rect rect) { |
Mitsuru Oshima | 1ecf5d2 | 2009-07-06 17:20:38 -0700 | [diff] [blame] | 271 | rect.scale(applicationInvertedScale); |
Mitsuru Oshima | 64f5934 | 2009-06-21 00:03:11 -0700 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | /** |
| 275 | * Translate the region in window to screen. |
| 276 | */ |
| 277 | public void translateRegionInWindowToScreen(Region transparentRegion) { |
Mitsuru Oshima | 1ecf5d2 | 2009-07-06 17:20:38 -0700 | [diff] [blame] | 278 | transparentRegion.scale(applicationScale); |
Mitsuru Oshima | 64f5934 | 2009-06-21 00:03:11 -0700 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | /** |
| 282 | * Apply translation to the canvas that is necessary to draw the content. |
| 283 | */ |
| 284 | public void translateCanvas(Canvas canvas) { |
Mike Reed | b5c17a6 | 2009-09-21 20:51:27 -0400 | [diff] [blame] | 285 | if (applicationScale == 1.5f) { |
| 286 | /* When we scale for compatibility, we can put our stretched |
| 287 | bitmaps and ninepatches on exacty 1/2 pixel boundaries, |
| 288 | which can give us inconsistent drawing due to imperfect |
| 289 | float precision in the graphics engine's inverse matrix. |
| 290 | |
| 291 | As a work-around, we translate by a tiny amount to avoid |
| 292 | landing on exact pixel centers and boundaries, giving us |
| 293 | the slop we need to draw consistently. |
| 294 | |
| 295 | This constant is meant to resolve to 1/255 after it is |
| 296 | scaled by 1.5 (applicationScale). Note, this is just a guess |
| 297 | as to what is small enough not to create its own artifacts, |
| 298 | and big enough to avoid the precision problems. Feel free |
| 299 | to experiment with smaller values as you choose. |
| 300 | */ |
| 301 | final float tinyOffset = 2.0f / (3 * 255); |
| 302 | canvas.translate(tinyOffset, tinyOffset); |
| 303 | } |
Mitsuru Oshima | 1ecf5d2 | 2009-07-06 17:20:38 -0700 | [diff] [blame] | 304 | canvas.scale(applicationScale, applicationScale); |
Mitsuru Oshima | 64f5934 | 2009-06-21 00:03:11 -0700 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | /** |
| 308 | * Translate the motion event captured on screen to the application's window. |
| 309 | */ |
| 310 | public void translateEventInScreenToAppWindow(MotionEvent event) { |
Mitsuru Oshima | 1ecf5d2 | 2009-07-06 17:20:38 -0700 | [diff] [blame] | 311 | event.scale(applicationInvertedScale); |
Mitsuru Oshima | 64f5934 | 2009-06-21 00:03:11 -0700 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | /** |
| 315 | * Translate the window's layout parameter, from application's view to |
| 316 | * Screen's view. |
| 317 | */ |
| 318 | public void translateWindowLayout(WindowManager.LayoutParams params) { |
Mitsuru Oshima | 1ecf5d2 | 2009-07-06 17:20:38 -0700 | [diff] [blame] | 319 | params.scale(applicationScale); |
Mitsuru Oshima | 64f5934 | 2009-06-21 00:03:11 -0700 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | /** |
| 323 | * Translate a Rect in application's window to screen. |
| 324 | */ |
| 325 | public void translateRectInAppWindowToScreen(Rect rect) { |
Mitsuru Oshima | 1ecf5d2 | 2009-07-06 17:20:38 -0700 | [diff] [blame] | 326 | rect.scale(applicationScale); |
Mitsuru Oshima | 64f5934 | 2009-06-21 00:03:11 -0700 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | /** |
| 330 | * Translate a Rect in screen coordinates into the app window's coordinates. |
| 331 | */ |
| 332 | public void translateRectInScreenToAppWindow(Rect rect) { |
Mitsuru Oshima | 1ecf5d2 | 2009-07-06 17:20:38 -0700 | [diff] [blame] | 333 | rect.scale(applicationInvertedScale); |
Mitsuru Oshima | 64f5934 | 2009-06-21 00:03:11 -0700 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | /** |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 337 | * Translate a Point in screen coordinates into the app window's coordinates. |
| 338 | */ |
| 339 | public void translatePointInScreenToAppWindow(PointF point) { |
| 340 | final float scale = applicationInvertedScale; |
| 341 | if (scale != 1.0f) { |
| 342 | point.x *= scale; |
| 343 | point.y *= scale; |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | /** |
Mitsuru Oshima | 64f5934 | 2009-06-21 00:03:11 -0700 | [diff] [blame] | 348 | * Translate the location of the sub window. |
| 349 | * @param params |
| 350 | */ |
| 351 | public void translateLayoutParamsInAppWindowToScreen(LayoutParams params) { |
Mitsuru Oshima | 1ecf5d2 | 2009-07-06 17:20:38 -0700 | [diff] [blame] | 352 | params.scale(applicationScale); |
Mitsuru Oshima | 64f5934 | 2009-06-21 00:03:11 -0700 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | /** |
| 356 | * Translate the content insets in application window to Screen. This uses |
| 357 | * the internal buffer for content insets to avoid extra object allocation. |
| 358 | */ |
| 359 | public Rect getTranslatedContentInsets(Rect contentInsets) { |
| 360 | if (mContentInsetsBuffer == null) mContentInsetsBuffer = new Rect(); |
| 361 | mContentInsetsBuffer.set(contentInsets); |
| 362 | translateRectInAppWindowToScreen(mContentInsetsBuffer); |
| 363 | return mContentInsetsBuffer; |
| 364 | } |
| 365 | |
| 366 | /** |
| 367 | * Translate the visible insets in application window to Screen. This uses |
Jeff Brown | fbf0977 | 2011-01-16 14:06:57 -0800 | [diff] [blame] | 368 | * the internal buffer for visible insets to avoid extra object allocation. |
Mitsuru Oshima | 64f5934 | 2009-06-21 00:03:11 -0700 | [diff] [blame] | 369 | */ |
Jeff Brown | fbf0977 | 2011-01-16 14:06:57 -0800 | [diff] [blame] | 370 | public Rect getTranslatedVisibleInsets(Rect visibleInsets) { |
Mitsuru Oshima | 1ecf5d2 | 2009-07-06 17:20:38 -0700 | [diff] [blame] | 371 | if (mVisibleInsetsBuffer == null) mVisibleInsetsBuffer = new Rect(); |
| 372 | mVisibleInsetsBuffer.set(visibleInsets); |
| 373 | translateRectInAppWindowToScreen(mVisibleInsetsBuffer); |
| 374 | return mVisibleInsetsBuffer; |
| 375 | } |
Jeff Brown | fbf0977 | 2011-01-16 14:06:57 -0800 | [diff] [blame] | 376 | |
| 377 | /** |
| 378 | * Translate the touchable area in application window to Screen. This uses |
| 379 | * the internal buffer for touchable area to avoid extra object allocation. |
| 380 | */ |
| 381 | public Region getTranslatedTouchableArea(Region touchableArea) { |
| 382 | if (mTouchableAreaBuffer == null) mTouchableAreaBuffer = new Region(); |
| 383 | mTouchableAreaBuffer.set(touchableArea); |
| 384 | mTouchableAreaBuffer.scale(applicationScale); |
| 385 | return mTouchableAreaBuffer; |
| 386 | } |
Mitsuru Oshima | 1ecf5d2 | 2009-07-06 17:20:38 -0700 | [diff] [blame] | 387 | } |
| 388 | |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 389 | public void applyToDisplayMetrics(DisplayMetrics inoutDm) { |
| 390 | if (!supportsScreen()) { |
| 391 | // This is a larger screen device and the app is not |
| 392 | // compatible with large screens, so diddle it. |
| 393 | CompatibilityInfo.updateCompatibleScreenFrame(inoutDm, null, inoutDm); |
Dianne Hackborn | 5be8de3 | 2011-05-24 18:11:57 -0700 | [diff] [blame] | 394 | } else { |
Dianne Hackborn | 81e56d5 | 2011-05-26 00:55:58 -0700 | [diff] [blame^] | 395 | inoutDm.widthPixels = inoutDm.unscaledWidthPixels; |
| 396 | inoutDm.heightPixels = inoutDm.unscaledHeightPixels; |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | if (isScalingRequired()) { |
| 400 | float invertedRatio = applicationInvertedScale; |
| 401 | inoutDm.density *= invertedRatio; |
| 402 | inoutDm.densityDpi = (int)((inoutDm.density*DisplayMetrics.DENSITY_DEFAULT)+.5f); |
| 403 | inoutDm.scaledDensity *= invertedRatio; |
| 404 | inoutDm.xdpi *= invertedRatio; |
| 405 | inoutDm.ydpi *= invertedRatio; |
| 406 | inoutDm.widthPixels = (int) (inoutDm.widthPixels * invertedRatio + 0.5f); |
| 407 | inoutDm.heightPixels = (int) (inoutDm.heightPixels * invertedRatio + 0.5f); |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | public void applyToConfiguration(Configuration inoutConfig) { |
| 412 | if (!supportsScreen()) { |
| 413 | // This is a larger screen device and the app is not |
| 414 | // compatible with large screens, so we are forcing it to |
| 415 | // run as if the screen is normal size. |
| 416 | inoutConfig.screenLayout = |
| 417 | (inoutConfig.screenLayout&~Configuration.SCREENLAYOUT_SIZE_MASK) |
| 418 | | Configuration.SCREENLAYOUT_SIZE_NORMAL; |
| 419 | } |
| 420 | } |
| 421 | |
Mitsuru Oshima | 1ecf5d2 | 2009-07-06 17:20:38 -0700 | [diff] [blame] | 422 | /** |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 423 | * Compute the frame Rect for applications runs under compatibility mode. |
Mitsuru Oshima | 1ecf5d2 | 2009-07-06 17:20:38 -0700 | [diff] [blame] | 424 | * |
| 425 | * @param dm the display metrics used to compute the frame size. |
| 426 | * @param orientation the orientation of the screen. |
| 427 | * @param outRect the output parameter which will contain the result. |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 428 | * @return Returns the scaling factor for the window. |
Mitsuru Oshima | 1ecf5d2 | 2009-07-06 17:20:38 -0700 | [diff] [blame] | 429 | */ |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 430 | public static float updateCompatibleScreenFrame(DisplayMetrics dm, |
| 431 | Rect outRect, DisplayMetrics outDm) { |
Dianne Hackborn | 81e56d5 | 2011-05-26 00:55:58 -0700 | [diff] [blame^] | 432 | final int width = dm.unscaledWidthPixels; |
| 433 | final int height = dm.unscaledHeightPixels; |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 434 | int shortSize, longSize; |
| 435 | if (width < height) { |
| 436 | shortSize = width; |
| 437 | longSize = height; |
Mitsuru Oshima | 1ecf5d2 | 2009-07-06 17:20:38 -0700 | [diff] [blame] | 438 | } else { |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 439 | shortSize = height; |
| 440 | longSize = width; |
Mitsuru Oshima | 64f5934 | 2009-06-21 00:03:11 -0700 | [diff] [blame] | 441 | } |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 442 | int newShortSize = (int)(DEFAULT_NORMAL_SHORT_DIMENSION * dm.density + 0.5f); |
| 443 | float aspect = ((float)longSize) / shortSize; |
| 444 | if (aspect > MAXIMUM_ASPECT_RATIO) { |
| 445 | aspect = MAXIMUM_ASPECT_RATIO; |
| 446 | } |
| 447 | int newLongSize = (int)(newShortSize * aspect + 0.5f); |
| 448 | int newWidth, newHeight; |
| 449 | if (width < height) { |
| 450 | newWidth = newShortSize; |
| 451 | newHeight = newLongSize; |
| 452 | } else { |
| 453 | newWidth = newLongSize; |
| 454 | newHeight = newShortSize; |
| 455 | } |
| 456 | |
| 457 | float sw = width/(float)newWidth; |
| 458 | float sh = height/(float)newHeight; |
| 459 | float scale = sw < sh ? sw : sh; |
| 460 | if (scale < 1) { |
| 461 | scale = 1; |
| 462 | } |
| 463 | |
| 464 | if (outRect != null) { |
| 465 | final int left = (int)((width-(newWidth*scale))/2); |
| 466 | final int top = (int)((height-(newHeight*scale))/2); |
| 467 | outRect.set(left, top, left+newWidth, top+newHeight); |
| 468 | } |
| 469 | |
| 470 | if (outDm != null) { |
| 471 | outDm.widthPixels = newWidth; |
| 472 | outDm.heightPixels = newHeight; |
| 473 | } |
| 474 | |
| 475 | return scale; |
| 476 | } |
| 477 | |
| 478 | @Override |
| 479 | public boolean equals(Object o) { |
| 480 | try { |
| 481 | CompatibilityInfo oc = (CompatibilityInfo)o; |
| 482 | if (mCompatibilityFlags != oc.mCompatibilityFlags) return false; |
| 483 | if (applicationDensity != oc.applicationDensity) return false; |
| 484 | if (applicationScale != oc.applicationScale) return false; |
| 485 | if (applicationInvertedScale != oc.applicationInvertedScale) return false; |
| 486 | return true; |
| 487 | } catch (ClassCastException e) { |
| 488 | return false; |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | @Override |
| 493 | public int hashCode() { |
| 494 | int result = 17; |
| 495 | result = 31 * result + mCompatibilityFlags; |
| 496 | result = 31 * result + applicationDensity; |
| 497 | result = 31 * result + Float.floatToIntBits(applicationScale); |
| 498 | result = 31 * result + Float.floatToIntBits(applicationInvertedScale); |
| 499 | return result; |
| 500 | } |
| 501 | |
| 502 | @Override |
| 503 | public int describeContents() { |
| 504 | return 0; |
| 505 | } |
| 506 | |
| 507 | @Override |
| 508 | public void writeToParcel(Parcel dest, int flags) { |
| 509 | dest.writeInt(mCompatibilityFlags); |
| 510 | dest.writeInt(applicationDensity); |
| 511 | dest.writeFloat(applicationScale); |
| 512 | dest.writeFloat(applicationInvertedScale); |
| 513 | } |
| 514 | |
| 515 | public static final Parcelable.Creator<CompatibilityInfo> CREATOR |
| 516 | = new Parcelable.Creator<CompatibilityInfo>() { |
| 517 | public CompatibilityInfo createFromParcel(Parcel source) { |
| 518 | return new CompatibilityInfo(source); |
| 519 | } |
| 520 | |
| 521 | public CompatibilityInfo[] newArray(int size) { |
| 522 | return new CompatibilityInfo[size]; |
| 523 | } |
| 524 | }; |
| 525 | |
| 526 | private CompatibilityInfo(Parcel source) { |
| 527 | mCompatibilityFlags = source.readInt(); |
| 528 | applicationDensity = source.readInt(); |
| 529 | applicationScale = source.readFloat(); |
| 530 | applicationInvertedScale = source.readFloat(); |
Mitsuru Oshima | 64f5934 | 2009-06-21 00:03:11 -0700 | [diff] [blame] | 531 | } |
Mitsuru Oshima | 569076c | 2009-07-02 20:06:08 -0700 | [diff] [blame] | 532 | } |