OLD | NEW |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <http://adblockplus.org/>, | 2 * This file is part of Adblock Plus <http://adblockplus.org/>, |
3 * Copyright (C) 2006-2014 Eyeo GmbH | 3 * Copyright (C) 2006-2014 Eyeo GmbH |
4 * | 4 * |
5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
8 * | 8 * |
9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
(...skipping 14 matching lines...) Expand all Loading... |
25 import java.io.InputStreamReader; | 25 import java.io.InputStreamReader; |
26 import java.util.ArrayList; | 26 import java.util.ArrayList; |
27 import java.util.Calendar; | 27 import java.util.Calendar; |
28 import java.util.LinkedHashMap; | 28 import java.util.LinkedHashMap; |
29 import java.util.List; | 29 import java.util.List; |
30 import java.util.Map; | 30 import java.util.Map; |
31 import java.util.TimeZone; | 31 import java.util.TimeZone; |
32 import java.util.regex.Pattern; | 32 import java.util.regex.Pattern; |
33 | 33 |
34 import org.adblockplus.android.updater.AlarmReceiver; | 34 import org.adblockplus.android.updater.AlarmReceiver; |
| 35 import org.apache.commons.lang.StringUtils; |
35 | 36 |
36 import android.app.ActivityManager; | 37 import android.app.ActivityManager; |
37 import android.app.ActivityManager.RunningServiceInfo; | 38 import android.app.ActivityManager.RunningServiceInfo; |
38 import android.app.AlarmManager; | 39 import android.app.AlarmManager; |
39 import android.app.Application; | 40 import android.app.Application; |
40 import android.app.PendingIntent; | 41 import android.app.PendingIntent; |
41 import android.content.Context; | 42 import android.content.Context; |
42 import android.content.Intent; | 43 import android.content.Intent; |
43 import android.content.SharedPreferences; | 44 import android.content.SharedPreferences; |
44 import android.content.SharedPreferences.Editor; | 45 import android.content.SharedPreferences.Editor; |
45 import android.content.pm.PackageInfo; | 46 import android.content.pm.PackageInfo; |
46 import android.content.pm.PackageManager; | 47 import android.content.pm.PackageManager; |
47 import android.content.pm.PackageManager.NameNotFoundException; | 48 import android.content.pm.PackageManager.NameNotFoundException; |
48 import android.net.ConnectivityManager; | 49 import android.net.ConnectivityManager; |
49 import android.net.NetworkInfo; | 50 import android.net.NetworkInfo; |
50 import android.net.Uri; | 51 import android.net.Uri; |
51 import android.os.Build; | 52 import android.os.Build; |
52 import android.preference.PreferenceManager; | 53 import android.preference.PreferenceManager; |
53 import android.provider.Settings; | 54 import android.provider.Settings; |
54 import android.util.Log; | 55 import android.util.Log; |
55 | 56 |
56 public class AdblockPlus extends Application | 57 public class AdblockPlus extends Application |
57 { | 58 { |
58 private final static String TAG = "Application"; | 59 private static final String TAG = Utils.getTag(AdblockPlus.class); |
59 | 60 |
60 private final static Pattern RE_JS = Pattern.compile(".*\\.js$", Pattern.CASE_
INSENSITIVE); | 61 private static final Pattern RE_JS = Pattern.compile(".*\\.js$", Pattern.CASE_
INSENSITIVE); |
61 private final static Pattern RE_CSS = Pattern.compile(".*\\.css$", Pattern.CAS
E_INSENSITIVE); | 62 private static final Pattern RE_CSS = Pattern.compile(".*\\.css$", Pattern.CAS
E_INSENSITIVE); |
62 private final static Pattern RE_IMAGE = Pattern.compile(".*\\.(?:gif|png|jpe?g
|bmp|ico)$", Pattern.CASE_INSENSITIVE); | 63 private static final Pattern RE_IMAGE = Pattern.compile(".*\\.(?:gif|png|jpe?g
|bmp|ico)$", Pattern.CASE_INSENSITIVE); |
63 private final static Pattern RE_FONT = Pattern.compile(".*\\.(?:ttf|woff)$", P
attern.CASE_INSENSITIVE); | 64 private static final Pattern RE_FONT = Pattern.compile(".*\\.(?:ttf|woff)$", P
attern.CASE_INSENSITIVE); |
64 private final static Pattern RE_HTML = Pattern.compile(".*\\.html?$", Pattern.
CASE_INSENSITIVE); | 65 private static final Pattern RE_HTML = Pattern.compile(".*\\.html?$", Pattern.
CASE_INSENSITIVE); |
65 | 66 |
66 /** | 67 /** |
| 68 * Update notification id. |
| 69 */ |
| 70 public static final int UPDATE_NOTIFICATION_ID = R.string.app_name + 1; |
| 71 /** |
67 * Broadcasted when filtering is enabled or disabled. | 72 * Broadcasted when filtering is enabled or disabled. |
68 */ | 73 */ |
69 public static final String BROADCAST_FILTERING_CHANGE = "org.adblockplus.andro
id.filtering.status"; | 74 public static final String BROADCAST_FILTERING_CHANGE = "org.adblockplus.andro
id.filtering.status"; |
70 /** | 75 /** |
71 * Broadcasted when subscription status changes. | 76 * Broadcasted when subscription status changes. |
72 */ | 77 */ |
73 public final static String BROADCAST_SUBSCRIPTION_STATUS = "org.adblockplus.an
droid.subscription.status"; | 78 public static final String BROADCAST_SUBSCRIPTION_STATUS = "org.adblockplus.an
droid.subscription.status"; |
74 /** | 79 /** |
75 * Broadcasted when filter match check is performed. | 80 * Broadcasted when filter match check is performed. |
76 */ | 81 */ |
77 public final static String BROADCAST_FILTER_MATCHES = "org.adblockplus.android
.filter.matches"; | 82 public static final String BROADCAST_FILTER_MATCHES = "org.adblockplus.android
.filter.matches"; |
78 | |
79 /** | 83 /** |
80 * Cached list of recommended subscriptions. | 84 * Cached list of recommended subscriptions. |
81 */ | 85 */ |
82 private Subscription[] subscriptions; | 86 private Subscription[] subscriptions; |
83 | |
84 /** | 87 /** |
85 * Indicates whether filtering is enabled or not. | 88 * Indicates whether filtering is enabled or not. |
86 */ | 89 */ |
87 private boolean filteringEnabled = false; | 90 private boolean filteringEnabled = false; |
88 | 91 |
89 private ABPEngine abpEngine; | 92 private ABPEngine abpEngine; |
90 | 93 |
91 private static AdblockPlus instance; | 94 private static AdblockPlus instance; |
92 | 95 |
93 private static class ReferrerMappingCache extends LinkedHashMap<String, String
> | 96 private static class ReferrerMappingCache extends LinkedHashMap<String, String
> |
94 { | 97 { |
95 private static final long serialVersionUID = 1L; | 98 private static final long serialVersionUID = 1L; |
96 private static final int MAX_SIZE = 5000; | 99 private static final int MAX_SIZE = 5000; |
97 | 100 |
98 public ReferrerMappingCache() | 101 public ReferrerMappingCache() |
99 { | 102 { |
100 super(MAX_SIZE + 1, 0.75f, true); | 103 super(MAX_SIZE + 1, 0.75f, true); |
101 } | 104 } |
102 | 105 |
103 @Override | 106 @Override |
104 protected boolean removeEldestEntry(Map.Entry<String, String> eldest) | 107 protected boolean removeEldestEntry(final Map.Entry<String, String> eldest) |
105 { | 108 { |
106 return size() > MAX_SIZE; | 109 return size() > MAX_SIZE; |
107 } | 110 } |
108 }; | 111 }; |
109 | 112 |
110 private ReferrerMappingCache referrerMapping = new ReferrerMappingCache(); | 113 private final ReferrerMappingCache referrerMapping = new ReferrerMappingCache(
); |
111 | 114 |
112 /** | 115 /** |
113 * Returns pointer to itself (singleton pattern). | 116 * Returns pointer to itself (singleton pattern). |
114 */ | 117 */ |
115 public static AdblockPlus getApplication() | 118 public static AdblockPlus getApplication() |
116 { | 119 { |
117 return instance; | 120 return instance; |
118 } | 121 } |
119 | 122 |
120 public int getBuildNumber() | 123 public int getBuildNumber() |
121 { | 124 { |
122 int buildNumber = -1; | 125 int buildNumber = -1; |
123 try | 126 try |
124 { | 127 { |
125 PackageInfo pi = getPackageManager().getPackageInfo(getPackageName(), 0); | 128 final PackageInfo pi = getPackageManager().getPackageInfo(getPackageName()
, 0); |
126 buildNumber = pi.versionCode; | 129 buildNumber = pi.versionCode; |
127 } | 130 } |
128 catch (NameNotFoundException e) | 131 catch (final NameNotFoundException e) |
129 { | 132 { |
130 // ignore - this shouldn't happen | 133 // ignore - this shouldn't happen |
131 Log.e(TAG, e.getMessage(), e); | 134 Log.e(TAG, e.getMessage(), e); |
132 } | 135 } |
133 return buildNumber; | 136 return buildNumber; |
134 } | 137 } |
135 | 138 |
136 /** | 139 /** |
137 * Opens Android application settings | 140 * Opens Android application settings |
138 */ | 141 */ |
139 public static void showAppDetails(Context context) | 142 public static void showAppDetails(final Context context) |
140 { | 143 { |
141 String packageName = context.getPackageName(); | 144 final String packageName = context.getPackageName(); |
142 Intent intent = new Intent(); | 145 final Intent intent = new Intent(); |
143 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) | 146 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) |
144 { | 147 { |
145 // above 2.3 | 148 // above 2.3 |
146 intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); | 149 intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); |
147 Uri uri = Uri.fromParts("package", packageName, null); | 150 final Uri uri = Uri.fromParts("package", packageName, null); |
148 intent.setData(uri); | 151 intent.setData(uri); |
149 } | 152 } |
150 else | 153 else |
151 { | 154 { |
152 // below 2.3 | 155 // below 2.3 |
153 final String appPkgName = (Build.VERSION.SDK_INT == Build.VERSION_CODES.FR
OYO ? "pkg" : "com.android.settings.ApplicationPkgName"); | 156 final String appPkgName = (Build.VERSION.SDK_INT == Build.VERSION_CODES.FR
OYO ? "pkg" : "com.android.settings.ApplicationPkgName"); |
154 intent.setAction(Intent.ACTION_VIEW); | 157 intent.setAction(Intent.ACTION_VIEW); |
155 intent.setClassName("com.android.settings", "com.android.settings.Installe
dAppDetails"); | 158 intent.setClassName("com.android.settings", "com.android.settings.Installe
dAppDetails"); |
156 intent.putExtra(appPkgName, packageName); | 159 intent.putExtra(appPkgName, packageName); |
157 } | 160 } |
158 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | 161 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
159 context.startActivity(intent); | 162 context.startActivity(intent); |
160 } | 163 } |
161 | 164 |
162 /** | 165 /** |
163 * Returns device name in user-friendly format | 166 * Returns device name in user-friendly format |
164 */ | 167 */ |
165 public static String getDeviceName() | 168 public static String getDeviceName() |
166 { | 169 { |
167 String manufacturer = Build.MANUFACTURER; | 170 final String manufacturer = Build.MANUFACTURER; |
168 String model = Build.MODEL; | 171 final String model = Build.MODEL; |
169 if (model.startsWith(manufacturer)) | 172 if (model.startsWith(manufacturer)) |
170 return capitalize(model); | 173 return Utils.capitalizeString(model); |
171 else | 174 else |
172 return capitalize(manufacturer) + " " + model; | 175 return Utils.capitalizeString(manufacturer) + " " + model; |
173 } | 176 } |
174 | 177 |
175 public static void appendRawTextFile(Context context, StringBuilder text, int
id) | 178 public static void appendRawTextFile(final Context context, final StringBuilde
r text, final int id) |
176 { | 179 { |
177 InputStream inputStream = context.getResources().openRawResource(id); | 180 // TODO: What about closing the resources? |
178 InputStreamReader in = new InputStreamReader(inputStream); | 181 final InputStream inputStream = context.getResources().openRawResource(id); |
179 BufferedReader buf = new BufferedReader(in); | 182 final InputStreamReader in = new InputStreamReader(inputStream); |
| 183 final BufferedReader buf = new BufferedReader(in); |
180 String line; | 184 String line; |
181 try | 185 try |
182 { | 186 { |
183 while ((line = buf.readLine()) != null) | 187 while ((line = buf.readLine()) != null) |
184 text.append(line); | 188 text.append(line); |
185 } | 189 } |
186 catch (IOException e) | 190 catch (final IOException e) |
187 { | 191 { |
| 192 // TODO: How about real logging? |
188 e.printStackTrace(); | 193 e.printStackTrace(); |
189 } | 194 } |
190 } | 195 } |
191 | 196 |
192 private static String capitalize(String s) | |
193 { | |
194 if (s == null || s.length() == 0) | |
195 return ""; | |
196 char first = s.charAt(0); | |
197 if (Character.isUpperCase(first)) | |
198 return s; | |
199 else | |
200 return Character.toUpperCase(first) + s.substring(1); | |
201 } | |
202 | |
203 /** | 197 /** |
204 * Checks if device has a WiFi connection available. | 198 * Checks if device has a WiFi connection available. |
205 */ | 199 */ |
206 public static boolean isWiFiConnected(Context context) | 200 public static boolean isWiFiConnected(final Context context) |
207 { | 201 { |
208 ConnectivityManager connectivityManager = (ConnectivityManager) context.getS
ystemService(Context.CONNECTIVITY_SERVICE); | 202 final ConnectivityManager connectivityManager = (ConnectivityManager) contex
t.getSystemService(Context.CONNECTIVITY_SERVICE); |
209 NetworkInfo networkInfo = null; | 203 NetworkInfo networkInfo = null; |
210 if (connectivityManager != null) | 204 if (connectivityManager != null) |
211 { | 205 { |
212 networkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_
WIFI); | 206 networkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_
WIFI); |
213 } | 207 } |
214 return networkInfo == null ? false : networkInfo.isConnected(); | 208 return networkInfo == null ? false : networkInfo.isConnected(); |
215 } | 209 } |
216 | 210 |
217 /** | 211 /** |
218 * Checks if ProxyService is running. | 212 * Checks if ProxyService is running. |
219 * | 213 * |
220 * @return true if service is running | 214 * @return true if service is running |
221 */ | 215 */ |
222 public boolean isServiceRunning() | 216 public boolean isServiceRunning() |
223 { | 217 { |
224 ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVIC
E); | 218 final ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_
SERVICE); |
225 // Actually it returns not only running services, so extra check is required | 219 // Actually it returns not only running services, so extra check is required |
226 for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VAL
UE)) | 220 for (final RunningServiceInfo service : manager.getRunningServices(Integer.M
AX_VALUE)) |
227 { | 221 { |
228 if (service.service.getClassName().equals(ProxyService.class.getCanonicalN
ame()) && service.pid > 0) | 222 if (service.service.getClassName().equals(ProxyService.class.getCanonicalN
ame()) && service.pid > 0) |
229 return true; | 223 return true; |
230 } | 224 } |
231 return false; | 225 return false; |
232 } | 226 } |
233 | 227 |
234 /** | 228 /** |
235 * Checks if application can write to external storage. | 229 * Checks if application can write to external storage. |
236 */ | 230 */ |
237 public boolean checkWriteExternalPermission() | 231 public boolean checkWriteExternalPermission() |
238 { | 232 { |
239 String permission = "android.permission.WRITE_EXTERNAL_STORAGE"; | 233 final String permission = "android.permission.WRITE_EXTERNAL_STORAGE"; |
240 int res = checkCallingOrSelfPermission(permission); | 234 final int res = checkCallingOrSelfPermission(permission); |
241 return res == PackageManager.PERMISSION_GRANTED; | 235 return res == PackageManager.PERMISSION_GRANTED; |
242 } | 236 } |
243 | 237 |
244 public boolean isFirstRun() | 238 public boolean isFirstRun() |
245 { | 239 { |
246 return abpEngine.isFirstRun(); | 240 return abpEngine.isFirstRun(); |
247 } | 241 } |
248 | 242 |
249 /** | 243 /** |
250 * Returns list of known subscriptions. | 244 * Returns list of known subscriptions. |
251 */ | 245 */ |
252 public Subscription[] getRecommendedSubscriptions() | 246 public Subscription[] getRecommendedSubscriptions() |
253 { | 247 { |
| 248 // TODO: Why don't we re-check? |
254 if (subscriptions == null) | 249 if (subscriptions == null) |
255 subscriptions = abpEngine.getRecommendedSubscriptions(); | 250 subscriptions = abpEngine.getRecommendedSubscriptions(); |
256 return subscriptions; | 251 return subscriptions; |
257 } | 252 } |
258 | 253 |
259 /** | 254 /** |
260 * Returns list of enabled subscriptions. | 255 * Returns list of enabled subscriptions. |
261 */ | 256 */ |
262 public Subscription[] getListedSubscriptions() | 257 public Subscription[] getListedSubscriptions() |
263 { | 258 { |
264 return abpEngine.getListedSubscriptions(); | 259 return abpEngine.getListedSubscriptions(); |
265 } | 260 } |
266 | 261 |
267 /** | 262 /** |
268 * Adds provided subscription and removes previous subscriptions if any. | 263 * Adds provided subscription and removes previous subscriptions if any. |
269 * | 264 * |
270 * @param url | 265 * @param url |
271 * URL of subscription to add | 266 * URL of subscription to add |
272 */ | 267 */ |
273 public void setSubscription(String url) | 268 public void setSubscription(final String url) |
274 { | 269 { |
275 Subscription[] subscriptions = abpEngine.getListedSubscriptions(); | 270 abpEngine.setSubscription(url); |
276 for (Subscription subscription : subscriptions) | |
277 { | |
278 abpEngine.removeSubscription(subscription.url); | |
279 } | |
280 abpEngine.addSubscription(url); | |
281 } | 271 } |
282 | 272 |
283 /** | 273 /** |
284 * Forces subscriptions refresh. | 274 * Forces subscriptions refresh. |
285 */ | 275 */ |
286 public void refreshSubscriptions() | 276 public void refreshSubscriptions() |
287 { | 277 { |
288 Subscription[] subscriptions = abpEngine.getListedSubscriptions(); | 278 abpEngine.refreshSubscriptions(); |
289 for (Subscription subscription : subscriptions) | |
290 { | |
291 abpEngine.refreshSubscription(subscription.url); | |
292 } | |
293 } | 279 } |
294 | 280 |
295 /** | 281 /** |
296 * Enforces subscription status update. | 282 * Enforces subscription status update. |
297 * | 283 * |
298 * @param url Subscription url | 284 * @param url Subscription url |
299 */ | 285 */ |
300 public void actualizeSubscriptionStatus(String url) | 286 public void updateSubscriptionStatus(final String url) |
301 { | 287 { |
302 abpEngine.actualizeSubscriptionStatus(url); | 288 abpEngine.updateSubscriptionStatus(url); |
303 } | 289 } |
304 | 290 |
305 | |
306 /** | 291 /** |
307 * Enables or disables Acceptable Ads | 292 * Enables or disables Acceptable Ads |
308 */ | 293 */ |
309 public void setAcceptableAdsEnabled(boolean enabled) | 294 public void setAcceptableAdsEnabled(final boolean enabled) |
310 { | 295 { |
311 abpEngine.setAcceptableAdsEnabled(enabled); | 296 abpEngine.setAcceptableAdsEnabled(enabled); |
312 } | 297 } |
313 | 298 |
314 public String getAcceptableAdsUrl() | 299 public String getAcceptableAdsUrl() |
315 { | 300 { |
316 final String documentationLink = abpEngine.getDocumentationLink(); | 301 final String documentationLink = abpEngine.getDocumentationLink(); |
317 final String locale = getResources().getConfiguration().locale.toString().re
place("_", "-"); | 302 final String locale = getResources().getConfiguration().locale.toString().re
place("_", "-"); |
318 return documentationLink.replace("%LINK%", "acceptable_ads").replace("%LANG%
", locale); | 303 return documentationLink.replace("%LINK%", "acceptable_ads").replace("%LANG%
", locale); |
319 } | 304 } |
320 | 305 |
321 public void setNotifiedAboutAcceptableAds(boolean notified) | 306 public void setNotifiedAboutAcceptableAds(final boolean notified) |
322 { | 307 { |
323 final SharedPreferences preferences = PreferenceManager.getDefaultSharedPref
erences(this); | 308 final SharedPreferences preferences = PreferenceManager.getDefaultSharedPref
erences(this); |
324 final Editor editor = preferences.edit(); | 309 final Editor editor = preferences.edit(); |
325 editor.putBoolean("notified_about_acceptable_ads", notified); | 310 editor.putBoolean("notified_about_acceptable_ads", notified); |
326 editor.commit(); | 311 editor.commit(); |
327 } | 312 } |
328 | 313 |
329 public boolean isNotifiedAboutAcceptableAds() | 314 public boolean isNotifiedAboutAcceptableAds() |
330 { | 315 { |
331 final SharedPreferences preferences = PreferenceManager.getDefaultSharedPref
erences(this); | 316 final SharedPreferences preferences = PreferenceManager.getDefaultSharedPref
erences(this); |
332 return preferences.getBoolean("notified_about_acceptable_ads", false); | 317 return preferences.getBoolean("notified_about_acceptable_ads", false); |
333 } | 318 } |
334 | 319 |
335 /** | 320 /** |
336 * Returns ElemHide selectors for domain. | 321 * Returns ElemHide selectors for domain. |
337 * | 322 * |
338 * @param domain The domain | 323 * @param domain The domain |
339 * @return A list of CSS selectors | 324 * @return A list of CSS selectors |
340 */ | 325 */ |
341 public String[] getSelectorsForDomain(final String domain) | 326 public String[] getSelectorsForDomain(final String domain) |
342 { | 327 { |
343 /* We need to ignore element hiding rules here to work around two bugs: | 328 /* We need to ignore element hiding rules here to work around two bugs: |
344 * 1. CSS is being injected even when there's an exception rule with $elemhi
de | 329 * 1. CSS is being injected even when there's an exception rule with $elemhi
de |
345 * 2. The injected CSS causes blank pages in Chrome for Android | 330 * 2. The injected CSS causes blank pages in Chrome for Android |
346 * | 331 * |
347 * Starting with 1.1.2, we ignored element hiding rules after download anywa
y, to keep the | 332 * Starting with 1.1.2, we ignored element hiding rules after download anywa
y, to keep the |
348 * memory usage down. Doing this with libadblockplus is trickier, but would
be the clean | 333 * memory usage down. Doing this with libadblockplus is trickier, but would
be the clean |
349 * solution. */ | 334 * solution. */ |
| 335 |
350 return null; | 336 return null; |
351 /* | 337 /* |
352 if (!filteringEnabled) | 338 if (!filteringEnabled) |
353 return null; | 339 return null; |
354 | 340 |
355 return abpEngine.getSelectorsForDomain(domain); | 341 return abpEngine.getSelectorsForDomain(domain); |
356 */ | 342 */ |
357 } | 343 } |
358 | 344 |
359 /** | 345 /** |
360 * Checks if filters match request parameters. | 346 * Checks if filters match request parameters. |
361 * | 347 * |
362 * @param url | 348 * @param url |
363 * Request URL | 349 * Request URL |
364 * @param query | 350 * @param query |
365 * Request query string | 351 * Request query string |
366 * @param referrer | 352 * @param referrer |
367 * Request referrer header | 353 * Request referrer header |
368 * @param accept | 354 * @param accept |
369 * Request accept header | 355 * Request accept header |
370 * @return true if matched filter was found | 356 * @return true if matched filter was found |
371 * @throws Exception | 357 * @throws Exception |
372 */ | 358 */ |
373 public boolean matches(String url, String query, String referrer, String accep
t) | 359 public boolean matches(final String url, final String query, final String refe
rrer, final String accept) |
374 { | 360 { |
375 final String fullUrl = !"".equals(query) ? url + "?" + query : url; | 361 final String fullUrl = !"".equals(query) ? url + "?" + query : url; |
376 if (referrer != null) | 362 if (referrer != null) |
377 referrerMapping.put(fullUrl, referrer); | 363 referrerMapping.put(fullUrl, referrer); |
378 | 364 |
379 if (!filteringEnabled) | 365 if (!filteringEnabled) |
380 return false; | 366 return false; |
381 | 367 |
382 String contentType = null; | 368 String contentType = null; |
383 | 369 |
(...skipping 18 matching lines...) Expand all Loading... |
402 else if (RE_FONT.matcher(url).matches()) | 388 else if (RE_FONT.matcher(url).matches()) |
403 contentType = "FONT"; | 389 contentType = "FONT"; |
404 else if (RE_HTML.matcher(url).matches()) | 390 else if (RE_HTML.matcher(url).matches()) |
405 contentType = "SUBDOCUMENT"; | 391 contentType = "SUBDOCUMENT"; |
406 } | 392 } |
407 if (contentType == null) | 393 if (contentType == null) |
408 contentType = "OTHER"; | 394 contentType = "OTHER"; |
409 | 395 |
410 final List<String> referrerChain = buildReferrerChain(referrer); | 396 final List<String> referrerChain = buildReferrerChain(referrer); |
411 Log.d("Referrer chain", fullUrl + ": " + referrerChain.toString()); | 397 Log.d("Referrer chain", fullUrl + ": " + referrerChain.toString()); |
412 String[] referrerChainArray = referrerChain.toArray(new String[referrerChain
.size()]); | 398 final String[] referrerChainArray = referrerChain.toArray(new String[referre
rChain.size()]); |
413 return abpEngine.matches(fullUrl, contentType, referrerChainArray); | 399 return abpEngine.matches(fullUrl, contentType, referrerChainArray); |
414 } | 400 } |
415 | 401 |
416 private List<String> buildReferrerChain(String url) | 402 private List<String> buildReferrerChain(String url) |
417 { | 403 { |
418 final List<String> referrerChain = new ArrayList<String>(); | 404 final List<String> referrerChain = new ArrayList<String>(); |
419 // We need to limit the chain length to ensure we don't block indefinitely i
f there's | 405 // We need to limit the chain length to ensure we don't block indefinitely i
f there's |
420 // a referrer loop. | 406 // a referrer loop. |
421 final int maxChainLength = 10; | 407 final int maxChainLength = 10; |
422 for (int i = 0; i < maxChainLength && url != null; i++) | 408 for (int i = 0; i < maxChainLength && url != null; i++) |
423 { | 409 { |
424 referrerChain.add(0, url); | 410 referrerChain.add(0, url); |
425 url = referrerMapping.get(url); | 411 url = referrerMapping.get(url); |
426 } | 412 } |
427 return referrerChain; | 413 return referrerChain; |
428 } | 414 } |
429 | 415 |
430 /** | 416 /** |
431 * Checks if filtering is enabled. | 417 * Checks if filtering is enabled. |
432 */ | 418 */ |
433 public boolean isFilteringEnabled() | 419 public boolean isFilteringEnabled() |
434 { | 420 { |
435 return filteringEnabled; | 421 return filteringEnabled; |
436 } | 422 } |
437 | 423 |
438 /** | 424 /** |
439 * Enables or disables filtering. | 425 * Enables or disables filtering. |
440 */ | 426 */ |
441 public void setFilteringEnabled(boolean enable) | 427 public void setFilteringEnabled(final boolean enable) |
442 { | 428 { |
443 filteringEnabled = enable; | 429 filteringEnabled = enable; |
444 sendBroadcast(new Intent(BROADCAST_FILTERING_CHANGE).putExtra("enabled", fil
teringEnabled)); | 430 sendBroadcast(new Intent(BROADCAST_FILTERING_CHANGE).putExtra("enabled", fil
teringEnabled)); |
445 } | 431 } |
446 | 432 |
447 /** | 433 /** |
448 * Starts ABP engine. It also initiates subscription refresh if it is enabled | 434 * Starts ABP engine. It also initiates subscription refresh if it is enabled
in user settings. |
449 * in user settings. | |
450 */ | 435 */ |
451 public void startEngine() | 436 public void startEngine() |
452 { | 437 { |
453 if (abpEngine == null) | 438 if (abpEngine == null) |
454 { | 439 { |
455 File basePath = getFilesDir(); | 440 final File basePath = getFilesDir(); |
456 abpEngine = new ABPEngine(this, basePath.getAbsolutePath()); | 441 abpEngine = ABPEngine.create(AdblockPlus.getApplication(), ABPEngine.gener
ateAppInfo(this), basePath.getAbsolutePath()); |
457 } | 442 } |
458 } | 443 } |
459 | 444 |
460 /** | 445 /** |
461 * Stops ABP engine. | 446 * Stops ABP engine. |
462 */ | 447 */ |
463 public void stopEngine() | 448 public void stopEngine() |
464 { | 449 { |
465 if (abpEngine != null) | 450 if (abpEngine != null) |
466 { | 451 { |
467 abpEngine.release(); | 452 abpEngine.dispose(); |
468 abpEngine = null; | 453 abpEngine = null; |
469 Log.i(TAG, "stopEngine"); | 454 Log.i(TAG, "stopEngine"); |
470 } | 455 } |
471 } | 456 } |
472 | 457 |
473 /** | 458 /** |
474 * Initiates immediate interactive check for available update. | 459 * Initiates immediate interactive check for available update. |
475 */ | 460 */ |
476 public void checkUpdates() | 461 public void checkUpdates() |
477 { | 462 { |
478 abpEngine.checkUpdates(); | 463 abpEngine.checkForUpdates(); |
479 } | 464 } |
480 | 465 |
481 /** | 466 /** |
482 * Sets Alarm to call updater after specified number of minutes or after one | 467 * Sets Alarm to call updater after specified number of minutes or after one |
483 * day if | 468 * day if |
484 * minutes are set to 0. | 469 * minutes are set to 0. |
485 * | 470 * |
486 * @param minutes | 471 * @param minutes |
487 * number of minutes to wait | 472 * number of minutes to wait |
488 */ | 473 */ |
489 public void scheduleUpdater(int minutes) | 474 public void scheduleUpdater(final int minutes) |
490 { | 475 { |
491 Calendar updateTime = Calendar.getInstance(); | 476 final Calendar updateTime = Calendar.getInstance(); |
492 | 477 |
493 if (minutes == 0) | 478 if (minutes == 0) |
494 { | 479 { |
495 // Start update checks at 10:00 GMT... | 480 // Start update checks at 10:00 GMT... |
496 updateTime.setTimeZone(TimeZone.getTimeZone("GMT")); | 481 updateTime.setTimeZone(TimeZone.getTimeZone("GMT")); |
497 updateTime.set(Calendar.HOUR_OF_DAY, 10); | 482 updateTime.set(Calendar.HOUR_OF_DAY, 10); |
498 updateTime.set(Calendar.MINUTE, 0); | 483 updateTime.set(Calendar.MINUTE, 0); |
499 // ...next day | 484 // ...next day |
500 updateTime.add(Calendar.HOUR_OF_DAY, 24); | 485 updateTime.add(Calendar.HOUR_OF_DAY, 24); |
501 // Spread out the “mass downloading” for 6 hours | 486 // Spread out the “mass downloading” for 6 hours |
502 updateTime.add(Calendar.MINUTE, (int) (Math.random() * 60 * 6)); | 487 updateTime.add(Calendar.MINUTE, (int) (Math.random() * 60 * 6)); |
503 } | 488 } |
504 else | 489 else |
505 { | 490 { |
506 updateTime.add(Calendar.MINUTE, minutes); | 491 updateTime.add(Calendar.MINUTE, minutes); |
507 } | 492 } |
508 | 493 |
509 Intent updater = new Intent(this, AlarmReceiver.class); | 494 final Intent updater = new Intent(this, AlarmReceiver.class); |
510 PendingIntent recurringUpdate = PendingIntent.getBroadcast(this, 0, updater,
PendingIntent.FLAG_CANCEL_CURRENT); | 495 final PendingIntent recurringUpdate = PendingIntent.getBroadcast(this, 0, up
dater, PendingIntent.FLAG_CANCEL_CURRENT); |
511 // Set non-waking alarm | 496 // Set non-waking alarm |
512 AlarmManager alarms = (AlarmManager) getSystemService(Context.ALARM_SERVICE)
; | 497 final AlarmManager alarms = (AlarmManager) getSystemService(Context.ALARM_SE
RVICE); |
513 alarms.set(AlarmManager.RTC, updateTime.getTimeInMillis(), recurringUpdate); | 498 alarms.set(AlarmManager.RTC, updateTime.getTimeInMillis(), recurringUpdate); |
514 } | 499 } |
515 | 500 |
516 @Override | 501 @Override |
517 public void onCreate() | 502 public void onCreate() |
518 { | 503 { |
519 super.onCreate(); | 504 super.onCreate(); |
520 instance = this; | 505 instance = this; |
521 | 506 |
522 // Check for crash report | 507 // Check for crash report |
523 try | 508 try |
524 { | 509 { |
525 InputStreamReader reportFile = new InputStreamReader(openFileInput(CrashHa
ndler.REPORT_FILE)); | 510 final InputStreamReader reportFile = new InputStreamReader(openFileInput(C
rashHandler.REPORT_FILE)); |
526 final char[] buffer = new char[0x1000]; | 511 final char[] buffer = new char[0x1000]; |
527 StringBuilder out = new StringBuilder(); | 512 final StringBuilder out = new StringBuilder(); |
528 int read; | 513 int read; |
529 do | 514 do |
530 { | 515 { |
531 read = reportFile.read(buffer, 0, buffer.length); | 516 read = reportFile.read(buffer, 0, buffer.length); |
532 if (read > 0) | 517 if (read > 0) |
533 out.append(buffer, 0, read); | 518 out.append(buffer, 0, read); |
534 } | 519 } |
535 while (read >= 0); | 520 while (read >= 0); |
536 String report = out.toString(); | 521 final String report = out.toString(); |
537 if (!"".equals(report)) | 522 if (StringUtils.isNotEmpty(report)) |
538 { | 523 { |
539 final Intent intent = new Intent(this, CrashReportDialog.class); | 524 final Intent intent = new Intent(this, CrashReportDialog.class); |
540 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | 525 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
541 intent.putExtra("report", report); | 526 intent.putExtra("report", report); |
542 startActivity(intent); | 527 startActivity(intent); |
543 } | 528 } |
544 } | 529 } |
545 catch (FileNotFoundException e) | 530 catch (final FileNotFoundException e) |
546 { | 531 { |
547 // ignore | 532 // ignore |
548 } | 533 } |
549 catch (IOException e) | 534 catch (final IOException e) |
550 { | 535 { |
551 // TODO Auto-generated catch block | |
552 Log.e(TAG, e.getMessage(), e); | 536 Log.e(TAG, e.getMessage(), e); |
553 } | 537 } |
554 | 538 |
555 // Set crash handler | 539 // Set crash handler |
556 Thread.setDefaultUncaughtExceptionHandler(new CrashHandler(this)); | 540 Thread.setDefaultUncaughtExceptionHandler(new CrashHandler(this)); |
557 | 541 |
558 // Initiate update check | 542 // Initiate update check |
559 scheduleUpdater(0); | 543 scheduleUpdater(0); |
560 } | 544 } |
561 } | 545 } |
OLD | NEW |