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