2017-03-07 05:41:51 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2017-03-07 05:41:51 +00:00
|
|
|
*
|
|
|
|
* On Apple TV, this implements back navigation using the TV remote's menu button.
|
|
|
|
* On iOS, this just implements a stub.
|
|
|
|
*
|
2018-05-11 02:06:46 +00:00
|
|
|
* @format
|
2017-03-07 05:41:51 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const Platform = require('Platform');
|
|
|
|
const TVEventHandler = require('TVEventHandler');
|
|
|
|
|
|
|
|
type BackPressEventName = $Enum<{
|
|
|
|
backPress: string,
|
|
|
|
}>;
|
|
|
|
|
|
|
|
function emptyFunction() {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Detect hardware button presses for back navigation.
|
|
|
|
*
|
|
|
|
* Android: Detect hardware back button presses, and programmatically invoke the default back button
|
|
|
|
* functionality to exit the app if there are no listeners or if none of the listeners return true.
|
|
|
|
*
|
|
|
|
* tvOS: Detect presses of the menu button on the TV remote. (Still to be implemented:
|
|
|
|
* programmatically disable menu button handling
|
|
|
|
* functionality to exit the app if there are no listeners or if none of the listeners return true.)
|
|
|
|
*
|
|
|
|
* iOS: Not applicable.
|
|
|
|
*
|
|
|
|
* The event subscriptions are called in reverse order (i.e. last registered subscription first),
|
|
|
|
* and if one subscription returns true then subscriptions registered earlier will not be called.
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
*
|
|
|
|
* ```javascript
|
|
|
|
* BackHandler.addEventListener('hardwareBackPress', function() {
|
|
|
|
* // this.onMainScreen and this.goBack are just examples, you need to use your own implementation here
|
|
|
|
* // Typically you would use the navigator here to go to the last state.
|
|
|
|
*
|
|
|
|
* if (!this.onMainScreen()) {
|
|
|
|
* this.goBack();
|
|
|
|
* return true;
|
|
|
|
* }
|
|
|
|
* return false;
|
|
|
|
* });
|
|
|
|
* ```
|
|
|
|
*/
|
|
|
|
let BackHandler;
|
|
|
|
|
2018-06-10 20:37:53 +00:00
|
|
|
if (Platform.isTV) {
|
2017-03-07 05:41:51 +00:00
|
|
|
const _tvEventHandler = new TVEventHandler();
|
2018-05-10 22:44:52 +00:00
|
|
|
const _backPressSubscriptions = new Set();
|
2017-03-07 05:41:51 +00:00
|
|
|
|
|
|
|
_tvEventHandler.enable(this, function(cmp, evt) {
|
|
|
|
if (evt && evt.eventType === 'menu') {
|
2018-05-10 22:44:52 +00:00
|
|
|
let invokeDefault = true;
|
2018-05-11 02:06:46 +00:00
|
|
|
const subscriptions = Array.from(
|
|
|
|
_backPressSubscriptions.values(),
|
|
|
|
).reverse();
|
2017-10-21 00:49:21 +00:00
|
|
|
|
2018-05-10 22:44:52 +00:00
|
|
|
for (let i = 0; i < subscriptions.length; ++i) {
|
2017-03-07 05:41:51 +00:00
|
|
|
if (subscriptions[i]()) {
|
|
|
|
invokeDefault = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (invokeDefault) {
|
|
|
|
BackHandler.exitApp();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
BackHandler = {
|
|
|
|
exitApp: emptyFunction,
|
|
|
|
|
2018-05-11 02:06:46 +00:00
|
|
|
addEventListener: function(
|
2017-03-07 05:41:51 +00:00
|
|
|
eventName: BackPressEventName,
|
2018-05-11 02:06:46 +00:00
|
|
|
handler: Function,
|
2017-03-07 05:41:51 +00:00
|
|
|
): {remove: () => void} {
|
|
|
|
_backPressSubscriptions.add(handler);
|
|
|
|
return {
|
|
|
|
remove: () => BackHandler.removeEventListener(eventName, handler),
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
removeEventListener: function(
|
|
|
|
eventName: BackPressEventName,
|
2018-05-11 02:06:46 +00:00
|
|
|
handler: Function,
|
2017-03-07 05:41:51 +00:00
|
|
|
): void {
|
|
|
|
_backPressSubscriptions.delete(handler);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
BackHandler = {
|
|
|
|
exitApp: emptyFunction,
|
|
|
|
addEventListener() {
|
|
|
|
return {
|
|
|
|
remove: emptyFunction,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
removeEventListener: emptyFunction,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = BackHandler;
|