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