OSDN Git Service

T29283
[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         private boolean elabledTouchEvent = true;
37
38         public IconFrameLayout(Context context) {
39                 super(context);
40         }
41
42         public IconFrameLayout(Context context, AttributeSet attrs) {
43                 super(context, attrs);
44         }
45
46         public IconFrameLayout(Context context, AttributeSet attrs, int defStyle) {
47                 super(context, attrs, defStyle);
48         }
49
50         public void setActivity(EverFolderActivity activity) {
51                 this.activity = activity;
52         }
53
54         @Override
55         protected void onLayout(boolean changed, int left, int top, int right,
56                         int bottom) {
57                 super.onLayout(changed, left, top, right, bottom);
58
59                 int size = this.getChildCount();
60                 for (int i = 0; i < size; i++) {
61                         LabelIconView vv = (LabelIconView) this.getChildAt(i);
62                         int x2 = vv.getInitX();
63                         int y2 = vv.getInitY();
64                         // vv.init(x2, y2);
65                         vv.layout(x2, y2, x2 + vv.getWidth(), y2 + vv.getHeight());
66                 }
67         }
68
69         public LabelIconView getSelectedTarget() {
70                 return selectedTarget;
71         }
72
73         public void addView(LabelIconView child) {
74                 addView(child, true);
75         }
76
77         public void addView(LabelIconView child, boolean isAddView) {
78                 if (isAddView) {
79                         super.addView(child);
80                 }
81                 labelIconViewList.add(child);
82         }
83
84         public void removeAllViews() {
85                 super.removeAllViews();
86                 labelIconViewList.clear();
87         }
88
89         public void moveTop(LabelIconView child) {
90                 this.removeView(child);
91                 this.addView(child);
92         }
93
94         private LabelIconView getView(int x, int y, LabelIconView v,
95                         List<LabelIconView> list, boolean flag) {
96                 Rect rect = new Rect();
97                 LabelIconView sv;
98
99                 if (v != null) {
100                         if (flag) {
101                                 v.getHitRect(rect);
102                                 if (rect.contains(x, y)) {
103                                         return v;
104                                 }
105                         }
106                 }
107                 for (LabelIconView view : list) {
108                         if (view != v) {
109                                 view.getHitRect(rect);
110                                 if (rect.contains(x, y)) {
111                                         return view;
112                                 }
113                         }
114                 }
115                 return null;
116         }
117
118         public void setScrollView(final IconScrollView scrollView) {
119                 this.scrollView = scrollView;
120                 this.setOnLongClickListener(this);
121                 // this.setLongClickable(true);
122
123                 anime = AnimationUtils.loadAnimation(context, R.anim.sample);
124                 anime.setAnimationListener(new AnimationListener() {
125                         public void onAnimationStart(Animation animation) {
126                         }
127
128                         public void onAnimationRepeat(Animation animation) {
129                         }
130
131                         public void onAnimationEnd(Animation animation) {
132                                 // if (isBt2Click) {
133                                 // bt2.performClick();
134                                 // isBt2Click = false;
135                                 // }
136                                 target = null;
137                                 destTarget = null;
138
139                                 scrollView.setScrollable(true);
140                                 scrollView.requestDisallowInterceptTouchEvent(false);
141                                 scrollView.invalidate();
142                         }
143                 });
144         }
145
146         public void refresh() {
147                 if (selectedTarget != null) {
148                         long id = selectedTarget.getNodeId();
149                         for (LabelIconView view : labelIconViewList) {
150                                 if (view.getNodeId() == id) {
151                                         selectedTarget = view;
152                                         target = view;
153                                         setSelected(selectedTarget);
154
155                                 }
156                         }
157                 }
158                 if (cutTarget != null) {
159                         long id = cutTarget.getNodeId();
160                         for (LabelIconView view : labelIconViewList) {
161                                 if (view.getNodeId() == id) {
162                                         cutTarget = view;
163                                         if (selectedTarget != cutTarget) {
164                                                 clearColorFilter(cutTarget);
165                                         }
166                                 }
167                         }
168                 }
169         }
170
171         public void clearTarget() {
172                 if (selectedTarget != null) {
173                         selectedTarget.clearColorFilter();
174                         selectedTarget = null;
175                 }
176         }
177
178         public LabelIconView getCutTarget() {
179                 return cutTarget;
180         }
181
182         public void setCutTarget(LabelIconView cutTarget) {
183                 setCutTarget(cutTarget, true);
184         }
185
186         public void setCutTarget(LabelIconView cutTarget, boolean isUnselected) {
187                 this.cutTarget = cutTarget;
188                 if (isUnselected) {
189                         selectedTarget = null;
190                         target = null;
191                 }
192         }
193
194         private void setSelected(LabelIconView t) {
195                 t.setAlpha(255);
196                 t.setColorFilter(0x88ff0000, Mode.LIGHTEN);
197         }
198
199         private void setAlpha(LabelIconView v) {
200                 v.setAlpha(255);
201         }
202
203         private void setGrayAlpha(LabelIconView v) {
204                 v.setAlpha(128);
205         }
206
207         private void clearColorFilter(LabelIconView v) {
208                 v.clearColorFilter();
209                 if (v == cutTarget) {
210                         setGrayAlpha(v);
211                 }
212         }
213
214         @Override
215         public boolean onTouchEvent(MotionEvent event) {
216                 try {
217                         if (!isElabledTouchEvent()) {
218                                 return super.onTouchEvent(event);
219                         }
220
221                         int x = (int) event.getRawX();
222                         int y = (int) event.getRawY();
223
224                         int dx = scrollView.getLeft();
225                         int dy = scrollView.getTop();
226
227                         x -= dx;
228                         y -= dy;
229
230                         int sx = x + scrollView.getScrollX();
231                         int sy = y + scrollView.getScrollY();
232                         LabelIconView v = getView(sx, sy, target, labelIconViewList, true);
233                         LabelIconView obj = (LabelIconView) v;
234
235                         // int size = labelIconViewList.size();
236                         if (target == null) {
237                                 target = obj;
238                         }
239                         if (target == null) {
240                                 return super.onTouchEvent(event);
241                         }
242
243                         // コメントにすると何が起きるか?
244                         // scrollView.requestDisallowInterceptTouchEvent(true);
245
246                         if (event.getAction() == MotionEvent.ACTION_MOVE) {
247                                 if (!longClickFlg) {
248                                         return super.onTouchEvent(event);
249                                 }
250                                 if (target instanceof StatusIconView) {
251                                         return super.onTouchEvent(event);
252                                 }
253                                 obj = target;
254
255                                 int sh = scrollView.getHeight();
256                                 int lh = this.getHeight();
257                                 int maxY = lh - sh;
258                                 int py = scrollView.getScrollY();
259                                 int sdy = 10;
260                                 int N = 100;
261                                 if (y < N) {
262                                         if (py > 0) {
263                                                 if (py < sdy) {
264                                                         sdy = py;
265                                                 }
266                                                 scrollView.smoothScrollTo(0, py - sdy);
267                                                 currentY -= sdy;
268                                         }
269                                 } else if (sh - y < N) {
270                                         if (py < maxY) {
271                                                 if (maxY - py < sdy) {
272                                                         sdy = maxY - py;
273                                                 }
274                                                 scrollView.smoothScrollTo(0, py + sdy);
275                                                 currentY += sdy;
276                                         }
277                                 }
278
279                                 int diffX = offsetX - x;
280                                 int diffY = offsetY - y;
281
282                                 currentX -= diffX;
283                                 currentY -= diffY;
284                                 obj.layout(currentX, currentY, currentX + obj.getWidth(),
285                                                 currentY + obj.getHeight());
286
287                                 offsetX = x;
288                                 offsetY = y;
289
290                                 v = getView(sx, sy, target, labelIconViewList, false);
291                                 if (destTarget == null) {
292                                         if (v != null) {
293                                                 destTarget = v;
294                                                 setGrayAlpha(destTarget);
295                                         }
296                                 } else {
297                                         if (v != destTarget) {
298                                                 setAlpha(destTarget);
299                                                 destTarget = v;
300                                                 if (destTarget != null) {
301                                                         setGrayAlpha(destTarget);
302                                                 }
303                                         }
304                                 }
305                         } else if (event.getAction() == MotionEvent.ACTION_DOWN) {
306                                 // if (obj == target){
307                                 if (obj != null) {
308                                         if (obj != target) {
309                                                 clearColorFilter(target);
310                                                 target = obj;
311                                         }
312                                         // if (target instanceof StatusIconView){
313                                         if (target.getSelectedView(sx - target.getLeft(), sy
314                                                         - target.getTop()) instanceof StatusIconView) {
315                                                 // StatusIconView siv = (StatusIconView) target;
316                                                 // LabelIconView rv = siv.getRoot();
317                                                 // long id = rv.getNodeId();
318                                                 long id = target.getNodeId();
319
320                                                 activity.execute(id);
321                                                 return super.onTouchEvent(event);
322                                                 // return true;
323                                         }
324
325                                         if (selectedTarget != null && selectedTarget == target) {
326                                                 long id = selectedTarget.getNodeId();
327
328                                                 clearColorFilter(target);
329                                                 target = null;
330                                                 selectedTarget = target;
331                                                 activity.targetSelectedChanged(true);
332
333                                                 activity.executeView(id);
334
335                                                 return super.onTouchEvent(event);
336                                         }
337
338                                         // ここに移動
339                                         scrollView.requestDisallowInterceptTouchEvent(true);
340
341                                         offsetX = x;
342                                         offsetY = y;
343                                         // TODO
344                                         currentX = obj.getLeft();
345                                         currentY = obj.getTop();
346
347                                         setSelected(target);
348                                         selectedTarget = target;
349                                         activity.targetSelectedChanged(true);
350
351                                         // コメントにしたが、デグレードするかも
352                                         // scrollView.setScrollable(false);
353                                 } else {
354                                         clearColorFilter(target);
355                                         target = null;
356                                         selectedTarget = target;
357                                         activity.targetSelectedChanged(false);
358                                 }
359                                 // return false;
360                         } else if (event.getAction() == MotionEvent.ACTION_UP) {
361                                 if (!longClickFlg) {
362                                         return super.onTouchEvent(event);
363                                 }
364                                 if (target instanceof StatusIconView) {
365                                         return super.onTouchEvent(event);
366                                         // return true;
367                                 }
368                                 obj = target;
369
370                                 // obj.setVisibility(View.GONE);
371                                 if (destTarget == null || destTarget instanceof StatusIconView) {
372                                         obj.setAnimation(anime);
373                                         obj.startAnimation(anime);
374                                         // layout.removeView(obj);
375
376                                         int srcX = target.getInitX();
377                                         int srcY = target.getInitY();
378                                         target.layout(srcX, srcY, srcX + target.getWidth(), srcY
379                                                         + target.getHeight());
380                                         setAlpha(target);
381                                 } else {
382                                         long src = target.getNodeId();
383                                         long dst = destTarget.getNodeId();
384                                         activity.execute(src, dst);
385
386                                         // TODO
387                                         target = null;
388                                         destTarget = null;
389
390                                         // T29171
391                                         clearTarget();
392
393                                         scrollView.setScrollable(true);
394                                         scrollView.requestDisallowInterceptTouchEvent(false);
395                                         scrollView.invalidate();
396                                 }
397
398                                 longClickFlg = false;
399                         }
400
401                         return super.onTouchEvent(event);
402                 } catch (Exception e) {
403                         e.printStackTrace();
404                         return false;
405                 }
406         }
407
408         @Override
409         public boolean onLongClick(View view) {
410                 if (!isElabledTouchEvent()) {
411                         return true;
412                 }
413                 if (target == null) {
414                         return false;
415                 }
416                 if (target instanceof StatusIconView) {
417                         return false;
418                 }
419
420                 LabelIconView v = (LabelIconView) target;
421                 setGrayAlpha(v);
422                 v.clearColorFilter();
423
424                 moveTop(target);
425
426                 longClickFlg = true;
427                 return true;
428         }
429
430         public boolean isElabledTouchEvent() {
431                 return elabledTouchEvent;
432         }
433
434         public void setElabledTouchEvent(boolean elabledTouchEvent) {
435                 this.elabledTouchEvent = elabledTouchEvent;
436         }
437 }