1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.compatibility.common.deviceinfo;
17 
18 import android.app.Activity;
19 import android.content.Context;
20 import android.content.res.Configuration;
21 import android.os.Bundle;
22 import android.server.wm.jetpack.utils.SidecarUtil;
23 import android.server.wm.jetpack.utils.ExtensionUtil;
24 import android.server.wm.jetpack.utils.Version;
25 import android.util.DisplayMetrics;
26 import android.view.Display;
27 import android.view.WindowManager;
28 
29 import com.android.compatibility.common.util.DeviceInfoStore;
30 import com.android.compatibility.common.util.DummyActivity;
31 
32 /**
33  * Screen device info collector.
34  */
35 public final class ScreenDeviceInfo extends DeviceInfo {
36 
37     @Override
collectDeviceInfo(DeviceInfoStore store)38     protected void collectDeviceInfo(DeviceInfoStore store) throws Exception {
39         DisplayMetrics metrics = new DisplayMetrics();
40         WindowManager windowManager =
41                 (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
42         Display display = windowManager.getDefaultDisplay();
43         display.getRealMetrics(metrics);
44 
45         store.addResult("width_pixels", metrics.widthPixels);
46         store.addResult("height_pixels", metrics.heightPixels);
47         store.addResult("x_dpi", metrics.xdpi);
48         store.addResult("y_dpi", metrics.ydpi);
49         store.addResult("density", metrics.density);
50         store.addResult("density_dpi", metrics.densityDpi);
51 
52         Configuration configuration = getContext().getResources().getConfiguration();
53         store.addResult("screen_size", getScreenSize(configuration));
54         store.addResult("smallest_screen_width_dp", configuration.smallestScreenWidthDp);
55 
56         // Add WindowManager Jetpack Library version and available display features.
57         addDisplayFeaturesIfPresent(store);
58     }
59 
addDisplayFeaturesIfPresent(DeviceInfoStore store)60     private void addDisplayFeaturesIfPresent(DeviceInfoStore store) throws Exception {
61         // Try to get display features from extensions. If extensions is not present, try sidecar.
62         // If neither is available, do nothing.
63         // TODO (b/202855636) store info from both extensions and sidecar if both are present
64         if (ExtensionUtil.isExtensionVersionValid()) {
65             // Extensions is available on device.
66             final Version extensionVersion = ExtensionUtil.getExtensionVersion();
67             store.addResult("wm_jetpack_version",
68                     "[Extensions]" + extensionVersion.toString());
69             final Activity activity = ScreenDeviceInfo.this.launchActivity(
70                     "com.android.compatibility.common.deviceinfo",
71                     DummyActivity.class,
72                     new Bundle());
73             int[] displayFeatureTypes = ExtensionUtil.getExtensionDisplayFeatureTypes(activity);
74             store.addArrayResult("display_features", displayFeatureTypes);
75         } else if (SidecarUtil.isSidecarVersionValid()) {
76             // Sidecar is available on device.
77             final Version sidecarVersion = SidecarUtil.getSidecarVersion();
78             store.addResult("wm_jetpack_version", "[Sidecar]" + sidecarVersion.toString());
79             final Activity activity = ScreenDeviceInfo.this.launchActivity(
80                     "com.android.compatibility.common.deviceinfo",
81                     DummyActivity.class,
82                     new Bundle());
83             int[] displayFeatureTypes = SidecarUtil.getSidecarDisplayFeatureTypes(activity);
84             store.addArrayResult("display_features", displayFeatureTypes);
85         }
86     }
87 
getScreenSize(Configuration configuration)88     private static String getScreenSize(Configuration configuration) {
89         int screenLayout = configuration.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK;
90         String screenSize = String.format("0x%x", screenLayout);
91         switch (screenLayout) {
92             case Configuration.SCREENLAYOUT_SIZE_SMALL:
93                 screenSize = "small";
94                 break;
95 
96             case Configuration.SCREENLAYOUT_SIZE_NORMAL:
97                 screenSize = "normal";
98                 break;
99 
100             case Configuration.SCREENLAYOUT_SIZE_LARGE:
101                 screenSize = "large";
102                 break;
103 
104             case Configuration.SCREENLAYOUT_SIZE_XLARGE:
105                 screenSize = "xlarge";
106                 break;
107 
108             case Configuration.SCREENLAYOUT_SIZE_UNDEFINED:
109                 screenSize = "undefined";
110                 break;
111         }
112         return screenSize;
113     }
114 }
115