├── .npmignore ├── LICENSE ├── README.md ├── android ├── build.gradle └── src │ └── main │ ├── AndroidManifest.xml │ └── java │ └── com │ └── rndetectnavbarandroid │ ├── RNDetectNavbarAndroidModule.java │ └── RNDetectNavbarAndroidPackage.java ├── index.js └── package.json /.npmignore: -------------------------------------------------------------------------------- 1 | # OSX 2 | # 3 | .DS_Store 4 | 5 | # Xcode 6 | # 7 | build/ 8 | *.pbxuser 9 | !default.pbxuser 10 | *.mode1v3 11 | !default.mode1v3 12 | *.mode2v3 13 | !default.mode2v3 14 | *.perspectivev3 15 | !default.perspectivev3 16 | xcuserdata 17 | *.xccheckout 18 | *.moved-aside 19 | DerivedData 20 | *.hmap 21 | *.ipa 22 | *.xcuserstate 23 | project.xcworkspace 24 | 25 | # node.js 26 | # 27 | node_modules/ 28 | npm-debug.log 29 | 30 | # WebStorm 31 | .idea 32 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 MockingBot 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # react-native-detect-navbar-android 2 | Detect soft navigation bar for Android devices, so you'll know which one has physical keys (Home, Back, Menu) and which does not. 3 | 4 | Note: this project is Android only, and it will not work if you use it to detect soft key being hidden. Pull Request is welcomed. 5 | 6 | ## Installation Process 7 | 8 | * download this from npm 9 | 10 | ```bash 11 | npm install react-native-detect-navbar-android --save 12 | ``` 13 | 14 | * Run `react-native link` or... 15 | 16 | * Edit `android/settings.gradle`: 17 | 18 | ```diff 19 | + include ':react-native-detect-navbar-android' 20 | + project(':react-native-detect-navbar-android').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-detect-navbar-android/android') 21 | ``` 22 | 23 | * Edit `android/app/build.gradle`: 24 | 25 | ```diff 26 | dependencies { 27 | compile fileTree(dir: 'libs', include: ['*.jar']) 28 | compile "com.android.support:appcompat-v7:23.0.1" 29 | compile "com.facebook.react:react-native:+" 30 | + compile project(':react-native-detect-navbar-android') 31 | } 32 | ``` 33 | 34 | * Edit your `android/app/src/main/java/.../MainApplication.java`: 35 | 36 | ```diff 37 | + import import com.rndetectnavbarandroid.RNDetectNavbarAndroidPackage; 38 | ``` 39 | 40 | ```diff 41 | @Override 42 | protected List getPackages() { 43 | return Arrays.asList( 44 | new MainReactPackage() 45 | + , new RNDetectNavbarAndroidPackage() 46 | ); 47 | } 48 | ``` 49 | 50 | ## Usage 51 | 52 | ```js 53 | import DetectNavbar from 'react-native-detect-navbar-android'; 54 | // or 55 | import {DetectNavbar} from 'react-native-detect-navbar-android'; 56 | // or 57 | const DetectNavbar = require('react-native-detect-navbar-android'); 58 | 59 | // methods (Android Only, don't call on iOS) 60 | DetectNavbar.hasSoftKeys().then((bool) => { 61 | if(bool) { 62 | console.log('Has Soft NavBar'); 63 | } else { 64 | console.log('Has Hard Key NavBar'); 65 | } 66 | }); 67 | ``` 68 | -------------------------------------------------------------------------------- /android/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | repositories { 3 | jcenter() 4 | } 5 | 6 | dependencies { 7 | classpath 'com.android.tools.build:gradle:1.1.3' 8 | } 9 | } 10 | 11 | apply plugin: 'com.android.library' 12 | 13 | android { 14 | compileSdkVersion rootProject.properties.get('compileSdkVersion', 23) 15 | buildToolsVersion rootProject.properties.get('buildToolsVersion', "23.0.1") 16 | 17 | defaultConfig { 18 | minSdkVersion 16 19 | targetSdkVersion rootProject.properties.get('targetSdkVersion', 22) 20 | versionCode 1 21 | versionName "1.0" 22 | } 23 | lintOptions { 24 | abortOnError false 25 | } 26 | } 27 | 28 | repositories { 29 | mavenCentral() 30 | } 31 | 32 | dependencies { 33 | implementation 'com.facebook.react:react-native:+' 34 | } 35 | -------------------------------------------------------------------------------- /android/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | -------------------------------------------------------------------------------- /android/src/main/java/com/rndetectnavbarandroid/RNDetectNavbarAndroidModule.java: -------------------------------------------------------------------------------- 1 | package com.rndetectnavbarandroid; 2 | 3 | import android.annotation.TargetApi; 4 | import android.app.Activity; 5 | import android.os.Build; 6 | import android.os.Bundle; 7 | import android.view.MenuItem; 8 | import android.annotation.SuppressLint; 9 | import android.view.View; 10 | import android.view.WindowManager; 11 | import android.view.KeyCharacterMap; 12 | import android.util.DisplayMetrics; 13 | import android.view.KeyEvent; 14 | import android.view.ViewConfiguration; 15 | import android.view.Display; 16 | import android.content.Context; 17 | import android.util.Log; 18 | 19 | import com.facebook.react.bridge.Promise; 20 | import com.facebook.react.bridge.ReactApplicationContext; 21 | import com.facebook.react.bridge.ReactContextBaseJavaModule; 22 | import com.facebook.react.bridge.ReactMethod; 23 | import com.facebook.react.bridge.UiThreadUtil; 24 | 25 | /** 26 | * {@link NativeModule} that allows changing the appearance of the menu bar. 27 | */ 28 | public class RNDetectNavbarAndroidModule extends ReactContextBaseJavaModule { 29 | 30 | ReactApplicationContext reactContext; 31 | 32 | public RNDetectNavbarAndroidModule(ReactApplicationContext reactContext) { 33 | super(reactContext); 34 | this.reactContext = reactContext; 35 | } 36 | 37 | @Override 38 | public String getName() { 39 | return "RNDetectNavbarAndroid"; 40 | } 41 | 42 | @ReactMethod 43 | public void hasSoftKeys(final Promise promise) { 44 | promise.resolve(hasImmersive()); 45 | } 46 | 47 | private static boolean hasImmersive; 48 | private static boolean cached = false; 49 | 50 | @SuppressLint ("NewApi") 51 | private boolean hasImmersive() { 52 | 53 | if (!cached) { 54 | if(Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { 55 | hasImmersive = false; 56 | cached = true; 57 | return false; 58 | } 59 | Display d = ((WindowManager) reactContext.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); 60 | 61 | DisplayMetrics realDisplayMetrics = new DisplayMetrics(); 62 | d.getRealMetrics(realDisplayMetrics); 63 | 64 | int realHeight = realDisplayMetrics.heightPixels; 65 | int realWidth = realDisplayMetrics.widthPixels; 66 | 67 | DisplayMetrics displayMetrics = new DisplayMetrics(); 68 | d.getMetrics(displayMetrics); 69 | 70 | int displayHeight = displayMetrics.heightPixels; 71 | int displayWidth = displayMetrics.widthPixels; 72 | 73 | hasImmersive = (realWidth > displayWidth) || (realHeight > displayHeight); 74 | cached = true; 75 | } 76 | 77 | return hasImmersive; 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /android/src/main/java/com/rndetectnavbarandroid/RNDetectNavbarAndroidPackage.java: -------------------------------------------------------------------------------- 1 | package com.rndetectnavbarandroid; 2 | 3 | import java.util.*; 4 | 5 | import com.facebook.react.ReactPackage; 6 | import com.facebook.react.bridge.NativeModule; 7 | import com.facebook.react.bridge.JavaScriptModule; 8 | import com.facebook.react.bridge.ReactApplicationContext; 9 | import com.facebook.react.uimanager.ViewManager; 10 | 11 | public class RNDetectNavbarAndroidPackage implements ReactPackage { 12 | 13 | @Override 14 | public List createNativeModules(ReactApplicationContext reactContext) { 15 | List modules = new ArrayList<>(); 16 | modules.add(new RNDetectNavbarAndroidModule(reactContext)); 17 | return modules; 18 | } 19 | 20 | public List> createJSModules() { 21 | return Collections.emptyList(); 22 | } 23 | 24 | @Override 25 | public List createViewManagers(ReactApplicationContext reactContext) { 26 | return Arrays.asList(); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | import {NativeModules, Platform} from 'react-native'; 4 | const {RNDetectNavbarAndroid} = NativeModules; 5 | 6 | function unSupportedError() { 7 | throw new Error('[react-native-detect-navbar-android] does not support iOS'); 8 | } 9 | 10 | const DetectNavbar = Platform.OS === 'android' ? { 11 | hasSoftKeys() { 12 | return RNDetectNavbarAndroid.hasSoftKeys(); 13 | } 14 | } : { 15 | hasSoftKeys: unSupportedError 16 | }; 17 | 18 | export {DetectNavbar}; 19 | export default DetectNavbar; 20 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-native-detect-navbar-android", 3 | "version": "0.3.0", 4 | "description": "Detect soft navigation bar for Android devices", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/lequanghuylc/react-native-detect-navbar-android.git" 12 | }, 13 | "keywords": [ 14 | "navbar", 15 | "android", 16 | "react", 17 | "native", 18 | "soft", 19 | "keys" 20 | ], 21 | "author": "Le Quang Huy", 22 | "license": "ISC", 23 | "bugs": { 24 | "url": "https://github.com/lequanghuylc/react-native-detect-navbar-android/issues" 25 | }, 26 | "homepage": "https://github.com/lequanghuylc/react-native-detect-navbar-android#readme" 27 | } 28 | --------------------------------------------------------------------------------