Skip to content

Commit

Permalink
fixed #186 #167
Browse files Browse the repository at this point in the history
  • Loading branch information
razerdp committed May 17, 2019
1 parent 2293283 commit 2106446
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 16 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,12 @@ QuickPopupBuilder支持链式调用生成一个基于QuickPopup的PopupWindow,
### 更新日志 ([历史更新](https://github.com/razerdp/BasePopup/blob/master/UpdateLog.md))

* **【Candy】2.2.1**(2019/05/16)
* 支持Service或者非ActivityContext里弹窗
* **【Candy】190516**
* 支持Service或者非ActivityContext里弹窗
* **【Candy】190517**
* 优化PopupUiUtils,优化获取屏幕宽高算法
* fixed [**#186**](https://github.com/razerdp/BasePopup/issues/186)[**#167**](https://github.com/razerdp/BasePopup/issues/167)


* **【Release】2.2.0**(2019/05/15)
* 正式版2.2.0隆重归来,这次正式版又是一个重构版本哦~
Expand Down
6 changes: 4 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@
package="razerdp.basepopup">

<application
android:name="razerdp.demo.app.PopupDemoApp"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:name="razerdp.demo.app.PopupDemoApp"
android:theme="@style/AppTheme"
tools:ignore="GoogleAppIndexingWarning">
<activity
android:name="razerdp.demo.DemoActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="@string/app_name"
android:windowSoftInputMode="adjustPan|stateHidden">
<intent-filter>
Expand All @@ -37,7 +38,8 @@
android:name="razerdp.demo.DemoDialogFragmentActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:windowSoftInputMode="adjustPan|stateHidden" />
<service android:name="razerdp.demo.services.DemoService"/>

<service android:name="razerdp.demo.services.DemoService" />
</application>

</manifest>
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ ext {
compileSdkVersion = 28
minSdkVersion = 16
targetSdkVersion = 28
versionCode = 60
versionName = '2.2.1.190516'
versionCode = 61
versionName = '2.2.1.190517'

candy = true
group = 'com.github.razerdp'
Expand Down
84 changes: 73 additions & 11 deletions lib/src/main/java/razerdp/util/PopupUiUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public class PopupUiUtils {
private static final int LANDSCAPE = 1;
private volatile static Point[] mRealSizes = new Point[2];
private static final Point point = new Point();
private static Rect navigationBarRect = new Rect();


public static int getNavigationBarHeight(Context context) {
if (!checkHasNavigationBar(context)) return 0;
Expand All @@ -47,23 +49,38 @@ private static int getNavigationBarHeightInternal(Context context) {
*/
@SuppressLint("NewApi")
public static boolean checkHasNavigationBar(Context context) {
navigationBarRect.setEmpty();
Activity act = PopupUtils.scanForActivity(context, 15);
if (act == null) return false;
Configuration conf = context.getResources().getConfiguration();
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
Window window = act.getWindow();
if (window == null) return false;
Display display = window.getWindowManager().getDefaultDisplay();
display.getRealSize(point);

View decorView = window.getDecorView();
Configuration conf = context.getResources().getConfiguration();
if (Configuration.ORIENTATION_LANDSCAPE == conf.orientation) {
View contentView = decorView.findViewById(android.R.id.content);
return (point.x != contentView.getWidth());
boolean result = point.x != contentView.getWidth();
if (result) {
if (decorView.getLeft() > 0) {
//on left
navigationBarRect.set(1, 0, 0, 0);
} else {
//on right
navigationBarRect.set(0, 0, 1, 0);
}
}
return result;
} else {
Rect rect = new Rect();
decorView.getWindowVisibleDisplayFrame(rect);
return (rect.bottom != point.y);
decorView.getWindowVisibleDisplayFrame(navigationBarRect);
boolean result = navigationBarRect.bottom != point.y;
navigationBarRect.setEmpty();
if (result) {
navigationBarRect.set(0, 0, 0, 1);
}
return result;
}
} else {
ViewGroup decorView = (ViewGroup) act.getWindow().getDecorView();
Expand All @@ -80,6 +97,25 @@ public static boolean checkHasNavigationBar(Context context) {
if (!TextUtils.isEmpty(resourceEntryName) && child.getId() != View.NO_ID && child.isShown()) {
if (TextUtils.equals("navigationbarbackground", resourceEntryName.toLowerCase()) ||
TextUtils.equals("immersion_navigation_bar_view", resourceEntryName.toLowerCase())) {
if (conf.orientation == Configuration.ORIENTATION_LANDSCAPE) {
//横屏
if (child.getWidth() > child.getHeight()) {
//bottom
navigationBarRect.set(0, 0, 0, 1);
//navigation never on top
} else {
//left or right
if (child.getLeft() == 0) {
navigationBarRect.set(1, 0, 0, 0);
} else {
navigationBarRect.set(0, 0, 1, 0);
}
}
} else {
//bottom
navigationBarRect.set(0, 0, 0, 1);
//navigation never on top
}
return true;
}
}
Expand All @@ -91,39 +127,65 @@ public static boolean checkHasNavigationBar(Context context) {
public static int getScreenHeightCompat(Context context) {
int orientation = context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT ? PORTRAIT : LANDSCAPE;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
return context.getResources().getDisplayMetrics().heightPixels + (orientation == PORTRAIT ? (!checkHasNavigationBar(context) ? getNavigationBarHeightInternal(context) : 0) : 0);
//如果没有navigation,则需要加上,因为displayMetrics不包含navigation
return context.getResources().getDisplayMetrics().heightPixels + (orientation == PORTRAIT ? getFixedPortratiNavigationBarHeight(context) : 0);
} else {
if (mRealSizes[orientation] == null) {
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
if (windowManager == null) {
return context.getResources().getDisplayMetrics().heightPixels + (orientation == PORTRAIT ? (!checkHasNavigationBar(context) ? getNavigationBarHeightInternal(context) : 0) : 0);
return context.getResources().getDisplayMetrics().heightPixels + (orientation == PORTRAIT ? getFixedPortratiNavigationBarHeight(context) : 0);
}
Display display = windowManager.getDefaultDisplay();
Point point = new Point();
display.getRealSize(point);
mRealSizes[orientation] = point;
}
return mRealSizes[orientation].y - (orientation == PORTRAIT ? getNavigationBarHeight(context) : 0);
//如果包含navigation,则减去,因为realsizes包含navigation
int offset = checkHasNavigationBar(context) ? navigationBarRect.bottom != 0 ? getNavigationBarHeightInternal(context) : 0 : 0;
return mRealSizes[orientation].y - offset;
}
}

public static int getScreenWidthCompat(Context context) {
int orientation = context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT ? PORTRAIT : LANDSCAPE;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
return context.getResources().getDisplayMetrics().widthPixels + (orientation == LANDSCAPE ? (!checkHasNavigationBar(context) ? getNavigationBarHeightInternal(context) : 0) : 0);
//如果没有navigation,则需要加上,因为displayMetrics不包含navigation
return context.getResources().getDisplayMetrics().widthPixels + (orientation == LANDSCAPE ? getFixedLandScapeNavigationBarHeight(context) : 0);
} else {
if (mRealSizes[orientation] == null) {
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
if (windowManager == null) {
return context.getResources().getDisplayMetrics().widthPixels + (orientation == LANDSCAPE ? (!checkHasNavigationBar(context) ? getNavigationBarHeightInternal(context) : 0) : 0);
return context.getResources().getDisplayMetrics().widthPixels + (orientation == LANDSCAPE ? getFixedLandScapeNavigationBarHeight(context) : 0);
}
Display display = windowManager.getDefaultDisplay();
Point point = new Point();
display.getRealSize(point);
mRealSizes[orientation] = point;
}
return mRealSizes[orientation].x - (orientation == LANDSCAPE ? getNavigationBarHeight(context) : 0);
//如果包含navigation,则减去,因为realsizes包含navigation
int offset = checkHasNavigationBar(context) ? navigationBarRect.right != 0 ? getNavigationBarHeightInternal(context) : 0 : 0;
return mRealSizes[orientation].x - offset;
}
}

private static int getFixedPortratiNavigationBarHeight(Context context) {
//只有navigation在下面,才返回
if (!checkHasNavigationBar(context)) {
if (navigationBarRect.bottom != 0) {
return getNavigationBarHeightInternal(context);
}
}
return 0;
}

private static int getFixedLandScapeNavigationBarHeight(Context context) {
//只有navigation在右边,才返回
if (!checkHasNavigationBar(context)) {
if (navigationBarRect.right != 0) {
return getNavigationBarHeightInternal(context);
}
}
return 0;
}

public static int getScreenOrientation(Context context) {
Expand Down

0 comments on commit 2106446

Please sign in to comment.