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