-
Notifications
You must be signed in to change notification settings - Fork 130
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat: Filter allowed CustomTabs browsers #353
Changes from 1 commit
056038f
7236981
f3b3b64
57badc1
1df97f9
2070eb9
e1692ff
4c25864
7985131
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -110,14 +110,13 @@ public BrowserPicker build() { | |
@Nullable | ||
String getBestBrowserPackage(@NonNull PackageManager pm) { | ||
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http:https://www.example.com")); | ||
ResolveInfo webHandler = pm.resolveActivity(browserIntent, | ||
Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ? PackageManager.MATCH_ALL : PackageManager.MATCH_DEFAULT_ONLY); | ||
ResolveInfo webHandler = pm.resolveActivity(browserIntent, PackageManager.MATCH_DEFAULT_ONLY); | ||
String defaultBrowser = null; | ||
if (webHandler != null) { | ||
defaultBrowser = webHandler.activityInfo.packageName; | ||
} | ||
|
||
final List<ResolveInfo> availableBrowsers = pm.queryIntentActivities(browserIntent, 0); | ||
final List<ResolveInfo> availableBrowsers = pm.queryIntentActivities(browserIntent, Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ? PackageManager.MATCH_ALL : 0); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix: when listing the installed browsers on marshmallow and above, the ALL flag should be used. Otherwise, the returned list has only one result. |
||
final List<String> regularBrowsers = new ArrayList<>(); | ||
final List<String> customTabsBrowsers = new ArrayList<>(); | ||
final boolean isFilterEnabled = allowedPackages != null; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,11 +26,12 @@ | |
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.hamcrest.Matchers.isOneOf; | ||
import static org.hamcrest.Matchers.nullValue; | ||
import static org.hamcrest.core.IsNull.notNullValue; | ||
import static org.mockito.Matchers.any; | ||
import static org.mockito.Matchers.anyInt; | ||
import static org.mockito.Matchers.eq; | ||
import static org.mockito.Matchers.intThat; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.spy; | ||
import static org.mockito.Mockito.when; | ||
|
@@ -327,7 +328,7 @@ static void setupBrowserContext(@NonNull Context context, @NonNull List<String> | |
PackageManager pm = mock(PackageManager.class); | ||
when(context.getPackageManager()).thenReturn(pm); | ||
ResolveInfo defaultPackage = resolveInfoForPackageName(defaultBrowserPackage); | ||
when(pm.resolveActivity(any(Intent.class), anyInt())).thenReturn(defaultPackage); | ||
when(pm.resolveActivity(any(Intent.class), eq(PackageManager.MATCH_DEFAULT_ONLY))).thenReturn(defaultPackage); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. better matchers, only by the values that we know we use. Will help catch changes like changing the prod code flags by mistake in the future 👍 |
||
|
||
List<ResolveInfo> allBrowsers = new ArrayList<>(); | ||
for (String browser : browserPackages) { | ||
|
@@ -337,7 +338,7 @@ static void setupBrowserContext(@NonNull Context context, @NonNull List<String> | |
} | ||
allBrowsers.add(info); | ||
} | ||
when(pm.queryIntentActivities(any(Intent.class), eq(0))).thenReturn(allBrowsers); | ||
when(pm.queryIntentActivities(any(Intent.class), intThat(isOneOf(0, PackageManager.MATCH_ALL)))).thenReturn(allBrowsers); | ||
} | ||
|
||
private static ResolveInfo resolveInfoForPackageName(@Nullable String packageName) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix: this line's purpose is to find the "default browser app" in the device. "Matching all" would return all the browsers and pick one from there, which is not always the default app.