2 * ownCloud Android client application
4 * Copyright (C) 2012 Bartek Przybylski
5 * Copyright (C) 2015 ownCloud Inc.
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.
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.
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/>.
21 package com
.owncloud
.android
.widgets
;
23 import java
.lang
.reflect
.InvocationTargetException
;
24 import java
.lang
.reflect
.Method
;
26 import com
.owncloud
.android
.R
;
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
;
38 public class ActionEditText
extends EditText
{
40 private String optionOneString
;
41 private int optionOneColor
;
42 private String optionTwoString
;
43 private int optionTwoColor
;
44 private Rect mTextBounds
, mButtonRect
;
46 private String badgeClickCallback
;
47 private Rect btn_rect
;
49 public ActionEditText(Context context
, AttributeSet attrs
) {
50 super(context
, attrs
);
53 mTextBounds
= new Rect();
54 mButtonRect
= new Rect();
57 public ActionEditText(Context context
, AttributeSet attrs
, int defStyle
) {
58 super(context
, attrs
, defStyle
);
61 mTextBounds
= new Rect();
62 mButtonRect
= new Rect();
66 protected void onDraw(Canvas canvas
) {
71 p
.getTextBounds(s
, 0, s
.length(), mTextBounds
);
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
;
80 if (s
.equals(optionOneString
))
81 p
.setColor(optionOneColor
);
83 p
.setColor(optionTwoColor
);
84 canvas
.drawRect(mButtonRect
, p
);
85 p
.setColor(Color
.GRAY
);
87 canvas
.drawText(s
, mButtonRect
.left
+ 3, mButtonRect
.bottom
88 - (mTextBounds
.height() / 2), p
);
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
))
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;
112 method
= getContext().getClass().getMethod(
113 badgeClickCallback
, paramtypes
);
114 method
.invoke(getContext(), this, s
);
116 } catch (NoSuchMethodException e
) {
118 } catch (IllegalArgumentException e
) {
120 } catch (IllegalAccessException e
) {
122 } catch (InvocationTargetException e
) {
133 private void getAttrs(AttributeSet attr
) {
134 TypedArray a
= getContext().obtainStyledAttributes(attr
,
135 R
.styleable
.ActionEditText
);
137 .getString(R
.styleable
.ActionEditText_optionOneString
);
139 .getString(R
.styleable
.ActionEditText_optionTwoString
);
140 optionOneColor
= a
.getColor(R
.styleable
.ActionEditText_optionOneColor
,
142 optionTwoColor
= a
.getColor(R
.styleable
.ActionEditText_optionTwoColor
,
144 badgeClickCallback
= a
145 .getString(R
.styleable
.ActionEditText_onBadgeClick
);