(Context context, Uri uri)
| 73 | )); |
| 74 | |
| 75 | @Nullable |
| 76 | public static Uri filter(Context context, Uri uri) { |
| 77 | if (uri.isOpaque()) |
| 78 | return null; |
| 79 | |
| 80 | String host = uri.getHost(); |
| 81 | if (TextUtils.isEmpty(host)) |
| 82 | return null; |
| 83 | |
| 84 | File file = getFile(context); |
| 85 | if (!file.exists()) |
| 86 | return null; |
| 87 | |
| 88 | List<String> removes = new ArrayList<>(); |
| 89 | List<String> importants = new ArrayList<>(); |
| 90 | List<String> excepts = new ArrayList<>(); |
| 91 | |
| 92 | try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)))) { |
| 93 | String line; |
| 94 | while ((line = br.readLine()) != null) { |
| 95 | // https://adguard.com/kb/general/ad-filtering/create-own-filters/#comments |
| 96 | if (TextUtils.isEmpty(line) || line.startsWith("!")) |
| 97 | continue; |
| 98 | |
| 99 | // rule = ["@@"] pattern [ "$" modifiers ] |
| 100 | // modifiers = [modifier0, modifier1[, ...[, modifierN]]] |
| 101 | |
| 102 | int dollar = line.indexOf('$'); |
| 103 | while (dollar > 0 && line.charAt(dollar - 1) == '\\') |
| 104 | dollar = line.indexOf('$', dollar + 1); |
| 105 | if (dollar < 0) { |
| 106 | // ||https-filtering-check.adtidy.org^ |
| 107 | if (!line.contains("##")) |
| 108 | Log.w("Adguard command missing line=" + line); |
| 109 | continue; |
| 110 | } |
| 111 | |
| 112 | String pattern = line.substring(0, dollar) |
| 113 | .replace("\\$", "$"); |
| 114 | String rest = line.substring(dollar + 1) |
| 115 | .replace("\\$", "$"); |
| 116 | |
| 117 | int start = 0; |
| 118 | List<String> modifiers = new ArrayList<>(); |
| 119 | while (start < rest.length()) { |
| 120 | int comma = rest.indexOf(',', start); |
| 121 | while (comma > 0 && rest.charAt(comma - 1) == '\\') |
| 122 | comma = rest.indexOf(',', comma + 1); |
| 123 | int end = (comma < 0 ? rest.length() : comma); |
| 124 | modifiers.add(rest.substring(start, end) |
| 125 | .replace("\\,", ",")); |
| 126 | start = (comma < 0 ? end : end + 1); |
| 127 | } |
| 128 | |
| 129 | String remove = null; |
| 130 | boolean important = false; |
| 131 | boolean matches = true; |
| 132 | List<String> contents = new ArrayList<>(); |
no test coverage detected