| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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.resources.system; | 5 package org.chromium.ui.resources.system; |
| 6 | 6 |
| 7 import android.content.Context; | |
| 8 import android.content.res.Resources; | 7 import android.content.res.Resources; |
| 9 import android.graphics.Bitmap; | 8 import android.graphics.Bitmap; |
| 10 import android.graphics.Canvas; | 9 import android.graphics.Canvas; |
| 11 import android.graphics.Paint; | 10 import android.graphics.Paint; |
| 12 import android.graphics.Paint.Style; | 11 import android.graphics.Paint.Style; |
| 13 import android.graphics.RectF; | 12 import android.graphics.RectF; |
| 14 | 13 |
| 15 import org.chromium.ui.gfx.DeviceDisplayInfo; | |
| 16 import org.chromium.ui.resources.Resource; | 14 import org.chromium.ui.resources.Resource; |
| 17 import org.chromium.ui.resources.SystemUIResourceType; | 15 import org.chromium.ui.resources.SystemUIResourceType; |
| 18 import org.chromium.ui.resources.async.AsyncPreloadResourceLoader; | 16 import org.chromium.ui.resources.async.AsyncPreloadResourceLoader; |
| 19 import org.chromium.ui.resources.statics.StaticResource; | 17 import org.chromium.ui.resources.statics.StaticResource; |
| 20 | 18 |
| 21 /** | 19 /** |
| 22 * Handles loading system specific resources like overscroll and edge glows. | 20 * Handles loading system specific resources like overscroll and edge glows. |
| 23 */ | 21 */ |
| 24 public class SystemResourceLoader extends AsyncPreloadResourceLoader { | 22 public class SystemResourceLoader extends AsyncPreloadResourceLoader { |
| 25 private static final float SIN_PI_OVER_6 = 0.5f; | 23 private static final float SIN_PI_OVER_6 = 0.5f; |
| 26 private static final float COS_PI_OVER_6 = 0.866f; | 24 private static final float COS_PI_OVER_6 = 0.866f; |
| 27 | 25 |
| 28 /** | 26 /** |
| 29 * Creates an instance of a {@link SystemResourceLoader}. | 27 * Creates an instance of a {@link SystemResourceLoader}. |
| 30 * @param resourceType The resource type this loader is responsible for load
ing. | 28 * @param resourceType The resource type this loader is responsible for load
ing. |
| 31 * @param callback The {@link ResourceLoaderCallback} to notify when a {
@link Resource} is | 29 * @param callback The {@link ResourceLoaderCallback} to notify when a {
@link Resource} is |
| 32 * done loading. | 30 * done loading. |
| 33 * @param resources A {@link Resources} instance to load assets from. | 31 * @param minScreenSideLengthPx The length (in pixels) of the smallest si
de of the screen. |
| 34 */ | 32 */ |
| 35 public SystemResourceLoader(int resourceType, ResourceLoaderCallback callbac
k, | 33 public SystemResourceLoader( |
| 36 final Context context) { | 34 int resourceType, ResourceLoaderCallback callback, final int minScre
enSideLengthPx) { |
| 37 super(resourceType, callback, new ResourceCreator() { | 35 super(resourceType, callback, new ResourceCreator() { |
| 38 @Override | 36 @Override |
| 39 public Resource create(int resId) { | 37 public Resource create(int resId) { |
| 40 return createResource(context, resId); | 38 return createResource(minScreenSideLengthPx, resId); |
| 41 } | 39 } |
| 42 }); | 40 }); |
| 43 } | 41 } |
| 44 | 42 |
| 45 private static Resource createResource(Context context, int resId) { | 43 private static Resource createResource(int minScreenSideLengthPx, int resId)
{ |
| 46 switch (resId) { | 44 switch (resId) { |
| 47 case SystemUIResourceType.OVERSCROLL_EDGE: | 45 case SystemUIResourceType.OVERSCROLL_EDGE: |
| 48 return StaticResource.create(Resources.getSystem(), | 46 return StaticResource.create(Resources.getSystem(), |
| 49 getResourceId("android:drawable/overscroll_edge"), 128,
12); | 47 getResourceId("android:drawable/overscroll_edge"), 128,
12); |
| 50 case SystemUIResourceType.OVERSCROLL_GLOW: | 48 case SystemUIResourceType.OVERSCROLL_GLOW: |
| 51 return StaticResource.create(Resources.getSystem(), | 49 return StaticResource.create(Resources.getSystem(), |
| 52 getResourceId("android:drawable/overscroll_glow"), 128,
64); | 50 getResourceId("android:drawable/overscroll_glow"), 128,
64); |
| 53 case SystemUIResourceType.OVERSCROLL_GLOW_L: | 51 case SystemUIResourceType.OVERSCROLL_GLOW_L: |
| 54 return createOverscrollGlowLBitmap(context); | 52 return createOverscrollGlowLBitmap(minScreenSideLengthPx); |
| 55 | 53 |
| 56 default: | 54 default: |
| 57 assert false; | 55 assert false; |
| 58 } | 56 } |
| 59 return null; | 57 return null; |
| 60 } | 58 } |
| 61 | 59 |
| 62 private static Resource createOverscrollGlowLBitmap(Context context) { | 60 private static Resource createOverscrollGlowLBitmap(int minScreenSideLengthP
x) { |
| 63 DeviceDisplayInfo displayInfo = DeviceDisplayInfo.create(context); | 61 float arcWidth = minScreenSideLengthPx * 0.5f / SIN_PI_OVER_6; |
| 64 int screenWidth = displayInfo.getPhysicalDisplayWidth() != 0 | |
| 65 ? displayInfo.getPhysicalDisplayWidth() : displayInfo.getDisplay
Width(); | |
| 66 int screenHeight = displayInfo.getPhysicalDisplayHeight() != 0 | |
| 67 ? displayInfo.getPhysicalDisplayHeight() : displayInfo.getDispla
yHeight(); | |
| 68 | |
| 69 float arcWidth = Math.min(screenWidth, screenHeight) * 0.5f / SIN_PI_OVE
R_6; | |
| 70 float y = COS_PI_OVER_6 * arcWidth; | 62 float y = COS_PI_OVER_6 * arcWidth; |
| 71 float height = arcWidth - y; | 63 float height = arcWidth - y; |
| 72 | 64 |
| 73 float arcRectX = -arcWidth / 2.f; | 65 float arcRectX = -arcWidth / 2.f; |
| 74 float arcRectY = -arcWidth - y; | 66 float arcRectY = -arcWidth - y; |
| 75 float arcRectWidth = arcWidth * 2.f; | 67 float arcRectWidth = arcWidth * 2.f; |
| 76 float arcRectHeight = arcWidth * 2.f; | 68 float arcRectHeight = arcWidth * 2.f; |
| 77 RectF arcRect = new RectF( | 69 RectF arcRect = new RectF( |
| 78 arcRectX, arcRectY, arcRectX + arcRectWidth, arcRectY + arcRectH
eight); | 70 arcRectX, arcRectY, arcRectX + arcRectWidth, arcRectY + arcRectH
eight); |
| 79 | 71 |
| 80 Paint arcPaint = new Paint(); | 72 Paint arcPaint = new Paint(); |
| 81 arcPaint.setAntiAlias(true); | 73 arcPaint.setAntiAlias(true); |
| 82 arcPaint.setAlpha(0xBB); | 74 arcPaint.setAlpha(0xBB); |
| 83 arcPaint.setStyle(Style.FILL); | 75 arcPaint.setStyle(Style.FILL); |
| 84 | 76 |
| 85 Bitmap bitmap = Bitmap.createBitmap((int) arcWidth, (int) height, Bitmap
.Config.ALPHA_8); | 77 Bitmap bitmap = Bitmap.createBitmap((int) arcWidth, (int) height, Bitmap
.Config.ALPHA_8); |
| 86 Canvas canvas = new Canvas(bitmap); | 78 Canvas canvas = new Canvas(bitmap); |
| 87 canvas.drawArc(arcRect, 45, 90, true, arcPaint); | 79 canvas.drawArc(arcRect, 45, 90, true, arcPaint); |
| 88 | 80 |
| 89 return new StaticResource(bitmap); | 81 return new StaticResource(bitmap); |
| 90 } | 82 } |
| 91 | 83 |
| 92 private static int getResourceId(String name) { | 84 private static int getResourceId(String name) { |
| 93 Resources systemResources = Resources.getSystem(); | 85 Resources systemResources = Resources.getSystem(); |
| 94 return systemResources.getIdentifier(name, null, null); | 86 return systemResources.getIdentifier(name, null, null); |
| 95 } | 87 } |
| 96 } | 88 } |
| OLD | NEW |