Skip to content

Commit c8c14c1

Browse files
committed
Fixed issues when browser don't close on Android and getting the response of auth redirection
1 parent 95eac0a commit c8c14c1

File tree

6 files changed

+63
-21
lines changed

6 files changed

+63
-21
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ in case of vulnerabilities.
2222

2323
## [Unreleased]
2424

25+
## [3.3.0] - 2019-11-12
26+
27+
### Fixed
28+
- Removed `waitForRedirectDelay` option and fixed race condition from **Android** to get the response of the redirection ([#36](https://github.com/proyecto26/react-native-inappbrowser/issues/36))
29+
- Fixed **Android** Activity issue closing the browser and restoring the state by using `onSaveInstanceState` ([#60](https://github.com/proyecto26/react-native-inappbrowser/issues/60)).
30+
2531
## [3.2.0] - 2019-11-10
2632

2733
### Added

README.md

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,6 @@ Property | Description
156156
`animations` (Object) | Sets the start and exit animations. [`{ startEnter, startExit, endEnter, endExit }`]
157157
`headers` (Object) | The data are key/value pairs, they will be sent in the HTTP request headers for the provided url. [`{ 'Authorization': 'Bearer ...' }`]
158158
`forceCloseOnRedirection` (Boolean) | Open Custom Tab in a new task to avoid issues redirecting back to app scheme. [`true`/`false`]
159-
`waitForRedirectDelay` (Number) | Sets a delay for wait the redirection using `openAuth` method.
160159
161160
### Demo
162161
@@ -197,8 +196,7 @@ import InAppBrowser from 'react-native-inappbrowser-reborn'
197196
},
198197
headers: {
199198
'my-custom-header': 'my custom header value'
200-
},
201-
waitForRedirectDelay: 0
199+
}
202200
})
203201
Alert.alert(JSON.stringify(result))
204202
}
@@ -270,8 +268,7 @@ import { getDeepLink } from './utilities'
270268
// Android Properties
271269
showTitle: false,
272270
enableUrlBarHiding: true,
273-
enableDefaultShare: true,
274-
waitForRedirectDelay: 1000
271+
enableDefaultShare: true
275272
}).then((response) => {
276273
if (response.type === 'success' &&
277274
response.url) {

android/src/main/java/com/proyecto26/inappbrowser/ChromeTabsManagerActivity.java

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@
1515
*/
1616
public class ChromeTabsManagerActivity extends Activity {
1717
static final String KEY_BROWSER_INTENT = "browserIntent";
18+
static final String BROWSER_RESULT_TYPE = "browserResultType";
19+
static final String DEFAULT_RESULT_TYPE = "dismiss";
1820

1921
private boolean mOpened = false;
22+
private String resultType = null;
2023

2124
public static Intent createStartIntent(Context context, Intent authIntent) {
2225
Intent intent = createBaseIntent(context);
@@ -41,10 +44,13 @@ protected void onCreate(Bundle savedInstanceState) {
4144
// This activity gets opened in 2 different ways. If the extra KEY_BROWSER_INTENT is present we
4245
// start that intent and if it is not it means this activity was started with FLAG_ACTIVITY_CLEAR_TOP
4346
// in order to close the intent that was started previously so we just close this.
44-
if (getIntent().hasExtra(KEY_BROWSER_INTENT)) {
47+
if (getIntent().hasExtra(KEY_BROWSER_INTENT)
48+
&& (savedInstanceState == null || savedInstanceState.getString(BROWSER_RESULT_TYPE) == null)
49+
) {
4550
Intent browserIntent = getIntent().getParcelableExtra(KEY_BROWSER_INTENT);
4651
browserIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
4752
startActivity(browserIntent);
53+
resultType = DEFAULT_RESULT_TYPE;
4854
} else {
4955
finish();
5056
}
@@ -60,14 +66,24 @@ protected void onResume() {
6066
if (!mOpened) {
6167
mOpened = true;
6268
} else {
63-
EventBus.getDefault().post(new ChromeTabsDismissedEvent("chrome tabs activity closed", "cancel"));
69+
resultType = "cancel";
6470
finish();
6571
}
6672
}
6773

6874
@Override
6975
protected void onDestroy() {
70-
EventBus.getDefault().post(new ChromeTabsDismissedEvent("chrome tabs activity destroyed", "dismiss"));
76+
if (resultType != null) {
77+
switch (resultType) {
78+
case "cancel":
79+
EventBus.getDefault().post(new ChromeTabsDismissedEvent("chrome tabs activity closed", resultType));
80+
break;
81+
default:
82+
EventBus.getDefault().post(new ChromeTabsDismissedEvent("chrome tabs activity destroyed", DEFAULT_RESULT_TYPE));
83+
break;
84+
}
85+
resultType = null;
86+
}
7187
super.onDestroy();
7288
}
7389

@@ -76,4 +92,16 @@ protected void onNewIntent(Intent intent) {
7692
super.onNewIntent(intent);
7793
setIntent(intent);
7894
}
95+
96+
@Override
97+
protected void onRestoreInstanceState(Bundle savedInstanceState) {
98+
super.onRestoreInstanceState(savedInstanceState);
99+
resultType = savedInstanceState.getString(BROWSER_RESULT_TYPE);
100+
}
101+
102+
@Override
103+
protected void onSaveInstanceState(Bundle savedInstanceState) {
104+
savedInstanceState.putString(BROWSER_RESULT_TYPE, DEFAULT_RESULT_TYPE);
105+
super.onSaveInstanceState(savedInstanceState);
106+
}
79107
}

example/App.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,7 @@ export default class App extends Component {
111111
toolbarColor: '#6200EE',
112112
secondaryToolbarColor: 'black',
113113
enableUrlBarHiding: true,
114-
enableDefaultShare: true,
115-
waitForRedirectDelay: 2000
114+
enableDefaultShare: true
116115
});
117116
await this.sleep(800);
118117
Alert.alert('Response', JSON.stringify(result));

index.d.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ declare module 'react-native-inappbrowser-reborn' {
5151
endEnter: string,
5252
endExit: string
5353
},
54-
headers?: { [key: string]: string },
55-
waitForRedirectDelay?: number
54+
headers?: { [key: string]: string }
5655
}
5756

5857
export type InAppBrowserOptions = InAppBrowserAndroidOptions | InAppBrowseriOSOptions;

index.js

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ type InAppBrowserAndroidOptions = {
5757
endEnter: string,
5858
endExit: string
5959
},
60-
headers?: { [key: string]: string },
61-
waitForRedirectDelay?: number
60+
headers?: { [key: string]: string }
6261
};
6362

6463
type InAppBrowserOptions = InAppBrowserAndroidOptions | InAppBrowseriOSOptions;
@@ -72,7 +71,6 @@ async function open(
7271
readerMode,
7372
modalEnabled,
7473
dismissButtonStyle,
75-
waitForRedirectDelay,
7674
enableBarCollapsing,
7775
preferredBarTintColor,
7876
preferredControlTintColor,
@@ -85,7 +83,6 @@ async function open(
8583
readerMode: !!readerMode,
8684
animated: animated !== undefined ? animated : true,
8785
modalEnabled: modalEnabled !== undefined ? modalEnabled : true,
88-
waitForRedirectDelay: waitForRedirectDelay || 0,
8986
enableBarCollapsing: !!enableBarCollapsing,
9087
preferredBarTintColor:
9188
preferredBarTintColor && processColor(preferredBarTintColor),
@@ -146,11 +143,8 @@ async function _openAuthSessionPolyfillAsync(
146143
try {
147144
response = await Promise.race([
148145
_waitForRedirectAsync(returnUrl),
149-
open(startUrl, options).then(result => {
150-
return new Promise(resolve => {
151-
// A delay to wait for the redirection or dismiss the browser instead
152-
setTimeout(() => resolve(result), options.waitForRedirectDelay);
153-
});
146+
open(startUrl, options).then(function(result) {
147+
return _checkResultAndReturnUrl(returnUrl, result);
154148
})
155149
]);
156150
} finally {
@@ -173,6 +167,25 @@ function _waitForRedirectAsync(returnUrl: string): Promise<RedirectResult> {
173167
});
174168
}
175169

170+
function _checkResultAndReturnUrl(
171+
returnUrl: string,
172+
result: RedirectResult
173+
): Promise<RedirectResult> {
174+
return new Promise(function(resolve) {
175+
Linking.getInitialURL()
176+
.then(function(url) {
177+
if (url && url.startsWith(returnUrl)) {
178+
resolve({ url: url, type: 'success' });
179+
} else {
180+
resolve(result);
181+
}
182+
})
183+
.catch(function() {
184+
resolve(result);
185+
});
186+
});
187+
}
188+
176189
async function isAvailable(): Promise<boolean> {
177190
if (Platform.OS === 'android') {
178191
return Promise.resolve(true);

0 commit comments

Comments
 (0)