summaryrefslogtreecommitdiff
path: root/src/plugins/emulator/src/com/motorola/studio/android/emulator/ui/controls/skin/AndroidSkinLayout.java
blob: 11fadd5fb9a6b661d487abba31ab3f557c0598ff (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
/*
* Copyright (C) 2012 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.motorola.studio.android.emulator.ui.controls.skin;

import org.eclipse.sequoyah.vnc.vncviewer.graphics.swt.SWTRemoteDisplay;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Layout;

import com.motorola.studio.android.emulator.core.model.IAndroidEmulatorInstance;
import com.motorola.studio.android.emulator.core.skin.AndroidSkinBean;
import com.motorola.studio.android.emulator.core.skin.ISkinKeyXmlTags;
import com.motorola.studio.android.emulator.ui.IAndroidUIConstants;
import com.motorola.studio.android.emulator.ui.controls.RemoteCLIDisplay;
import com.motorola.studio.android.emulator.ui.controls.UIHelper;

/**
 * DESCRIPTION:
 * This class implements the layout used to draw skins
 * It is a very specific implementation that applies only to Android Emulator
 * The position and size of the widgets are read from the skin bean provided
 * during layout construction
 *
 * RESPONSIBILITY:
 * - Draw the widgets that composes the skin in the correct position and
 * with the correct size
 *
 * COLABORATORS:
 * None.
 *
 * USAGE:
 * Use of this class is restricted to the classes in this package
 */
class AndroidSkinLayout extends Layout
{
    /**
     * Object that holds information from the skin.xml file
     * of the skin being currently used
     */
    private final AndroidSkinBean skin;

    /**
     * Reference to the CLI display that will be placed by this layout
     */
    private RemoteCLIDisplay cliDisplayChild = null;

    /**
     * Reference to the main display that will be placed by this layout
     */
    private SWTRemoteDisplay mainDisplayChild = null;

    /**
     * Flag that indicates if the skin contains an open external display
     * placeholder
     */
    private final boolean openExternalDisplayAvailable;

    /**
     * Flag that indicates if the skin contains an external display
     * placeholder
     */
    private final boolean externalDisplayAvailable;

    /**
     * Flag that indicates that the flip is supported. This flag has nothing to do with slide
     */
    private final boolean isFlipSupported;

    /**
     * Creates a new AndroidSkinLayout object.
     * As the image dimensions are not provided with this constructor, it is not possible
     * to have automatic zoom factor calculation. Using this constructor will set the
     * initial zoom policy to "100%"
     *
     * @param skin An skin bean containing the parameters used to place the
     *             widgets
     * @param isFlipSupported Flag that indicates if flip (not slide) is supported            
     */
    AndroidSkinLayout(AndroidSkinBean skin, boolean isFlipSupported)
    {
        this.skin = skin;

        this.isFlipSupported = isFlipSupported;
        openExternalDisplayAvailable = skin.isOpenExternalDisplayAvailable();
        externalDisplayAvailable = skin.isExternalDisplayAvailable();
    }

    /**
     * @see org.eclipse.swt.widgets.Layout#computeSize(Composite, int, int, boolean)
     */
    @Override
    protected Point computeSize(Composite composite, int wHint, int hHint, boolean flushCache)
    {
        if (!(composite instanceof SkinComposite))
        {
            throw new IllegalArgumentException();
        }

        Point size;

        // Retrieve needed data from composite
        IAndroidEmulatorInstance instance = UIHelper.getInstanceAssociatedToControl(composite);
        boolean flipSlideClosed = false;
        if (instance != null)
        {
            //flipSlideClosed = instance.isFlipSlideClosed();
            flipSlideClosed = false;
        }
        double zoomFactor = ((SkinComposite) composite).getZoomFactor();

        if (externalDisplayAvailable)
        {
            if (openExternalDisplayAvailable)
            {
                if (!flipSlideClosed)
                {
                    // Scenario 1:
                    //   A) Available displays: INTERNAL, OPEN EXTERNAL, EXTERNAL
                    //   B) Flip is open
                    //   C) Displays being showed: INTERNAL, OPEN EXTERNAL 
                    size = allAvailable(zoomFactor);
                }
                else
                {
                    // Scenario 2:
                    //   A) Available displays: INTERNAL, OPEN EXTERNAL, EXTERNAL
                    //   B) Flip is closed
                    //   C) Display being showed: EXTERNAL                 	
                    size = flipClosed(zoomFactor);
                }
            }
            else
            {
                if (!flipSlideClosed)
                {
                    // Scenario 3:
                    //   A) Available displays: INTERNAL, EXTERNAL
                    //   B) Flip is opened
                    //   C) Display being showed: INTERNAL                	
                    size = openExternalUnavailable(zoomFactor);
                }
                else
                {
                    // Scenario 4:
                    //   A) Available displays: INTERNAL, EXTERNAL
                    //   B) Flip is closed
                    //   C) Display being showed: EXTERNAL                	
                    size = openExternalUnavailableAndFlipClosed(zoomFactor);
                }
            }
        }
        else
        {
            // Scenario 5:
            //   A) Available display: INTERNAL
            //   B) Flip is opened or closed
            //   C) Display being showed: INTERNAL         	
            size = onlyInternal(zoomFactor);
        }

        return size;
    }

    /**
     * This method is called by computeSize when all displays are available and the flip is opened  
     * 
     * @param zoomFactor The zoom factor used to calculate the size
     * 
     * @return The size
     */
    private Point allAvailable(double zoomFactor)
    {
        Point size;

        int x1 = skin.getSkinPropertyValue(ISkinKeyXmlTags.SKIN_INTERNAL_VIEW_X);
        int y1 =
                Math.min(skin.getSkinPropertyValue(ISkinKeyXmlTags.SKIN_INTERNAL_VIEW_Y),
                        skin.getSkinPropertyValue(ISkinKeyXmlTags.SKIN_OPEN_EXTERNAL_VIEW_Y));
        int x2 =
                skin.getSkinPropertyValue(ISkinKeyXmlTags.SKIN_OPEN_EXTERNAL_VIEW_X)
                        + skin.getSkinPropertyValue(ISkinKeyXmlTags.SKIN_OPEN_EXTERNAL_VIEW_WIDTH);
        int y2 =
                Math.max(
                        skin.getSkinPropertyValue(ISkinKeyXmlTags.SKIN_INTERNAL_VIEW_Y)
                                + skin.getSkinPropertyValue(ISkinKeyXmlTags.SKIN_INTERNAL_VIEW_HEIGHT),
                        skin.getSkinPropertyValue(ISkinKeyXmlTags.SKIN_OPEN_EXTERNAL_VIEW_Y)
                                + skin.getSkinPropertyValue(ISkinKeyXmlTags.SKIN_OPEN_EXTERNAL_VIEW_HEIGHT));

        size = new Point((int) ((x2 - x1) * zoomFactor), (int) ((y2 - y1) * zoomFactor));

        return size;
    }

    /**
     * This method is called by computeSize when all displays are available and the flip is closed  
     * 
     * @param zoomFactor The zoom factor used to calculate the size
     * 
     * @return The size
     */
    private Point flipClosed(double zoomFactor)
    {
        Point size =
                new Point(
                        (int) (skin.getSkinPropertyValue(ISkinKeyXmlTags.SKIN_EXTERNAL_VIEW_WIDTH) * zoomFactor),
                        (int) (skin.getSkinPropertyValue(ISkinKeyXmlTags.SKIN_EXTERNAL_VIEW_HEIGHT) * zoomFactor));

        return size;
    }

    /**
     * This method is called by computeSize when all displays but open external display are available 
     * and the flip is opened  
     * 
     * @param zoomFactor The zoom factor used to calculate the size
     * 
     * @return The size
     */
    private Point openExternalUnavailable(double zoomFactor)
    {
        Point size =
                new Point(
                        (int) (skin.getSkinPropertyValue(ISkinKeyXmlTags.SKIN_INTERNAL_VIEW_WIDTH) * zoomFactor),
                        (int) (skin.getSkinPropertyValue(ISkinKeyXmlTags.SKIN_INTERNAL_VIEW_HEIGHT) * zoomFactor));

        return size;
    }

    /**
     * This method is called by computeSize when all displays but open external display are available 
     * and the flip is closed  
     * 
     * @param zoomFactor The zoom factor used to calculate the size
     * 
     * @return The size
     */
    private Point openExternalUnavailableAndFlipClosed(double zoomFactor)
    {
        Point size =
                new Point(
                        (int) (skin.getSkinPropertyValue(ISkinKeyXmlTags.SKIN_EXTERNAL_VIEW_WIDTH) * zoomFactor),
                        (int) (skin.getSkinPropertyValue(ISkinKeyXmlTags.SKIN_EXTERNAL_VIEW_HEIGHT) * zoomFactor));

        return size;
    }

    /**
     * This method is called by computeSize when only the internal display is available
     * 
     * @param zoomFactor The zoom factor used to calculate the size
     * 
     * @return The size
     */
    private Point onlyInternal(double zoomFactor)
    {
        Point size =
                new Point(
                        (int) (skin.getSkinPropertyValue(ISkinKeyXmlTags.SKIN_INTERNAL_VIEW_WIDTH) * zoomFactor),
                        (int) (skin.getSkinPropertyValue(ISkinKeyXmlTags.SKIN_INTERNAL_VIEW_HEIGHT) * zoomFactor));

        return size;
    }

    /**
     * @see org.eclipse.swt.widgets.Layout#layout(Composite, boolean)
     */
    @Override
    protected void layout(Composite composite, boolean flushCache)
    {
        if (!(composite instanceof SkinComposite))
        {
            // If this composite is not a SkinComposite, no layout should be done
            return;
        }

        boolean canProceed = true;

        if (flushCache || (mainDisplayChild == null) || (cliDisplayChild == null))
        {
            Control[] children = composite.getChildren();
            canProceed = checkChidren(children);
        }

        if (canProceed)
        {
            // Retrieve needed data from composite
            SkinComposite skinComposite = (SkinComposite) composite;
            IAndroidEmulatorInstance instance = UIHelper.getInstanceAssociatedToControl(composite);
            boolean flipSlideClosed = false;
            if (instance != null)
            {
                //flipSlideClosed = instance.isFlipSlideClosed();
                flipSlideClosed = false;
            }

            skinComposite.recalculateZoomFactor();
            skinComposite.updateDisplayRectangle();

            double zoomFactor = skinComposite.getZoomFactor();
            Rectangle displayRectangle = skinComposite.getDisplayRectangle();

            // Handling main display case
            if (mainDisplayChild != null)
            {
                layoutMainDisplay(zoomFactor, displayRectangle);

                if ((isFlipSupported) && (flipSlideClosed))
                {
                    // On phones that support flip, the main display is hidden
                    // when the flip is closed
                    mainDisplayChild.setVisible(false);
                }
                else
                {
                    mainDisplayChild.setVisible(true);
                }
            }

            // Handling CLI display case
            if (cliDisplayChild != null)
            {
                layoutCliDisplay(zoomFactor, displayRectangle, flipSlideClosed);

                if (((externalDisplayAvailable) && (!flipSlideClosed))
                        || ((openExternalDisplayAvailable) && (flipSlideClosed)))
                {
                    // The CLI display is shown in 2 situations: 
                    // 1. When the flip is closed and there is information about
                    // external display
                    // 2. When the flip is opened and there is information about
                    // open external display
                    cliDisplayChild.setVisible(true);
                }
                else
                {
                    cliDisplayChild.setVisible(false);
                }
            }
        }

    }

    /**
     * Checks if the composite children are as expected by the layout
     * It is needed to check if the composite's children are, at most:
     * 1. One main display
     * 2. One CLI display
     *
     * @param children Array of composite children to check
     *
     * @return true if children are as expected; false otherwise
     */
    private boolean checkChidren(Control[] children)
    {

        RemoteCLIDisplay cliDisplayInstance = null;
        SWTRemoteDisplay mainDisplayInstance = null;
        boolean childrenOk = true;

        // We need to check if the composite's children are, at most:
        // 
        // 1. One main display
        // 2. One CLI display
        for (Control child : children)
        {
            if (child instanceof SWTRemoteDisplay)
            {
                if (mainDisplayInstance == null)
                {
                    mainDisplayInstance = (SWTRemoteDisplay) child;
                    mainDisplayChild = mainDisplayInstance;
                }
                else
                {
                    childrenOk = false;

                    break;
                }
            }
            else if (child instanceof RemoteCLIDisplay)
            {
                if (cliDisplayInstance == null)
                {
                    cliDisplayInstance = (RemoteCLIDisplay) child;
                    cliDisplayChild = cliDisplayInstance;
                }
                else
                {
                    childrenOk = false;

                    break;
                }
            }
            else
            {
                childrenOk = false;

                break;
            }
        }

        return childrenOk;
    }

    /**
     * Performs the layout for main display
     * 
     * @param zoomFactor The zoom factor to use when dimensioning the main display.
     * @param displayRectangle The area of skin that is being shown at the view. It can
     *                         be only a part of the skin if the view client area
     *                         is smaller than the skin size. It is used to calculate translation.
     */
    private void layoutMainDisplay(double zoomFactor, Rectangle displayRectangle)
    {
        // Computes main display position
        int x =
                (int) (skin.getSkinPropertyValue(ISkinKeyXmlTags.SKIN_INTERNAL_VIEW_X) * zoomFactor)
                        - displayRectangle.x;
        int y =
                (int) (skin.getSkinPropertyValue(ISkinKeyXmlTags.SKIN_INTERNAL_VIEW_Y) * zoomFactor)
                        - displayRectangle.y;
        int width =
                (int) (skin.getSkinPropertyValue(ISkinKeyXmlTags.SKIN_INTERNAL_VIEW_WIDTH)
                        * zoomFactor * skin.getEmbeddedViewScale());
        int height =
                (int) (skin.getSkinPropertyValue(ISkinKeyXmlTags.SKIN_INTERNAL_VIEW_HEIGHT)
                        * zoomFactor * skin.getEmbeddedViewScale());

        // Sets main display size and position                 
        mainDisplayChild.setZoomFactor(zoomFactor * skin.getEmbeddedViewScale());
        mainDisplayChild.setBounds(x, y, width, height);
    }

    /**
     * Performs the layout for CLI display
     * 
     * @param zoomFactor The zoom factor to use when dimensioning the main display
     * @param displayRectangle The area of skin that is being shown at the view. It can
     *                         be only a part of the skin if the view client area
     *                         is smaller than the skin size. It is used to calculate translation.
     * @param isFlipSlideClosed True if the flip or slide is currently closed. False otherwise.                  
     */
    private void layoutCliDisplay(double zoomFactor, Rectangle displayRectangle,
            boolean flipSlideClosed)
    {
        // Computes CLI display position
        int x = 0;
        int y = 0;
        int width = 0;
        int height = 0;

        if (!flipSlideClosed && openExternalDisplayAvailable)
        {
            x =
                    (int) (skin.getSkinPropertyValue(ISkinKeyXmlTags.SKIN_OPEN_EXTERNAL_VIEW_X) * zoomFactor)
                            - displayRectangle.x;
            y =
                    (int) (skin.getSkinPropertyValue(ISkinKeyXmlTags.SKIN_OPEN_EXTERNAL_VIEW_Y) * zoomFactor)
                            - displayRectangle.y;
            width =
                    (int) (skin.getSkinPropertyValue(ISkinKeyXmlTags.SKIN_OPEN_EXTERNAL_VIEW_WIDTH)
                            * zoomFactor * IAndroidUIConstants.DISPLAY_TO_SKIN_RATIO);
            height =
                    (int) (skin
                            .getSkinPropertyValue(ISkinKeyXmlTags.SKIN_OPEN_EXTERNAL_VIEW_HEIGHT)
                            * zoomFactor * IAndroidUIConstants.DISPLAY_TO_SKIN_RATIO);
        }
        else if (flipSlideClosed && externalDisplayAvailable)
        {
            x =
                    (int) ((skin.getSkinPropertyValue(ISkinKeyXmlTags.SKIN_EXTERNAL_VIEW_X) * zoomFactor) - displayRectangle.x);
            y =
                    (int) ((skin.getSkinPropertyValue(ISkinKeyXmlTags.SKIN_EXTERNAL_VIEW_Y) * zoomFactor) - displayRectangle.y);
            width =
                    (int) (skin.getSkinPropertyValue(ISkinKeyXmlTags.SKIN_EXTERNAL_VIEW_WIDTH)
                            * zoomFactor * IAndroidUIConstants.DISPLAY_TO_SKIN_RATIO);
            height =
                    (int) (skin.getSkinPropertyValue(ISkinKeyXmlTags.SKIN_EXTERNAL_VIEW_HEIGHT)
                            * zoomFactor * IAndroidUIConstants.DISPLAY_TO_SKIN_RATIO);
        }

        // Sets cli display size                      
        cliDisplayChild.setZoomFactor(zoomFactor * IAndroidUIConstants.DISPLAY_TO_SKIN_RATIO);
        cliDisplayChild.setBounds(x, y, width, height);
    }

    void dispose()
    {
        cliDisplayChild = null;
        mainDisplayChild = null;
    }
}