OSDN Git Service

T29234
[everfolder/source.git] / source / workspace / EverFolder / src / com / yuji / ef / IconFrameLayout.java
1 package com.yuji.ef;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import android.content.Context;
7 import android.graphics.Color;
8 import android.graphics.PorterDuff.Mode;
9 import android.graphics.Rect;
10 import android.util.AttributeSet;
11 import android.view.MotionEvent;
12 import android.view.View;
13 import android.view.View.OnLongClickListener;
14 import android.view.animation.Animation;
15 import android.view.animation.Animation.AnimationListener;
16 import android.view.animation.AnimationUtils;
17 import android.widget.FrameLayout;
18
19 public class IconFrameLayout extends FrameLayout implements OnLongClickListener {
20         private EverFolderActivity activity = null;
21
22         private LabelIconView target = null;
23         private LabelIconView destTarget = null;
24         private LabelIconView selectedTarget = null;
25         private LabelIconView cutTarget = null;
26         private List<LabelIconView> labelIconViewList = new ArrayList<LabelIconView>();
27         
28         private Animation anime;
29         private int currentX;
30         private int currentY;
31         private int offsetX;
32         private int offsetY;
33         private boolean longClickFlg = false;
34         private IconScrollView scrollView;
35         private Context context = this.getContext();
36
37         public IconFrameLayout(Context context) {
38                 super(context);
39         }
40
41         public IconFrameLayout(Context context, AttributeSet attrs) {
42                 super(context, attrs);
43         }
44
45         public IconFrameLayout(Context context, AttributeSet attrs, int defStyle) {
46                 super(context, attrs, defStyle);
47         }
48
49         public void setActivity(EverFolderActivity activity) {
50                 this.activity = activity;
51         }
52         
53         @Override
54         protected void onLayout(boolean changed, int left, int top, int right,
55                         int bottom) {
56                 super.onLayout(changed, left, top, right, bottom);
57
58                 int size = this.getChildCount();
59                 for (int i = 0; i < size; i++) {
60                         LabelIconView vv = (LabelIconView) this.getChildAt(i);
61                         int x2 = vv.getInitX();
62                         int y2 = vv.getInitY();
63                         // vv.init(x2, y2);
64                         vv.layout(x2, y2, x2 + vv.getWidth(), y2 + vv.getHeight());
65                 }
66         }
67
68         public LabelIconView getSelectedTarget() {
69                 return selectedTarget;
70         }
71         
72         public void addView(LabelIconView child) {
73                 super.addView(child);
74                 labelIconViewList.add(child);
75         }
76
77         public void removeAllViews(){
78                 super.removeAllViews();
79                 labelIconViewList.clear();
80         }
81         
82         public void moveTop(LabelIconView child){
83                 this.removeView(child);
84                 this.addView(child);
85         }
86         
87         private LabelIconView getView(int x, int y, LabelIconView v, List<LabelIconView> list, boolean flag) {
88                 Rect rect = new Rect();
89
90                 if (v != null) {
91                         if (flag){
92                                 v.getHitRect(rect);             
93                                 if (rect.contains(x, y)) {
94                                         return v;
95                                 }
96                         }
97                 }
98                 for (LabelIconView view : list) {
99                         if (view != v) {
100                                 view.getHitRect(rect);          
101                                 if (rect.contains(x, y)) {
102                                         
103                                         return view;
104                                 }
105                         }
106                 }
107                 return null;
108         }
109
110         public void setScrollView(final IconScrollView scrollView) {
111                 this.scrollView = scrollView;
112                 this.setOnLongClickListener(this);
113                 //this.setLongClickable(true);
114                 
115                 anime = AnimationUtils.loadAnimation(context, R.anim.sample);
116                 anime.setAnimationListener(new AnimationListener() {
117                         public void onAnimationStart(Animation animation) {
118                         }
119
120                         public void onAnimationRepeat(Animation animation) {
121                         }
122
123                         public void onAnimationEnd(Animation animation) {
124                                 // if (isBt2Click) {
125                                 // bt2.performClick();
126                                 // isBt2Click = false;
127                                 // }
128                                 target = null;
129                                 destTarget = null;
130                                 
131                                 scrollView.setScrollable(true);
132                                 scrollView.requestDisallowInterceptTouchEvent(false);
133                                 scrollView.invalidate();
134                         }
135                 });
136         }
137         
138         public void refresh(){
139                 if (selectedTarget != null){
140                         long id = selectedTarget.getNodeId();
141                         for (LabelIconView view : labelIconViewList){
142                                 if (view.getNodeId() == id){
143                                         selectedTarget = view;
144                                         target = view;
145                                         selectedTarget.setAlpha(255);
146                                         selectedTarget.setColorFilter(Color.RED, Mode.LIGHTEN);
147                                 }
148                         }
149                 }
150                 if (cutTarget != null){
151                         long id = cutTarget.getNodeId();
152                         for (LabelIconView view : labelIconViewList){
153                                 if (view.getNodeId() == id){
154                                         cutTarget = view;
155                                         cutTarget.clearColorFilter();
156                                         cutTarget.setAlpha(64);
157                                 }
158                         }
159                 }               
160         }
161         
162         public LabelIconView getCutTarget(){
163                 return cutTarget;
164         }
165         
166         public void setCutTarget(LabelIconView cutTarget){
167                 this.cutTarget = cutTarget;
168                 selectedTarget = null;
169                 target = null;
170         }
171         
172         @Override
173         public boolean onTouchEvent(MotionEvent event) {
174                 try {
175                         int x = (int) event.getRawX();
176                         int y = (int) event.getRawY();
177
178                         int dx = scrollView.getLeft();
179                         int dy = scrollView.getTop();
180
181                         x -= dx;
182                         y -= dy;
183                         
184                         int sx = x + scrollView.getScrollX();
185                         int sy = y + scrollView.getScrollY();
186                         LabelIconView v = getView(sx, sy, target, labelIconViewList, true);
187                         LabelIconView obj = (LabelIconView) v;
188
189                         //int size = labelIconViewList.size();
190                         if (target == null) {
191                                 target = obj;
192                         }
193                         if (target == null){
194                                 return super.onTouchEvent(event);                               
195                         }
196
197                         this.getParent().requestDisallowInterceptTouchEvent(true);                      
198                         
199                         if (event.getAction() == MotionEvent.ACTION_MOVE) {
200                                 if (!longClickFlg) {
201                                         return super.onTouchEvent(event);
202                                 }
203                                 if (target instanceof StatusIconView){
204                                         return super.onTouchEvent(event);                                       
205                                 }
206                                 obj = target;
207                                 
208                                 int sh = scrollView.getHeight();
209                                 int lh = this.getHeight();
210                                 int maxY = lh - sh;
211                                 int py = scrollView.getScrollY();
212                                 int sdy = 10;
213                                 int N = 100;
214                                 if (y < N){
215                                         if (py > 0){
216                                                 if (py < sdy){
217                                                         sdy = py;
218                                                 }
219                                                 scrollView.smoothScrollTo(0, py - sdy);
220                                                 currentY -= sdy;
221                                         }
222                                 }
223                                 else if (sh - y < N){
224                                         if (py < maxY){
225                                                 if (maxY - py < sdy){
226                                                         sdy = maxY - py;
227                                                 }
228                                                 scrollView.smoothScrollTo(0, py + sdy);
229                                                 currentY += sdy;
230                                         }
231                                 }
232
233                                 int diffX = offsetX - x;
234                                 int diffY = offsetY - y;
235
236                                 currentX -= diffX;
237                                 currentY -= diffY;
238                                 // currentX = x;;
239                                 // currentY = y;
240                                 obj.layout(currentX, currentY, currentX + obj.getWidth(),
241                                                 currentY + obj.getHeight());
242
243                                 offsetX = x;
244                                 offsetY = y;
245
246                                 v = getView(sx, sy, target, labelIconViewList, false);
247                                 if (destTarget == null) {
248                                         if (v != null){
249                                                 destTarget = v;
250                                                 destTarget.setAlpha(128);
251                                         }
252                                 } else {
253                                         if (v != destTarget){
254                                                 destTarget.setAlpha(255);
255                                                 destTarget = v;
256                                                 if (destTarget != null){
257                                                         destTarget.setAlpha(128);
258                                                 }
259                                         }
260                                 }
261                         } else if (event.getAction() == MotionEvent.ACTION_DOWN) {
262                                 //if (obj == target){
263                                 if (obj != null){
264                                         if (obj != target){
265                                                 target.clearColorFilter();
266                                                 target = obj;
267                                         }
268                                         if (target instanceof StatusIconView){
269                                                 StatusIconView siv = (StatusIconView)target;
270                                                 LabelIconView rv = siv.getRoot();
271                                                 long id = rv.getNodeId();
272
273                                                 activity.execute(id);
274                                                 return super.onTouchEvent(event);                                       
275                                         }
276                                         
277                                         if (selectedTarget != null){
278                                                 long id = selectedTarget.getNodeId();
279
280                                                 target.clearColorFilter();
281                                                 target = null;
282                                                 selectedTarget = target;
283                                                 activity.targetSelectedChanged(true);
284                                                 
285                                                 activity.executeView(id);
286
287                                                 return super.onTouchEvent(event);                                       
288                                         }
289                                         offsetX = x;
290                                         offsetY = y;
291                                         // TODO
292                                         currentX = obj.getLeft();
293                                         currentY = obj.getTop();
294         
295                                         target.setAlpha(255);
296                                         target.setColorFilter(Color.RED, Mode.LIGHTEN);
297                                         selectedTarget = target;
298                                         activity.targetSelectedChanged(true);
299                                         
300                                         scrollView.setScrollable(false);
301                                 }
302                                 else {
303                                         target.clearColorFilter();
304                                         target = null;
305                                         selectedTarget = target;
306                                         activity.targetSelectedChanged(false);
307                                 }
308                                 // return false;
309                         } else if (event.getAction() == MotionEvent.ACTION_UP) {
310                                 if (!longClickFlg) {
311                                         return super.onTouchEvent(event);
312                                 }
313                                 if (target instanceof StatusIconView){
314                                         return super.onTouchEvent(event);                                       
315                                 }
316                                 obj = target;
317
318                                 // obj.setVisibility(View.GONE);
319                                 if (destTarget == null || destTarget instanceof StatusIconView) {
320                                         obj.setAnimation(anime);
321                                         obj.startAnimation(anime);
322                                         // layout.removeView(obj);
323
324                                         int srcX = target.getInitX();
325                                         int srcY = target.getInitY();
326                                         target.layout(srcX, srcY, srcX + target.getWidth(), srcY
327                                                         + target.getHeight());
328                                         target.setAlpha(255);
329                                 } else {
330                                         long src = target.getNodeId();
331                                         long dst = destTarget.getNodeId();
332                                         activity.execute(src, dst);
333                                         
334                                         // TODO
335                                         target = null;
336                                         destTarget = null;
337
338                                         // T29171
339                                         selectedTarget = null;
340                                         
341                                         scrollView.setScrollable(true);
342                                         scrollView.requestDisallowInterceptTouchEvent(false);
343                                         scrollView.invalidate();
344                                 }
345
346                                 longClickFlg = false;
347                         }
348                         
349                         return super.onTouchEvent(event);
350                 } catch (Exception e) {
351                         e.printStackTrace();
352                         return false;
353                 }
354         }
355
356         @Override
357         public boolean onLongClick(View view) {
358                 if (target == null) {
359                         return false;
360                 }
361                 if (target instanceof StatusIconView){
362                         return false;
363                 }
364                 
365                 LabelIconView v = (LabelIconView) target;
366                 v.setAlpha(128);
367                 v.clearColorFilter();
368
369                 moveTop(target);
370                 
371                 longClickFlg = true;
372                 return true;
373         }
374 }