1 /*
2  * Copyright (C) 2016 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.google.android.car.kitchensink.displayinfo;
17 
18 import android.annotation.Nullable;
19 import android.app.ActivityManager;
20 import android.content.Context;
21 import android.content.pm.ConfigurationInfo;
22 import android.content.res.Resources;
23 import android.graphics.Point;
24 import android.os.Bundle;
25 import android.util.DisplayMetrics;
26 import android.view.LayoutInflater;
27 import android.view.View;
28 import android.view.ViewGroup;
29 import android.widget.LinearLayout;
30 import android.widget.TextView;
31 
32 import androidx.fragment.app.Fragment;
33 
34 import com.google.android.car.kitchensink.R;
35 
36 /**
37  * Displays info about the display this is run on.
38  */
39 public class DisplayInfoFragment extends Fragment {
40 
41     private LinearLayout list;
42 
43     @Nullable
44     @Override
onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)45     public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
46             @Nullable Bundle savedInstanceState) {
47         View view = inflater.inflate(R.layout.display_info, container, false);
48 
49         list = (LinearLayout) view.findViewById(R.id.list);
50 
51         return view;
52     }
53 
54     @Override
onStart()55     public void onStart() {
56         super.onStart();
57         Point screenSize = new Point();
58         getActivity().getWindowManager().getDefaultDisplay().getSize(screenSize);
59         addTextView("window size(px): " + screenSize.x + " x " + screenSize.y);
60         addTextView("display density(dpi): " + getResources().getDisplayMetrics().densityDpi);
61         addTextView("display default density(dpi): "
62                 + getResources().getDisplayMetrics().DENSITY_DEFAULT);
63 
64         addTextView("======================================");
65 
66         ActivityManager am =
67                 (ActivityManager) getContext().getSystemService(Context.ACTIVITY_SERVICE);
68         ConfigurationInfo ci = am.getDeviceConfigurationInfo();
69         addTextView("OpenGL ES version: " + ci.getGlEsVersion());
70 
71         addTextView("======================================");
72         addTextView("All size are in DP.");
73         View rootView = getActivity().findViewById(android.R.id.content);
74         addTextView("view size: "
75                 + convertPixelsToDp(rootView.getWidth(), getContext())
76                 + " x " + convertPixelsToDp(rootView.getHeight(), getContext()));
77 
78         addTextView("window size: "
79                 + convertPixelsToDp(screenSize.x, getContext())
80                 + " x " + convertPixelsToDp(screenSize.y, getContext()));
81 
82         addDimenText("car_keyline_1");
83         addDimenText("car_keyline_2");
84         addDimenText("car_keyline_3");
85         addDimenText("car_keyline_4");
86         addDimenText("car_margin");
87         addDimenText("car_gutter_size");
88         addDimenText("car_primary_icon_size");
89         addDimenText("car_secondary_icon_size");
90         addDimenText("car_title_size");
91         addDimenText("car_title2_size");
92         addDimenText("car_headline1_size");
93         addDimenText("car_headline2_size");
94         addDimenText("car_headline3_size");
95         addDimenText("car_headline4_size");
96         addDimenText("car_body1_size");
97         addDimenText("car_body2_size");
98         addDimenText("car_body3_size");
99         addDimenText("car_body4_size");
100         addDimenText("car_body5_size");
101         addDimenText("car_action1_size");
102         addDimenText("car_touch_target_size");
103         addDimenText("car_action_bar_height");
104     }
105 
addDimenText(String dimenName)106     private void addDimenText(String dimenName) {
107         String value;
108         try {
109             float dimen = convertPixelsToDp(
110                     getResources().getDimensionPixelSize(
111                             getResources().getIdentifier(
112                                     dimenName, "dimen", getContext().getPackageName())),
113                     getContext());
114             value = Float.toString(dimen);
115         } catch (Resources.NotFoundException e) {
116             value = "Resource Not Found";
117         }
118         addTextView(dimenName + " : " + value);
119     }
120 
addTextView(String text)121     private void addTextView(String text) {
122         TextView textView = new TextView(getContext());
123         textView.setTextAppearance(R.style.TextAppearance_CarUi_Body2);
124         textView.setText(text);
125         list.addView(textView);
126     }
127 
convertPixelsToDp(float px, Context context)128     private static float convertPixelsToDp(float px, Context context){
129         Resources resources = context.getResources();
130         DisplayMetrics metrics = resources.getDisplayMetrics();
131         float dp = px / ((float) metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT);
132         return dp;
133     }
134 }
135