1 /*
2  * Copyright (C) 2015 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.graphics.drawable;
18 
19 import android.graphics.Rect;
20 import android.util.DisplayMetrics;
21 
22 /**
23  * Abstract class that handles size & positioning common to the ripple & focus states.
24  */
25 abstract class RippleComponent {
26     protected final RippleDrawable mOwner;
27 
28     /** Bounds used for computing max radius. May be modified by the owner. */
29     protected final Rect mBounds;
30 
31     /** Whether we have an explicit maximum radius. */
32     private boolean mHasMaxRadius;
33 
34     /** How big this ripple should be when fully entered. */
35     protected float mTargetRadius;
36 
37     /** Screen density used to adjust pixel-based constants. */
38     protected float mDensityScale;
39 
RippleComponent(RippleDrawable owner, Rect bounds)40     public RippleComponent(RippleDrawable owner, Rect bounds) {
41         mOwner = owner;
42         mBounds = bounds;
43     }
44 
onBoundsChange()45     public void onBoundsChange() {
46         if (!mHasMaxRadius) {
47             mTargetRadius = getTargetRadius(mBounds);
48             onTargetRadiusChanged(mTargetRadius);
49         }
50     }
51 
setup(float maxRadius, int densityDpi)52     public final void setup(float maxRadius, int densityDpi) {
53         if (maxRadius >= 0) {
54             mHasMaxRadius = true;
55             mTargetRadius = maxRadius;
56         } else {
57             mTargetRadius = getTargetRadius(mBounds);
58         }
59 
60         mDensityScale = densityDpi * DisplayMetrics.DENSITY_DEFAULT_SCALE;
61 
62         onTargetRadiusChanged(mTargetRadius);
63     }
64 
getTargetRadius(Rect bounds)65     private static float getTargetRadius(Rect bounds) {
66         final float halfWidth = bounds.width() / 2.0f;
67         final float halfHeight = bounds.height() / 2.0f;
68         return (float) Math.sqrt(halfWidth * halfWidth + halfHeight * halfHeight);
69     }
70 
71     /**
72      * Populates {@code bounds} with the maximum drawing bounds of the ripple
73      * relative to its center. The resulting bounds should be translated into
74      * parent drawable coordinates before use.
75      *
76      * @param bounds the rect to populate with drawing bounds
77      */
getBounds(Rect bounds)78     public void getBounds(Rect bounds) {
79         final int r = (int) Math.ceil(mTargetRadius);
80         bounds.set(-r, -r, r, r);
81     }
82 
invalidateSelf()83     protected final void invalidateSelf() {
84         mOwner.invalidateSelf(false);
85     }
86 
onHotspotBoundsChanged()87     protected final void onHotspotBoundsChanged() {
88         if (!mHasMaxRadius) {
89             mTargetRadius = getTargetRadius(mBounds);
90             onTargetRadiusChanged(mTargetRadius);
91         }
92     }
93 
94     /**
95      * Called when the target radius changes.
96      *
97      * @param targetRadius the new target radius
98      */
onTargetRadiusChanged(float targetRadius)99     protected void onTargetRadiusChanged(float targetRadius) {
100         // Stub.
101     }
102 }
103