1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
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.
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.
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/>.
19 package com
.owncloud
.android
.widgets
;
21 import java
.lang
.reflect
.InvocationTargetException
;
22 import java
.lang
.reflect
.Method
;
24 import com
.owncloud
.android
.R
;
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
;
36 public class ActionEditText
extends EditText
{
38 private String optionOneString
;
39 private int optionOneColor
;
40 private String optionTwoString
;
41 private int optionTwoColor
;
42 private Rect mTextBounds
, mButtonRect
;
44 private String badgeClickCallback
;
45 private Rect btn_rect
;
47 public ActionEditText(Context context
, AttributeSet attrs
) {
48 super(context
, attrs
);
51 mTextBounds
= new Rect();
52 mButtonRect
= new Rect();
55 public ActionEditText(Context context
, AttributeSet attrs
, int defStyle
) {
56 super(context
, attrs
, defStyle
);
59 mTextBounds
= new Rect();
60 mButtonRect
= new Rect();
64 protected void onDraw(Canvas canvas
) {
69 p
.getTextBounds(s
, 0, s
.length(), mTextBounds
);
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
;
78 if (s
.equals(optionOneString
))
79 p
.setColor(optionOneColor
);
81 p
.setColor(optionTwoColor
);
82 canvas
.drawRect(mButtonRect
, p
);
83 p
.setColor(Color
.GRAY
);
85 canvas
.drawText(s
, mButtonRect
.left
+ 3, mButtonRect
.bottom
86 - (mTextBounds
.height() / 2), p
);
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
))
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;
110 method
= getContext().getClass().getMethod(
111 badgeClickCallback
, paramtypes
);
112 method
.invoke(getContext(), this, s
);
114 } catch (NoSuchMethodException e
) {
116 } catch (IllegalArgumentException e
) {
118 } catch (IllegalAccessException e
) {
120 } catch (InvocationTargetException e
) {
131 private void getAttrs(AttributeSet attr
) {
132 TypedArray a
= getContext().obtainStyledAttributes(attr
,
133 R
.styleable
.ActionEditText
);
135 .getString(R
.styleable
.ActionEditText_optionOneString
);
137 .getString(R
.styleable
.ActionEditText_optionTwoString
);
138 optionOneColor
= a
.getColor(R
.styleable
.ActionEditText_optionOneColor
,
140 optionTwoColor
= a
.getColor(R
.styleable
.ActionEditText_optionTwoColor
,
142 badgeClickCallback
= a
143 .getString(R
.styleable
.ActionEditText_onBadgeClick
);