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
;
25 import android
.content
.Context
;
26 import android
.content
.res
.TypedArray
;
27 import android
.graphics
.Canvas
;
28 import android
.graphics
.Color
;
29 import android
.graphics
.Paint
;
30 import android
.graphics
.Rect
;
31 import android
.util
.AttributeSet
;
32 import android
.view
.MotionEvent
;
33 import android
.widget
.EditText
;
35 public class ActionEditText
extends EditText
{
37 private String optionOneString
;
38 private int optionOneColor
;
39 private String optionTwoString
;
40 private int optionTwoColor
;
41 private Rect mTextBounds
, mButtonRect
;
43 private String badgeClickCallback
;
44 private Rect btn_rect
;
46 public ActionEditText(Context context
, AttributeSet attrs
) {
47 super(context
, attrs
);
50 mTextBounds
= new Rect();
51 mButtonRect
= new Rect();
54 public ActionEditText(Context context
, AttributeSet attrs
, int defStyle
) {
55 super(context
, attrs
, defStyle
);
58 mTextBounds
= new Rect();
59 mButtonRect
= new Rect();
63 protected void onDraw(Canvas canvas
) {
68 p
.getTextBounds(s
, 0, s
.length(), mTextBounds
);
70 getDrawingRect(mButtonRect
);
71 mButtonRect
.top
+= 10;
72 mButtonRect
.bottom
-= 10;
73 mButtonRect
.left
= (int) (getWidth() - mTextBounds
.width() - 18);
74 mButtonRect
.right
= getWidth() - 10;
75 btn_rect
= mButtonRect
;
77 if (s
.equals(optionOneString
))
78 p
.setColor(optionOneColor
);
80 p
.setColor(optionTwoColor
);
81 canvas
.drawRect(mButtonRect
, p
);
82 p
.setColor(Color
.GRAY
);
84 canvas
.drawText(s
, mButtonRect
.left
+ 3, mButtonRect
.bottom
85 - (mTextBounds
.height() / 2), p
);
91 public boolean onTouchEvent(MotionEvent event
) {
92 int touchX
= (int) event
.getX();
93 int touchY
= (int) event
.getY();
94 boolean r
= super.onTouchEvent(event
);
95 if (event
.getAction() == MotionEvent
.ACTION_UP
) {
96 if (btn_rect
.contains(touchX
, touchY
)) {
97 if (s
.equals(optionTwoString
))
101 if (badgeClickCallback
!= null
) {
102 @SuppressWarnings("rawtypes")
103 Class
[] paramtypes
= new Class
[2];
104 paramtypes
[0] = android
.view
.View
.class;
105 paramtypes
[1] = String
.class;
109 method
= getContext().getClass().getMethod(
110 badgeClickCallback
, paramtypes
);
111 method
.invoke(getContext(), this, s
);
113 } catch (NoSuchMethodException e
) {
115 } catch (IllegalArgumentException e
) {
117 } catch (IllegalAccessException e
) {
119 } catch (InvocationTargetException e
) {
130 private void getAttrs(AttributeSet attr
) {
131 TypedArray a
= getContext().obtainStyledAttributes(attr
,
132 R
.styleable
.ActionEditText
);
134 .getString(R
.styleable
.ActionEditText_optionOneString
);
136 .getString(R
.styleable
.ActionEditText_optionTwoString
);
137 optionOneColor
= a
.getColor(R
.styleable
.ActionEditText_optionOneColor
,
139 optionTwoColor
= a
.getColor(R
.styleable
.ActionEditText_optionTwoColor
,
141 badgeClickCallback
= a
142 .getString(R
.styleable
.ActionEditText_onBadgeClick
);