aboutsummaryrefslogtreecommitdiffstats
path: root/src/org/cyanogenmod/wallpapers/photophase/widgets/DispositionView.java
blob: 22d00a6fe3c35cd3e71de8fd9ab7a73ccf8f842b (plain)
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
/*
 * Copyright (C) 2013 The CyanogenMod 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 org.cyanogenmod.wallpapers.photophase.widgets;

import android.animation.Animator;
import android.animation.Animator.AnimatorListener;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Rect;
import android.os.Vibrator;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;
import android.widget.ImageView.ScaleType;

import org.cyanogenmod.wallpapers.photophase.R;
import org.cyanogenmod.wallpapers.photophase.animations.Evaluators;
import org.cyanogenmod.wallpapers.photophase.model.Disposition;
import org.cyanogenmod.wallpapers.photophase.utils.DispositionUtil;
import org.cyanogenmod.wallpapers.photophase.utils.MERAlgorithm;
import org.cyanogenmod.wallpapers.photophase.widgets.ResizeFrame.OnResizeListener;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * A class that allow to select the frames disposition visually
 */
public class DispositionView extends RelativeLayout implements OnLongClickListener, OnResizeListener {

    /**
     * An interface to communicate the selection/unselection of a frame
     */
    public interface OnFrameSelectedListener {
        /**
         * Invoked when a frame is selected
         *
         * @param v The frame view selected
         */
        void onFrameSelectedListener(View v);
        /**
         * Invoked when a frame is unselected
         */
        void onFrameUnselectedListener();
    }

    private boolean mChanged;
    private List<Disposition> mDispositions;
    private int mCols;
    private int mRows;

    /*package*/ View mTarget;
    private ResizeFrame mResizeFrame;
    private int mInternalPadding;
    private Rect mOldResizeFrameLocation;

    private OnFrameSelectedListener mOnFrameSelectedListener;

    private Vibrator mVibrator;

    /**
     * Constructor of <code>DispositionView</code>.
     *
     * @param context The current context
     */
    public DispositionView(Context context) {
        super(context);
        init();
    }

    /**
     * Constructor of <code>DispositionView</code>.
     *
     * @param context The current context
     * @param attrs The attributes of the XML tag that is inflating the view.
     */
    public DispositionView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    /**
     * Constructor of <code>DispositionView</code>.
     *
     * @param context The current context
     * @param attrs The attributes of the XML tag that is inflating the view.
     * @param defStyle The default style to apply to this view. If 0, no style
     *        will be applied (beyond what is included in the theme). This may
     *        either be an attribute resource, whose value will be retrieved
     *        from the current theme, or an explicit style resource.
     */
    public DispositionView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    /**
     * Initialize the view
     */
    private void init() {
        mVibrator = (Vibrator)getContext().getSystemService(Context.VIBRATOR_SERVICE);
        mInternalPadding = (int)getResources().getDimension(R.dimen.disposition_frame_padding);
    }

    /**
     * Method that returns the dispositions drawn on this view
     *
     * @return List<Disposition> The dispositions drawn
     */
    public List<Disposition> getDispositions() {
        return mDispositions;
    }

    /**
     * Method that sets the disposition to draw on this view
     *
     * @param dispositions The dispositions to draw
     * @param cols The number of cols
     * @param rows The number of rows
     */
    public void setDispositions(
            List<Disposition> dispositions, int cols, int rows) {
        mDispositions = dispositions;
        mCols = cols;
        mRows = rows;

        // Remove all the current views and add the new ones
        recreateDispositions(true);
        mResizeFrame.setVisibility(View.GONE);
        mChanged = false;
    }

    /**
     * Method that sets the resize frame view
     *
     * @param resizeFrame The resize frame view
     */
    public void setResizeFrame(ResizeFrame resizeFrame) {
        mResizeFrame = resizeFrame;
        mResizeFrame.setOnResizeListener(this);
    }

    /**
     * Method that set the listener for listen frame selection/unselection events
     *
     * @param onFrameSelectedListener The callback
     */
    public void setOnFrameSelectedListener(OnFrameSelectedListener onFrameSelectedListener) {
        this.mOnFrameSelectedListener = onFrameSelectedListener;
    }

    /**
     * Method that returns if the view was changed
     *
     * @return boolean true if the view was changed
     */
    public boolean isChanged() {
        return mChanged;
    }

    /**
     * Method that recreates all the dispositions
     *
     * @param animate If the recreate should be done with an animation
     */
    private void recreateDispositions(boolean animate) {
        // Remove all the current views and add the new ones
        removeAllViews();
        for (Disposition disposition : mDispositions) {
            createFrame(getLocationFromDisposition(disposition), animate);
        }
        mOldResizeFrameLocation = null;
        mTarget = null;
        if (mOnFrameSelectedListener != null) {
            mOnFrameSelectedListener.onFrameUnselectedListener();
        }
    }

    /**
     * Method that request the deletion of the current selected frame
     */
    @SuppressWarnings("boxing")
    public void deleteCurrentFrame() {
        if (mTarget == null) return;

        final Disposition targetDisposition = resizerToDisposition();

        // Get valid dispositions to move
        final List<Disposition> adjacents = findAdjacentsDispositions(targetDisposition);
        if (adjacents == null) {
            // Nothing to do
            Toast.makeText(getContext(),
                    R.string.pref_disposition_unable_delete_advise, Toast.LENGTH_SHORT).show();
            return;
        }

        // Hide resizer
        mResizeFrame.setVisibility(View.GONE);

        // Animate adjacents views
        List<Animator> animators = new ArrayList<Animator>();
        animators.add(ObjectAnimator.ofFloat(mTarget, "scaleX", 1.0f, 0.0f));
        animators.add(ObjectAnimator.ofFloat(mTarget, "scaleY", 1.0f, 0.0f));

        Disposition first = null;
        for (Disposition adjacent : adjacents) {
            // Extract the view and remove from dispositions
            View v = findViewFromRect(getLocationFromDisposition(adjacent));
            mDispositions.remove(adjacent);

            // Clone first disposition
            if (first == null) {
                first = new Disposition();
                first.x = adjacent.x;
                first.y = adjacent.y;
                first.w = adjacent.w;
                first.h = adjacent.h;
            }

            // Add animators and fix the adjacent
            if (v != null) {
                if (first.x < targetDisposition.x) {
                    // From Left to Right
                    int width = mTarget.getWidth() + mInternalPadding;
                    animators.add(ValueAnimator.ofObject(
                            new Evaluators.WidthEvaluator(v), v.getWidth(), v.getWidth() + width));

                    // Update the adjacent
                    adjacent.w += targetDisposition.w;
                    mDispositions.add(adjacent);

                } else if (first.x > targetDisposition.x) {
                    // From Right to Left
                    int width = mTarget.getWidth() + mInternalPadding;
                    animators.add(ValueAnimator.ofObject(
                            new Evaluators.WidthEvaluator(v), v.getWidth(), v.getWidth() + width));
                    animators.add(ObjectAnimator.ofFloat(v, "x", v.getX(), mTarget.getX()));

                    // Update the adjacent
                    adjacent.x = targetDisposition.x;
                    adjacent.w += targetDisposition.w;
                    mDispositions.add(adjacent);

                } else if (first.y < targetDisposition.y) {
                    // From Top to Bottom
                    int height = mTarget.getHeight() + mInternalPadding;
                    animators.add(ValueAnimator.ofObject(
                            new Evaluators.HeightEvaluator(v), v.getHeight(), v.getHeight() + height));

                    // Update the adjacent
                    adjacent.h += targetDisposition.h;
                    mDispositions.add(adjacent);

                } else if (first.y > targetDisposition.y) {
                    // From Bottom to Top
                    int height = mTarget.getHeight() + mInternalPadding;
                    animators.add(ValueAnimator.ofObject(
                            new Evaluators.HeightEvaluator(v), v.getHeight(), v.getHeight() + height));
                    animators.add(ObjectAnimator.ofFloat(v, "y", v.getY(), mTarget.getY()));

                    // Update the adjacent
                    adjacent.y = targetDisposition.y;
                    adjacent.h += targetDisposition.h;
                    mDispositions.add(adjacent);
                }
            }
        }
        if (animators.size() > 0) {
            AnimatorSet animSet = new AnimatorSet();
            animSet.playTogether(animators);
            animSet.setDuration(getResources().getInteger(R.integer.disposition_hide_anim));
            animSet.setInterpolator(new AccelerateInterpolator());
            animSet.addListener(new AnimatorListener() {
                @Override
                public void onAnimationStart(Animator animation) {
                    // Ignore
                }
                @Override
                public void onAnimationRepeat(Animator animation) {
                    // Ignore
                }
                @Override
                public void onAnimationEnd(Animator animation) {
                    finishDeleteAnimation(targetDisposition);
                }

                @Override
                public void onAnimationCancel(Animator animation) {
                    finishDeleteAnimation(targetDisposition);
                }
            });
            animSet.start();
        }
    }

    /**
     * Method that finalizes the delete animation
     *
     * @param target The disposition target
     */
    /*package*/ void finishDeleteAnimation(Disposition target) {
        removeView(mTarget);
        mDispositions.remove(target);
        Collections.sort(mDispositions);
        mChanged = true;

        // Clean status
        mOldResizeFrameLocation = null;
        mTarget = null;
        if (mOnFrameSelectedListener != null) {
            mOnFrameSelectedListener.onFrameUnselectedListener();
        }
    }

    /**
     * Method that create a new frame to be drawn in the specified location
     *
     * @param r The location relative to the parent layout
     * @return v The new view
     */
    private View createFrame(Rect r, boolean animate) {
        int padding = (int)getResources().getDimension(R.dimen.disposition_frame_padding);
        final ImageView v = new ImageView(getContext());
        v.setImageResource(R.drawable.ic_camera);
        v.setScaleType(ScaleType.CENTER);
        v.setBackgroundColor(getResources().getColor(R.color.disposition_frame_bg_color));
        RelativeLayout.LayoutParams params =
                new RelativeLayout.LayoutParams(r.width() - padding, r.height() - padding);
        v.setX(r.left + padding);
        v.setY(r.top + padding);
        v.setOnLongClickListener(this);
        if (animate) {
            v.setVisibility(View.INVISIBLE);
        }
        addView(v, params);

        // Animate the view
        if (animate) {
            post(new Runnable() {
                @Override
                public void run() {
                    Animation anim = AnimationUtils.loadAnimation(
                                        getContext(), R.anim.display_with_bounce);
                    anim.setFillBefore(true);
                    anim.setFillAfter(true);
                    v.startAnimation(anim);
                }
            });
        }

        return v;
    }

    /**
     * Method that returns the location of the frame from its disposition
     *
     * @param disposition The source disposition
     * @return Rect The location on parent view
     */
    private Rect getLocationFromDisposition(Disposition disposition) {
        int w = getMeasuredWidth() - (getPaddingLeft() + getPaddingRight());
        int h = getMeasuredHeight() - (getPaddingTop() + getPaddingBottom());
        int cw = w / mCols;
        int ch = h / mRows;

        Rect location = new Rect();
        location.left = disposition.x * cw;
        location.top = disposition.y * ch;
        location.right = location.left + disposition.w * cw;
        location.bottom = location.top + disposition.h * ch;
        return location;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean onLongClick(View v) {
        if (!selectTarget(v)) return false;
        mVibrator.vibrate(300);
        return true;
    }

    @Override
    public void onStartResize(int mode) {
        mOldResizeFrameLocation = new Rect(
                                        mResizeFrame.getLeft(),
                                        mResizeFrame.getTop(),
                                        mResizeFrame.getRight(),
                                        mResizeFrame.getBottom());
    }

    @Override
    public void onResize(int mode, int delta) {
        if (mTarget == null) return;

        int w = getMeasuredWidth() - (getPaddingLeft() + getPaddingRight());
        int h = getMeasuredHeight() - (getPaddingTop() + getPaddingBottom());
        int minWidth = (w / mCols) + (w / mCols) / 2;
        int minHeight = (h / mRows) + (h / mRows) / 2;

        FrameLayout.LayoutParams params =
                (FrameLayout.LayoutParams)mResizeFrame.getLayoutParams();
        switch (mode) {
            case Gravity.LEFT:
                float newpos = mResizeFrame.getX() + delta;
                if ((delta < 0 && newpos < (getPaddingLeft() * -1)) ||
                    (delta > 0 && newpos > (mResizeFrame.getX() + params.width - minWidth))) {
                    return;
                }
                mResizeFrame.setX(newpos);
                params.width -= delta;
                break;
            case Gravity.RIGHT:
                if ((delta < 0 && ((params.width + delta) < minWidth)) ||
                    (delta > 0 && (mResizeFrame.getX() + delta + params.width) > (getPaddingLeft() + getMeasuredWidth()))) {
                    return;
                }
                params.width += delta;
                break;
            case Gravity.TOP:
                newpos = mResizeFrame.getY() + delta;
                if ((delta < 0 && newpos < (getPaddingTop() * -1)) ||
                    (delta > 0 && newpos > (mResizeFrame.getY() + params.height - minHeight))) {
                    return;
                }
                mResizeFrame.setY(newpos);
                params.height -= delta;
                break;
            case Gravity.BOTTOM:
                if ((delta < 0 && ((params.height + delta) < minHeight)) ||
                    (delta > 0 && (mResizeFrame.getY() + delta + params.height) > (getPaddingTop() + getMeasuredHeight()))) {
                    return;
                }
                params.height += delta;
                break;

            default:
                break;
        }
        mResizeFrame.setLayoutParams(params);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void onEndResize(final int mode) {
        if (mTarget == null) return;

        // Compute the removed dispositions
        computeRemovedDispositions(mode);
        recreateDispositions(false);
        computeNewDispositions(mode);

        // Finish resize (select the target and create the new dispositions)
        post(new Runnable() {
            @Override
            public void run() {
                // Select the target
                View v = findTargetFromResizeFrame();
                if (v != null) {
                    selectTarget(v);
                }
            }
        });
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void onCancel() {
        if (mOldResizeFrameLocation != null) {
            mTarget.setLeft(mOldResizeFrameLocation.left);
            mTarget.setRight(mOldResizeFrameLocation.right);
            mTarget.setTop(mOldResizeFrameLocation.top);
            mTarget.setBottom(mOldResizeFrameLocation.bottom);
        }
        mOldResizeFrameLocation = null;
        mTarget = null;
        if (mOnFrameSelectedListener != null) {
            mOnFrameSelectedListener.onFrameUnselectedListener();
        }
    }

    /**
     * Method that returns the target view for the current resize frame
     *
     * @return The target view
     */
    /*package*/ View findTargetFromResizeFrame() {
        int count = getChildCount();
        for (int i = 0; i < count; i++) {
            View v = getChildAt(i);
            if (v.getX() < (mResizeFrame.getX() + (mResizeFrame.getWidth() / 2)) &&
                (v.getX() + v.getWidth()) > (mResizeFrame.getX() + (mResizeFrame.getWidth() / 2)) &&
                v.getY() < (mResizeFrame.getY() + (mResizeFrame.getHeight() / 2)) &&
                (v.getY() + v.getHeight()) > (mResizeFrame.getY() + (mResizeFrame.getHeight() / 2))) {
                return v;
            }
        }
        return null;
    }

    /**
     * Method that returns the view under the rect
     *
     * @return The view
     */
    private View findViewFromRect(Rect r) {
        int count = getChildCount();
        for (int i = 0; i < count; i++) {
            View v = getChildAt(i);
            if (v.getX() < (r.left + (r.width() / 2)) &&
                (v.getX() + v.getWidth()) > (r.left + (r.width() / 2)) &&
                v.getY() < (r.top + (r.height() / 2)) &&
                (v.getY() + v.getHeight()) > (r.top + (r.height() / 2))) {
                return v;
            }
        }
        return null;
    }

    /**
     * Method that select a view as the target of to resize
     *
     * @param v The target view
     */
    /*package*/ boolean selectTarget(View v) {
        //Do not do long click if we do not have a target
        if (mTarget != null && v.equals(mTarget)) return false;

        // Show the resize frame view just in place of the current clicked view
        mResizeFrame.hide();
        FrameLayout.LayoutParams frameParams =
                (FrameLayout.LayoutParams)mResizeFrame.getLayoutParams();
        int padding = mInternalPadding + mResizeFrame.getNeededPadding();
        frameParams.width = v.getWidth() + (padding * 2);
        frameParams.height = v.getHeight() + (padding * 2);
        mResizeFrame.setX(v.getX() - padding);
        mResizeFrame.setY(v.getY() - padding);
        mResizeFrame.show();

        // Save the new view
        mTarget = v;
        if (mOnFrameSelectedListener != null) {
            mOnFrameSelectedListener.onFrameSelectedListener(v);
        }
        return true;
    }

    /**
     * Computes the removed layout disposition based on the actual resize frame
     *
     * @param mode The resize mode
     */
    private void computeRemovedDispositions(int mode) {
        // Transform the resizer to a dispositions object
        Disposition resizer = resizerToDisposition();

        // Delete all overlapped
        int count = mDispositions.size();
        for (int i = count - 1; i >= 0; i--) {
            Disposition disposition = mDispositions.get(i);
            if (!isVisible(disposition) || isOverlapped(resizer, disposition)) {
                mDispositions.remove(disposition);
            }
        }

        // Add the new disposition
        mDispositions.add(resizer);
        Collections.sort(mDispositions);

        mChanged = true;
    }

    /**
     * Computes the new layout disposition based on the actual resize frame
     *
     * @param mode The resize mode
     */
    private void computeNewDispositions(int mode) {
        // Fill the empty areas
        do {
            byte[][] dispositionMatrix = DispositionUtil.toMatrix(mDispositions, mCols, mRows);
            Rect rect = MERAlgorithm.getMaximalEmptyRectangle(dispositionMatrix);
            if (rect.width() == 0 && rect.height() == 0) {
                // No more empty areas
                break;
            }
            Disposition disposition = DispositionUtil.fromRect(rect);
            createFrame(getLocationFromDisposition(disposition), true);
            mDispositions.add(disposition);
        } while (true);

        // Now the view was changed and should be reported
        Collections.sort(mDispositions);
        mChanged = true;
    }

    /**
     * Method that converts the resize frame to a dispostion reference
     *
     * @return Disposition The disposition reference
     */
    private Disposition resizerToDisposition() {
        int w = getMeasuredWidth() - (getPaddingLeft() + getPaddingRight());
        int h = getMeasuredHeight() - (getPaddingTop() + getPaddingBottom());
        int cw = w / mCols;
        int ch = h / mRows;

        //Remove overlapped areas
        Disposition resizer = new Disposition();
        resizer.x = Math.round(mResizeFrame.getX() / cw);
        resizer.y = Math.round(mResizeFrame.getY() / ch);
        resizer.w = Math.round(mResizeFrame.getWidth() / cw);
        resizer.h = Math.round(mResizeFrame.getHeight() / ch);

        // Fix disposition (limits)
        resizer.x = Math.max(resizer.x, 0);
        resizer.y = Math.max(resizer.y, 0);
        resizer.w = Math.min(resizer.w, mCols - resizer.x);
        resizer.h = Math.min(resizer.h, mRows - resizer.y);

        return resizer;
    }

    /**
     * Method that returns all dispositions that matched exactly (in one side) with
     * the argument disposition.
     *
     * @param disposition The disposition to check
     */
    private List<Disposition> findAdjacentsDispositions(Disposition disposition) {
        if (mDispositions.size() <= 1) return null;

        // Check left size
        if (disposition.x != 0) {
            List<Disposition> dispositions = new ArrayList<Disposition>();
            for (Disposition d : mDispositions) {
                if (d.compareTo(disposition) != 0) {
                    if ((d.x + d.w) == disposition.x &&
                        (d.y >= disposition.y) && ((d.y + d.h) <= (disposition.y + disposition.h))) {
                        dispositions.add(d);
                    }
                }
            }
            // Check if the sum of all the dispositions matches the disposition
            int sum = 0;
            for (Disposition d : dispositions) {
                sum += d.h;
            }
            if (sum == disposition.h) {
                return dispositions;
            }
        }
        // Check top size
        if (disposition.y != 0) {
            List<Disposition> dispositions = new ArrayList<Disposition>();
            for (Disposition d : mDispositions) {
                if (d.compareTo(disposition) != 0) {
                    if ((d.y + d.h) == disposition.y &&
                        (d.x >= disposition.x) && ((d.x + d.w) <= (disposition.x + disposition.w))) {
                        dispositions.add(d);
                    }
                }
            }
            // Check if the sum of all the dispositions matches the disposition
            int sum = 0;
            for (Disposition d : dispositions) {
                sum += d.w;
            }
            if (sum == disposition.w) {
                return dispositions;
            }
        }
        // Check right size
        if ((disposition.x + disposition.w) != mCols) {
            List<Disposition> dispositions = new ArrayList<Disposition>();
            for (Disposition d : mDispositions) {
                if (d.compareTo(disposition) != 0) {
                    if ((d.x) == (disposition.x + disposition.w) &&
                        (d.y >= disposition.y) && ((d.y + d.h) <= (disposition.y + disposition.h))) {
                        dispositions.add(d);
                    }
                }
            }
            // Check if the sum of all the dispositions matches the disposition
            int sum = 0;
            for (Disposition d : dispositions) {
                sum += d.h;
            }
            if (sum == disposition.h) {
                return dispositions;
            }
        }
        // Check bottom size
        if ((disposition.y + disposition.h) != mRows) {
            List<Disposition> dispositions = new ArrayList<Disposition>();
            for (Disposition d : mDispositions) {
                if (d.compareTo(disposition) != 0) {
                    if ((d.y) == (disposition.y + disposition.h) &&
                        (d.x >= disposition.x) && ((d.x + d.w) <= (disposition.x + disposition.w))) {
                        dispositions.add(d);
                    }
                }
            }
            // Check if the sum of all the dispositions matches the disposition
            int sum = 0;
            for (Disposition d : dispositions) {
                sum += d.w;
            }
            if (sum == disposition.w) {
                return dispositions;
            }
        }
        return null;
    }

    /**
     * Method that checks if a dispositions overlaps another other disposition
     *
     * @param d1 One disposition
     * @param d2 Another disposition
     * @return boolean true if d1 overlaps d2
     */
    private static boolean isOverlapped(Disposition d1, Disposition d2) {
        Rect r1 = new Rect(d1.x, d1.y, d1.x + d1.w, d1.y + d1.h);
        Rect r2 = new Rect(d2.x, d2.y, d2.x + d2.w, d2.y + d2.h);
        return r1.intersect(r2);
    }

    /**
     * Method that checks if a dispositions is visible
     *
     * @param d The disposition to check
     * @return boolean true if d is visible
     */
    private static boolean isVisible(Disposition d) {
        return d.w > 0 && d.h > 0;
    }
}