@Test public void categoriesWork() { final Map<String, Trie> categories = new HashMap<>(); final Map<String, String> categoryPrefMap = new HashMap<>(); final int CAT_COUNT = 4; final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(RuntimeEnvironment.application.getApplicationContext()); { final SharedPreferences.Editor editor = preferences.edit(); for (int i = 0; i < CAT_COUNT; i++) { final String domain = "category" + i + ".com"; final Trie trie = Trie.createRootNode(); trie.put(FocusString.create(domain).reverse()); final String categoryName = "category" + i; categories.put(categoryName, trie); editor.putBoolean(categoryName, false); categoryPrefMap.put(categoryName, categoryName); } editor.apply(); } final UrlMatcher matcher = new UrlMatcher(RuntimeEnvironment.application, categoryPrefMap, categories, null); final int allEnabledPattern = (1 << CAT_COUNT) - 1; for (int categoryPattern = 0; categoryPattern <= allEnabledPattern; categoryPattern++) { final SharedPreferences.Editor editor = preferences.edit(); for (int currentCategory = 0; currentCategory < CAT_COUNT; currentCategory++) { final int currentBit = 1 << currentCategory; final boolean enabled = ((currentBit & categoryPattern) == currentBit); editor.putBoolean("category" + currentCategory, enabled); if (categoryPattern == 0) { assertFalse("All categories should be disabled for categorypattern==0", enabled); } else if (categoryPattern == allEnabledPattern) { assertTrue("All categories should be enabled for categorypattern=='111....'", enabled); } else if (categoryPattern == Integer.parseInt("1100", 2)) { if (currentCategory < 2) { assertFalse("Categories 0/1 expected to be disabled", enabled); } else { assertTrue("Categories >= 2 expected to be enabled", enabled); } } } editor.apply(); for (int currentCategory = 0; currentCategory < CAT_COUNT; currentCategory++) { final int currentBit = 1 << currentCategory; final boolean enabled = ((currentBit & categoryPattern) == currentBit); final String url = "http: Assert.assertEquals("Incorrect category matched for combo=" + categoryPattern + " url=" + url, enabled, matcher.matches(Uri.parse(url), Uri.parse("http: } } }
public boolean matches(final Uri resourceURI, final Uri pageURI) { final String path = resourceURI.getPath(); if (path == null) { return false; } if (blockWebfonts) { for (final String extension : WEBFONT_EXTENSIONS) { if (path.endsWith(extension)) { return true; } } } final String resourceURLString = resourceURI.toString(); if (previouslyUnmatched.contains(resourceURLString)) { return false; } if (entityList != null && entityList.isWhiteListed(pageURI, resourceURI)) { return false; } final String resourceHost = resourceURI.getHost(); final String pageHost = pageURI.getHost(); if (pageHost != null && pageHost.equals(resourceHost)) { return false; } if (previouslyMatched.contains(resourceURLString)) { return true; } final FocusString revhost = FocusString.create(resourceHost).reverse(); for (final Map.Entry<String, Trie> category : categories.entrySet()) { if (enabledCategories.contains(category.getKey()) && category.getValue().findNode(revhost) != null) { previouslyMatched.add(resourceURLString); return true; } } previouslyUnmatched.add(resourceURLString); return false; }
UrlMatcher implements  SharedPreferences.OnSharedPreferenceChangeListener { public boolean matches(final Uri resourceURI, final Uri pageURI) { final String path = resourceURI.getPath(); if (path == null) { return false; } if (blockWebfonts) { for (final String extension : WEBFONT_EXTENSIONS) { if (path.endsWith(extension)) { return true; } } } final String resourceURLString = resourceURI.toString(); if (previouslyUnmatched.contains(resourceURLString)) { return false; } if (entityList != null && entityList.isWhiteListed(pageURI, resourceURI)) { return false; } final String resourceHost = resourceURI.getHost(); final String pageHost = pageURI.getHost(); if (pageHost != null && pageHost.equals(resourceHost)) { return false; } if (previouslyMatched.contains(resourceURLString)) { return true; } final FocusString revhost = FocusString.create(resourceHost).reverse(); for (final Map.Entry<String, Trie> category : categories.entrySet()) { if (enabledCategories.contains(category.getKey()) && category.getValue().findNode(revhost) != null) { previouslyMatched.add(resourceURLString); return true; } } previouslyUnmatched.add(resourceURLString); return false; } }
UrlMatcher implements  SharedPreferences.OnSharedPreferenceChangeListener { public boolean matches(final Uri resourceURI, final Uri pageURI) { final String path = resourceURI.getPath(); if (path == null) { return false; } if (blockWebfonts) { for (final String extension : WEBFONT_EXTENSIONS) { if (path.endsWith(extension)) { return true; } } } final String resourceURLString = resourceURI.toString(); if (previouslyUnmatched.contains(resourceURLString)) { return false; } if (entityList != null && entityList.isWhiteListed(pageURI, resourceURI)) { return false; } final String resourceHost = resourceURI.getHost(); final String pageHost = pageURI.getHost(); if (pageHost != null && pageHost.equals(resourceHost)) { return false; } if (previouslyMatched.contains(resourceURLString)) { return true; } final FocusString revhost = FocusString.create(resourceHost).reverse(); for (final Map.Entry<String, Trie> category : categories.entrySet()) { if (enabledCategories.contains(category.getKey()) && category.getValue().findNode(revhost) != null) { previouslyMatched.add(resourceURLString); return true; } } previouslyUnmatched.add(resourceURLString); return false; } UrlMatcher(final Context context,
                                     @NonNull final Map<String, String> categoryPrefMap,
                                     @NonNull final Map<String, Trie> categoryMap,
                                     @Nullable final EntityList entityList); @VisibleForTesting  UrlMatcher(final String[] patterns); }
UrlMatcher implements  SharedPreferences.OnSharedPreferenceChangeListener { public boolean matches(final Uri resourceURI, final Uri pageURI) { final String path = resourceURI.getPath(); if (path == null) { return false; } if (blockWebfonts) { for (final String extension : WEBFONT_EXTENSIONS) { if (path.endsWith(extension)) { return true; } } } final String resourceURLString = resourceURI.toString(); if (previouslyUnmatched.contains(resourceURLString)) { return false; } if (entityList != null && entityList.isWhiteListed(pageURI, resourceURI)) { return false; } final String resourceHost = resourceURI.getHost(); final String pageHost = pageURI.getHost(); if (pageHost != null && pageHost.equals(resourceHost)) { return false; } if (previouslyMatched.contains(resourceURLString)) { return true; } final FocusString revhost = FocusString.create(resourceHost).reverse(); for (final Map.Entry<String, Trie> category : categories.entrySet()) { if (enabledCategories.contains(category.getKey()) && category.getValue().findNode(revhost) != null) { previouslyMatched.add(resourceURLString); return true; } } previouslyUnmatched.add(resourceURLString); return false; } UrlMatcher(final Context context,
                                     @NonNull final Map<String, String> categoryPrefMap,
                                     @NonNull final Map<String, Trie> categoryMap,
                                     @Nullable final EntityList entityList); @VisibleForTesting  UrlMatcher(final String[] patterns); static UrlMatcher loadMatcher(final Context context, final int blockListFile, final int[] blockListOverrides, final int entityListFile); @Override void onSharedPreferenceChanged(final SharedPreferences sharedPreferences, final String prefName); Set<String> getCategories(); void setCategoryEnabled(final String category, final boolean enabled); boolean matches(final Uri resourceURI, final Uri pageURI); }
UrlMatcher implements  SharedPreferences.OnSharedPreferenceChangeListener { public boolean matches(final Uri resourceURI, final Uri pageURI) { final String path = resourceURI.getPath(); if (path == null) { return false; } if (blockWebfonts) { for (final String extension : WEBFONT_EXTENSIONS) { if (path.endsWith(extension)) { return true; } } } final String resourceURLString = resourceURI.toString(); if (previouslyUnmatched.contains(resourceURLString)) { return false; } if (entityList != null && entityList.isWhiteListed(pageURI, resourceURI)) { return false; } final String resourceHost = resourceURI.getHost(); final String pageHost = pageURI.getHost(); if (pageHost != null && pageHost.equals(resourceHost)) { return false; } if (previouslyMatched.contains(resourceURLString)) { return true; } final FocusString revhost = FocusString.create(resourceHost).reverse(); for (final Map.Entry<String, Trie> category : categories.entrySet()) { if (enabledCategories.contains(category.getKey()) && category.getValue().findNode(revhost) != null) { previouslyMatched.add(resourceURLString); return true; } } previouslyUnmatched.add(resourceURLString); return false; } UrlMatcher(final Context context,
                                     @NonNull final Map<String, String> categoryPrefMap,
                                     @NonNull final Map<String, Trie> categoryMap,
                                     @Nullable final EntityList entityList); @VisibleForTesting  UrlMatcher(final String[] patterns); static UrlMatcher loadMatcher(final Context context, final int blockListFile, final int[] blockListOverrides, final int entityListFile); @Override void onSharedPreferenceChanged(final SharedPreferences sharedPreferences, final String prefName); Set<String> getCategories(); void setCategoryEnabled(final String category, final boolean enabled); boolean matches(final Uri resourceURI, final Uri pageURI);  }