Skip to content
This repository has been archived by the owner on Jun 30, 2022. It is now read-only.

修复全面屏下,NavigationBar导致Modal的计算问题 #29

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 0 additions & 29 deletions .eslintrc.js

This file was deleted.

2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,5 @@ buck-out/

# Bundle artifact
*.jsbundle
yarn.lock
package-lock.json
29 changes: 3 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,42 +12,19 @@ Remove the StatusBar background for Modal on Android

### Usage

#### First

```bash
npm install react-native-modal-translucent --save
# or
yarn add react-native-modal-translucent
```

#### Second

```javascript
$ react-native link react-native-modal-translucent
```

#### Finally

Modify package.json
If your RN version is below 0.60, you need to link manually.

```diff
"scripts": {
"start": "node node_modules/react-native/local-cli/cli.js start",
"test": "jest",
+ "fix-modal": "node node_modules/react-native-modal-translucent/scripts/translucent-modal.js",
+ "postinstall": "npm run fix-modal"
}
```

Run fix-modal once

```bash
npm run fix-modal
react-native link react-native-modal-translucent
```

That is All !

Now run the app and see the effect.
Now run the App and see the Effect.

## Caveat

Expand Down
6 changes: 0 additions & 6 deletions android/.classpath

This file was deleted.

23 changes: 0 additions & 23 deletions android/.project

This file was deleted.

2 changes: 0 additions & 2 deletions android/.settings/org.eclipse.buildship.core.prefs

This file was deleted.

19 changes: 6 additions & 13 deletions android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
buildscript {
repositories {
google()
jcenter()
}

dependencies {
classpath 'com.android.tools.build:gradle:3.3.2'
}
def safeExtGet(prop, fallback) {
rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
}

apply plugin: 'com.android.library'

android {
compileSdkVersion rootProject.ext.compileSdkVersion ?: 28
buildToolsVersion rootProject.ext.buildToolsVersion ?: "28.0.3"
compileSdkVersion safeExtGet('compileSdkVersion', 26)
buildToolsVersion safeExtGet('buildToolsVersion', '28.0.3')

defaultConfig {
minSdkVersion 16
targetSdkVersion rootProject.ext.targetSdkVersion ?: 28
minSdkVersion safeExtGet('minSdkVersion', 16)
targetSdkVersion safeExtGet('targetSdkVersion', 26)
versionCode 1
versionName "1.0"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
package me.listenzz.modal;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Point;
import android.os.Build;
import androidx.annotation.NonNull;
import android.view.Display;
import android.view.ViewGroup;
import android.view.WindowManager;

import com.facebook.infer.annotation.Assertions;

import java.lang.reflect.Method;

import static android.view.View.NO_ID;

public class TranslucentModalHostHelper {
private static final Point MIN_POINT = new Point();
private static final Point MAX_POINT = new Point();
private static final Point SIZE_POINT = new Point();
private static final String NAVIGATION = "navigationBarBackground";

/**
* To get the size of the screen, we use information from the WindowManager and
Expand All @@ -25,7 +33,7 @@ public class TranslucentModalHostHelper {
* and landscape on tablets.
* This should only be called on the native modules/shadow nodes thread.
*/
public static Point getModalHostSize(Context context) {
public static Point getModalHostSize(Context context, Activity activity) {
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = Assertions.assertNotNull(wm).getDefaultDisplay();
// getCurrentSizeRange will return the min and max width and height that the window can be
Expand All @@ -47,12 +55,99 @@ public static Point getModalHostSize(Context context) {
statusBarHeight = (int) resources.getDimension(statusBarId);
}

int navigationBarHeight = realNavigationHeight(context, activity);

if (SIZE_POINT.x < SIZE_POINT.y) {
// If we are vertical the width value comes from min width and height comes from max height
return new Point(MIN_POINT.x, MAX_POINT.y + statusBarHeight);
return new Point(MIN_POINT.x, MAX_POINT.y + statusBarHeight + navigationBarHeight);
} else {
// If we are horizontal the width value comes from max width and height comes from min height
return new Point(MAX_POINT.x, MIN_POINT.y + statusBarHeight);
return new Point(MAX_POINT.x, MIN_POINT.y + statusBarHeight + navigationBarHeight);
}
}

/**
* 判断是否存在NavigationBar
* @param context
* @return
*/
private static boolean hasNavigationBar(Context context) {
boolean hasNavigationBar = false;
Resources rs = context.getResources();
int id = rs.getIdentifier("config_showNavigationBar", "bool", "android");
if (id > 0) {
hasNavigationBar = rs.getBoolean(id);
}
try {
//反射获取SystemProperties类,并调用它的get方法
Class systemPropertiesClass = Class.forName("android.os.SystemProperties");
Method m = systemPropertiesClass.getMethod("get", String.class);
String navBarOverride = (String) m.invoke(systemPropertiesClass, "qemu.hw.mainkeys");
if ("1".equals(navBarOverride)) {
hasNavigationBar = false;
} else if ("0".equals(navBarOverride)) {
hasNavigationBar = true;
}
} catch (Exception e) {
e.printStackTrace();
}
return hasNavigationBar;
}

/**
* 全面屏判断
* @param activity
* @return
*/
private static boolean navigationGestureEnabled(@NonNull Activity activity) {
ViewGroup vp = (ViewGroup) activity.getWindow().getDecorView();
if (vp != null) {
for (int i = 0; i < vp.getChildCount(); i++) {
vp.getChildAt(i).getContext().getPackageName();
if (vp.getChildAt(i).getId() != NO_ID && NAVIGATION.equals(activity.getResources().getResourceEntryName(vp.getChildAt(i).getId()))) {
return true;
}
}
}
return false;
}

/**
* 获取NavigationBar的高度
* @param context
* @return
*/
private static int getNavigationHeight(Context context) {
if (context == null) {
return 0;
}
Resources resources = context.getResources();
int resourceId = resources.getIdentifier("navigation_bar_height",
"dimen", "android");
int height = 0;
if (resourceId > 0) {
//获取NavigationBar的高度
height = resources.getDimensionPixelSize(resourceId);
}
return height;
}

/**
* 获取真实的NavigationHeight
* @param context
* @param activity
* @return
*/
private static int realNavigationHeight(Context context, Activity activity) {
int navigationBarHeight = 0;
String manufacturer = Build.MANUFACTURER;
// 这个字符串可以自己定义,例如判断华为就填写huawei,魅族就填写meizu
if ("huawei".equalsIgnoreCase(manufacturer)) {
} else {
if (hasNavigationBar(context) && !navigationGestureEnabled(activity)) {
navigationBarHeight = getNavigationHeight(context);
}
}
return navigationBarHeight;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@
import com.facebook.react.views.modal.ReactModalHostManager;
import com.facebook.react.views.modal.ReactModalHostView;

@ReactModule(name = TranslucentModalHostManager.REACT_CLASS)
@ReactModule(name = ReactModalHostManager.REACT_CLASS)
public class TranslucentModalHostManager extends ReactModalHostManager {
protected static final String REACT_CLASS = "TranslucentModalHostView";

@Override
public String getName() {
return REACT_CLASS;
public boolean canOverrideExistingModule() {
return true;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class TranslucentModalHostShadowNode extends LayoutShadowNode {
@Override
public void addChildAt(ReactShadowNodeImpl child, int i) {
super.addChildAt(child, i);
Point modalSize = TranslucentModalHostHelper.getModalHostSize(getThemedContext());
Point modalSize = TranslucentModalHostHelper.getModalHostSize(getThemedContext(), getThemedContext().getCurrentActivity());
child.setStyleWidth(modalSize.x);
child.setStyleHeight(modalSize.y);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.support.v4.view.ViewCompat;
import android.view.View;
import android.view.Window;
import android.view.WindowInsets;
import android.view.WindowManager;

import androidx.core.view.ViewCompat;

import com.facebook.react.bridge.ReactContext;
import com.facebook.react.views.modal.ReactModalHostView;

Expand Down
28 changes: 12 additions & 16 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,42 +1,38 @@
{
"name": "react-native-modal-translucent",
"version": "3.0.1",
"name": "react-native-mtranslucentmodal",
"version": "5.0.0",
"description": "Remove the StatusBar background for Modal on Android.",
"repository": {
"type": "git",
"url": "https://github.com/listenzz/react-native-modal-translucent.git"
"url": "https://github.com/bashen1/react-native-modal-translucent.git"
},
"nativePackage": true,
"author": "listen <listenzz@163.com>",
"author": "bashen1 <maochunjie@gmail.com>",
"license": "MIT",
"homepage": "https://github.com/listenzz/react-native-modal-translucent",
"homepage": "https://github.com/bashen1/react-native-modal-translucent",
"keywords": [
"react-native",
"modal",
"translucent",
"statusbar"
],
"scripts": {
"start": "node node_modules/react-native/local-cli/cli.js start",
"start": "react-native start",
"test": "echo \"Error: no test specified\" && exit 1",
"lint": "./node_modules/.bin/eslint --fix --ext .js ./ && echo \"lint finished\" ",
"modal": "node scripts/translucent-modal.js"
"lint": "./node_modules/.bin/eslint --fix --ext .js ./ && echo \"lint finished\" "
},
"peerDependencies": {
"react": ">=16.0.0",
"react-native": ">=0.51.0"
"react-native": ">=0.60.0"
},
"devDependencies": {
"@babel/core": "^7.3.4",
"@babel/runtime": "^7.3.4",
"babel-eslint": "^10.0.1",
"eslint": "^5.15.3",
"eslint-config-prettier": "^4.1.0",
"eslint-plugin-prettier": "^3.0.1",
"babel-jest": "^24.5.0",
"jest": "^24.5.0",
"prettier": "^1.16.4",
"react": "16.8.3",
"react-native": "0.59.0"
"metro-react-native-babel-preset": "^0.55.0",
"react": "16.8.6",
"react-native": "0.60.4"
},
"jest": {
"preset": "react-native"
Expand Down
Loading