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