diff --git a/.gitignore b/.gitignore index 9ad9872..1855297 100644 --- a/.gitignore +++ b/.gitignore @@ -2,5 +2,5 @@ lib/ node_modules/ npm-debug.log .idea/ - +android/build *.iml diff --git a/README.md b/README.md index 8ac053b..5be442f 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 @@ -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');` @@ -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()` diff --git a/android/build.gradle b/android/build.gradle index 361ce62..c470e07 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -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 { @@ -16,5 +25,5 @@ android { } dependencies { - compile 'com.facebook.react:react-native:+' + implementation 'com.facebook.react:react-native:+' } diff --git a/android/src/main/java/ca/jaysoo/extradimensions/ExtraDimensionsModule.java b/android/src/main/java/ca/jaysoo/extradimensions/ExtraDimensionsModule.java index 0f9fc03..330a3d2 100644 --- a/android/src/main/java/ca/jaysoo/extradimensions/ExtraDimensionsModule.java +++ b/android/src/main/java/ca/jaysoo/extradimensions/ExtraDimensionsModule.java @@ -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; @@ -78,10 +79,17 @@ public Map 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"); @@ -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) { @@ -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) { diff --git a/index.d.ts b/index.d.ts index 566b0eb..af4ea1f 100644 --- a/index.d.ts +++ b/index.d.ts @@ -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" { diff --git a/index.js b/index.js index 9184c7c..13a5f72 100644 --- a/index.js +++ b/index.js @@ -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 } diff --git a/package.json b/package.json index 8b7e6a2..cfc1d2c 100644 --- a/package.json +++ b/package.json @@ -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": { @@ -18,11 +18,6 @@ "react", "android" ], - "rnpm": { - "android": { - "packageInstance": "new ExtraDimensionsPackage()" - } - }, "author": "Jack Hsu (http://jaysoo.ca/)", "license": "ISC" } diff --git a/react-native.config.js b/react-native.config.js new file mode 100644 index 0000000..772fa66 --- /dev/null +++ b/react-native.config.js @@ -0,0 +1,9 @@ +module.exports = { + dependency: { + platforms: { + android: { + packageInstance: "new ExtraDimensionsPackage()" + } + } + } +};