content: /*
   * Licensed to the Apache Software Foundation (ASF) under one
   * or more contributor license agreements.  See the NOTICE file
   * distributed with this work for additional information
   * regarding copyright ownership.  The ASF licenses this file
   * to you under the Apache License, Version 2.0 (the
   * "License"); you may not use this file except in compliance
   * with the License.  You may obtain a copy of the License at
   * 
   *   http://www.apache.org/licenses/LICENSE-2.0
   * 
   * Unless required by applicable law or agreed to in writing,
   * software distributed under the License is distributed on an
   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   * KIND, either express or implied.  See the License for the
   * specific language governing permissions and limitations
   * under the License.
   */
  package org.apache.weex.commons.util;
  
  import android.content.res.TypedArray;
  import android.graphics.Point;
  import android.os.Build;
  import android.support.v7.app.ActionBar;
  import android.support.v7.app.AppCompatActivity;
  import android.util.Log;
  
  import java.lang.reflect.Field;
  import java.lang.reflect.Method;
  
  public class ScreenUtil {
      private static final String TAG = "WXTBUtil";
  
      private static boolean isSupportSmartBar = false;
  
      static {
          isSupportSmartBar = isSupportSmartBar();
      }
      public static int getDisplayWidth(AppCompatActivity activity){
          int width=0;
          if (activity != null && activity.getWindowManager() != null && activity.getWindowManager().getDefaultDisplay() != null) {
              Point point=new Point();
              activity.getWindowManager().getDefaultDisplay().getSize(point);
              width = point.x;
          }
          return width;
      }
  
      public static int getDisplayHeight(AppCompatActivity activity) {
          int height = 0;
          if (activity != null && activity.getWindowManager() != null && activity.getWindowManager().getDefaultDisplay() != null) {
              Point point=new Point();
              activity.getWindowManager().getDefaultDisplay().getSize(point);
              height=point.y;
          }
  
          Log.e(TAG, "isSupportSmartBar:" + isSupportSmartBar);
  
          if (isSupportSmartBar) {
              int smartBarHeight = getSmartBarHeight(activity);
              Log.e(TAG, "smartBarHeight:" + smartBarHeight);
              height -= smartBarHeight;
          }
  
          if (activity.getSupportActionBar() != null) {
            int actionbar= activity.getSupportActionBar().getHeight();
            if(actionbar==0){
              TypedArray actionbarSizeTypedArray=activity.obtainStyledAttributes(new int[]{android.R.attr.actionBarSize});
              actionbar= (int) actionbarSizeTypedArray.getDimension(0,0);
            }
            Log.d(TAG, "actionbar:" + actionbar);
            height -= actionbar;
          }
  
          int status = getStatusBarHeight(activity);
          Log.d(TAG, "status:" + status);
  
          height -= status;
  
          Log.d(TAG,"height:"+height);
          return height;
      }
  
      private static int getStatusBarHeight(AppCompatActivity activity) {
          Class<?> c;
          Object obj;
          Field field;
          int x;
          int statusBarHeight = 0;
          try {
              c = Class.forName("com.android.internal.R$dimen");
              obj = c.newInstance();
              field = c.getField("status_bar_height");
              x = Integer.parseInt(field.get(obj).toString());
              statusBarHeight = activity.getResources().getDimensionPixelSize(x);
          } catch (Exception e1) {
              e1.printStackTrace();
          }
          return statusBarHeight;
      }
  
      private static int getSmartBarHeight(AppCompatActivity activity) {
          ActionBar actionbar = activity.getSupportActionBar();
          if (actionbar != null)
              try {
                  Class c = Class.forName("com.android.internal.R$dimen");
                  Object obj = c.newInstance();
                  Field field = c.getField("mz_action_button_min_height");
                  int height = Integer.parseInt(field.get(obj).toString());
                  return activity.getResources().getDimensionPixelSize(height);
              } catch (Exception e) {
                  e.printStackTrace();
                  actionbar.getHeight();
              }
          return 0;
      }
  
      private static boolean isSupportSmartBar() {
          try {
              final Method method = Build.class.getMethod("hasSmartBar");
              return method != null;
          } catch (final Exception e) {
              return false;
          }
      }
  }
  