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
.TypedArray
; 
   9 import android
.graphics
.Canvas
; 
  10 import android
.graphics
.Color
; 
  11 import android
.graphics
.Paint
; 
  12 import android
.graphics
.Rect
; 
  13 import android
.util
.AttributeSet
; 
  14 import android
.view
.MotionEvent
; 
  15 import android
.widget
.EditText
; 
  17 public class ActionEditText 
extends EditText 
{ 
  19     private String optionOneString
; 
  20     private int optionOneColor
; 
  21     private String optionTwoString
; 
  22     private int optionTwoColor
; 
  23     private Rect mTextBounds
, mButtonRect
; 
  25     private String badgeClickCallback
; 
  26     private Rect btn_rect
; 
  28     public ActionEditText(Context context
, AttributeSet attrs
) { 
  29         super(context
, attrs
); 
  32         mTextBounds 
= new Rect(); 
  33         mButtonRect 
= new Rect(); 
  36     public ActionEditText(Context context
, AttributeSet attrs
, int defStyle
) { 
  37         super(context
, attrs
, defStyle
); 
  40         mTextBounds 
= new Rect(); 
  41         mButtonRect 
= new Rect(); 
  45     protected void onDraw(Canvas canvas
) { 
  50         p
.getTextBounds(s
, 0, s
.length(), mTextBounds
); 
  52         getDrawingRect(mButtonRect
); 
  53         mButtonRect
.top 
+= 10; 
  54         mButtonRect
.bottom 
-= 10; 
  55         mButtonRect
.left 
= (int) (getWidth() - mTextBounds
.width() - 18); 
  56         mButtonRect
.right 
= getWidth() - 10; 
  57         btn_rect 
= mButtonRect
; 
  59         if (s
.equals(optionOneString
)) 
  60             p
.setColor(optionOneColor
); 
  62             p
.setColor(optionTwoColor
); 
  63         canvas
.drawRect(mButtonRect
, p
); 
  64         p
.setColor(Color
.GRAY
); 
  66         canvas
.drawText(s
, mButtonRect
.left 
+ 3, mButtonRect
.bottom
 
  67                 - (mTextBounds
.height() / 2), p
); 
  73     public boolean onTouchEvent(MotionEvent event
) { 
  74         int touchX 
= (int) event
.getX(); 
  75         int touchY 
= (int) event
.getY(); 
  76         boolean r 
= super.onTouchEvent(event
); 
  77         if (event
.getAction() == MotionEvent
.ACTION_UP
) { 
  78             if (btn_rect
.contains(touchX
, touchY
)) { 
  79                 if (s
.equals(optionTwoString
)) 
  83                 if (badgeClickCallback 
!= null
) { 
  84                     @SuppressWarnings("rawtypes") 
  85                     Class
[] paramtypes 
= new Class
[2]; 
  86                     paramtypes
[0] = android
.view
.View
.class; 
  87                     paramtypes
[1] = String
.class; 
  91                         method 
= getContext().getClass().getMethod( 
  92                                 badgeClickCallback
, paramtypes
); 
  93                         method
.invoke(getContext(), this, s
); 
  95                     } catch (NoSuchMethodException e
) { 
  97                     } catch (IllegalArgumentException e
) { 
  99                     } catch (IllegalAccessException e
) { 
 101                     } catch (InvocationTargetException e
) { 
 112     private void getAttrs(AttributeSet attr
) { 
 113         TypedArray a 
= getContext().obtainStyledAttributes(attr
, 
 114                 R
.styleable
.ActionEditText
); 
 116                 .getString(R
.styleable
.ActionEditText_optionOneString
); 
 118                 .getString(R
.styleable
.ActionEditText_optionTwoString
); 
 119         optionOneColor 
= a
.getColor(R
.styleable
.ActionEditText_optionOneColor
, 
 121         optionTwoColor 
= a
.getColor(R
.styleable
.ActionEditText_optionTwoColor
, 
 123         badgeClickCallback 
= a
 
 124                 .getString(R
.styleable
.ActionEditText_onBadgeClick
);