More robust instant photo uploads
[pub/Android/ownCloud.git] / src / com / owncloud / android / widgets / ActionEditText.java
1 package com.owncloud.android.widgets;
2
3 import java.lang.reflect.InvocationTargetException;
4 import java.lang.reflect.Method;
5
6 import com.owncloud.android.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;
16
17 public class ActionEditText extends EditText {
18 private String s;
19 private String optionOneString;
20 private int optionOneColor;
21 private String optionTwoString;
22 private int optionTwoColor;
23 private Rect mTextBounds, mButtonRect;
24
25 private String badgeClickCallback;
26 private Rect btn_rect;
27
28 public ActionEditText(Context context, AttributeSet attrs) {
29 super(context, attrs);
30 getAttrs(attrs);
31 s = optionOneString;
32 mTextBounds = new Rect();
33 mButtonRect = new Rect();
34 }
35
36 public ActionEditText(Context context, AttributeSet attrs, int defStyle) {
37 super(context, attrs, defStyle);
38 getAttrs(attrs);
39 s = optionOneString;
40 mTextBounds = new Rect();
41 mButtonRect = new Rect();
42 }
43
44 @Override
45 protected void onDraw(Canvas canvas) {
46 super.onDraw(canvas);
47
48 Paint p = getPaint();
49
50 p.getTextBounds(s, 0, s.length(), mTextBounds);
51
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;
58
59 if (s.equals(optionOneString))
60 p.setColor(optionOneColor);
61 else
62 p.setColor(optionTwoColor);
63 canvas.drawRect(mButtonRect, p);
64 p.setColor(Color.GRAY);
65
66 canvas.drawText(s, mButtonRect.left + 3, mButtonRect.bottom
67 - (mTextBounds.height() / 2), p);
68
69 invalidate();
70 }
71
72 @Override
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))
80 s = optionOneString;
81 else
82 s = 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;
88 Method method;
89 try {
90
91 method = getContext().getClass().getMethod(
92 badgeClickCallback, paramtypes);
93 method.invoke(getContext(), this, s);
94
95 } catch (NoSuchMethodException e) {
96 e.printStackTrace();
97 } catch (IllegalArgumentException e) {
98 e.printStackTrace();
99 } catch (IllegalAccessException e) {
100 e.printStackTrace();
101 } catch (InvocationTargetException e) {
102 e.printStackTrace();
103 }
104
105 invalidate();
106 }
107 }
108 }
109 return r;
110 }
111
112 private void getAttrs(AttributeSet attr) {
113 TypedArray a = getContext().obtainStyledAttributes(attr,
114 R.styleable.ActionEditText);
115 optionOneString = a
116 .getString(R.styleable.ActionEditText_optionOneString);
117 optionTwoString = a
118 .getString(R.styleable.ActionEditText_optionTwoString);
119 optionOneColor = a.getColor(R.styleable.ActionEditText_optionOneColor,
120 0x00ff00);
121 optionTwoColor = a.getColor(R.styleable.ActionEditText_optionTwoColor,
122 0xff0000);
123 badgeClickCallback = a
124 .getString(R.styleable.ActionEditText_onBadgeClick);
125 }
126
127 }