Update year in license
[pub/Android/ownCloud.git] / src / com / owncloud / android / widgets / ActionEditText.java
1 /**
2 * ownCloud Android client application
3 *
4 * Copyright (C) 2012 Bartek Przybylski
5 * Copyright (C) 2015 ownCloud Inc.
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2,
9 * as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
21 package com.owncloud.android.widgets;
22
23 import java.lang.reflect.InvocationTargetException;
24 import java.lang.reflect.Method;
25
26 import com.owncloud.android.R;
27
28 import android.content.Context;
29 import android.content.res.TypedArray;
30 import android.graphics.Canvas;
31 import android.graphics.Color;
32 import android.graphics.Paint;
33 import android.graphics.Rect;
34 import android.util.AttributeSet;
35 import android.view.MotionEvent;
36 import android.widget.EditText;
37
38 public class ActionEditText extends EditText {
39 private String s;
40 private String optionOneString;
41 private int optionOneColor;
42 private String optionTwoString;
43 private int optionTwoColor;
44 private Rect mTextBounds, mButtonRect;
45
46 private String badgeClickCallback;
47 private Rect btn_rect;
48
49 public ActionEditText(Context context, AttributeSet attrs) {
50 super(context, attrs);
51 getAttrs(attrs);
52 s = optionOneString;
53 mTextBounds = new Rect();
54 mButtonRect = new Rect();
55 }
56
57 public ActionEditText(Context context, AttributeSet attrs, int defStyle) {
58 super(context, attrs, defStyle);
59 getAttrs(attrs);
60 s = optionOneString;
61 mTextBounds = new Rect();
62 mButtonRect = new Rect();
63 }
64
65 @Override
66 protected void onDraw(Canvas canvas) {
67 super.onDraw(canvas);
68
69 Paint p = getPaint();
70
71 p.getTextBounds(s, 0, s.length(), mTextBounds);
72
73 getDrawingRect(mButtonRect);
74 mButtonRect.top += 10;
75 mButtonRect.bottom -= 10;
76 mButtonRect.left = (int) (getWidth() - mTextBounds.width() - 18);
77 mButtonRect.right = getWidth() - 10;
78 btn_rect = mButtonRect;
79
80 if (s.equals(optionOneString))
81 p.setColor(optionOneColor);
82 else
83 p.setColor(optionTwoColor);
84 canvas.drawRect(mButtonRect, p);
85 p.setColor(Color.GRAY);
86
87 canvas.drawText(s, mButtonRect.left + 3, mButtonRect.bottom
88 - (mTextBounds.height() / 2), p);
89
90 invalidate();
91 }
92
93 @Override
94 public boolean onTouchEvent(MotionEvent event) {
95 int touchX = (int) event.getX();
96 int touchY = (int) event.getY();
97 boolean r = super.onTouchEvent(event);
98 if (event.getAction() == MotionEvent.ACTION_UP) {
99 if (btn_rect.contains(touchX, touchY)) {
100 if (s.equals(optionTwoString))
101 s = optionOneString;
102 else
103 s = optionTwoString;
104 if (badgeClickCallback != null) {
105 @SuppressWarnings("rawtypes")
106 Class[] paramtypes = new Class[2];
107 paramtypes[0] = android.view.View.class;
108 paramtypes[1] = String.class;
109 Method method;
110 try {
111
112 method = getContext().getClass().getMethod(
113 badgeClickCallback, paramtypes);
114 method.invoke(getContext(), this, s);
115
116 } catch (NoSuchMethodException e) {
117 e.printStackTrace();
118 } catch (IllegalArgumentException e) {
119 e.printStackTrace();
120 } catch (IllegalAccessException e) {
121 e.printStackTrace();
122 } catch (InvocationTargetException e) {
123 e.printStackTrace();
124 }
125
126 invalidate();
127 }
128 }
129 }
130 return r;
131 }
132
133 private void getAttrs(AttributeSet attr) {
134 TypedArray a = getContext().obtainStyledAttributes(attr,
135 R.styleable.ActionEditText);
136 optionOneString = a
137 .getString(R.styleable.ActionEditText_optionOneString);
138 optionTwoString = a
139 .getString(R.styleable.ActionEditText_optionTwoString);
140 optionOneColor = a.getColor(R.styleable.ActionEditText_optionOneColor,
141 0x00ff00);
142 optionTwoColor = a.getColor(R.styleable.ActionEditText_optionTwoColor,
143 0xff0000);
144 badgeClickCallback = a
145 .getString(R.styleable.ActionEditText_onBadgeClick);
146 }
147
148 }