1/*
2 * Copyright (C) 2013 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 com.android.inputmethod.latin.setup;
18
19import android.content.Context;
20import android.content.res.ColorStateList;
21import android.graphics.Canvas;
22import android.graphics.Paint;
23import android.graphics.Path;
24import android.support.v4.view.ViewCompat;
25import android.util.AttributeSet;
26import android.view.LayoutInflater;
27import android.view.View;
28import android.widget.LinearLayout;
29import android.widget.TextView;
30
31import com.android.inputmethod.latin.R;
32
33public final class SetupStartIndicatorView extends LinearLayout {
34    public SetupStartIndicatorView(final Context context, final AttributeSet attrs) {
35        super(context, attrs);
36        setOrientation(HORIZONTAL);
37        LayoutInflater.from(context).inflate(R.layout.setup_start_indicator_label, this);
38
39        final LabelView labelView = (LabelView)findViewById(R.id.setup_start_label);
40        labelView.setIndicatorView(findViewById(R.id.setup_start_indicator));
41    }
42
43    public static final class LabelView extends TextView {
44        private View mIndicatorView;
45
46        public LabelView(final Context context, final AttributeSet attrs) {
47            super(context, attrs);
48        }
49
50        public void setIndicatorView(final View indicatorView) {
51            mIndicatorView = indicatorView;
52        }
53
54        // TODO: Once we stop supporting ICS, uncomment {@link #setPressed(boolean)} method and
55        // remove this method.
56        @Override
57        protected void drawableStateChanged() {
58            super.drawableStateChanged();
59            for (final int state : getDrawableState()) {
60                if (state == android.R.attr.state_pressed) {
61                    updateIndicatorView(true /* pressed */);
62                    return;
63                }
64            }
65            updateIndicatorView(false /* pressed */);
66        }
67
68        // TODO: Once we stop supporting ICS, uncomment this method and remove
69        // {@link #drawableStateChanged()} method.
70//        @Override
71//        public void setPressed(final boolean pressed) {
72//            super.setPressed(pressed);
73//            updateIndicatorView(pressed);
74//        }
75
76        private void updateIndicatorView(final boolean pressed) {
77            if (mIndicatorView != null) {
78                mIndicatorView.setPressed(pressed);
79                mIndicatorView.invalidate();
80            }
81        }
82    }
83
84    public static final class IndicatorView extends View {
85        private final Path mIndicatorPath = new Path();
86        private final Paint mIndicatorPaint = new Paint();
87        private final ColorStateList mIndicatorColor;
88
89        public IndicatorView(final Context context, final AttributeSet attrs) {
90            super(context, attrs);
91            mIndicatorColor = getResources().getColorStateList(
92                    R.color.setup_step_action_background);
93            mIndicatorPaint.setStyle(Paint.Style.FILL);
94        }
95
96        @Override
97        protected void onDraw(final Canvas canvas) {
98            super.onDraw(canvas);
99            final int layoutDirection = ViewCompat.getLayoutDirection(this);
100            final int width = getWidth();
101            final int height = getHeight();
102            final float halfHeight = height / 2.0f;
103            final Path path = mIndicatorPath;
104            path.rewind();
105            if (layoutDirection == ViewCompat.LAYOUT_DIRECTION_RTL) {
106                // Left arrow
107                path.moveTo(width, 0.0f);
108                path.lineTo(0.0f, halfHeight);
109                path.lineTo(width, height);
110            } else { // LAYOUT_DIRECTION_LTR
111                // Right arrow
112                path.moveTo(0.0f, 0.0f);
113                path.lineTo(width, halfHeight);
114                path.lineTo(0.0f, height);
115            }
116            path.close();
117            final int[] stateSet = getDrawableState();
118            final int color = mIndicatorColor.getColorForState(stateSet, 0);
119            mIndicatorPaint.setColor(color);
120            canvas.drawPath(path, mIndicatorPaint);
121        }
122    }
123}
124