Skip to content

Commit f1ab326

Browse files
committed
add withDisabledCustomTabsPackages option and validate when launching auth url
1 parent 643d9dc commit f1ab326

3 files changed

Lines changed: 74 additions & 11 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ private void launchAsDefault(Context context, Uri uri) {
149149
} catch (InterruptedException ignored) {
150150
}
151151
Log.d(TAG, "Launching URI. Custom Tabs available: " + available);
152-
final Intent intent = customTabsOptions.toIntent(context, session.get());
152+
final Intent intent = customTabsOptions.toIntent(context, session.get(), this.preferredPackage);
153153
intent.setData(uri);
154154
context.startActivity(intent);
155155
}

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

Lines changed: 31 additions & 4 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,10 @@ boolean hasCompatibleBrowser(@NonNull PackageManager pm) {
4450
return getPreferredPackage(pm) != null;
4551
}
4652

53+
boolean isDisabledCustomTabBrowser(@NonNull String preferredPackage) {
54+
return disabledCustomTabsPackages != null && disabledCustomTabsPackages.contains(preferredPackage);
55+
}
56+
4757
/**
4858
* Create a new CustomTabsOptions.Builder instance.
4959
*
@@ -56,7 +66,12 @@ public static Builder newBuilder() {
5666

5767

5868
@SuppressLint("ResourceType")
59-
Intent toIntent(@NonNull Context context, @Nullable CustomTabsSession session) {
69+
Intent toIntent(@NonNull Context context, @Nullable CustomTabsSession session, @Nullable String preferredPackage) {
70+
71+
if (preferredPackage != null && this.isDisabledCustomTabBrowser(preferredPackage)) {
72+
return new Intent(Intent.ACTION_VIEW);
73+
}
74+
6075
final CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder(session)
6176
.setShowTitle(showTitle)
6277
.setShareState(CustomTabsIntent.SHARE_STATE_OFF);
@@ -85,13 +100,15 @@ protected CustomTabsOptions(@NonNull Parcel in) {
85100
showTitle = in.readByte() != 0;
86101
toolbarColor = in.readInt();
87102
browserPicker = in.readParcelable(BrowserPicker.class.getClassLoader());
103+
disabledCustomTabsPackages = in.createStringArrayList();
88104
}
89105

90106
@Override
91107
public void writeToParcel(@NonNull Parcel dest, int flags) {
92108
dest.writeByte((byte) (showTitle ? 1 : 0));
93109
dest.writeInt(toolbarColor);
94110
dest.writeParcelable(browserPicker, flags);
111+
dest.writeStringList(disabledCustomTabsPackages);
95112
}
96113

97114
@Override
@@ -120,10 +137,14 @@ public static class Builder {
120137
@NonNull
121138
private BrowserPicker browserPicker;
122139

140+
@Nullable
141+
private List<String> disabledCustomTabsPackages;
142+
123143
Builder() {
124144
this.showTitle = false;
125145
this.toolbarColor = 0;
126146
this.browserPicker = BrowserPicker.newBuilder().build();
147+
this.disabledCustomTabsPackages = null;
127148
}
128149

129150
/**
@@ -171,14 +192,20 @@ public Builder withBrowserPicker(@NonNull BrowserPicker browserPicker) {
171192
return this;
172193
}
173194

195+
@NonNull
196+
public Builder withDisabledCustomTabsPackages(List<String> disabledCustomTabsPackages) {
197+
this.disabledCustomTabsPackages = disabledCustomTabsPackages;
198+
return this;
199+
}
200+
174201
/**
175202
* Create a new CustomTabsOptions instance with the customization settings.
176203
*
177204
* @return an instance of CustomTabsOptions with the customization settings.
178205
*/
179206
@NonNull
180207
public CustomTabsOptions build() {
181-
return new CustomTabsOptions(showTitle, toolbarColor, browserPicker);
208+
return new CustomTabsOptions(showTitle, toolbarColor, browserPicker, disabledCustomTabsPackages);
182209
}
183210
}
184211

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

Lines changed: 42 additions & 6 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;
@@ -74,7 +78,7 @@ public void shouldHaveDefaultValues() {
7478
CustomTabsOptions options = CustomTabsOptions.newBuilder().build();
7579
assertThat(options, is(notNullValue()));
7680

77-
Intent intent = options.toIntent(context, null);
81+
Intent intent = options.toIntent(context, null, null);
7882

7983
assertThat(intent, is(notNullValue()));
8084
assertThat(intent.hasExtra(CustomTabsIntent.EXTRA_TOOLBAR_COLOR), is(false));
@@ -89,7 +93,7 @@ public void shouldHaveDefaultValues() {
8993
CustomTabsOptions parceledOptions = CustomTabsOptions.CREATOR.createFromParcel(parcel);
9094
assertThat(parceledOptions, is(notNullValue()));
9195

92-
Intent parceledIntent = parceledOptions.toIntent(context, null);
96+
Intent parceledIntent = parceledOptions.toIntent(context, null, null);
9397
assertThat(parceledIntent, is(notNullValue()));
9498
assertThat(parceledIntent.hasExtra(CustomTabsIntent.EXTRA_TOOLBAR_COLOR), is(false));
9599
assertThat(parceledIntent.hasExtra(CustomTabsIntent.EXTRA_TITLE_VISIBILITY_STATE), is(true));
@@ -105,7 +109,7 @@ public void shouldSetShowTitle() {
105109
.build();
106110
assertThat(options, is(notNullValue()));
107111

108-
Intent intent = options.toIntent(context, null);
112+
Intent intent = options.toIntent(context, null, null);
109113

110114
assertThat(intent, is(notNullValue()));
111115
assertThat(intent.hasExtra(CustomTabsIntent.EXTRA_TITLE_VISIBILITY_STATE), is(true));
@@ -118,7 +122,7 @@ public void shouldSetShowTitle() {
118122
CustomTabsOptions parceledOptions = CustomTabsOptions.CREATOR.createFromParcel(parcel);
119123
assertThat(parceledOptions, is(notNullValue()));
120124

121-
Intent parceledIntent = parceledOptions.toIntent(context, null);
125+
Intent parceledIntent = parceledOptions.toIntent(context, null, null);
122126
assertThat(parceledIntent, is(notNullValue()));
123127
assertThat(parceledIntent.hasExtra(CustomTabsIntent.EXTRA_TITLE_VISIBILITY_STATE), is(true));
124128
assertThat(parceledIntent.getIntExtra(CustomTabsIntent.EXTRA_TITLE_VISIBILITY_STATE, CustomTabsIntent.NO_TITLE), is(CustomTabsIntent.SHOW_PAGE_TITLE));
@@ -131,7 +135,7 @@ public void shouldSetToolbarColor() {
131135
.build();
132136
assertThat(options, is(notNullValue()));
133137

134-
Intent intent = options.toIntent(context, null);
138+
Intent intent = options.toIntent(context, null, null);
135139

136140
assertThat(intent, is(notNullValue()));
137141
assertThat(intent.hasExtra(CustomTabsIntent.EXTRA_TOOLBAR_COLOR), is(true));
@@ -145,7 +149,7 @@ public void shouldSetToolbarColor() {
145149
CustomTabsOptions parceledOptions = CustomTabsOptions.CREATOR.createFromParcel(parcel);
146150
assertThat(parceledOptions, is(notNullValue()));
147151

148-
Intent parceledIntent = parceledOptions.toIntent(context, null);
152+
Intent parceledIntent = parceledOptions.toIntent(context, null, null);
149153
assertThat(parceledIntent, is(notNullValue()));
150154
assertThat(parceledIntent.hasExtra(CustomTabsIntent.EXTRA_TOOLBAR_COLOR), is(true));
151155
assertThat(parceledIntent.getIntExtra(CustomTabsIntent.EXTRA_TOOLBAR_COLOR, 0), is(resolvedColor));
@@ -174,4 +178,36 @@ 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+
CustomTabsOptions options = CustomTabsOptions.newBuilder()
185+
.withDisabledCustomTabsPackages(List.of("com.auth0.browser"))
186+
.withToolbarColor(android.R.color.black)
187+
.build();
188+
assertThat(options, is(notNullValue()));
189+
190+
Intent intentNoExtras = options.toIntent(context, null, "com.auth0.browser");
191+
192+
assertThat(intentNoExtras, is(notNullValue()));
193+
assertThat(intentNoExtras.getExtras(), is(nullValue()));
194+
assertEquals(intentNoExtras.getAction(), "android.intent.action.VIEW");
195+
196+
Intent intentWithToolbarExtra = options.toIntent(context, null, "com.another.browser");
197+
assertThat(intentWithToolbarExtra, is(notNullValue()));
198+
assertThat(intentWithToolbarExtra.hasExtra(CustomTabsIntent.EXTRA_TOOLBAR_COLOR), is(true));
199+
int resolvedColor = ContextCompat.getColor(context, android.R.color.black);
200+
assertThat(intentWithToolbarExtra.getIntExtra(CustomTabsIntent.EXTRA_TOOLBAR_COLOR, 0), is(resolvedColor));
201+
202+
Parcel parcel = Parcel.obtain();
203+
options.writeToParcel(parcel, 0);
204+
parcel.setDataPosition(0);
205+
CustomTabsOptions parceledOptions = CustomTabsOptions.CREATOR.createFromParcel(parcel);
206+
assertThat(parceledOptions, is(notNullValue()));
207+
208+
Intent parceledIntent = parceledOptions.toIntent(context, null, "com.auth0.browser");
209+
assertThat(parceledIntent, is(notNullValue()));
210+
assertThat(parceledIntent.getExtras(), is(nullValue()));
211+
assertEquals(parceledIntent.getAction(), "android.intent.action.VIEW");
212+
}
177213
}

0 commit comments

Comments
 (0)