From dc9e075737c7f36729e4223b01bf71f7660c22f6 Mon Sep 17 00:00:00 2001 From: Krzysztof Ciombor Date: Fri, 8 Dec 2017 14:37:39 +0100 Subject: [PATCH] Add docs for building for Android TV and unify them with Apple TV docs --- docs/building-for-apple-tv.md | 252 ++++++++++++++++++++++++++++++++-- website/i18n/en.json | 2 +- website/sidebars.json | 2 +- 3 files changed, 242 insertions(+), 14 deletions(-) diff --git a/docs/building-for-apple-tv.md b/docs/building-for-apple-tv.md index 0fbd2de3751..34edc6b8b58 100644 --- a/docs/building-for-apple-tv.md +++ b/docs/building-for-apple-tv.md @@ -1,9 +1,85 @@ --- -id: building-for-apple-tv -title: Building For Apple TV +id: building-for-tv-devices +title: Building For TV Devices --- -Apple TV support has been implemented with the intention of making existing React Native iOS applications "just work" on tvOS, with few or no changes needed in the JavaScript code for the applications. + + +TV devices support has been implemented with the intention of making existing React Native applications "just work" on Apple TV and Android TV, with few or no changes needed in the JavaScript code for the applications. + +
+ + +
+ + The RNTester app supports Apple TV; use the `RNTester-tvOS` build target to build for tvOS. @@ -17,22 +93,75 @@ The RNTester app supports Apple TV; use the `RNTester-tvOS` build target to buil ```javascript var Platform = require('Platform'); -var running_on_apple_tv = Platform.isTVOS; +var running_on_tv = Platform.isTV; + +// If you want to be more specific and only detect devices running tvOS +// (but no Android TV devices) you can use: +var running_on_apple_tv = Platform.isTVOS +``` + + + +## Build changes + +* _Native layer_: To run React Native project on Android TV make sure to make the following changes to `AndroidManifest.xml` + +```xml + + + ... + + ... + + + + ... + ``` +* _JavaScript layer_: Support for Android TV has been added to `Platform.android.js`. You can check whether code is running on Android TV by doing + +```js +var Platform = require('Platform'); +var running_on_android_tv = Platform.isTV; +``` + + + ## Code changes -* _General support for tvOS_: Apple TV specific changes in native code are all wrapped by the TARGET_OS_TV define. These include changes to suppress APIs that are not supported on tvOS (e.g. web views, sliders, switches, status bar, etc.), and changes to support user input from the TV remote or keyboard. + + +* _General support for tvOS_: Apple TV specific changes in native code are all wrapped by the TARGET_OS_TV define. These include changes to suppress APIs that are not supported on tvOS (e.g. web views, sliders, switches, status bar, etc.), and changes to support user input from the TV remote or keyboard. + +* _Common codebase_: Since tvOS and iOS share most Objective-C and JavaScript code in common, most documentation for iOS applies equally to tvOS. -* _Common codebase_: Since tvOS and iOS share most Objective-C and JavaScript code in common, most documentation for iOS applies equally to tvOS. +* _Access to touchable controls_: When running on Apple TV, the native view class is `RCTTVView`, which has additional methods to make use of the tvOS focus engine. The `Touchable` mixin has code added to detect focus changes and use existing methods to style the components properly and initiate the proper actions when the view is selected using the TV remote, so `TouchableHighlight` and `TouchableOpacity` will "just work". In particular: -* _Access to touchable controls_: When running on Apple TV, the native view class is `RCTTVView`, which has additional methods to make use of the tvOS focus engine. The `Touchable` mixin has code added to detect focus changes and use existing methods to style the components properly and initiate the proper actions when the view is selected using the TV remote, so `TouchableHighlight` and `TouchableOpacity` will "just work". In particular: + * `touchableHandleActivePressIn` will be executed when the touchable view goes into focus + * `touchableHandleActivePressOut` will be executed when the touchable view goes out of focus + * `touchableHandlePress` will be executed when the touchable view is actually selected by pressing the "select" button on the TV remote. + + + +* _Access to touchable controls_: When running on Android TV the Android framework will automatically apply a directional navigation scheme based on relative position of focusable elements in your views. The `Touchable` mixin has code added to detect focus changes and use existing methods to style the components properly and initiate the proper actions when the view is selected using the TV remote, so `TouchableHighlight`, `TouchableOpacity` and `TouchableNativeFeedback` will "just work". In particular: * `touchableHandleActivePressIn` will be executed when the touchable view goes into focus * `touchableHandleActivePressOut` will be executed when the touchable view goes out of focus * `touchableHandlePress` will be executed when the touchable view is actually selected by pressing the "select" button on the TV remote. -* _TV remote/keyboard input_: A new native class, `RCTTVRemoteHandler`, sets up gesture recognizers for TV remote events. When TV remote events occur, this class fires notifications that are picked up by `RCTTVNavigationEventEmitter` (a subclass of `RCTEventEmitter`), that fires a JS event. This event will be picked up by instances of the `TVEventHandler` JavaScript object. Application code that needs to implement custom handling of TV remote events can create an instance of `TVEventHandler` and listen for these events, as in the following code: + + +* _TV remote/keyboard input_: A new native class, `RCTTVRemoteHandler`, sets up gesture recognizers for TV remote events. When TV remote events occur, this class fires notifications that are picked up by `RCTTVNavigationEventEmitter` (a subclass of `RCTEventEmitter`), that fires a JS event. This event will be picked up by instances of the `TVEventHandler` JavaScript object. Application code that needs to implement custom handling of TV remote events can create an instance of `TVEventHandler` and listen for these events, as in the following code: + + + +* _TV remote/keyboard input_: A new native class, `ReactAndroidTVRootViewHelper`, sets up key events handlers for TV remote events. When TV remote events occur, this class fires a JS event. This event will be picked up by instances of the `TVEventHandler` JavaScript object. Application code that needs to implement custom handling of TV remote events can create an instance of `TVEventHandler` and listen for these events, as in the following code: + + ```javascript var TVEventHandler = require('TVEventHandler'); @@ -77,14 +206,113 @@ class Game2048 extends React.Component { } ``` -* _Dev Menu support_: On the simulator, cmd-D will bring up the developer menu, just like on iOS. To bring it up on a real Apple TV device, make a long press on the play/pause button on the remote. (Please do not shake the Apple TV device, that will not work :) ) + -* _TV remote animations_: `RCTTVView` native code implements Apple-recommended parallax animations to help guide the eye as the user navigates through views. The animations can be disabled or adjusted with new optional view properties. +* _Dev Menu support_: On the simulator, cmd-D will bring up the developer menu, just like on iOS. To bring it up on a real Apple TV device, make a long press on the play/pause button on the remote. (Please do not shake the Apple TV device, that will not work :) ) + +* _TV remote animations_: `RCTTVView` native code implements Apple-recommended parallax animations to help guide the eye as the user navigates through views. The animations can be disabled or adjusted with new optional view properties. * _Back navigation with the TV remote menu button_: The `BackHandler` component, originally written to support the Android back button, now also supports back navigation on the Apple TV using the menu button on the TV remote. -* _TabBarIOS behavior_: The `TabBarIOS` component wraps the native `UITabBar` API, which works differently on Apple TV. To avoid jittery rerendering of the tab bar in tvOS (see [this issue](https://github.com/facebook/react-native/issues/15081)), the selected tab bar item can only be set from Javascript on initial render, and is controlled after that by the user through native code. +* _TabBarIOS behavior_: The `TabBarIOS` component wraps the native `UITabBar` API, which works differently on Apple TV. To avoid jittery rerendering of the tab bar in tvOS (see [this issue](https://github.com/facebook/react-native/issues/15081)), the selected tab bar item can only be set from Javascript on initial render, and is controlled after that by the user through native code. + + + +* _Dev Menu support_: On the simulator, cmd-M will bring up the developer menu, just like on Android. To bring it up on a real Android TV device, make a long press on the play/pause button on the remote. (Please do not shake the Android TV device, that will not work :) ) + + + +* _Known issues_: + + * [ListView scrolling](https://github.com/facebook/react-native/issues/12793). The issue can be easily worked around by setting `removeClippedSubviews` to false in ListView and similar components. For more discussion of this issue, see [this PR](https://github.com/facebook/react-native/pull/12944). + + * _Known issues_: - * [ListView scrolling](https://github.com/facebook/react-native/issues/12793). The issue can be easily worked around by setting `removeClippedSubviews` to false in ListView and similar components. For more discussion of this issue, see [this PR](https://github.com/facebook/react-native/pull/12944). + * `InputText` components do not work for now (i.e. they cannot receive focus). + + diff --git a/website/i18n/en.json b/website/i18n/en.json index e09dd9ce72e..65463db7169 100644 --- a/website/i18n/en.json +++ b/website/i18n/en.json @@ -21,7 +21,7 @@ "asyncstorage": "AsyncStorage", "backandroid": "BackAndroid", "backhandler": "BackHandler", - "building-for-apple-tv": "Building For Apple TV", + "building-for-tv-devices": "Building For TV Devices", "button": "Button", "cameraroll": "CameraRoll", "checkbox": "CheckBox", diff --git a/website/sidebars.json b/website/sidebars.json index f512da909f1..8089b017603 100644 --- a/website/sidebars.json +++ b/website/sidebars.json @@ -31,6 +31,7 @@ "direct-manipulation", "colors", "integration-with-existing-apps", + "building-for-tv-devices", "running-on-device", "upgrading", "troubleshooting" @@ -41,7 +42,6 @@ "linking-libraries-ios", "running-on-simulator-ios", "communication-ios", - "building-for-apple-tv", "app-extensions" ], "Guides (Android)": [