Skip to content

Commit f03048f

Browse files
bennycaopmathew92
andauthored
Add to CustomTabsOptions ability to disable opening auth in custom tab (#806)
Co-authored-by: Prince Mathew <[email protected]>
1 parent 12799c7 commit f03048f

File tree

2 files changed

+94
-3
lines changed

2 files changed

+94
-3
lines changed

auth0/src/main/java/com/auth0/android/provider/CustomTabsOptions.java

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,20 @@
77
import android.net.Uri;
88
import android.os.Parcel;
99
import android.os.Parcelable;
10+
1011
import androidx.annotation.ColorRes;
1112
import androidx.annotation.NonNull;
1213
import androidx.annotation.Nullable;
1314
import androidx.browser.customtabs.CustomTabColorSchemeParams;
1415
import androidx.browser.customtabs.CustomTabsIntent;
1516
import androidx.browser.customtabs.CustomTabsSession;
16-
import androidx.browser.trusted.TrustedWebActivityIntent;
1717
import androidx.browser.trusted.TrustedWebActivityIntentBuilder;
1818
import androidx.core.content.ContextCompat;
1919

2020
import com.auth0.android.authentication.AuthenticationException;
2121

22+
import java.util.List;
23+
2224
/**
2325
* Holder for Custom Tabs customization options. Use {@link CustomTabsOptions#newBuilder()} to begin.
2426
*/
@@ -29,10 +31,14 @@ public class CustomTabsOptions implements Parcelable {
2931
private final int toolbarColor;
3032
private final BrowserPicker browserPicker;
3133

32-
private CustomTabsOptions(boolean showTitle, @ColorRes int toolbarColor, @NonNull BrowserPicker browserPicker) {
34+
@Nullable
35+
private final List<String> disabledCustomTabsPackages;
36+
37+
private CustomTabsOptions(boolean showTitle, @ColorRes int toolbarColor, @NonNull BrowserPicker browserPicker, @Nullable List<String> disabledCustomTabsPackages) {
3338
this.showTitle = showTitle;
3439
this.toolbarColor = toolbarColor;
3540
this.browserPicker = browserPicker;
41+
this.disabledCustomTabsPackages = disabledCustomTabsPackages;
3642
}
3743

3844
@Nullable
@@ -44,6 +50,16 @@ boolean hasCompatibleBrowser(@NonNull PackageManager pm) {
4450
return getPreferredPackage(pm) != null;
4551
}
4652

53+
/**
54+
* Returns whether the browser preferred package has custom tab disabled or not.
55+
*
56+
* @param preferredPackage the preferred browser package name.
57+
* @return whether the browser preferred package has custom tab disabled or not.
58+
*/
59+
boolean isDisabledCustomTabBrowser(@NonNull String preferredPackage) {
60+
return disabledCustomTabsPackages != null && disabledCustomTabsPackages.contains(preferredPackage);
61+
}
62+
4763
/**
4864
* Create a new CustomTabsOptions.Builder instance.
4965
*
@@ -57,6 +73,12 @@ public static Builder newBuilder() {
5773

5874
@SuppressLint("ResourceType")
5975
Intent toIntent(@NonNull Context context, @Nullable CustomTabsSession session) {
76+
String preferredPackage = this.getPreferredPackage(context.getPackageManager());
77+
78+
if (preferredPackage != null && this.isDisabledCustomTabBrowser(preferredPackage)) {
79+
return new Intent(Intent.ACTION_VIEW);
80+
}
81+
6082
final CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder(session)
6183
.setShowTitle(showTitle)
6284
.setShareState(CustomTabsIntent.SHARE_STATE_OFF);
@@ -85,13 +107,15 @@ protected CustomTabsOptions(@NonNull Parcel in) {
85107
showTitle = in.readByte() != 0;
86108
toolbarColor = in.readInt();
87109
browserPicker = in.readParcelable(BrowserPicker.class.getClassLoader());
110+
disabledCustomTabsPackages = in.createStringArrayList();
88111
}
89112

90113
@Override
91114
public void writeToParcel(@NonNull Parcel dest, int flags) {
92115
dest.writeByte((byte) (showTitle ? 1 : 0));
93116
dest.writeInt(toolbarColor);
94117
dest.writeParcelable(browserPicker, flags);
118+
dest.writeStringList(disabledCustomTabsPackages);
95119
}
96120

97121
@Override
@@ -120,10 +144,14 @@ public static class Builder {
120144
@NonNull
121145
private BrowserPicker browserPicker;
122146

147+
@Nullable
148+
private List<String> disabledCustomTabsPackages;
149+
123150
Builder() {
124151
this.showTitle = false;
125152
this.toolbarColor = 0;
126153
this.browserPicker = BrowserPicker.newBuilder().build();
154+
this.disabledCustomTabsPackages = null;
127155
}
128156

129157
/**
@@ -171,14 +199,27 @@ public Builder withBrowserPicker(@NonNull BrowserPicker browserPicker) {
171199
return this;
172200
}
173201

202+
/**
203+
* Define a list of browser packages that disables the launching of authentication on custom tabs.
204+
* The authentication url will launch on the preferred package external browser.
205+
*
206+
* @param disabledCustomTabsPackages list of browser packages.
207+
* @return the current builder instance
208+
*/
209+
@NonNull
210+
public Builder withDisabledCustomTabsPackages(List<String> disabledCustomTabsPackages) {
211+
this.disabledCustomTabsPackages = disabledCustomTabsPackages;
212+
return this;
213+
}
214+
174215
/**
175216
* Create a new CustomTabsOptions instance with the customization settings.
176217
*
177218
* @return an instance of CustomTabsOptions with the customization settings.
178219
*/
179220
@NonNull
180221
public CustomTabsOptions build() {
181-
return new CustomTabsOptions(showTitle, toolbarColor, browserPicker);
222+
return new CustomTabsOptions(showTitle, toolbarColor, browserPicker, disabledCustomTabsPackages);
182223
}
183224
}
184225

auth0/src/test/java/com/auth0/android/provider/CustomTabsOptionsTest.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,16 @@
1414
import org.robolectric.Robolectric;
1515
import org.robolectric.RobolectricTestRunner;
1616

17+
import java.util.ArrayList;
18+
import java.util.Arrays;
1719
import java.util.Collections;
20+
import java.util.List;
1821

1922
import static org.hamcrest.MatcherAssert.assertThat;
2023
import static org.hamcrest.core.Is.is;
2124
import static org.hamcrest.core.IsNull.notNullValue;
2225
import static org.hamcrest.core.IsNull.nullValue;
26+
import static org.junit.Assert.assertEquals;
2327
import static org.mockito.Matchers.any;
2428
import static org.mockito.Mockito.mock;
2529
import static org.mockito.Mockito.spy;
@@ -174,4 +178,50 @@ public void shouldSetBrowserPicker() {
174178
String preferredPackageNow = parceledOptions.getPreferredPackage(activity.getPackageManager());
175179
assertThat(preferredPackageNow, is("com.auth0.browser"));
176180
}
181+
182+
@Test
183+
public void shouldSetDisabledCustomTabPackages() {
184+
Activity activity = spy(Robolectric.setupActivity(Activity.class));
185+
BrowserPickerTest.setupBrowserContext(activity, Collections.singletonList("com.auth0.browser"), null, null);
186+
BrowserPicker browserPicker = BrowserPicker.newBuilder().build();
187+
188+
CustomTabsOptions options = CustomTabsOptions.newBuilder()
189+
.withBrowserPicker(browserPicker)
190+
.withDisabledCustomTabsPackages(List.of("com.auth0.browser"))
191+
.withToolbarColor(android.R.color.black)
192+
.build();
193+
assertThat(options, is(notNullValue()));
194+
195+
Intent intentNoExtras = options.toIntent(activity, null);
196+
197+
assertThat(intentNoExtras, is(notNullValue()));
198+
assertThat(intentNoExtras.getExtras(), is(nullValue()));
199+
assertEquals(intentNoExtras.getAction(), "android.intent.action.VIEW");
200+
201+
Parcel parcel = Parcel.obtain();
202+
options.writeToParcel(parcel, 0);
203+
parcel.setDataPosition(0);
204+
CustomTabsOptions parceledOptions = CustomTabsOptions.CREATOR.createFromParcel(parcel);
205+
assertThat(parceledOptions, is(notNullValue()));
206+
207+
Intent parceledIntent = parceledOptions.toIntent(activity, null);
208+
assertThat(parceledIntent, is(notNullValue()));
209+
assertThat(parceledIntent.getExtras(), is(nullValue()));
210+
assertEquals(parceledIntent.getAction(), "android.intent.action.VIEW");
211+
212+
BrowserPickerTest.setupBrowserContext(activity, Collections.singletonList("com.another.browser"), null, null);
213+
BrowserPicker browserPicker2 = BrowserPicker.newBuilder().build();
214+
215+
CustomTabsOptions options2 = CustomTabsOptions.newBuilder()
216+
.withBrowserPicker(browserPicker2)
217+
.withDisabledCustomTabsPackages(List.of("com.auth0.browser"))
218+
.withToolbarColor(android.R.color.black)
219+
.build();
220+
221+
Intent intentWithToolbarExtra = options2.toIntent(activity, null);
222+
assertThat(intentWithToolbarExtra, is(notNullValue()));
223+
assertThat(intentWithToolbarExtra.hasExtra(CustomTabsIntent.EXTRA_TOOLBAR_COLOR), is(true));
224+
int resolvedColor = ContextCompat.getColor(activity, android.R.color.black);
225+
assertThat(intentWithToolbarExtra.getIntExtra(CustomTabsIntent.EXTRA_TOOLBAR_COLOR, 0), is(resolvedColor));
226+
}
177227
}

0 commit comments

Comments
 (0)