summaryrefslogtreecommitdiff
path: root/sdk-common/src/main/java/com/android/ide/common/rendering/HardwareConfigHelper.java
blob: bcc2f39108691aab3c65ab06fba23c20f4e047b2 (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
/*
 * 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.android.ide.common.rendering;

import static com.android.ide.common.rendering.api.HardwareConfig.*;

import com.android.annotations.NonNull;
import com.android.annotations.Nullable;
import com.android.ide.common.rendering.api.HardwareConfig;
import com.android.resources.ScreenOrientation;
import com.android.sdklib.devices.ButtonType;
import com.android.sdklib.devices.Device;
import com.android.sdklib.devices.Screen;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Helper method to create a {@link HardwareConfig} object.
 *
 * The base data comes from a {@link Device} object, with additional data provided on the helper
 * object.
 *
 * Since {@link HardwareConfig} is immutable, this allows creating one in several (optional)
 * steps more easily.
 *
 */
public class HardwareConfigHelper {

    @NonNull
    private final Device mDevice;
    @NonNull
    private ScreenOrientation mScreenOrientation = ScreenOrientation.PORTRAIT;

    // optional
    private int mMaxRenderWidth = -1;
    private int mMaxRenderHeight = -1;
    private int mOverrideRenderWidth = -1;
    private int mOverrideRenderHeight = -1;

    /**
     * Creates a new helper for a given device.
     * @param device the device to provide the base data.
     */
    public HardwareConfigHelper(@NonNull Device device) {
        mDevice = device;
    }

    /**
     * Sets the orientation of the config.
     * @param screenOrientation the orientation.
     * @return this (such that chains of setters can be stringed together)
     */
    @NonNull
    public HardwareConfigHelper setOrientation(@NonNull ScreenOrientation screenOrientation) {
        mScreenOrientation = screenOrientation;
        return this;
    }

    /**
     * Overrides the width and height to be used during rendering.
     *
     * A value of -1 will make the rendering use the normal width and height coming from the
     * {@link Device} object.
     *
     * @param overrideRenderWidth the width in pixels of the layout to be rendered
     * @param overrideRenderHeight the height in pixels of the layout to be rendered
     * @return this (such that chains of setters can be stringed together)
     */
    @NonNull
    public HardwareConfigHelper setOverrideRenderSize(int overrideRenderWidth,
            int overrideRenderHeight) {
        mOverrideRenderWidth = overrideRenderWidth;
        mOverrideRenderHeight = overrideRenderHeight;
        return this;
    }

    /**
     * Sets the max width and height to be used during rendering.
     *
     * A value of -1 will make the rendering use the normal width and height coming from the
     * {@link Device} object.
     *
     * @param maxRenderWidth the max width in pixels of the layout to be rendered
     * @param maxRenderHeight the max height in pixels of the layout to be rendered
     * @return this (such that chains of setters can be stringed together)
     */
    @NonNull
    public HardwareConfigHelper setMaxRenderSize(int maxRenderWidth, int maxRenderHeight) {
        mMaxRenderWidth = maxRenderWidth;
        mMaxRenderHeight = maxRenderHeight;
        return this;
    }

    /**
     * Creates and returns the HardwareConfig object.
     * @return the config
     */
    @SuppressWarnings("SuspiciousNameCombination") // Deliberately swapping orientations
    @NonNull
    public HardwareConfig getConfig() {
        Screen screen = mDevice.getDefaultHardware().getScreen();

        // compute width and height to take orientation into account.
        int x = screen.getXDimension();
        int y = screen.getYDimension();
        int width, height;

        if (x > y) {
            if (mScreenOrientation == ScreenOrientation.LANDSCAPE) {
                width = x;
                height = y;
            } else {
                width = y;
                height = x;
            }
        } else {
            if (mScreenOrientation == ScreenOrientation.LANDSCAPE) {
                width = y;
                height = x;
            } else {
                width = x;
                height = y;
            }
        }

        if (mOverrideRenderHeight != -1) {
            width = mOverrideRenderWidth;
        }

        if (mOverrideRenderHeight != -1) {
            height = mOverrideRenderHeight;
        }

        if (mMaxRenderWidth != -1) {
            width = mMaxRenderWidth;
        }

        if (mMaxRenderHeight != -1) {
            height = mMaxRenderHeight;
        }

        return new HardwareConfig(
                width,
                height,
                screen.getPixelDensity(),
                (float) screen.getXdpi(),
                (float) screen.getYdpi(),
                screen.getSize(),
                mScreenOrientation,
                mDevice.getDefaultHardware().getScreen().getScreenRound(),
                mDevice.getDefaultHardware().getButtonType() == ButtonType.SOFT);
    }

    // ---- Device Display Helpers ----

    private static final Pattern GENERIC_PATTERN =
            Pattern.compile("(\\d+\\.?\\d*)\" (.+?)( \\(.*Nexus.*\\))?"); //$NON-NLS-1$

    /**
     * Returns a user-displayable description of the given Nexus device
     * @param device the device to check
     * @return the label
     * @see #isNexus(com.android.sdklib.devices.Device)
     */
    @NonNull
    public static String getNexusLabel(@NonNull Device device) {
        String name = device.getDisplayName();
        Screen screen = device.getDefaultHardware().getScreen();
        float length = (float) screen.getDiagonalLength();
        // Round dimensions to the nearest tenth
        length = Math.round(10 * length) / 10.0f;
        return String.format(Locale.US, "%1$s (%3$s\", %2$s)",
                name, getResolutionString(device), Float.toString(length));
    }

    /**
     * Returns a user-displayable description of the given Nexus device.
     * Optimized for display in menus where multiple other devices are also
     * listed.
     *
     * @param device the device to check
     * @return the label
     * @see #isNexus(com.android.sdklib.devices.Device)
     */
    @NonNull
    public static String getNexusMenuLabel(@NonNull Device device) {
        String name = device.getDisplayName();
        if (isTv(device)) {
            // Example: 1080p, 1920x1080, xhdpi (TV)
            if (name.startsWith("Television (")) {
                name = name.substring("Television (".length());
                if (name.endsWith(")")) {
                    name = name.substring(0, name.length() - 1);
                }
            }
            return String.format(Locale.US, "%1$s, %2$s (TV)", name, getResolutionString(device));
        } else if (isWear(device)) {
            // Example: 320x200, tvpdi (Round Chin)
            if (name.startsWith("Android Wear ")) {
                name = name.substring("Android Wear ".length());
            } else if (name.startsWith("Wear OS ")) {
                name = name.substring("Wear OS ".length());
            }
            return String.format(Locale.US, "%1$s (%2$s)", getResolutionString(device), name);
        } else {
            // Example: 5.2", 1920x1080, 420dpi (Nexus 5X)
            if (name.endsWith(")")) {
                // Drop parentheses, e.g. Nexus 7 (2012) to Nexus 7 2012
                name = name.replace("(", "").replace(")", "");
            }
            Screen screen = device.getDefaultHardware().getScreen();
            float length = (float) screen.getDiagonalLength();
            // Round dimensions to the nearest tenth
            length = Math.round(10 * length) / 10.0f;
            return String.format(Locale.US, "%1$s, %2$s (%3$s)", Float.toString(length),
                                 getResolutionString(device), name);
        }
    }

    /**
     * Returns a user-displayable description of the given generic device
     * @param device the device to check
     * @return the label
     * @see #isGeneric(Device)
     */
    @NonNull
    public static String getGenericLabel(@NonNull Device device) {
        // * Use the same precision for all devices (all but one specify decimals)
        // * Add some leading space such that the dot ends up roughly in the
        //   same space
        // * Add in screen resolution and density
        String name = device.getDisplayName();
        Matcher matcher = GENERIC_PATTERN.matcher(name);
        if (matcher.matches()) {
            String size = matcher.group(1);
            String n = matcher.group(2);
            int dot = size.indexOf('.');
            if (dot == -1) {
                size += ".0";
                dot = size.length() - 2;
            }
            for (int i = 0; i < 2 - dot; i++) {
                size = ' ' + size;
            }
            name = size + "\" " + n;
        }

        return String.format(Locale.US, "%1$s (%2$s)", name,
                getResolutionString(device));
    }

    /**
     * Returns a user displayable screen resolution string for the given device
     * @param device the device to look up the string for
     * @return a user displayable string
     */
    @NonNull
    public static String getResolutionString(@NonNull Device device) {
        Screen screen = device.getDefaultHardware().getScreen();
        return String.format(Locale.US,
                "%1$d \u00D7 %2$d, %3$s", // U+00D7: Unicode multiplication sign
                screen.getXDimension(),
                screen.getYDimension(),
                screen.getPixelDensity().getResourceValue());
    }

    /**
     * Returns true if the given device is a generic device
     * @param device the device to check
     * @return true if the device is generic
     */
    public static boolean isGeneric(@NonNull Device device) {
        return MANUFACTURER_GENERIC.equals(device.getManufacturer());
    }

    /**
     * Returns true if the given device is a Nexus device
     * @param device the device to check
     * @return true if the device is a Nexus
     */
    public static boolean isNexus(@NonNull Device device) {
        return MANUFACTURER_GOOGLE.equals(device.getManufacturer());
    }

    /**
     * Whether the given device is a wear device
     */
    public static boolean isWear(@Nullable Device device) {
        return device != null && "android-wear".equals(device.getTagId());
    }

    /**
     * Whether the given device is an Android Things device
     */
    public static boolean isThings(@Nullable Device device) {
        return device != null && "android-things".equals(device.getTagId());
    }

    /**
     * Whether the given device is a TV device
     */
    public static boolean isTv(@Nullable Device device) {
        return device != null
                && ("android-tv".equals(device.getTagId())
                        || "google-tv".equals(device.getTagId()));
    }

    /** Whether the given device is an Automotive device */
    public static boolean isAutomotive(@Nullable Device device) {
        return device != null
                && ("android-automotive".equals(device.getTagId())
                        || "android-automotive-playstore".equals(device.getTagId()));
    }

    /** Whether the given device is a PC device */
    public static boolean isDesktop(@Nullable Device device) {
        return device != null && "android-desktop".equals(device.getTagId());
    }

    /**
     * Whether the given device appears to be a mobile device (e.g. not wear, tv, auto, etc)
     */
    public static boolean isMobile(@Nullable Device device) {
        return !isTv(device)
                && !isWear(device)
                && !isThings(device)
                && !isAutomotive(device)
                && !isDesktop(device);
    }

    /**
     * Returns the rank of the given nexus device. This can be used to order
     * the devices chronologically.
     *
     * @param device the device to look up the rank for
     * @return the rank of the device
     */
    public static int nexusRank(Device device) {
        String id = device.getId();
        switch (id) {
            case DEVICE_NEXUS_ONE:
                return 1;
            case DEVICE_NEXUS_S:
                return 2;
            case DEVICE_GALAXY_NEXUS:
                return 3;
            case DEVICE_NEXUS_7:
                return 4; // 2012 version
            case DEVICE_NEXUS_10:
                return 5;
            case DEVICE_NEXUS_4:
                return 6;
            case DEVICE_NEXUS_7_2013:
                return 7;
            case DEVICE_NEXUS_5:
                return 8;
            case DEVICE_NEXUS_9:
                return 9;
            case DEVICE_NEXUS_6:
                return 10;
            case DEVICE_PIXEL_C:
                return 11;
            case DEVICE_NEXUS_6P:
                return 12;
            case DEVICE_NEXUS_5X:
                return 13;
            case DEVICE_PIXEL:
                return 14;
            case DEVICE_PIXEL_XL:
                return 15;
            default:
                return 100; // devices released in the future?
        }
    }

    /**
     * Sorts the given list of Nexus devices according to rank
     * @param list the list to sort
     */
    public static void sortNexusListByRank(@NonNull List<Device> list) {
        Collections.sort(list, (device1, device2) -> {
            // Sort by screen size
            // Descending order of age
            return nexusRank(device2) - nexusRank(device1);
        });
    }

    /**
     * Sorts the given list of Nexus devices according to rank
     * @param list the list to sort
     */
    public static void sortDevicesByScreenSize(@NonNull List<Device> list) {
        Collections.sort(list, (device1, device2) -> {

            Screen screen1 = device1.getDefaultHardware().getScreen();
            float length1 = (float) screen1.getDiagonalLength();

            Screen screen2 = device2.getDefaultHardware().getScreen();
            float length2 = (float) screen2.getDiagonalLength();

            return (int) Math.signum(length1 - length2);
        });
    }
}