1213a4a086b54904cee543adf60b16fc1a61efe38Doris Liu/*
2213a4a086b54904cee543adf60b16fc1a61efe38Doris Liu * Copyright (C) 2014 The Android Open Source Project
3213a4a086b54904cee543adf60b16fc1a61efe38Doris Liu *
4213a4a086b54904cee543adf60b16fc1a61efe38Doris Liu * Licensed under the Apache License, Version 2.0 (the "License");
5213a4a086b54904cee543adf60b16fc1a61efe38Doris Liu * you may not use this file except in compliance with the License.
6213a4a086b54904cee543adf60b16fc1a61efe38Doris Liu * You may obtain a copy of the License at
7213a4a086b54904cee543adf60b16fc1a61efe38Doris Liu *
8213a4a086b54904cee543adf60b16fc1a61efe38Doris Liu *      http://www.apache.org/licenses/LICENSE-2.0
9213a4a086b54904cee543adf60b16fc1a61efe38Doris Liu *
10213a4a086b54904cee543adf60b16fc1a61efe38Doris Liu * Unless required by applicable law or agreed to in writing, software
11213a4a086b54904cee543adf60b16fc1a61efe38Doris Liu * distributed under the License is distributed on an "AS IS" BASIS,
12213a4a086b54904cee543adf60b16fc1a61efe38Doris Liu * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13213a4a086b54904cee543adf60b16fc1a61efe38Doris Liu * See the License for the specific language governing permissions and
14213a4a086b54904cee543adf60b16fc1a61efe38Doris Liu * limitations under the License.
15213a4a086b54904cee543adf60b16fc1a61efe38Doris Liu */
16213a4a086b54904cee543adf60b16fc1a61efe38Doris Liu
17213a4a086b54904cee543adf60b16fc1a61efe38Doris Liupackage com.android.camera.ui;
18213a4a086b54904cee543adf60b16fc1a61efe38Doris Liu
19213a4a086b54904cee543adf60b16fc1a61efe38Doris Liuimport android.content.Context;
20213a4a086b54904cee543adf60b16fc1a61efe38Doris Liuimport android.graphics.Canvas;
21040e3c053cc0d2097332de24cf5328a26a8e7332Doris Liuimport android.graphics.drawable.Drawable;
22213a4a086b54904cee543adf60b16fc1a61efe38Doris Liuimport android.graphics.drawable.GradientDrawable;
23213a4a086b54904cee543adf60b16fc1a61efe38Doris Liuimport android.util.AttributeSet;
24040e3c053cc0d2097332de24cf5328a26a8e7332Doris Liuimport android.view.View;
25213a4a086b54904cee543adf60b16fc1a61efe38Doris Liu
26213a4a086b54904cee543adf60b16fc1a61efe38Doris Liuimport com.android.camera2.R;
27213a4a086b54904cee543adf60b16fc1a61efe38Doris Liu
28213a4a086b54904cee543adf60b16fc1a61efe38Doris Liu/**
29213a4a086b54904cee543adf60b16fc1a61efe38Doris Liu * This class encapsulates the logic of drawing different states of the icon in
30213a4a086b54904cee543adf60b16fc1a61efe38Doris Liu * mode drawer for when it is highlighted (to indicate the current module), or when
31213a4a086b54904cee543adf60b16fc1a61efe38Doris Liu * it is selected by the user. It handles the internal state change like a state
32213a4a086b54904cee543adf60b16fc1a61efe38Doris Liu * list drawable. The advantage over a state list drawable is that in the class
33213a4a086b54904cee543adf60b16fc1a61efe38Doris Liu * multiple states can be rendered using the same drawable with some color modification,
34213a4a086b54904cee543adf60b16fc1a61efe38Doris Liu * whereas a state list drawable would require a different drawable for each state.
35213a4a086b54904cee543adf60b16fc1a61efe38Doris Liu */
36040e3c053cc0d2097332de24cf5328a26a8e7332Doris Liupublic class ModeIconView extends View {
37213a4a086b54904cee543adf60b16fc1a61efe38Doris Liu    private final GradientDrawable mBackground;
380496fcaa51f88f6c753975473b971941e4090cfaSpike Sprague
39213a4a086b54904cee543adf60b16fc1a61efe38Doris Liu    private final int mIconBackgroundSize;
40213a4a086b54904cee543adf60b16fc1a61efe38Doris Liu    private int mHighlightColor;
41213a4a086b54904cee543adf60b16fc1a61efe38Doris Liu    private final int mBackgroundDefaultColor;
42040e3c053cc0d2097332de24cf5328a26a8e7332Doris Liu    private final int mIconDrawableSize;
43040e3c053cc0d2097332de24cf5328a26a8e7332Doris Liu    private Drawable mIconDrawable = null;
44213a4a086b54904cee543adf60b16fc1a61efe38Doris Liu
45213a4a086b54904cee543adf60b16fc1a61efe38Doris Liu    public ModeIconView(Context context, AttributeSet attrs) {
46213a4a086b54904cee543adf60b16fc1a61efe38Doris Liu        super(context, attrs);
47213a4a086b54904cee543adf60b16fc1a61efe38Doris Liu        mBackgroundDefaultColor = getResources().getColor(R.color.mode_selector_icon_background);
48213a4a086b54904cee543adf60b16fc1a61efe38Doris Liu        mIconBackgroundSize = getResources().getDimensionPixelSize(
49213a4a086b54904cee543adf60b16fc1a61efe38Doris Liu                R.dimen.mode_selector_icon_block_width);
50213a4a086b54904cee543adf60b16fc1a61efe38Doris Liu        mBackground = (GradientDrawable) getResources()
51213a4a086b54904cee543adf60b16fc1a61efe38Doris Liu                .getDrawable(R.drawable.mode_icon_background).mutate();
52213a4a086b54904cee543adf60b16fc1a61efe38Doris Liu        mBackground.setBounds(0, 0, mIconBackgroundSize, mIconBackgroundSize);
53040e3c053cc0d2097332de24cf5328a26a8e7332Doris Liu        mIconDrawableSize = getResources().getDimensionPixelSize(
54040e3c053cc0d2097332de24cf5328a26a8e7332Doris Liu                R.dimen.mode_selector_icon_drawable_size);
55040e3c053cc0d2097332de24cf5328a26a8e7332Doris Liu    }
56040e3c053cc0d2097332de24cf5328a26a8e7332Doris Liu
57040e3c053cc0d2097332de24cf5328a26a8e7332Doris Liu    /**
58040e3c053cc0d2097332de24cf5328a26a8e7332Doris Liu     * Sets the drawable that shows the icon of the mode.
59040e3c053cc0d2097332de24cf5328a26a8e7332Doris Liu     *
60040e3c053cc0d2097332de24cf5328a26a8e7332Doris Liu     * @param drawable drawable of the mode icon
61040e3c053cc0d2097332de24cf5328a26a8e7332Doris Liu     */
62040e3c053cc0d2097332de24cf5328a26a8e7332Doris Liu    public void setIconDrawable(Drawable drawable) {
63040e3c053cc0d2097332de24cf5328a26a8e7332Doris Liu        mIconDrawable = drawable;
640496fcaa51f88f6c753975473b971941e4090cfaSpike Sprague
65040e3c053cc0d2097332de24cf5328a26a8e7332Doris Liu        // Center icon in the background.
66040e3c053cc0d2097332de24cf5328a26a8e7332Doris Liu        if (mIconDrawable != null) {
67040e3c053cc0d2097332de24cf5328a26a8e7332Doris Liu            mIconDrawable.setBounds(mIconBackgroundSize / 2 - mIconDrawableSize / 2,
68040e3c053cc0d2097332de24cf5328a26a8e7332Doris Liu                    mIconBackgroundSize / 2 - mIconDrawableSize / 2,
69040e3c053cc0d2097332de24cf5328a26a8e7332Doris Liu                    mIconBackgroundSize / 2 + mIconDrawableSize / 2,
70040e3c053cc0d2097332de24cf5328a26a8e7332Doris Liu                    mIconBackgroundSize / 2 + mIconDrawableSize / 2);
71040e3c053cc0d2097332de24cf5328a26a8e7332Doris Liu            invalidate();
72040e3c053cc0d2097332de24cf5328a26a8e7332Doris Liu        }
73213a4a086b54904cee543adf60b16fc1a61efe38Doris Liu    }
74213a4a086b54904cee543adf60b16fc1a61efe38Doris Liu
75213a4a086b54904cee543adf60b16fc1a61efe38Doris Liu    @Override
76213a4a086b54904cee543adf60b16fc1a61efe38Doris Liu    public void draw(Canvas canvas) {
77040e3c053cc0d2097332de24cf5328a26a8e7332Doris Liu        super.draw(canvas);
783c3b31dfd5ec7a43e89cd4695cc8c498afc3a91dSpike Sprague        mBackground.draw(canvas);
79040e3c053cc0d2097332de24cf5328a26a8e7332Doris Liu        if (mIconDrawable != null) {
80040e3c053cc0d2097332de24cf5328a26a8e7332Doris Liu            mIconDrawable.draw(canvas);
81040e3c053cc0d2097332de24cf5328a26a8e7332Doris Liu        }
820496fcaa51f88f6c753975473b971941e4090cfaSpike Sprague    }
83040e3c053cc0d2097332de24cf5328a26a8e7332Doris Liu
840496fcaa51f88f6c753975473b971941e4090cfaSpike Sprague    /**
850496fcaa51f88f6c753975473b971941e4090cfaSpike Sprague     * @return A clone of the icon drawable associated with this view.
860496fcaa51f88f6c753975473b971941e4090cfaSpike Sprague     */
870496fcaa51f88f6c753975473b971941e4090cfaSpike Sprague    public Drawable getIconDrawableClone() {
880496fcaa51f88f6c753975473b971941e4090cfaSpike Sprague        return mIconDrawable.getConstantState().newDrawable();
890496fcaa51f88f6c753975473b971941e4090cfaSpike Sprague    }
900496fcaa51f88f6c753975473b971941e4090cfaSpike Sprague
910496fcaa51f88f6c753975473b971941e4090cfaSpike Sprague    /**
920496fcaa51f88f6c753975473b971941e4090cfaSpike Sprague     * @return The size of the icon drawable.
930496fcaa51f88f6c753975473b971941e4090cfaSpike Sprague     */
940496fcaa51f88f6c753975473b971941e4090cfaSpike Sprague    public int getIconDrawableSize() {
950496fcaa51f88f6c753975473b971941e4090cfaSpike Sprague        return mIconDrawableSize;
96213a4a086b54904cee543adf60b16fc1a61efe38Doris Liu    }
97213a4a086b54904cee543adf60b16fc1a61efe38Doris Liu
98213a4a086b54904cee543adf60b16fc1a61efe38Doris Liu    /**
99213a4a086b54904cee543adf60b16fc1a61efe38Doris Liu     * This gets called when the selected state is changed. When selected, the background
100213a4a086b54904cee543adf60b16fc1a61efe38Doris Liu     * drawable will use a solid pre-defined color to indicate selection.
101213a4a086b54904cee543adf60b16fc1a61efe38Doris Liu     *
102213a4a086b54904cee543adf60b16fc1a61efe38Doris Liu     * @param selected true when selected, false otherwise.
103213a4a086b54904cee543adf60b16fc1a61efe38Doris Liu     */
1040496fcaa51f88f6c753975473b971941e4090cfaSpike Sprague    @Override
105213a4a086b54904cee543adf60b16fc1a61efe38Doris Liu    public void setSelected(boolean selected) {
106213a4a086b54904cee543adf60b16fc1a61efe38Doris Liu        if (selected) {
107213a4a086b54904cee543adf60b16fc1a61efe38Doris Liu            mBackground.setColor(mHighlightColor);
108213a4a086b54904cee543adf60b16fc1a61efe38Doris Liu        } else {
109213a4a086b54904cee543adf60b16fc1a61efe38Doris Liu            mBackground.setColor(mBackgroundDefaultColor);
110213a4a086b54904cee543adf60b16fc1a61efe38Doris Liu        }
1110496fcaa51f88f6c753975473b971941e4090cfaSpike Sprague
112213a4a086b54904cee543adf60b16fc1a61efe38Doris Liu        invalidate();
113213a4a086b54904cee543adf60b16fc1a61efe38Doris Liu    }
114213a4a086b54904cee543adf60b16fc1a61efe38Doris Liu
115213a4a086b54904cee543adf60b16fc1a61efe38Doris Liu    /**
116213a4a086b54904cee543adf60b16fc1a61efe38Doris Liu     * Sets the color that will be used in the drawable for highlight state.
117213a4a086b54904cee543adf60b16fc1a61efe38Doris Liu     *
118213a4a086b54904cee543adf60b16fc1a61efe38Doris Liu     * @param highlightColor color for the highlight state
119213a4a086b54904cee543adf60b16fc1a61efe38Doris Liu     */
120213a4a086b54904cee543adf60b16fc1a61efe38Doris Liu    public void setHighlightColor(int highlightColor) {
121213a4a086b54904cee543adf60b16fc1a61efe38Doris Liu        mHighlightColor = highlightColor;
1220496fcaa51f88f6c753975473b971941e4090cfaSpike Sprague    }
1230496fcaa51f88f6c753975473b971941e4090cfaSpike Sprague
1240496fcaa51f88f6c753975473b971941e4090cfaSpike Sprague    /**
1250496fcaa51f88f6c753975473b971941e4090cfaSpike Sprague     * @return The highlightColor color the the highlight state.
1260496fcaa51f88f6c753975473b971941e4090cfaSpike Sprague     */
1270496fcaa51f88f6c753975473b971941e4090cfaSpike Sprague    public int getHighlightColor() {
1280496fcaa51f88f6c753975473b971941e4090cfaSpike Sprague        return mHighlightColor;
129213a4a086b54904cee543adf60b16fc1a61efe38Doris Liu    }
130213a4a086b54904cee543adf60b16fc1a61efe38Doris Liu}
131