11package com .mylhyl .circledialog ;
22
3- import android .annotation .TargetApi ;
43import android .app .Activity ;
5- import android .content .Context ;
64import android .content .res .Configuration ;
75import android .content .res .Resources ;
8- import android .os .Build ;
96import android .util .DisplayMetrics ;
10- import android .util .TypedValue ;
11- import android .view .ViewConfiguration ;
12-
13- import java .lang .reflect .Method ;
147
158final class SystemBarConfig {
169
1710 private static final String STATUS_BAR_HEIGHT_RES_NAME = "status_bar_height" ;
18- private static final String NAV_BAR_HEIGHT_RES_NAME = "navigation_bar_height" ;
19- private static final String NAV_BAR_HEIGHT_LANDSCAPE_RES_NAME = "navigation_bar_height_landscape" ;
20- private static final String SHOW_NAV_BAR_RES_NAME = "config_showNavigationBar" ;
2111
2212 private final int mStatusBarHeight ;
23- private final int mActionBarHeight ;
24- private final boolean mHasNavigationBar ;
25- private final int mNavigationBarHeight ;
26- private final boolean mInPortrait ;
2713 private final int mScreenWidth ;
2814 private final int mScreenHeight ;
2915
3016 public SystemBarConfig (Activity activity ) {
3117 Resources res = activity .getResources ();
32- mInPortrait = (res .getConfiguration ().orientation == Configuration .ORIENTATION_PORTRAIT );
3318 mStatusBarHeight = getInternalDimensionSize (res , STATUS_BAR_HEIGHT_RES_NAME );
34- mActionBarHeight = getActionBarHeight (activity );
35- mNavigationBarHeight = getNavigationBarHeight (activity );
36- mHasNavigationBar = (mNavigationBarHeight > 0 );
3719
3820 DisplayMetrics metrics = new DisplayMetrics ();
3921 activity .getWindowManager ().getDefaultDisplay ().getMetrics (metrics );
@@ -51,77 +33,6 @@ private int getInternalDimensionSize(Resources res, String key) {
5133 return result ;
5234 }
5335
54- //通过此方法获取action bar的高度
55- @ TargetApi (14 )
56- private int getActionBarHeight (Context context ) {
57- int result = 0 ;
58- if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .ICE_CREAM_SANDWICH ) {
59- TypedValue tv = new TypedValue ();
60- context .getTheme ().resolveAttribute (android .R .attr .actionBarSize , tv , true );
61- result = TypedValue .complexToDimensionPixelSize (tv .data , context .getResources ().getDisplayMetrics ());
62- }
63- return result ;
64- }
65-
66- //通过此方法获取navigation bar的高度
67- @ TargetApi (14 )
68- public int getNavigationBarHeight (Context context ) {
69- Resources res = context .getResources ();
70- int result = 0 ;
71- if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .ICE_CREAM_SANDWICH ) {
72- if (hasNavBar (context )) {
73- String key ;
74- if (mInPortrait ) {
75- key = NAV_BAR_HEIGHT_RES_NAME ;
76- } else {
77- key = NAV_BAR_HEIGHT_LANDSCAPE_RES_NAME ;
78- }
79- return getInternalDimensionSize (res , key );
80- }
81- }
82- return result ;
83- }
84-
85- //通过此方法判断是否存在navigation bar
86- @ TargetApi (14 )
87- private boolean hasNavBar (Context context ) {
88- Resources res = context .getResources ();
89- int resourceId = res .getIdentifier (SHOW_NAV_BAR_RES_NAME , "bool" , "android" );
90- if (resourceId != 0 ) {
91- boolean hasNav = res .getBoolean (resourceId );
92- // 查看是否有通过系统属性来控制navigation bar。
93- if ("1" .equals (getNavBarOverride ())) {
94- hasNav = false ;
95- } else if ("0" .equals (getNavBarOverride ())) {
96- hasNav = true ;
97- }
98- return hasNav ;
99- } else {
100- //可通过此方法来查看设备是否存在物理按键(menu,back,home键)。
101- return !ViewConfiguration .get (context ).hasPermanentMenuKey ();
102- }
103- }
104-
105- // 安卓系统允许修改系统的属性来控制navigation bar的显示和隐藏,此方法用来判断是否有修改过相关属性。
106- // (修改系统文件,在build.prop最后加入qemu.hw.mainkeys=1即可隐藏navigation bar)
107- // 相关属性模拟器中有使用。
108- // 当返回值等于"1"表示隐藏navigation bar,等于"0"表示显示navigation bar。
109- @ TargetApi (19 )
110- private String getNavBarOverride () {
111- String isNavBarOverride = null ;
112- if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .KITKAT ) {
113- try {
114- Class c = Class .forName ("android.os.SystemProperties" );
115- Method m = c .getDeclaredMethod ("get" , String .class );
116- m .setAccessible (true );
117- isNavBarOverride = (String ) m .invoke (null , "qemu.hw.mainkeys" );
118- } catch (Throwable e ) {
119- isNavBarOverride = null ;
120- }
121- }
122- return isNavBarOverride ;
123- }
124-
12536 public int [] getScreenSize () {
12637 return new int []{mScreenWidth , mScreenHeight };
12738 }
@@ -143,31 +54,4 @@ public int getStatusBarHeight() {
14354 return mStatusBarHeight ;
14455 }
14556
146- /**
147- * 获取action bar的高度
148- *
149- * @return action bar高度的像素值
150- */
151- public int getActionBarHeight () {
152- return mActionBarHeight ;
153- }
154-
155- /**
156- * 判断此设备是否有navigation bar虚拟按键栏
157- *
158- * @return true表示有,false表示无
159- */
160- public boolean hasNavigtionBar () {
161- return mHasNavigationBar ;
162- }
163-
164- /**
165- * 获取navigation bar虚拟按键栏的高度
166- *
167- * @return 返回navigation bar虚拟按键栏的高度的像素值,如果设备没有navigation bar虚拟按键栏则返回0
168- */
169- public int getNavigationBarHeight () {
170- return mNavigationBarHeight ;
171- }
172-
17357}
0 commit comments