FloatingActionButtonImpl.java revision 8603357c9433b9aef1805413a3bfc4a4f8decc50
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
17package android.support.design.widget;
18
19import android.content.res.ColorStateList;
20import android.content.res.Resources;
21import android.graphics.PorterDuff;
22import android.graphics.drawable.Drawable;
23import android.support.annotation.Nullable;
24import android.support.design.R;
25import android.view.View;
26import android.view.ViewTreeObserver;
27
28abstract class FloatingActionButtonImpl {
29
30    interface InternalVisibilityChangedListener {
31        public void onShown();
32        public void onHidden();
33    }
34
35    static final int SHOW_HIDE_ANIM_DURATION = 200;
36
37    static final int[] PRESSED_ENABLED_STATE_SET = {android.R.attr.state_pressed,
38            android.R.attr.state_enabled};
39    static final int[] FOCUSED_ENABLED_STATE_SET = {android.R.attr.state_focused,
40            android.R.attr.state_enabled};
41    static final int[] EMPTY_STATE_SET = new int[0];
42
43    final View mView;
44    final ShadowViewDelegate mShadowViewDelegate;
45
46    private ViewTreeObserver.OnPreDrawListener mPreDrawListener;
47
48    FloatingActionButtonImpl(View view, ShadowViewDelegate shadowViewDelegate) {
49        mView = view;
50        mShadowViewDelegate = shadowViewDelegate;
51    }
52
53    abstract void setBackgroundDrawable(Drawable originalBackground, ColorStateList backgroundTint,
54            PorterDuff.Mode backgroundTintMode, int rippleColor, int borderWidth);
55
56    abstract void setBackgroundTintList(ColorStateList tint);
57
58    abstract void setBackgroundTintMode(PorterDuff.Mode tintMode);
59
60    abstract void setRippleColor(int rippleColor);
61
62    abstract void setElevation(float elevation);
63
64    abstract void setPressedTranslationZ(float translationZ);
65
66    abstract void onDrawableStateChanged(int[] state);
67
68    abstract void jumpDrawableToCurrentState();
69
70    abstract void hide(@Nullable InternalVisibilityChangedListener listener);
71
72    abstract void show(@Nullable InternalVisibilityChangedListener listener);
73
74    void onAttachedToWindow() {
75        if (requirePreDrawListener()) {
76            ensurePreDrawListener();
77            mView.getViewTreeObserver().addOnPreDrawListener(mPreDrawListener);
78        }
79    }
80
81    void onDetachedFromWindow() {
82        if (mPreDrawListener != null) {
83            mView.getViewTreeObserver().removeOnPreDrawListener(mPreDrawListener);
84            mPreDrawListener = null;
85        }
86    }
87
88    boolean requirePreDrawListener() {
89        return false;
90    }
91
92    CircularBorderDrawable createBorderDrawable(int borderWidth, ColorStateList backgroundTint) {
93        final Resources resources = mView.getResources();
94        CircularBorderDrawable borderDrawable = newCircularDrawable();
95        borderDrawable.setGradientColors(
96                resources.getColor(R.color.design_fab_stroke_top_outer_color),
97                resources.getColor(R.color.design_fab_stroke_top_inner_color),
98                resources.getColor(R.color.design_fab_stroke_end_inner_color),
99                resources.getColor(R.color.design_fab_stroke_end_outer_color));
100        borderDrawable.setBorderWidth(borderWidth);
101        borderDrawable.setBorderTint(backgroundTint);
102        return borderDrawable;
103    }
104
105    CircularBorderDrawable newCircularDrawable() {
106        return new CircularBorderDrawable();
107    }
108
109    void onPreDraw() {
110    }
111
112    private void ensurePreDrawListener() {
113        if (mPreDrawListener == null) {
114            mPreDrawListener = new ViewTreeObserver.OnPreDrawListener() {
115                @Override
116                public boolean onPreDraw() {
117                    FloatingActionButtonImpl.this.onPreDraw();
118                    return true;
119                }
120            };
121        }
122    }
123}
124