1 package eu
.alefzero
.owncloud
.widgets
;
3 import java
.lang
.reflect
.InvocationTargetException
;
4 import java
.lang
.reflect
.Method
;
6 import eu
.alefzero
.owncloud
.R
;
7 import android
.content
.Context
;
8 import android
.content
.res
.Resources
;
9 import android
.content
.res
.TypedArray
;
10 import android
.graphics
.Canvas
;
11 import android
.graphics
.Color
;
12 import android
.graphics
.Paint
;
13 import android
.graphics
.Rect
;
14 import android
.text
.InputType
;
15 import android
.util
.AttributeSet
;
16 import android
.util
.Log
;
17 import android
.view
.MotionEvent
;
18 import android
.view
.View
;
19 import android
.widget
.CheckBox
;
20 import android
.widget
.EditText
;
21 import android
.widget
.TextView
;
23 public class ActionEditText
extends EditText
{
25 private String optionOneString
;
26 private int optionOneColor
;
27 private String optionTwoString
;
28 private int optionTwoColor
;
30 private String badgeClickCallback
;
31 private Rect btn_rect
;
33 public ActionEditText(Context context
, AttributeSet attrs
) {
34 super(context
, attrs
);
39 public ActionEditText(Context context
, AttributeSet attrs
, int defStyle
) {
40 super(context
, attrs
, defStyle
);
46 protected void onDraw(Canvas canvas
) {
51 Rect text_bounds
= new Rect();
53 p
.getTextBounds(s
, 0, s
.length(), text_bounds
);
58 r
.left
= (int)(getWidth() - text_bounds
.width() - 18);
59 r
.right
= getWidth() - 10;
62 if (s
.equals(optionOneString
))
63 p
.setColor(optionOneColor
);
65 p
.setColor(optionTwoColor
);
66 canvas
.drawRect(r
, p
);
67 p
.setColor(Color
.GRAY
);
69 canvas
.drawText(s
, r
.left
+ 3, r
.bottom
- (text_bounds
.height()/2), p
);
75 public boolean onTouchEvent(MotionEvent event
) {
76 int touchX
= (int) event
.getX();
77 int touchY
= (int) event
.getY();
78 boolean r
= super.onTouchEvent(event
);
79 if (event
.getAction() == MotionEvent
.ACTION_UP
) {
80 if (btn_rect
.contains(touchX
, touchY
)) {
81 if (s
.equals(optionTwoString
)) s
= optionOneString
;
82 else s
= optionTwoString
;
83 if (badgeClickCallback
!= null
) {
84 Class
[] paramtypes
= new Class
[2];
85 paramtypes
[0] = android
.view
.View
.class;
86 paramtypes
[1] = String
.class;
89 method
= this.getClass().getMethod(badgeClickCallback
, paramtypes
);
90 method
.invoke(this, this, s
);
91 } catch (NoSuchMethodException e
) {
93 } catch (IllegalArgumentException e
) {
94 // TODO Auto-generated catch block
96 } catch (IllegalAccessException e
) {
97 // TODO Auto-generated catch block
99 } catch (InvocationTargetException e
) {
100 // TODO Auto-generated catch block
111 private void getAttrs(AttributeSet attr
) {
112 TypedArray a
= getContext().obtainStyledAttributes(attr
, R
.styleable
.ActionEditText
);
113 optionOneString
= a
.getString(R
.styleable
.ActionEditText_optionOneString
);
114 optionTwoString
= a
.getString(R
.styleable
.ActionEditText_optionTwoString
);
115 optionOneColor
= a
.getColor(R
.styleable
.ActionEditText_optionOneColor
, 0x00ff00);
116 optionTwoColor
= a
.getColor(R
.styleable
.ActionEditText_optionTwoColor
, 0xff0000);
117 badgeClickCallback
= a
.getString(R
.styleable
.ActionEditText_onBadgeClick
);
120 public void sslBadgeClick(View view
, String val
) {
124 public void passwordBadgeClick(View view
, String val
) {
126 if(val
.equals("Hide")) {
127 ((TextView
)view
).setInputType(InputType
.TYPE_CLASS_TEXT
| InputType
.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD
);
129 ((TextView
)view
).setInputType(InputType
.TYPE_CLASS_TEXT
| InputType
.TYPE_TEXT_VARIATION_PASSWORD
);