summaryrefslogtreecommitdiff
path: root/tests/src/com/android/launcher3/deviceemulator/models/DeviceEmulationData.java
blob: 36235134f5eddbf77b704dcd4e6f1b6af02fe5d1 (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
/*
 * Copyright (C) 2022 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.launcher3.deviceemulator.models;

import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation;

import static com.android.launcher3.ResourceUtils.NAVBAR_HEIGHT;
import static com.android.launcher3.ResourceUtils.NAVBAR_HEIGHT_LANDSCAPE;
import static com.android.launcher3.ResourceUtils.NAVBAR_LANDSCAPE_LEFT_RIGHT_SIZE;
import static com.android.launcher3.ResourceUtils.getDimenByName;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Rect;
import android.os.Build;
import android.util.ArrayMap;

import com.android.launcher3.InvariantDeviceProfile;
import com.android.launcher3.util.DisplayController;
import com.android.launcher3.util.IOUtils;
import com.android.launcher3.util.IntArray;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.InputStream;
import java.util.Arrays;
import java.util.Map;

public class DeviceEmulationData {

    public final int width;
    public final int height;
    public final int density;
    public final String name;
    public final String[] grids;
    public final Rect cutout;
    public final Map<String, Integer> resourceOverrides;

    private static final String[] EMULATED_SYSTEM_RESOURCES = new String[]{
            NAVBAR_HEIGHT,
            NAVBAR_HEIGHT_LANDSCAPE,
            NAVBAR_LANDSCAPE_LEFT_RIGHT_SIZE,
            "status_bar_height",
    };

    public DeviceEmulationData(int width, int height, int density, Rect cutout, String name,
            String[] grid,
            Map<String, Integer> resourceOverrides) {
        this.width = width;
        this.height = height;
        this.density = density;
        this.name = name;
        this.grids = grid;
        this.cutout = cutout;
        this.resourceOverrides = resourceOverrides;
    }

    public static DeviceEmulationData deviceFromJSON(JSONObject json) throws JSONException {
        int width = json.getInt("width");
        int height = json.getInt("height");
        int density = json.getInt("density");
        String name = json.getString("name");

        JSONArray gridArray = json.getJSONArray("grids");
        String[] grids = new String[gridArray.length()];
        for (int i = 0, count = grids.length; i < count; i++) {
            grids[i] = gridArray.getString(i);
        }

        IntArray deviceCutout = IntArray.fromConcatString(json.getString("cutout"));
        Rect cutout = new Rect(deviceCutout.get(0), deviceCutout.get(1), deviceCutout.get(2),
                deviceCutout.get(3));


        JSONObject resourceOverridesJson = json.getJSONObject("resourceOverrides");
        Map<String, Integer> resourceOverrides = new ArrayMap<>();
        for (String key : resourceOverridesJson.keySet()) {
            resourceOverrides.put(key, resourceOverridesJson.getInt(key));
        }
        return new DeviceEmulationData(width, height, density, cutout, name, grids,
                resourceOverrides);
    }

    @Override
    public String toString() {
        JSONObject json = new JSONObject();
        try {
            json.put("width", width);
            json.put("height", height);
            json.put("density", density);
            json.put("name", name);
            json.put("cutout", IntArray.wrap(
                    cutout.left, cutout.top, cutout.right, cutout.bottom).toConcatString());

            JSONArray gridArray = new JSONArray();
            Arrays.stream(grids).forEach(gridArray::put);
            json.put("grids", gridArray);


            JSONObject resourceOverrides = new JSONObject();
            for (Map.Entry<String, Integer> e : this.resourceOverrides.entrySet()) {
                resourceOverrides.put(e.getKey(), e.getValue());
            }
            json.put("resourceOverrides", resourceOverrides);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json.toString();
    }

    public static DeviceEmulationData getCurrentDeviceData(Context context) {
        DisplayController.Info info = DisplayController.INSTANCE.get(context).getInfo();
        String[] grids = InvariantDeviceProfile.INSTANCE.get(context)
                .parseAllGridOptions(context).stream()
                .map(go -> go.name).toArray(String[]::new);
        String code = Build.MODEL.replaceAll("\\s", "").toLowerCase();

        Map<String, Integer> resourceOverrides = new ArrayMap<>();
        for (String s : EMULATED_SYSTEM_RESOURCES) {
            resourceOverrides.put(s, getDimenByName(s, context.getResources(), 0));
        }
        return new DeviceEmulationData(info.currentSize.x, info.currentSize.y,
                info.densityDpi, info.cutout, code, grids, resourceOverrides);
    }

    public static DeviceEmulationData getDevice(String deviceCode) throws Exception {
        return DeviceEmulationData.deviceFromJSON(readJSON().getJSONObject(deviceCode));
    }

    private static JSONObject readJSON() throws Exception {
        Context context = getInstrumentation().getContext();
        Resources myRes = context.getResources();
        int resId = myRes.getIdentifier("devices", "raw", context.getPackageName());
        try (InputStream is = myRes.openRawResource(resId)) {
            return new JSONObject(new String(IOUtils.toByteArray(is)));
        }
    }

}