Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
d9c581f
Update README.md
Sunhat Jul 13, 2018
57d1eba
Use project-wide properties and new dependency
KarlosQ Jul 19, 2018
93afb3a
Refactor
KarlosQ Jul 19, 2018
089d3db
Refactor
KarlosQ Jul 19, 2018
9615b20
fix: support react-native 0.57
sondremare Oct 8, 2018
acf4d5a
chore: release 0.22.0
sondremare Oct 8, 2018
7e775da
Merge pull request #35 from KarlosQ/patch-1
Sunhat Oct 11, 2018
c008d1b
update README.md (#37)
Oct 11, 2018
8a1aa14
Update version
Oct 30, 2018
88a1160
Update README.md on how to link package with RN (#38)
Sunhat Nov 12, 2018
0985a9d
Update README.md
Sunhat Dec 20, 2018
4526f4a
Merge branch 'pr/39'
Dec 20, 2018
59b553e
Merge branch 'master' of https://github.com/Sunhat/react-native-extra…
Dec 20, 2018
2af954f
1.0.0
Dec 20, 2018
057777b
Update README.md
Sunhat Dec 20, 2018
7c344b6
fix: Build with RN 0.58.4 and android SDK 28 fail (#42)
sebqq Feb 20, 2019
9c6cdfd
Exlude android/build files
Sunhat Feb 21, 2019
3e642c6
Checks for hardware buttons
Sunhat Feb 21, 2019
e87e79d
Convert floats to int
Sunhat Feb 21, 2019
229411a
added SOFT_MENU_BAR_ENABLED const
Sunhat Feb 21, 2019
2c2d2ff
Update package.json
Sunhat Feb 21, 2019
7aed11e
Better instructions
Sunhat Feb 21, 2019
54ffd5e
Update package.json
Sunhat Feb 21, 2019
962479b
try catch if project hasn't been rebuilt
Sunhat Feb 21, 2019
bd4f398
Update package.json
Sunhat Feb 21, 2019
7a08950
IMPORTANT FIX
Sunhat Feb 21, 2019
f9ae500
Update version
Sunhat Feb 21, 2019
a690cbf
Tidy up
Sunhat Feb 25, 2019
bfa058f
Revert Auto-Rounding feature.
Sunhat Feb 25, 2019
be42fd0
Add semi-colons
Sunhat Feb 25, 2019
58f2b85
Make constants accessible through funcs
Sunhat Feb 25, 2019
a199cc3
v1.2
Sunhat Feb 25, 2019
6048ecb
Update README.md
Sunhat Feb 25, 2019
72091b9
Update README.md
Sunhat Feb 25, 2019
0d411df
Version bump
Sunhat Feb 25, 2019
ad019bd
Fixed some typos in readme and helper functions (#44)
juan-cortes Feb 27, 2019
33503f2
Fixed no has property issue (#48)
irfansener Mar 4, 2019
0d118b7
switch to module syntax in order to support future tree shaking. (#49)
EyMaddis Mar 20, 2019
7cd366e
Update version
Sunhat Mar 30, 2019
5b3e14e
Update code for SOFT_MENU_BAR_ENABLED
Sunhat Mar 31, 2019
4615f5e
Update Version
Sunhat Mar 31, 2019
da3e749
Update ExtraDimensionsModule.java
Sunhat Mar 31, 2019
c41652e
Fixed getSoftMenuBarHeight() not returning the right value on notched…
mathieubruguier Apr 2, 2019
202f91f
Update version
Sunhat Apr 2, 2019
9cfbb17
Fixed build issue and hasPermanentMenuKey() returning the wrong value…
mathieubruguier Apr 2, 2019
0d449d2
Update package.json
Sunhat Apr 2, 2019
659fb4c
migrate to react-native.config (#57)
janczizikow Sep 9, 2019
940ffe2
Update README.md
Sunhat May 29, 2022
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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ lib/
node_modules/
npm-debug.log
.idea/

android/build
*.iml
43 changes: 32 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@

### Notice
This project is dead. As a React Native developer, I do not use this package. It is not needed.

## ExtraDimensions

This module allows you to access additional display metrics on Android devices. (RN 0.47.0+)
This module allows you to access additional display metrics on Android devices. (RN 0.57.0+)

- Actual width and height of the screen (including elements such as soft menu bar)
- Soft menu height
Expand All @@ -23,14 +27,14 @@ view needs to fill up the real screen size.
npm install react-native-extra-dimensions-android --save
```

2. link using rnpm
2. linking

```
rnpm link react-native-extra-dimensions-android
react-native link react-native-extra-dimensions-android
```

3. You may have to register the module (in android/app/src/main/java/com/YOUR-PROJECT-NAME/MainApplication.java)

2b. You may have to register the module (in android/app/src/main/java/com/YOUR-PROJECT-NAME/MainApplication.java)
`react-native link` should automatically do the following for you. If it doesn't, you might have to add it yourself.

```
import ca.jaysoo.extradimensions.ExtraDimensionsPackage; // <--- import
Expand All @@ -46,7 +50,10 @@ view needs to fill up the real screen size.
......
}
```

3. As this is a package with Java, you'll need to rebuild the project.

e.g. `react-native run-android`

4. Whenever you want to use it within React Native code now you can:

`var ExtraDimensions = require('react-native-extra-dimensions-android');`
Expand All @@ -61,12 +68,26 @@ Or, if you are using ES6

### API

There is only one method `get(dimension: string)` that takes in a dimension name, and returns its value as a `number`.
`ExtraDimensions.get(dimension: string)` that takes in a dimension name, and returns its value as a `number`.

Supported dimensions are:

- `REAL_WINDOW_HEIGHT` - Actual height of screen including system decor elements
- `REAL_WINDOW_WIDTH` - Actual width of screen including system decor elements
- `STATUS_BAR_HEIGHT` - Height of the status bar
- `REAL_WINDOW_HEIGHT` - Actual height of screen including system decor elements
- `REAL_WINDOW_WIDTH` - Actual width of screen including system decor elements
- `STATUS_BAR_HEIGHT` - Height of the status bar
- `SOFT_MENU_BAR_HEIGHT` - Height of the soft menu bar (supported on most new Android devices)
- `SMART_BAR_HEIGHT` - Height of the MeiZu's device smart bar
- `SMART_BAR_HEIGHT` - Height of the MeiZu's device smart bar

Alternatively, there are methods for each constant, to fulfill autocomplete in your IDE

`ExtraDimensions.getRealWindowHeight()`

`ExtraDimensions.getRealWindowWidth()`

`ExtraDimensions.getStatusBarHeight()`

`ExtraDimensions.getSoftMenuBarHeight()`

`ExtraDimensions.getSmartBarHeight()`

`ExtraDimensions.isSoftMenuBarEnabled()`
17 changes: 13 additions & 4 deletions android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
def safeExtGet(prop, fallback) {
rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
}

apply plugin: 'com.android.library'

def DEFAULT_COMPILE_SDK_VERSION = 27
def DEFAULT_BUILD_TOOLS_VERSION = "27.0.3"
def DEFAULT_TARGET_SDK_VERSION = 26
def DEFAULT_SUPPORT_LIB_VERSION = "27.0.2"

android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
compileSdkVersion rootProject.hasProperty('compileSdkVersion') ? rootProject.compileSdkVersion : DEFAULT_COMPILE_SDK_VERSION
buildToolsVersion rootProject.hasProperty('buildToolsVersion') ? rootProject.buildToolsVersion : DEFAULT_BUILD_TOOLS_VERSION

defaultConfig {
minSdkVersion 16
targetSdkVersion 22
targetSdkVersion rootProject.hasProperty('targetSdkVersion') ? rootProject.targetSdkVersion : DEFAULT_TARGET_SDK_VERSION
versionCode 1
versionName "1.0"
ndk {
Expand All @@ -16,5 +25,5 @@ android {
}

dependencies {
compile 'com.facebook.react:react-native:+'
implementation 'com.facebook.react:react-native:+'
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import android.provider.Settings;
import android.content.res.Resources;
import android.view.WindowManager;
import android.view.ViewConfiguration;

import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.LifecycleEventListener;
Expand Down Expand Up @@ -78,10 +79,17 @@ public Map<String, Object> getConstants() {
constants.put("STATUS_BAR_HEIGHT", getStatusBarHeight(metrics));
constants.put("SOFT_MENU_BAR_HEIGHT", getSoftMenuBarHeight(metrics));
constants.put("SMART_BAR_HEIGHT", getSmartBarHeight(metrics));
constants.put("SOFT_MENU_BAR_ENABLED", hasPermanentMenuKey());

return constants;
}

private boolean hasPermanentMenuKey() {
final Context ctx = getReactApplicationContext();
int id = ctx.getResources().getIdentifier("config_showNavigationBar", "bool", "android");
return !(id > 0 && ctx.getResources().getBoolean(id));
}

private float getStatusBarHeight(DisplayMetrics metrics) {
final Context ctx = getReactApplicationContext();
final int heightResId = ctx.getResources().getIdentifier("status_bar_height", "dimen", "android");
Expand All @@ -92,15 +100,15 @@ private float getStatusBarHeight(DisplayMetrics metrics) {
}

private float getSoftMenuBarHeight(DisplayMetrics metrics) {
final float realHeight = getRealHeight(metrics);
if(hasPermanentMenuKey()) {
return 0;
}
final Context ctx = getReactApplicationContext();
final DisplayMetrics usableMetrics = ctx.getResources().getDisplayMetrics();

((WindowManager) mReactContext.getSystemService(Context.WINDOW_SERVICE))
.getDefaultDisplay().getMetrics(metrics);
final int usableHeight = usableMetrics.heightPixels;

return Math.max(0, realHeight - usableHeight / metrics.density);
final int heightResId = ctx.getResources().getIdentifier("navigation_bar_height", "dimen", "android");
return
heightResId > 0
? ctx.getResources().getDimensionPixelSize(heightResId) / metrics.density
: 0;
}

private float getRealHeight(DisplayMetrics metrics) {
Expand All @@ -119,24 +127,19 @@ private float getSmartBarHeight(DisplayMetrics metrics) {
final boolean autoHideSmartBar = Settings.System.getInt(context.getContentResolver(),
"mz_smartbar_auto_hide", 0) == 1;

if (isMeiZu) {
if (autoHideSmartBar) {
return 0;
} else {
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 context.getResources().getDimensionPixelSize(height) / metrics.density;
} catch (Throwable e) { // 不自动隐藏smartbar同时又没有smartbar高度字段供访问,取系统navigationbar的高度
return getNormalNavigationBarHeight(context) / metrics.density;
}
}
} else {
if (!isMeiZu || autoHideSmartBar) {
return 0;
//return getNormalNavigationBarHeight(context) / metrics.density;
}
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 context.getResources().getDimensionPixelSize(height) / metrics.density;
} catch (Throwable e) { // 不自动隐藏smartbar同时又没有smartbar高度字段供访问,取系统navigationbar的高度
return getNormalNavigationBarHeight(context) / metrics.density;
}
//return getNormalNavigationBarHeight(context) / metrics.density;
}

protected static float getNormalNavigationBarHeight(final Context ctx) {
Expand Down
6 changes: 6 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ declare type Dimensions =

declare interface ExtraDimensions {
get: (dim: Dimensions) => number;
getRealWindowHeight: () => number;
getRealWindowWidth: () => number;
getStatusBarHeight: () => number;
getSoftMenuBarHeight: () => number;
getSmartBarHeight: () => number;
isSoftMenuBarEnabled: () => number;
}

declare module "react-native-extra-dimensions-android" {
Expand Down
69 changes: 55 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,58 @@
const React = require('react');
var { NativeModules, Platform } = require('react-native');
import { NativeModules, Platform } from 'react-native';

if (Platform.OS === 'android') {
module.exports = {
get(dim) {
return NativeModules.ExtraDimensions[dim];
}
};
} else {
module.exports = {
get(dim) {
console.warn('react-native-extra-dimensions-android is only available on Android');
return 0;
export function get(dim) {
if (Platform.OS !== 'android') {

console.warn('react-native-extra-dimensions-android is only available on Android. Trying to access', dim);
return 0;
} else { // android
try {
if (!NativeModules.ExtraDimensions) {
throw "ExtraDimensions not defined. Try rebuilding your project. e.g. react-native run-android";
}
const result = NativeModules.ExtraDimensions[dim];

if (typeof result !== 'number') {
return result;
}
return result;
} catch (e) {
console.error(e);
}
};
}
}

export function getRealWindowHeight() {
return get('REAL_WINDOW_HEIGHT');
}

export function getRealWindowWidth() {
return get('REAL_WINDOW_WIDTH');
}

export function getStatusBarHeight() {
return get('STATUS_BAR_HEIGHT');
}

export function getSoftMenuBarHeight() {
return get('SOFT_MENU_BAR_HEIGHT');
}

export function getSmartBarHeight() {
return get('SMART_BAR_HEIGHT');
}

export function isSoftMenuBarEnabled() {
return get('SOFT_MENU_BAR_ENABLED');
}

// stay compatible with pre-es6 exports
export default {
get,
getRealWindowHeight,
getRealWindowWidth,
getStatusBarHeight,
getSoftMenuBarHeight,
getSmartBarHeight,
isSoftMenuBarEnabled
}
7 changes: 1 addition & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-extra-dimensions-android",
"version": "0.21.0",
"version": "1.2.5",
"description": "Access additional display metrics on Android devices: status bar height, soft menu bar height, real screen size.",
"main": "index.js",
"repository": {
Expand All @@ -18,11 +18,6 @@
"react",
"android"
],
"rnpm": {
"android": {
"packageInstance": "new ExtraDimensionsPackage()"
}
},
"author": "Jack Hsu <jack.hsu@gmail.com> (http://jaysoo.ca/)",
"license": "ISC"
}
9 changes: 9 additions & 0 deletions react-native.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = {
dependency: {
platforms: {
android: {
packageInstance: "new ExtraDimensionsPackage()"
}
}
}
};