-
Notifications
You must be signed in to change notification settings - Fork 9
/
CellLayout.java
1741 lines (1491 loc) · 64 KB
/
CellLayout.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.launcher2;
import java.util.Arrays;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.TimeInterpolator;
import android.animation.ValueAnimator;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.PointF;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Region;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewDebug;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.LayoutAnimationController;
import com.android.launcher.R;
public class CellLayout extends ViewGroup {
static final String TAG = "CellLayout";
private int mCellWidth;
private int mCellHeight;
private int mLeftPadding;
private int mRightPadding;
private int mTopPadding;
private int mBottomPadding;
private int mCountX;
private int mCountY;
private int mWidthGap;
private int mHeightGap;
private final Rect mRect = new Rect();
private final CellInfo mCellInfo = new CellInfo();
// These are temporary variables to prevent having to allocate a new object just to
// return an (x, y) value from helper functions. Do NOT use them to maintain other state.
private final int[] mTmpCellXY = new int[2];
private final int[] mTmpPoint = new int[2];
private final PointF mTmpPointF = new PointF();
boolean[][] mOccupied;
private OnTouchListener mInterceptTouchListener;
private float mBackgroundAlpha;
private float mBackgroundAlphaMultiplier = 1.0f;
private Drawable mNormalBackground;
private Drawable mActiveBackground;
private Drawable mActiveGlowBackground;
private Drawable mNormalBackgroundMini;
private Drawable mNormalGlowBackgroundMini;
private Drawable mActiveBackgroundMini;
private Drawable mActiveGlowBackgroundMini;
private Rect mBackgroundRect;
private Rect mGlowBackgroundRect;
private float mGlowBackgroundScale;
private float mGlowBackgroundAlpha;
private boolean mAcceptsDrops = true;
// If we're actively dragging something over this screen, mIsDragOverlapping is true
private boolean mIsDragOverlapping = false;
private boolean mIsDragOccuring = false;
private boolean mIsDefaultDropTarget = false;
private final Point mDragCenter = new Point();
// These arrays are used to implement the drag visualization on x-large screens.
// They are used as circular arrays, indexed by mDragOutlineCurrent.
private Point[] mDragOutlines = new Point[4];
private float[] mDragOutlineAlphas = new float[mDragOutlines.length];
private InterruptibleInOutAnimator[] mDragOutlineAnims =
new InterruptibleInOutAnimator[mDragOutlines.length];
// Used as an index into the above 3 arrays; indicates which is the most current value.
private int mDragOutlineCurrent = 0;
private final Paint mDragOutlinePaint = new Paint();
private BubbleTextView mPressedOrFocusedIcon;
private Drawable mCrosshairsDrawable = null;
private InterruptibleInOutAnimator mCrosshairsAnimator = null;
private float mCrosshairsVisibility = 0.0f;
// When a drag operation is in progress, holds the nearest cell to the touch point
private final int[] mDragCell = new int[2];
private boolean mDragging = false;
private TimeInterpolator mEaseOutInterpolator;
private CellLayoutChildren mChildren;
public CellLayout(Context context) {
this(context, null);
}
public CellLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CellLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show
// the user where a dragged item will land when dropped.
setWillNotDraw(false);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0);
mCellWidth = a.getDimensionPixelSize(R.styleable.CellLayout_cellWidth, 10);
mCellHeight = a.getDimensionPixelSize(R.styleable.CellLayout_cellHeight, 10);
mWidthGap = a.getDimensionPixelSize(R.styleable.CellLayout_widthGap, -1);
mHeightGap = a.getDimensionPixelSize(R.styleable.CellLayout_heightGap, -1);
mLeftPadding =
a.getDimensionPixelSize(R.styleable.CellLayout_xAxisStartPadding, 10);
mRightPadding =
a.getDimensionPixelSize(R.styleable.CellLayout_xAxisEndPadding, 10);
mTopPadding =
a.getDimensionPixelSize(R.styleable.CellLayout_yAxisStartPadding, 10);
mBottomPadding =
a.getDimensionPixelSize(R.styleable.CellLayout_yAxisEndPadding, 10);
mCountX = LauncherModel.getCellCountX();
mCountY = LauncherModel.getCellCountY();
mOccupied = new boolean[mCountX][mCountY];
a.recycle();
setAlwaysDrawnWithCacheEnabled(false);
final Resources res = getResources();
if (LauncherApplication.isScreenXLarge()) {
mNormalBackground = res.getDrawable(R.drawable.homescreen_large_blue);
mActiveBackground = res.getDrawable(R.drawable.homescreen_large_green);
mActiveGlowBackground = res.getDrawable(R.drawable.homescreen_large_green_strong);
mNormalBackgroundMini = res.getDrawable(R.drawable.homescreen_small_blue);
mNormalGlowBackgroundMini = res.getDrawable(R.drawable.homescreen_small_blue_strong);
mActiveBackgroundMini = res.getDrawable(R.drawable.homescreen_small_green);
mActiveGlowBackgroundMini = res.getDrawable(R.drawable.homescreen_small_green_strong);
mNormalBackground.setFilterBitmap(true);
mActiveBackground.setFilterBitmap(true);
mActiveGlowBackground.setFilterBitmap(true);
mNormalBackgroundMini.setFilterBitmap(true);
mNormalGlowBackgroundMini.setFilterBitmap(true);
mActiveBackgroundMini.setFilterBitmap(true);
mActiveGlowBackgroundMini.setFilterBitmap(true);
}
// Initialize the data structures used for the drag visualization.
mCrosshairsDrawable = res.getDrawable(R.drawable.gardening_crosshairs);
mEaseOutInterpolator = new DecelerateInterpolator(2.5f); // Quint ease out
// Set up the animation for fading the crosshairs in and out
int animDuration = res.getInteger(R.integer.config_crosshairsFadeInTime);
mCrosshairsAnimator = new InterruptibleInOutAnimator(animDuration, 0.0f, 1.0f);
mCrosshairsAnimator.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
public void onAnimationUpdate(ValueAnimator animation) {
mCrosshairsVisibility = ((Float) animation.getAnimatedValue()).floatValue();
invalidate();
}
});
mCrosshairsAnimator.getAnimator().setInterpolator(mEaseOutInterpolator);
for (int i = 0; i < mDragOutlines.length; i++) {
mDragOutlines[i] = new Point(-1, -1);
}
// When dragging things around the home screens, we show a green outline of
// where the item will land. The outlines gradually fade out, leaving a trail
// behind the drag path.
// Set up all the animations that are used to implement this fading.
final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime);
final float fromAlphaValue = 0;
final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha);
Arrays.fill(mDragOutlineAlphas, fromAlphaValue);
for (int i = 0; i < mDragOutlineAnims.length; i++) {
final InterruptibleInOutAnimator anim =
new InterruptibleInOutAnimator(duration, fromAlphaValue, toAlphaValue);
anim.getAnimator().setInterpolator(mEaseOutInterpolator);
final int thisIndex = i;
anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
public void onAnimationUpdate(ValueAnimator animation) {
final Bitmap outline = (Bitmap)anim.getTag();
// If an animation is started and then stopped very quickly, we can still
// get spurious updates we've cleared the tag. Guard against this.
if (outline == null) {
if (false) {
Object val = animation.getAnimatedValue();
Log.d(TAG, "anim " + thisIndex + " update: " + val +
", isStopped " + anim.isStopped());
}
// Try to prevent it from continuing to run
animation.cancel();
} else {
mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue();
final int left = mDragOutlines[thisIndex].x;
final int top = mDragOutlines[thisIndex].y;
CellLayout.this.invalidate(left, top,
left + outline.getWidth(), top + outline.getHeight());
}
}
});
// The animation holds a reference to the drag outline bitmap as long is it's
// running. This way the bitmap can be GCed when the animations are complete.
anim.getAnimator().addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) {
anim.setTag(null);
}
}
});
mDragOutlineAnims[i] = anim;
}
mBackgroundRect = new Rect();
mGlowBackgroundRect = new Rect();
setHoverScale(1.0f);
setHoverAlpha(1.0f);
mChildren = new CellLayoutChildren(context);
mChildren.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap);
addView(mChildren);
}
private void invalidateBubbleTextView(BubbleTextView icon) {
final int padding = icon.getPressedOrFocusedBackgroundPadding();
invalidate(icon.getLeft() + getLeftPadding() - padding,
icon.getTop() + getTopPadding() - padding,
icon.getRight() + getLeftPadding() + padding,
icon.getBottom() + getTopPadding() + padding);
}
void setPressedOrFocusedIcon(BubbleTextView icon) {
// We draw the pressed or focused BubbleTextView's background in CellLayout because it
// requires an expanded clip rect (due to the glow's blur radius)
BubbleTextView oldIcon = mPressedOrFocusedIcon;
mPressedOrFocusedIcon = icon;
if (oldIcon != null) {
invalidateBubbleTextView(oldIcon);
}
if (mPressedOrFocusedIcon != null) {
invalidateBubbleTextView(mPressedOrFocusedIcon);
}
}
public CellLayoutChildren getChildrenLayout() {
if (getChildCount() > 0) {
return (CellLayoutChildren) getChildAt(0);
}
return null;
}
public void setIsDefaultDropTarget(boolean isDefaultDropTarget) {
if (mIsDefaultDropTarget != isDefaultDropTarget) {
mIsDefaultDropTarget = isDefaultDropTarget;
invalidate();
}
}
void setIsDragOccuring(boolean isDragOccuring) {
if (mIsDragOccuring != isDragOccuring) {
mIsDragOccuring = isDragOccuring;
invalidate();
}
}
void setIsDragOverlapping(boolean isDragOverlapping) {
if (mIsDragOverlapping != isDragOverlapping) {
mIsDragOverlapping = isDragOverlapping;
invalidate();
}
}
boolean getIsDragOverlapping() {
return mIsDragOverlapping;
}
private void updateGlowRect() {
float marginFraction = (mGlowBackgroundScale - 1.0f) / 2.0f;
int marginX = (int) (marginFraction * (mBackgroundRect.right - mBackgroundRect.left));
int marginY = (int) (marginFraction * (mBackgroundRect.bottom - mBackgroundRect.top));
mGlowBackgroundRect.set(mBackgroundRect.left - marginX, mBackgroundRect.top - marginY,
mBackgroundRect.right + marginX, mBackgroundRect.bottom + marginY);
invalidate();
}
public void setHoverScale(float scaleFactor) {
if (scaleFactor != mGlowBackgroundScale) {
mGlowBackgroundScale = scaleFactor;
updateGlowRect();
if (getParent() != null) {
((View) getParent()).invalidate();
}
}
}
public float getHoverScale() {
return mGlowBackgroundScale;
}
public float getHoverAlpha() {
return mGlowBackgroundAlpha;
}
public void setHoverAlpha(float alpha) {
mGlowBackgroundAlpha = alpha;
invalidate();
}
void animateDrop() {
if (LauncherApplication.isScreenXLarge()) {
Resources res = getResources();
float onDropScale = res.getInteger(R.integer.config_screenOnDropScalePercent) / 100.0f;
ObjectAnimator scaleUp = ObjectAnimator.ofFloat(this, "hoverScale", onDropScale);
scaleUp.setDuration(res.getInteger(R.integer.config_screenOnDropScaleUpDuration));
ObjectAnimator scaleDown = ObjectAnimator.ofFloat(this, "hoverScale", 1.0f);
scaleDown.setDuration(res.getInteger(R.integer.config_screenOnDropScaleDownDuration));
ObjectAnimator alphaFadeOut = ObjectAnimator.ofFloat(this, "hoverAlpha", 0.0f);
alphaFadeOut.setStartDelay(res.getInteger(R.integer.config_screenOnDropAlphaFadeDelay));
alphaFadeOut.setDuration(res.getInteger(R.integer.config_screenOnDropAlphaFadeDelay));
AnimatorSet bouncer = new AnimatorSet();
bouncer.play(scaleUp).before(scaleDown);
bouncer.play(scaleUp).with(alphaFadeOut);
bouncer.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationStart(Animator animation) {
setIsDragOverlapping(true);
}
@Override
public void onAnimationEnd(Animator animation) {
setIsDragOverlapping(false);
setHoverScale(1.0f);
setHoverAlpha(1.0f);
}
});
bouncer.start();
}
}
@Override
protected void onDraw(Canvas canvas) {
// When we're large, we are either drawn in a "hover" state (ie when dragging an item to
// a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
// When we're small, we are either drawn normally or in the "accepts drops" state (during
// a drag). However, we also drag the mini hover background *over* one of those two
// backgrounds
if (LauncherApplication.isScreenXLarge() && mBackgroundAlpha > 0.0f) {
Drawable bg;
boolean mini = getScaleX() < 0.5f;
if (mIsDragOverlapping) {
// In the mini case, we draw the active_glow bg *over* the active background
bg = mini ? mActiveBackgroundMini : mActiveGlowBackground;
} else if (mIsDragOccuring && mAcceptsDrops) {
bg = mini ? mActiveBackgroundMini : mActiveBackground;
} else if (mIsDefaultDropTarget && mini) {
bg = mNormalGlowBackgroundMini;
} else {
bg = mini ? mNormalBackgroundMini : mNormalBackground;
}
bg.setAlpha((int) (mBackgroundAlpha * mBackgroundAlphaMultiplier * 255));
bg.setBounds(mBackgroundRect);
bg.draw(canvas);
if (mini && mIsDragOverlapping) {
boolean modifiedClipRect = false;
if (mGlowBackgroundScale > 1.0f) {
// If the hover background's scale is greater than 1, we'll be drawing outside
// the bounds of this CellLayout. Get around that by temporarily increasing the
// size of the clip rect
float marginFraction = (mGlowBackgroundScale - 1.0f) / 2.0f;
Rect clipRect = canvas.getClipBounds();
int marginX = (int) (marginFraction * (clipRect.right - clipRect.left));
int marginY = (int) (marginFraction * (clipRect.bottom - clipRect.top));
canvas.save(Canvas.CLIP_SAVE_FLAG);
canvas.clipRect(-marginX, -marginY,
getWidth() + marginX, getHeight() + marginY, Region.Op.REPLACE);
modifiedClipRect = true;
}
mActiveGlowBackgroundMini.setAlpha(
(int) (mBackgroundAlpha * mGlowBackgroundAlpha * 255));
mActiveGlowBackgroundMini.setBounds(mGlowBackgroundRect);
mActiveGlowBackgroundMini.draw(canvas);
if (modifiedClipRect) {
canvas.restore();
}
}
}
if (mCrosshairsVisibility > 0.0f) {
final int countX = mCountX;
final int countY = mCountY;
final float MAX_ALPHA = 0.4f;
final int MAX_VISIBLE_DISTANCE = 600;
final float DISTANCE_MULTIPLIER = 0.002f;
final Drawable d = mCrosshairsDrawable;
final int width = d.getIntrinsicWidth();
final int height = d.getIntrinsicHeight();
int x = getLeftPadding() - (mWidthGap / 2) - (width / 2);
for (int col = 0; col <= countX; col++) {
int y = getTopPadding() - (mHeightGap / 2) - (height / 2);
for (int row = 0; row <= countY; row++) {
mTmpPointF.set(x - mDragCenter.x, y - mDragCenter.y);
float dist = mTmpPointF.length();
// Crosshairs further from the drag point are more faint
float alpha = Math.min(MAX_ALPHA,
DISTANCE_MULTIPLIER * (MAX_VISIBLE_DISTANCE - dist));
if (alpha > 0.0f) {
d.setBounds(x, y, x + width, y + height);
d.setAlpha((int) (alpha * 255 * mCrosshairsVisibility));
d.draw(canvas);
}
y += mCellHeight + mHeightGap;
}
x += mCellWidth + mWidthGap;
}
}
final Paint paint = mDragOutlinePaint;
for (int i = 0; i < mDragOutlines.length; i++) {
final float alpha = mDragOutlineAlphas[i];
if (alpha > 0) {
final Point p = mDragOutlines[i];
final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
paint.setAlpha((int)(alpha + .5f));
canvas.drawBitmap(b, p.x, p.y, paint);
}
}
// We draw the pressed or focused BubbleTextView's background in CellLayout because it
// requires an expanded clip rect (due to the glow's blur radius)
if (mPressedOrFocusedIcon != null) {
final int padding = mPressedOrFocusedIcon.getPressedOrFocusedBackgroundPadding();
final Bitmap b = mPressedOrFocusedIcon.getPressedOrFocusedBackground();
if (b != null) {
canvas.drawBitmap(b,
mPressedOrFocusedIcon.getLeft() + getLeftPadding() - padding,
mPressedOrFocusedIcon.getTop() + getTopPadding() - padding,
null);
}
}
}
@Override
public void cancelLongPress() {
super.cancelLongPress();
// Cancel long press for all children
final int count = getChildCount();
for (int i = 0; i < count; i++) {
final View child = getChildAt(i);
child.cancelLongPress();
}
}
public void setOnInterceptTouchListener(View.OnTouchListener listener) {
mInterceptTouchListener = listener;
}
int getCountX() {
return mCountX;
}
int getCountY() {
return mCountY;
}
public boolean addViewToCellLayout(
View child, int index, int childId, LayoutParams params, boolean markCells) {
final LayoutParams lp = params;
// Generate an id for each view, this assumes we have at most 256x256 cells
// per workspace screen
if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
// If the horizontal or vertical span is set to -1, it is taken to
// mean that it spans the extent of the CellLayout
if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
child.setId(childId);
mChildren.addView(child, index, lp);
if (markCells) markCellsAsOccupiedForView(child);
return true;
}
return false;
}
public void setAcceptsDrops(boolean acceptsDrops) {
if (mAcceptsDrops != acceptsDrops) {
mAcceptsDrops = acceptsDrops;
invalidate();
}
}
public boolean getAcceptsDrops() {
return mAcceptsDrops;
}
@Override
public void removeAllViews() {
clearOccupiedCells();
mChildren.removeAllViews();
}
@Override
public void removeAllViewsInLayout() {
clearOccupiedCells();
mChildren.removeAllViewsInLayout();
}
public void removeViewWithoutMarkingCells(View view) {
mChildren.removeView(view);
}
@Override
public void removeView(View view) {
markCellsAsUnoccupiedForView(view);
mChildren.removeView(view);
}
@Override
public void removeViewAt(int index) {
markCellsAsUnoccupiedForView(mChildren.getChildAt(index));
mChildren.removeViewAt(index);
}
@Override
public void removeViewInLayout(View view) {
markCellsAsUnoccupiedForView(view);
mChildren.removeViewInLayout(view);
}
@Override
public void removeViews(int start, int count) {
for (int i = start; i < start + count; i++) {
markCellsAsUnoccupiedForView(mChildren.getChildAt(i));
}
mChildren.removeViews(start, count);
}
@Override
public void removeViewsInLayout(int start, int count) {
for (int i = start; i < start + count; i++) {
markCellsAsUnoccupiedForView(mChildren.getChildAt(i));
}
mChildren.removeViewsInLayout(start, count);
}
public void drawChildren(Canvas canvas) {
mChildren.draw(canvas);
}
void buildChildrenLayer() {
mChildren.buildLayer();
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
mCellInfo.screen = ((ViewGroup) getParent()).indexOfChild(this);
}
public void setTagToCellInfoForPoint(int touchX, int touchY) {
final CellInfo cellInfo = mCellInfo;
final Rect frame = mRect;
final int x = touchX + mScrollX;
final int y = touchY + mScrollY;
final int count = mChildren.getChildCount();
boolean found = false;
for (int i = count - 1; i >= 0; i--) {
final View child = mChildren.getChildAt(i);
final LayoutParams lp = (LayoutParams) child.getLayoutParams();
if ((child.getVisibility() == VISIBLE || child.getAnimation() != null) &&
lp.isLockedToGrid) {
child.getHitRect(frame);
if (frame.contains(x, y)) {
cellInfo.cell = child;
cellInfo.cellX = lp.cellX;
cellInfo.cellY = lp.cellY;
cellInfo.spanX = lp.cellHSpan;
cellInfo.spanY = lp.cellVSpan;
cellInfo.valid = true;
found = true;
break;
}
}
}
if (!found) {
final int cellXY[] = mTmpCellXY;
pointToCellExact(x, y, cellXY);
cellInfo.cell = null;
cellInfo.cellX = cellXY[0];
cellInfo.cellY = cellXY[1];
cellInfo.spanX = 1;
cellInfo.spanY = 1;
cellInfo.valid = cellXY[0] >= 0 && cellXY[1] >= 0 && cellXY[0] < mCountX &&
cellXY[1] < mCountY && !mOccupied[cellXY[0]][cellXY[1]];
}
setTag(cellInfo);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev)) {
return true;
}
final int action = ev.getAction();
final CellInfo cellInfo = mCellInfo;
if (action == MotionEvent.ACTION_DOWN) {
setTagToCellInfoForPoint((int) ev.getX(), (int) ev.getY());
} else if (action == MotionEvent.ACTION_UP) {
cellInfo.cell = null;
cellInfo.cellX = -1;
cellInfo.cellY = -1;
cellInfo.spanX = 0;
cellInfo.spanY = 0;
cellInfo.valid = false;
setTag(cellInfo);
}
return false;
}
@Override
public CellInfo getTag() {
return (CellInfo) super.getTag();
}
/**
* Given a point, return the cell that strictly encloses that point
* @param x X coordinate of the point
* @param y Y coordinate of the point
* @param result Array of 2 ints to hold the x and y coordinate of the cell
*/
void pointToCellExact(int x, int y, int[] result) {
final int hStartPadding = getLeftPadding();
final int vStartPadding = getTopPadding();
result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
final int xAxis = mCountX;
final int yAxis = mCountY;
if (result[0] < 0) result[0] = 0;
if (result[0] >= xAxis) result[0] = xAxis - 1;
if (result[1] < 0) result[1] = 0;
if (result[1] >= yAxis) result[1] = yAxis - 1;
}
/**
* Given a point, return the cell that most closely encloses that point
* @param x X coordinate of the point
* @param y Y coordinate of the point
* @param result Array of 2 ints to hold the x and y coordinate of the cell
*/
void pointToCellRounded(int x, int y, int[] result) {
pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
}
/**
* Given a cell coordinate, return the point that represents the upper left corner of that cell
*
* @param cellX X coordinate of the cell
* @param cellY Y coordinate of the cell
*
* @param result Array of 2 ints to hold the x and y coordinate of the point
*/
void cellToPoint(int cellX, int cellY, int[] result) {
final int hStartPadding = getLeftPadding();
final int vStartPadding = getTopPadding();
result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
}
/**
* Given a cell coordinate, return the point that represents the upper left corner of that cell
*
* @param cellX X coordinate of the cell
* @param cellY Y coordinate of the cell
*
* @param result Array of 2 ints to hold the x and y coordinate of the point
*/
void cellToCenterPoint(int cellX, int cellY, int[] result) {
final int hStartPadding = getLeftPadding();
final int vStartPadding = getTopPadding();
result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap) + mCellWidth / 2;
result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap) + mCellHeight / 2;
}
int getCellWidth() {
return mCellWidth;
}
int getCellHeight() {
return mCellHeight;
}
int getWidthGap() {
return mWidthGap;
}
int getHeightGap() {
return mHeightGap;
}
int getLeftPadding() {
return mLeftPadding;
}
int getTopPadding() {
return mTopPadding;
}
int getRightPadding() {
return mRightPadding;
}
int getBottomPadding() {
return mBottomPadding;
}
Rect getContentRect(Rect r) {
if (r == null) {
r = new Rect();
}
int left = getPaddingLeft();
int top = getPaddingTop();
int right = left + getWidth() - mLeftPadding - mRightPadding;
int bottom = top + getHeight() - mTopPadding - mBottomPadding;
r.set(left, top, right, bottom);
return r;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO: currently ignoring padding
int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
}
final int cellWidth = mCellWidth;
final int cellHeight = mCellHeight;
int numWidthGaps = mCountX - 1;
int numHeightGaps = mCountY - 1;
if (mWidthGap < 0 || mHeightGap < 0) {
int vSpaceLeft = heightSpecSize - mTopPadding - mBottomPadding - (cellHeight * mCountY);
mHeightGap = vSpaceLeft / numHeightGaps;
int hSpaceLeft = widthSpecSize - mLeftPadding - mRightPadding - (cellWidth * mCountX);
mWidthGap = hSpaceLeft / numWidthGaps;
// center it around the min gaps
int minGap = Math.min(mWidthGap, mHeightGap);
mWidthGap = mHeightGap = minGap;
}
// Initial values correspond to widthSpecMode == MeasureSpec.EXACTLY
int newWidth = widthSpecSize;
int newHeight = heightSpecSize;
if (widthSpecMode == MeasureSpec.AT_MOST) {
newWidth = mLeftPadding + mRightPadding + (mCountX * cellWidth) +
((mCountX - 1) * mWidthGap);
newHeight = mTopPadding + mBottomPadding + (mCountY * cellHeight) +
((mCountY - 1) * mHeightGap);
setMeasuredDimension(newWidth, newHeight);
}
int count = getChildCount();
for (int i = 0; i < count; i++) {
View child = getChildAt(i);
int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(newWidth, MeasureSpec.EXACTLY);
int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(newHeight,
MeasureSpec.EXACTLY);
child.measure(childWidthMeasureSpec, childheightMeasureSpec);
}
setMeasuredDimension(newWidth, newHeight);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int count = getChildCount();
for (int i = 0; i < count; i++) {
View child = getChildAt(i);
child.layout(mLeftPadding, mTopPadding, r - l - mRightPadding , b - t - mBottomPadding);
}
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mBackgroundRect.set(0, 0, w, h);
updateGlowRect();
}
@Override
protected void setChildrenDrawingCacheEnabled(boolean enabled) {
mChildren.setChildrenDrawingCacheEnabled(enabled);
}
@Override
protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
mChildren.setChildrenDrawnWithCacheEnabled(enabled);
}
public float getBackgroundAlpha() {
return mBackgroundAlpha;
}
public void setFastBackgroundAlpha(float alpha) {
mBackgroundAlpha = alpha;
}
public void setBackgroundAlphaMultiplier(float multiplier) {
mBackgroundAlphaMultiplier = multiplier;
}
public float getBackgroundAlphaMultiplier() {
return mBackgroundAlphaMultiplier;
}
public void setBackgroundAlpha(float alpha) {
mBackgroundAlpha = alpha;
invalidate();
}
// Need to return true to let the view system know we know how to handle alpha-- this is
// because when our children have an alpha of 0.0f, they are still rendering their "dimmed"
// versions
@Override
protected boolean onSetAlpha(int alpha) {
return true;
}
public void setAlpha(float alpha) {
setChildrenAlpha(alpha);
super.setAlpha(alpha);
}
public void setFastAlpha(float alpha) {
setFastChildrenAlpha(alpha);
super.setFastAlpha(alpha);
}
private void setChildrenAlpha(float alpha) {
final int childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
getChildAt(i).setAlpha(alpha);
}
}
private void setFastChildrenAlpha(float alpha) {
final int childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
getChildAt(i).setFastAlpha(alpha);
}
}
public View getChildAt(int x, int y) {
return mChildren.getChildAt(x, y);
}
/**
* Estimate where the top left cell of the dragged item will land if it is dropped.
*
* @param originX The X value of the top left corner of the item
* @param originY The Y value of the top left corner of the item
* @param spanX The number of horizontal cells that the item spans
* @param spanY The number of vertical cells that the item spans
* @param result The estimated drop cell X and Y.
*/
void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) {
final int countX = mCountX;
final int countY = mCountY;
// pointToCellRounded takes the top left of a cell but will pad that with
// cellWidth/2 and cellHeight/2 when finding the matching cell
pointToCellRounded(originX, originY, result);
// If the item isn't fully on this screen, snap to the edges
int rightOverhang = result[0] + spanX - countX;
if (rightOverhang > 0) {
result[0] -= rightOverhang; // Snap to right
}
result[0] = Math.max(0, result[0]); // Snap to left
int bottomOverhang = result[1] + spanY - countY;
if (bottomOverhang > 0) {
result[1] -= bottomOverhang; // Snap to bottom
}
result[1] = Math.max(0, result[1]); // Snap to top
}
void visualizeDropLocation(
View v, Bitmap dragOutline, int originX, int originY, int spanX, int spanY) {
final int oldDragCellX = mDragCell[0];
final int oldDragCellY = mDragCell[1];
final int[] nearest = findNearestVacantArea(originX, originY, spanX, spanY, v, mDragCell);
if (v != null) {
mDragCenter.set(originX + (v.getWidth() / 2), originY + (v.getHeight() / 2));
} else {
mDragCenter.set(originX, originY);
}
if (nearest != null && (nearest[0] != oldDragCellX || nearest[1] != oldDragCellY)) {
// Find the top left corner of the rect the object will occupy
final int[] topLeft = mTmpPoint;
cellToPoint(nearest[0], nearest[1], topLeft);
int left = topLeft[0];
int top = topLeft[1];
if (v != null) {
// When drawing the drag outline, it did not account for margin offsets
// added by the view's parent.
MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
left += lp.leftMargin;
top += lp.topMargin;
// Offsets due to the size difference between the View and the dragOutline.
// There is a size difference to account for the outer blur, which may lie
// outside the bounds of the view.
left += (v.getWidth() - dragOutline.getWidth()) / 2;
top += (v.getHeight() - dragOutline.getHeight()) / 2;
} else {
// Center the drag outline in the cell
left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
- dragOutline.getWidth()) / 2;
top += ((mCellHeight * spanY) + ((spanY - 1) * mHeightGap)