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.content.Context;
19 import android.content.res.Configuration;
20 import android.util.DisplayMetrics;
21 import android.view.Display;
22 import android.view.WindowManager;
23 
24 import com.android.compatibility.common.util.DeviceInfoStore;
25 
26 /**
27  * Screen device info collector.
28  */
29 public final class ScreenDeviceInfo extends DeviceInfo {
30 
31     @Override
collectDeviceInfo(DeviceInfoStore store)32     protected void collectDeviceInfo(DeviceInfoStore store) throws Exception {
33         DisplayMetrics metrics = new DisplayMetrics();
34         WindowManager windowManager =
35                 (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
36         Display display = windowManager.getDefaultDisplay();
37         display.getRealMetrics(metrics);
38 
39         store.addResult("width_pixels", metrics.widthPixels);
40         store.addResult("height_pixels", metrics.heightPixels);
41         store.addResult("x_dpi", metrics.xdpi);
42         store.addResult("y_dpi", metrics.ydpi);
43         store.addResult("density", metrics.density);
44         store.addResult("density_dpi", metrics.densityDpi);
45 
46         Configuration configuration = getContext().getResources().getConfiguration();
47         store.addResult("screen_size", getScreenSize(configuration));
48         store.addResult("smallest_screen_width_dp", configuration.smallestScreenWidthDp);
49     }
50 
getScreenSize(Configuration configuration)51     private static String getScreenSize(Configuration configuration) {
52         int screenLayout = configuration.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK;
53         String screenSize = String.format("0x%x", screenLayout);
54         switch (screenLayout) {
55             case Configuration.SCREENLAYOUT_SIZE_SMALL:
56                 screenSize = "small";
57                 break;
58 
59             case Configuration.SCREENLAYOUT_SIZE_NORMAL:
60                 screenSize = "normal";
61                 break;
62 
63             case Configuration.SCREENLAYOUT_SIZE_LARGE:
64                 screenSize = "large";
65                 break;
66 
67             case Configuration.SCREENLAYOUT_SIZE_XLARGE:
68                 screenSize = "xlarge";
69                 break;
70 
71             case Configuration.SCREENLAYOUT_SIZE_UNDEFINED:
72                 screenSize = "undefined";
73                 break;
74         }
75         return screenSize;
76     }
77 }
78