| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.ui.display; | 5 package org.chromium.ui.display; |
| 6 | 6 |
| 7 import android.annotation.TargetApi; | 7 import android.annotation.TargetApi; |
| 8 import android.content.Context; | 8 import android.content.Context; |
| 9 import android.graphics.PixelFormat; | 9 import android.graphics.PixelFormat; |
| 10 import android.graphics.Point; | 10 import android.graphics.Point; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 29 /** | 29 /** |
| 30 * DisplayAndroidObserver interface for changes to this Display. | 30 * DisplayAndroidObserver interface for changes to this Display. |
| 31 */ | 31 */ |
| 32 public interface DisplayAndroidObserver { | 32 public interface DisplayAndroidObserver { |
| 33 /** | 33 /** |
| 34 * Called whenever the screen orientation changes. | 34 * Called whenever the screen orientation changes. |
| 35 * | 35 * |
| 36 * @param orientation One of Surface.ROTATION_* values. | 36 * @param orientation One of Surface.ROTATION_* values. |
| 37 */ | 37 */ |
| 38 void onRotationChanged(int rotation); | 38 void onRotationChanged(int rotation); |
| 39 |
| 40 /** |
| 41 * Called whenever the screen density changes. |
| 42 * |
| 43 * @param screen density, aka Density Independent Pixel scale. |
| 44 */ |
| 45 void onDIPScaleChanged(float dipScale); |
| 39 } | 46 } |
| 40 | 47 |
| 41 private static final String TAG = "DisplayAndroid"; | 48 private static final String TAG = "DisplayAndroid"; |
| 42 | 49 |
| 43 private static final DisplayAndroidObserver[] EMPTY_OBSERVER_ARRAY = | 50 private static final DisplayAndroidObserver[] EMPTY_OBSERVER_ARRAY = |
| 44 new DisplayAndroidObserver[0]; | 51 new DisplayAndroidObserver[0]; |
| 45 | 52 |
| 46 private final int mSdkDisplayId; | 53 private final int mSdkDisplayId; |
| 47 private final WeakHashMap<DisplayAndroidObserver, Object /* null */> mObserv
ers; | 54 private final WeakHashMap<DisplayAndroidObserver, Object /* null */> mObserv
ers; |
| 48 // Do NOT add strong references to objects with potentially complex lifetime
, like Context. | 55 // Do NOT add strong references to objects with potentially complex lifetime
, like Context. |
| (...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 242 mSize = new Point(); | 249 mSize = new Point(); |
| 243 mPhysicalSize = new Point(); | 250 mPhysicalSize = new Point(); |
| 244 mDisplayMetrics = new DisplayMetrics(); | 251 mDisplayMetrics = new DisplayMetrics(); |
| 245 mPixelFormatId = PixelFormat.RGBA_8888; | 252 mPixelFormatId = PixelFormat.RGBA_8888; |
| 246 updateFromDisplay(display); | 253 updateFromDisplay(display); |
| 247 } | 254 } |
| 248 | 255 |
| 249 @SuppressWarnings("deprecation") | 256 @SuppressWarnings("deprecation") |
| 250 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1) | 257 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1) |
| 251 /* package */ void updateFromDisplay(Display display) { | 258 /* package */ void updateFromDisplay(Display display) { |
| 259 float oldDensity = mDisplayMetrics.density; |
| 260 |
| 252 display.getSize(mSize); | 261 display.getSize(mSize); |
| 253 display.getMetrics(mDisplayMetrics); | 262 display.getMetrics(mDisplayMetrics); |
| 254 | 263 |
| 255 if (hasForcedDIPScale()) mDisplayMetrics.density = sForcedDIPScale.float
Value(); | 264 if (hasForcedDIPScale()) mDisplayMetrics.density = sForcedDIPScale.float
Value(); |
| 256 | 265 |
| 257 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { | 266 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { |
| 258 display.getRealSize(mPhysicalSize); | 267 display.getRealSize(mPhysicalSize); |
| 259 } | 268 } |
| 260 | 269 |
| 261 // JellyBean MR1 and later always uses RGBA_8888. | 270 // JellyBean MR1 and later always uses RGBA_8888. |
| 262 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) { | 271 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) { |
| 263 mPixelFormatId = display.getPixelFormat(); | 272 mPixelFormatId = display.getPixelFormat(); |
| 264 } | 273 } |
| 265 | 274 |
| 266 int newRotation = display.getRotation(); | 275 int newRotation = display.getRotation(); |
| 267 boolean rotationChanged = newRotation != mRotation; | 276 boolean rotationChanged = newRotation != mRotation; |
| 268 mRotation = newRotation; | 277 mRotation = newRotation; |
| 269 | 278 |
| 270 if (rotationChanged) { | 279 if (rotationChanged) { |
| 271 DisplayAndroidObserver[] observers = getObservers(); | 280 DisplayAndroidObserver[] observers = getObservers(); |
| 272 for (DisplayAndroidObserver o : observers) { | 281 for (DisplayAndroidObserver o : observers) { |
| 273 o.onRotationChanged(mRotation); | 282 o.onRotationChanged(mRotation); |
| 274 } | 283 } |
| 275 } | 284 } |
| 285 |
| 286 // Intentional comparison of floats: we assume that if scales differ, |
| 287 // they differ significantly. |
| 288 boolean dipScaleChanged = oldDensity != mDisplayMetrics.density; |
| 289 if (dipScaleChanged) { |
| 290 DisplayAndroidObserver[] observers = getObservers(); |
| 291 for (DisplayAndroidObserver o : observers) { |
| 292 o.onDIPScaleChanged(mDisplayMetrics.density); |
| 293 } |
| 294 } |
| 276 } | 295 } |
| 277 | 296 |
| 278 private DisplayAndroidObserver[] getObservers() { | 297 private DisplayAndroidObserver[] getObservers() { |
| 279 // Makes a copy to allow concurrent edit. | 298 // Makes a copy to allow concurrent edit. |
| 280 return mObservers.keySet().toArray(EMPTY_OBSERVER_ARRAY); | 299 return mObservers.keySet().toArray(EMPTY_OBSERVER_ARRAY); |
| 281 } | 300 } |
| 282 } | 301 } |
| OLD | NEW |