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 
17 package com.android.inputmethod.latin.setup;
18 
19 import android.content.Context;
20 import android.content.res.ColorStateList;
21 import android.graphics.Canvas;
22 import android.graphics.Paint;
23 import android.graphics.Path;
24 import android.support.v4.view.ViewCompat;
25 import android.util.AttributeSet;
26 import android.view.LayoutInflater;
27 import android.view.View;
28 import android.widget.LinearLayout;
29 import android.widget.TextView;
30 
31 import com.android.inputmethod.latin.R;
32 
33 public final class SetupStartIndicatorView extends LinearLayout {
SetupStartIndicatorView(final Context context, final AttributeSet attrs)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 
LabelView(final Context context, final AttributeSet attrs)46         public LabelView(final Context context, final AttributeSet attrs) {
47             super(context, attrs);
48         }
49 
setIndicatorView(final View indicatorView)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
drawableStateChanged()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 
updateIndicatorView(final boolean pressed)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 
IndicatorView(final Context context, final AttributeSet attrs)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
onDraw(final Canvas canvas)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