2020-02-19 20:52:05 +00:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2020-10-30 13:59:44 +00:00
|
|
|
import { FlatList, Linking } from 'react-native';
|
2020-02-19 20:52:05 +00:00
|
|
|
|
|
|
|
import I18n from '../i18n';
|
|
|
|
import { withTheme } from '../theme';
|
|
|
|
import { themes } from '../constants/colors';
|
|
|
|
import StatusBar from '../containers/StatusBar';
|
2020-10-30 13:59:44 +00:00
|
|
|
import * as List from '../containers/List';
|
2020-02-19 20:52:05 +00:00
|
|
|
import { DEFAULT_BROWSER_KEY } from '../utils/openLink';
|
|
|
|
import { isIOS } from '../utils/deviceInfo';
|
2020-06-15 14:00:46 +00:00
|
|
|
import SafeAreaView from '../containers/SafeAreaView';
|
2020-08-19 17:14:22 +00:00
|
|
|
import UserPreferences from '../lib/userPreferences';
|
2020-07-30 19:51:13 +00:00
|
|
|
import { logEvent, events } from '../utils/log';
|
2020-02-19 20:52:05 +00:00
|
|
|
|
|
|
|
const DEFAULT_BROWSERS = [
|
|
|
|
{
|
2020-07-24 19:23:34 +00:00
|
|
|
title: 'In_app',
|
2020-02-19 20:52:05 +00:00
|
|
|
value: 'inApp'
|
|
|
|
},
|
|
|
|
{
|
2020-07-24 19:23:34 +00:00
|
|
|
title: isIOS ? 'Safari' : 'Browser',
|
2020-02-19 20:52:05 +00:00
|
|
|
value: 'systemDefault:'
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
const BROWSERS = [
|
|
|
|
{
|
|
|
|
title: 'Chrome',
|
|
|
|
value: 'googlechrome:'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Firefox',
|
|
|
|
value: 'firefox:'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Brave',
|
|
|
|
value: 'brave:'
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
class DefaultBrowserView extends React.Component {
|
2020-07-31 18:30:36 +00:00
|
|
|
static navigationOptions = () => ({
|
2020-06-15 14:00:46 +00:00
|
|
|
title: I18n.t('Default_browser')
|
2020-07-31 18:30:36 +00:00
|
|
|
})
|
2020-02-19 20:52:05 +00:00
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
theme: PropTypes.string
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2020-05-08 16:37:49 +00:00
|
|
|
this.state = {
|
|
|
|
browser: null,
|
|
|
|
supported: []
|
|
|
|
};
|
2020-02-19 20:52:05 +00:00
|
|
|
if (isIOS) {
|
|
|
|
this.init();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async componentDidMount() {
|
|
|
|
this.mounted = true;
|
2020-08-19 17:14:22 +00:00
|
|
|
const browser = await UserPreferences.getStringAsync(DEFAULT_BROWSER_KEY);
|
|
|
|
this.setState({ browser });
|
2020-02-19 20:52:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
init = () => {
|
|
|
|
BROWSERS.forEach((browser) => {
|
|
|
|
const { value } = browser;
|
|
|
|
Linking.canOpenURL(value).then((installed) => {
|
|
|
|
if (installed) {
|
|
|
|
if (this.mounted) {
|
|
|
|
this.setState(({ supported }) => ({ supported: [...supported, browser] }));
|
|
|
|
} else {
|
|
|
|
const { supported } = this.state;
|
|
|
|
this.state.supported = [...supported, browser];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
isSelected = (value) => {
|
|
|
|
const { browser } = this.state;
|
|
|
|
if (!browser && value === 'inApp') {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return browser === value;
|
|
|
|
}
|
|
|
|
|
|
|
|
changeDefaultBrowser = async(newBrowser) => {
|
2020-07-30 19:51:13 +00:00
|
|
|
logEvent(events.DB_CHANGE_DEFAULT_BROWSER, { browser: newBrowser });
|
2020-02-19 20:52:05 +00:00
|
|
|
try {
|
|
|
|
const browser = newBrowser !== 'inApp' ? newBrowser : null;
|
2020-08-19 17:14:22 +00:00
|
|
|
await UserPreferences.setStringAsync(DEFAULT_BROWSER_KEY, browser);
|
2020-02-19 20:52:05 +00:00
|
|
|
this.setState({ browser });
|
|
|
|
} catch {
|
2020-07-30 19:51:13 +00:00
|
|
|
logEvent(events.DB_CHANGE_DEFAULT_BROWSER_F);
|
2020-02-19 20:52:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
renderIcon = () => {
|
|
|
|
const { theme } = this.props;
|
2020-10-30 13:59:44 +00:00
|
|
|
return <List.Icon name='check' color={themes[theme].tintColor} />;
|
2020-02-19 20:52:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
renderItem = ({ item }) => {
|
|
|
|
const { title, value } = item;
|
|
|
|
return (
|
2020-10-30 13:59:44 +00:00
|
|
|
<List.Item
|
2020-07-24 19:23:34 +00:00
|
|
|
title={I18n.t(title, { defaultValue: title })}
|
2020-02-19 20:52:05 +00:00
|
|
|
onPress={() => this.changeDefaultBrowser(value)}
|
|
|
|
testID={`default-browser-view-${ title }`}
|
|
|
|
right={this.isSelected(value) ? this.renderIcon : null}
|
2020-10-30 13:59:44 +00:00
|
|
|
translateTitle={false}
|
2020-02-19 20:52:05 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-10-30 13:59:44 +00:00
|
|
|
renderHeader = () => (
|
|
|
|
<>
|
|
|
|
<List.Header title='Choose_where_you_want_links_be_opened' />
|
|
|
|
<List.Separator />
|
|
|
|
</>
|
|
|
|
)
|
2020-02-19 20:52:05 +00:00
|
|
|
|
|
|
|
render() {
|
|
|
|
const { supported } = this.state;
|
|
|
|
return (
|
2020-10-30 13:59:44 +00:00
|
|
|
<SafeAreaView testID='default-browser-view'>
|
|
|
|
<StatusBar />
|
2020-02-19 20:52:05 +00:00
|
|
|
<FlatList
|
|
|
|
data={DEFAULT_BROWSERS.concat(supported)}
|
|
|
|
keyExtractor={item => item.value}
|
2020-10-30 13:59:44 +00:00
|
|
|
contentContainerStyle={List.styles.contentContainerStyleFlatList}
|
2020-02-19 20:52:05 +00:00
|
|
|
renderItem={this.renderItem}
|
|
|
|
ListHeaderComponent={this.renderHeader}
|
2020-10-30 13:59:44 +00:00
|
|
|
ListFooterComponent={List.Separator}
|
|
|
|
ItemSeparatorComponent={List.Separator}
|
2020-02-19 20:52:05 +00:00
|
|
|
/>
|
|
|
|
</SafeAreaView>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default withTheme(DefaultBrowserView);
|