Prevented server check due to orientation change in login view
[pub/Android/ownCloud.git] / src / com / owncloud / android / widgets / ActionEditText.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
4 *
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.
8 *
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.
13 *
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/>.
16 *
17 */
18
19 package com.owncloud.android.widgets;
20
21 import java.lang.reflect.InvocationTargetException;
22 import java.lang.reflect.Method;
23
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;
34
35 public class ActionEditText extends EditText {
36 private String s;
37 private String optionOneString;
38 private int optionOneColor;
39 private String optionTwoString;
40 private int optionTwoColor;
41 private Rect mTextBounds, mButtonRect;
42
43 private String badgeClickCallback;
44 private Rect btn_rect;
45
46 public ActionEditText(Context context, AttributeSet attrs) {
47 super(context, attrs);
48 getAttrs(attrs);
49 s = optionOneString;
50 mTextBounds = new Rect();
51 mButtonRect = new Rect();
52 }
53
54 public ActionEditText(Context context, AttributeSet attrs, int defStyle) {
55 super(context, attrs, defStyle);
56 getAttrs(attrs);
57 s = optionOneString;
58 mTextBounds = new Rect();
59 mButtonRect = new Rect();
60 }
61
62 @Override
63 protected void onDraw(Canvas canvas) {
64 super.onDraw(canvas);
65
66 Paint p = getPaint();
67
68 p.getTextBounds(s, 0, s.length(), mTextBounds);
69
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;
76
77 if (s.equals(optionOneString))
78 p.setColor(optionOneColor);
79 else
80 p.setColor(optionTwoColor);
81 canvas.drawRect(mButtonRect, p);
82 p.setColor(Color.GRAY);
83
84 canvas.drawText(s, mButtonRect.left + 3, mButtonRect.bottom
85 - (mTextBounds.height() / 2), p);
86
87 invalidate();
88 }
89
90 @Override
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))
98 s = optionOneString;
99 else
100 s = 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;
106 Method method;
107 try {
108
109 method = getContext().getClass().getMethod(
110 badgeClickCallback, paramtypes);
111 method.invoke(getContext(), this, s);
112
113 } catch (NoSuchMethodException e) {
114 e.printStackTrace();
115 } catch (IllegalArgumentException e) {
116 e.printStackTrace();
117 } catch (IllegalAccessException e) {
118 e.printStackTrace();
119 } catch (InvocationTargetException e) {
120 e.printStackTrace();
121 }
122
123 invalidate();
124 }
125 }
126 }
127 return r;
128 }
129
130 private void getAttrs(AttributeSet attr) {
131 TypedArray a = getContext().obtainStyledAttributes(attr,
132 R.styleable.ActionEditText);
133 optionOneString = a
134 .getString(R.styleable.ActionEditText_optionOneString);
135 optionTwoString = a
136 .getString(R.styleable.ActionEditText_optionTwoString);
137 optionOneColor = a.getColor(R.styleable.ActionEditText_optionOneColor,
138 0x00ff00);
139 optionTwoColor = a.getColor(R.styleable.ActionEditText_optionTwoColor,
140 0xff0000);
141 badgeClickCallback = a
142 .getString(R.styleable.ActionEditText_onBadgeClick);
143 }
144
145 }