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); |
| 220 |
225 // Actually it returns not only running services, so extra check is required | 221 // Actually it returns not only running services, so extra check is required |
226 for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VAL
UE)) | 222 for (final RunningServiceInfo service : manager.getRunningServices(Integer.M
AX_VALUE)) |
227 { | 223 { |
228 if (service.service.getClassName().equals(ProxyService.class.getCanonicalN
ame()) && service.pid > 0) | 224 if (service.service.getClassName().equals(ProxyService.class.getCanonicalN
ame()) && service.pid > 0) |
229 return true; | 225 return true; |
230 } | 226 } |
231 return false; | 227 return false; |
232 } | 228 } |
233 | 229 |
234 /** | 230 /** |
235 * Checks if application can write to external storage. | 231 * Checks if application can write to external storage. |
236 */ | 232 */ |
237 public boolean checkWriteExternalPermission() | 233 public boolean checkWriteExternalPermission() |
238 { | 234 { |
239 String permission = "android.permission.WRITE_EXTERNAL_STORAGE"; | 235 final String permission = "android.permission.WRITE_EXTERNAL_STORAGE"; |
240 int res = checkCallingOrSelfPermission(permission); | 236 final int res = checkCallingOrSelfPermission(permission); |
241 return res == PackageManager.PERMISSION_GRANTED; | 237 return res == PackageManager.PERMISSION_GRANTED; |
242 } | 238 } |
243 | 239 |
244 public boolean isFirstRun() | 240 public boolean isFirstRun() |
245 { | 241 { |
246 return abpEngine.isFirstRun(); | 242 return abpEngine.isFirstRun(); |
247 } | 243 } |
248 | 244 |
249 /** | 245 /** |
250 * Returns list of known subscriptions. | 246 * Returns list of known subscriptions. |
251 */ | 247 */ |
252 public Subscription[] getRecommendedSubscriptions() | 248 public Subscription[] getRecommendedSubscriptions() |
253 { | 249 { |
| 250 // TODO: Why don't we re-check? |
254 if (subscriptions == null) | 251 if (subscriptions == null) |
255 subscriptions = abpEngine.getRecommendedSubscriptions(); | 252 subscriptions = abpEngine.getRecommendedSubscriptions(); |
256 return subscriptions; | 253 return subscriptions; |
257 } | 254 } |
258 | 255 |
259 /** | 256 /** |
260 * Returns list of enabled subscriptions. | 257 * Returns list of enabled subscriptions. |
261 */ | 258 */ |
262 public Subscription[] getListedSubscriptions() | 259 public Subscription[] getListedSubscriptions() |
263 { | 260 { |
264 return abpEngine.getListedSubscriptions(); | 261 return abpEngine.getListedSubscriptions(); |
265 } | 262 } |
266 | 263 |
267 /** | 264 /** |
268 * Adds provided subscription and removes previous subscriptions if any. | 265 * Adds provided subscription and removes previous subscriptions if any. |
269 * | 266 * |
270 * @param url | 267 * @param url |
271 * URL of subscription to add | 268 * URL of subscription to add |
272 */ | 269 */ |
273 public void setSubscription(String url) | 270 public void setSubscription(final String url) |
274 { | 271 { |
275 Subscription[] subscriptions = abpEngine.getListedSubscriptions(); | 272 abpEngine.setSubscription(url); |
276 for (Subscription subscription : subscriptions) | |
277 { | |
278 abpEngine.removeSubscription(subscription.url); | |
279 } | |
280 abpEngine.addSubscription(url); | |
281 } | 273 } |
282 | 274 |
283 /** | 275 /** |
284 * Forces subscriptions refresh. | 276 * Forces subscriptions refresh. |
285 */ | 277 */ |
286 public void refreshSubscriptions() | 278 public void refreshSubscriptions() |
287 { | 279 { |
288 Subscription[] subscriptions = abpEngine.getListedSubscriptions(); | 280 abpEngine.refreshSubscriptions(); |
289 for (Subscription subscription : subscriptions) | |
290 { | |
291 abpEngine.refreshSubscription(subscription.url); | |
292 } | |
293 } | 281 } |
294 | 282 |
295 /** | 283 /** |
296 * Enforces subscription status update. | 284 * Enforces subscription status update. |
297 * | 285 * |
298 * @param url Subscription url | 286 * @param url Subscription url |
299 */ | 287 */ |
300 public void actualizeSubscriptionStatus(String url) | 288 public void updateSubscriptionStatus(final String url) |
301 { | 289 { |
302 abpEngine.actualizeSubscriptionStatus(url); | 290 abpEngine.updateSubscriptionStatus(url); |
303 } | 291 } |
304 | 292 |
305 | |
306 /** | 293 /** |
307 * Enables or disables Acceptable Ads | 294 * Enables or disables Acceptable Ads |
308 */ | 295 */ |
309 public void setAcceptableAdsEnabled(boolean enabled) | 296 public void setAcceptableAdsEnabled(final boolean enabled) |
310 { | 297 { |
311 abpEngine.setAcceptableAdsEnabled(enabled); | 298 abpEngine.setAcceptableAdsEnabled(enabled); |
312 } | 299 } |
313 | 300 |
314 public String getAcceptableAdsUrl() | 301 public String getAcceptableAdsUrl() |
315 { | 302 { |
316 final String documentationLink = abpEngine.getDocumentationLink(); | 303 final String documentationLink = abpEngine.getDocumentationLink(); |
317 final String locale = getResources().getConfiguration().locale.toString().re
place("_", "-"); | 304 final String locale = getResources().getConfiguration().locale.toString().re
place("_", "-"); |
318 return documentationLink.replace("%LINK%", "acceptable_ads").replace("%LANG%
", locale); | 305 return documentationLink.replace("%LINK%", "acceptable_ads").replace("%LANG%
", locale); |
319 } | 306 } |
320 | 307 |
321 public void setNotifiedAboutAcceptableAds(boolean notified) | 308 public void setNotifiedAboutAcceptableAds(final boolean notified) |
322 { | 309 { |
323 final SharedPreferences preferences = PreferenceManager.getDefaultSharedPref
erences(this); | 310 final SharedPreferences preferences = PreferenceManager.getDefaultSharedPref
erences(this); |
324 final Editor editor = preferences.edit(); | 311 final Editor editor = preferences.edit(); |
325 editor.putBoolean("notified_about_acceptable_ads", notified); | 312 editor.putBoolean("notified_about_acceptable_ads", notified); |
326 editor.commit(); | 313 editor.commit(); |
327 } | 314 } |
328 | 315 |
329 public boolean isNotifiedAboutAcceptableAds() | 316 public boolean isNotifiedAboutAcceptableAds() |
330 { | 317 { |
331 final SharedPreferences preferences = PreferenceManager.getDefaultSharedPref
erences(this); | 318 final SharedPreferences preferences = PreferenceManager.getDefaultSharedPref
erences(this); |
332 return preferences.getBoolean("notified_about_acceptable_ads", false); | 319 return preferences.getBoolean("notified_about_acceptable_ads", false); |
333 } | 320 } |
334 | 321 |
335 /** | 322 /** |
336 * Returns ElemHide selectors for domain. | 323 * Returns ElemHide selectors for domain. |
337 * | 324 * |
338 * @param domain The domain | 325 * @param domain The domain |
339 * @return A list of CSS selectors | 326 * @return A list of CSS selectors |
340 */ | 327 */ |
341 public String[] getSelectorsForDomain(final String domain) | 328 public String[] getSelectorsForDomain(final String domain) |
342 { | 329 { |
343 /* We need to ignore element hiding rules here to work around two bugs: | 330 /* 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 | 331 * 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 | 332 * 2. The injected CSS causes blank pages in Chrome for Android |
346 * | 333 * |
347 * Starting with 1.1.2, we ignored element hiding rules after download anywa
y, to keep the | 334 * 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 | 335 * memory usage down. Doing this with libadblockplus is trickier, but would
be the clean |
349 * solution. */ | 336 * solution. |
| 337 */ |
350 return null; | 338 return null; |
351 /* | 339 /* |
352 if (!filteringEnabled) | 340 if (!filteringEnabled) |
353 return null; | 341 return null; |
354 | 342 |
355 return abpEngine.getSelectorsForDomain(domain); | 343 return abpEngine.getSelectorsForDomain(domain); |
356 */ | 344 */ |
357 } | 345 } |
358 | 346 |
359 /** | 347 /** |
360 * Checks if filters match request parameters. | 348 * Checks if filters match request parameters. |
361 * | 349 * |
362 * @param url | 350 * @param url |
363 * Request URL | 351 * Request URL |
364 * @param query | 352 * @param query |
365 * Request query string | 353 * Request query string |
366 * @param referrer | 354 * @param referrer |
367 * Request referrer header | 355 * Request referrer header |
368 * @param accept | 356 * @param accept |
369 * Request accept header | 357 * Request accept header |
370 * @return true if matched filter was found | 358 * @return true if matched filter was found |
371 * @throws Exception | 359 * @throws Exception |
372 */ | 360 */ |
373 public boolean matches(String url, String query, String referrer, String accep
t) | 361 public boolean matches(final String url, final String query, final String refe
rrer, final String accept) |
374 { | 362 { |
375 final String fullUrl = !"".equals(query) ? url + "?" + query : url; | 363 final String fullUrl = !"".equals(query) ? url + "?" + query : url; |
376 if (referrer != null) | 364 if (referrer != null) |
377 referrerMapping.put(fullUrl, referrer); | 365 referrerMapping.put(fullUrl, referrer); |
378 | 366 |
379 if (!filteringEnabled) | 367 if (!filteringEnabled) |
380 return false; | 368 return false; |
381 | 369 |
382 String contentType = null; | 370 String contentType = null; |
383 | 371 |
(...skipping 18 matching lines...) Expand all Loading... |
402 else if (RE_FONT.matcher(url).matches()) | 390 else if (RE_FONT.matcher(url).matches()) |
403 contentType = "FONT"; | 391 contentType = "FONT"; |
404 else if (RE_HTML.matcher(url).matches()) | 392 else if (RE_HTML.matcher(url).matches()) |
405 contentType = "SUBDOCUMENT"; | 393 contentType = "SUBDOCUMENT"; |
406 } | 394 } |
407 if (contentType == null) | 395 if (contentType == null) |
408 contentType = "OTHER"; | 396 contentType = "OTHER"; |
409 | 397 |
410 final List<String> referrerChain = buildReferrerChain(referrer); | 398 final List<String> referrerChain = buildReferrerChain(referrer); |
411 Log.d("Referrer chain", fullUrl + ": " + referrerChain.toString()); | 399 Log.d("Referrer chain", fullUrl + ": " + referrerChain.toString()); |
412 String[] referrerChainArray = referrerChain.toArray(new String[referrerChain
.size()]); | 400 final String[] referrerChainArray = referrerChain.toArray(new String[referre
rChain.size()]); |
413 return abpEngine.matches(fullUrl, contentType, referrerChainArray); | 401 return abpEngine.matches(fullUrl, contentType, referrerChainArray); |
414 } | 402 } |
415 | 403 |
416 private List<String> buildReferrerChain(String url) | 404 private List<String> buildReferrerChain(String url) |
417 { | 405 { |
418 final List<String> referrerChain = new ArrayList<String>(); | 406 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 | 407 // We need to limit the chain length to ensure we don't block indefinitely i
f there's |
420 // a referrer loop. | 408 // a referrer loop. |
421 final int maxChainLength = 10; | 409 final int maxChainLength = 10; |
422 for (int i = 0; i < maxChainLength && url != null; i++) | 410 for (int i = 0; i < maxChainLength && url != null; i++) |
423 { | 411 { |
424 referrerChain.add(0, url); | 412 referrerChain.add(0, url); |
425 url = referrerMapping.get(url); | 413 url = referrerMapping.get(url); |
426 } | 414 } |
427 return referrerChain; | 415 return referrerChain; |
428 } | 416 } |
429 | 417 |
430 /** | 418 /** |
431 * Checks if filtering is enabled. | 419 * Checks if filtering is enabled. |
432 */ | 420 */ |
433 public boolean isFilteringEnabled() | 421 public boolean isFilteringEnabled() |
434 { | 422 { |
435 return filteringEnabled; | 423 return filteringEnabled; |
436 } | 424 } |
437 | 425 |
438 /** | 426 /** |
439 * Enables or disables filtering. | 427 * Enables or disables filtering. |
440 */ | 428 */ |
441 public void setFilteringEnabled(boolean enable) | 429 public void setFilteringEnabled(final boolean enable) |
442 { | 430 { |
443 filteringEnabled = enable; | 431 filteringEnabled = enable; |
444 sendBroadcast(new Intent(BROADCAST_FILTERING_CHANGE).putExtra("enabled", fil
teringEnabled)); | 432 sendBroadcast(new Intent(BROADCAST_FILTERING_CHANGE).putExtra("enabled", fil
teringEnabled)); |
445 } | 433 } |
446 | 434 |
447 /** | 435 /** |
448 * Starts ABP engine. It also initiates subscription refresh if it is enabled | 436 * Starts ABP engine. It also initiates subscription refresh if it is enabled
in user settings. |
449 * in user settings. | |
450 */ | 437 */ |
451 public void startEngine() | 438 public void startEngine() |
452 { | 439 { |
453 if (abpEngine == null) | 440 if (abpEngine == null) |
454 { | 441 { |
455 File basePath = getFilesDir(); | 442 final File basePath = getFilesDir(); |
456 abpEngine = new ABPEngine(this, basePath.getAbsolutePath()); | 443 abpEngine = ABPEngine.create(AdblockPlus.getApplication(), ABPEngine.gener
ateAppInfo(this), basePath.getAbsolutePath()); |
457 } | 444 } |
458 } | 445 } |
459 | 446 |
460 /** | 447 /** |
461 * Stops ABP engine. | 448 * Stops ABP engine. |
462 */ | 449 */ |
463 public void stopEngine() | 450 public void stopEngine() |
464 { | 451 { |
465 if (abpEngine != null) | 452 if (abpEngine != null) |
466 { | 453 { |
467 abpEngine.release(); | 454 abpEngine.dispose(); |
468 abpEngine = null; | 455 abpEngine = null; |
469 Log.i(TAG, "stopEngine"); | 456 Log.i(TAG, "stopEngine"); |
470 } | 457 } |
471 } | 458 } |
472 | 459 |
473 /** | 460 /** |
474 * Initiates immediate interactive check for available update. | 461 * Initiates immediate interactive check for available update. |
475 */ | 462 */ |
476 public void checkUpdates() | 463 public void checkUpdates() |
477 { | 464 { |
478 abpEngine.checkUpdates(); | 465 abpEngine.checkForUpdates(); |
479 } | 466 } |
480 | 467 |
481 /** | 468 /** |
482 * Sets Alarm to call updater after specified number of minutes or after one | 469 * Sets Alarm to call updater after specified number of minutes or after one |
483 * day if | 470 * day if |
484 * minutes are set to 0. | 471 * minutes are set to 0. |
485 * | 472 * |
486 * @param minutes | 473 * @param minutes |
487 * number of minutes to wait | 474 * number of minutes to wait |
488 */ | 475 */ |
489 public void scheduleUpdater(int minutes) | 476 public void scheduleUpdater(final int minutes) |
490 { | 477 { |
491 Calendar updateTime = Calendar.getInstance(); | 478 final Calendar updateTime = Calendar.getInstance(); |
492 | 479 |
493 if (minutes == 0) | 480 if (minutes == 0) |
494 { | 481 { |
495 // Start update checks at 10:00 GMT... | 482 // Start update checks at 10:00 GMT... |
496 updateTime.setTimeZone(TimeZone.getTimeZone("GMT")); | 483 updateTime.setTimeZone(TimeZone.getTimeZone("GMT")); |
497 updateTime.set(Calendar.HOUR_OF_DAY, 10); | 484 updateTime.set(Calendar.HOUR_OF_DAY, 10); |
498 updateTime.set(Calendar.MINUTE, 0); | 485 updateTime.set(Calendar.MINUTE, 0); |
499 // ...next day | 486 // ...next day |
500 updateTime.add(Calendar.HOUR_OF_DAY, 24); | 487 updateTime.add(Calendar.HOUR_OF_DAY, 24); |
501 // Spread out the “mass downloading” for 6 hours | 488 // Spread out the “mass downloading” for 6 hours |
502 updateTime.add(Calendar.MINUTE, (int) (Math.random() * 60 * 6)); | 489 updateTime.add(Calendar.MINUTE, (int) (Math.random() * 60 * 6)); |
503 } | 490 } |
504 else | 491 else |
505 { | 492 { |
506 updateTime.add(Calendar.MINUTE, minutes); | 493 updateTime.add(Calendar.MINUTE, minutes); |
507 } | 494 } |
508 | 495 |
509 Intent updater = new Intent(this, AlarmReceiver.class); | 496 final Intent updater = new Intent(this, AlarmReceiver.class); |
510 PendingIntent recurringUpdate = PendingIntent.getBroadcast(this, 0, updater,
PendingIntent.FLAG_CANCEL_CURRENT); | 497 final PendingIntent recurringUpdate = PendingIntent.getBroadcast(this, 0, up
dater, PendingIntent.FLAG_CANCEL_CURRENT); |
511 // Set non-waking alarm | 498 // Set non-waking alarm |
512 AlarmManager alarms = (AlarmManager) getSystemService(Context.ALARM_SERVICE)
; | 499 final AlarmManager alarms = (AlarmManager) getSystemService(Context.ALARM_SE
RVICE); |
513 alarms.set(AlarmManager.RTC, updateTime.getTimeInMillis(), recurringUpdate); | 500 alarms.set(AlarmManager.RTC, updateTime.getTimeInMillis(), recurringUpdate); |
514 } | 501 } |
515 | 502 |
516 @Override | 503 @Override |
517 public void onCreate() | 504 public void onCreate() |
518 { | 505 { |
519 super.onCreate(); | 506 super.onCreate(); |
520 instance = this; | 507 instance = this; |
521 | 508 |
522 // Check for crash report | 509 // Check for crash report |
523 try | 510 try |
524 { | 511 { |
525 InputStreamReader reportFile = new InputStreamReader(openFileInput(CrashHa
ndler.REPORT_FILE)); | 512 final InputStreamReader reportFile = new InputStreamReader(openFileInput(C
rashHandler.REPORT_FILE)); |
526 final char[] buffer = new char[0x1000]; | 513 final char[] buffer = new char[0x1000]; |
527 StringBuilder out = new StringBuilder(); | 514 final StringBuilder out = new StringBuilder(); |
528 int read; | 515 int read; |
529 do | 516 do |
530 { | 517 { |
531 read = reportFile.read(buffer, 0, buffer.length); | 518 read = reportFile.read(buffer, 0, buffer.length); |
532 if (read > 0) | 519 if (read > 0) |
533 out.append(buffer, 0, read); | 520 out.append(buffer, 0, read); |
534 } | 521 } |
535 while (read >= 0); | 522 while (read >= 0); |
536 String report = out.toString(); | 523 final String report = out.toString(); |
537 if (!"".equals(report)) | 524 if (!Strings.isEmpty(report)) |
538 { | 525 { |
539 final Intent intent = new Intent(this, CrashReportDialog.class); | 526 final Intent intent = new Intent(this, CrashReportDialog.class); |
540 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | 527 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
541 intent.putExtra("report", report); | 528 intent.putExtra("report", report); |
542 startActivity(intent); | 529 startActivity(intent); |
543 } | 530 } |
544 } | 531 } |
545 catch (FileNotFoundException e) | 532 catch (final FileNotFoundException e) |
546 { | 533 { |
547 // ignore | 534 // ignore |
548 } | 535 } |
549 catch (IOException e) | 536 catch (final IOException e) |
550 { | 537 { |
551 // TODO Auto-generated catch block | |
552 Log.e(TAG, e.getMessage(), e); | 538 Log.e(TAG, e.getMessage(), e); |
553 } | 539 } |
554 | 540 |
555 // Set crash handler | 541 // Set crash handler |
556 Thread.setDefaultUncaughtExceptionHandler(new CrashHandler(this)); | 542 Thread.setDefaultUncaughtExceptionHandler(new CrashHandler(this)); |
557 | 543 |
558 // Initiate update check | 544 // Initiate update check |
559 scheduleUpdater(0); | 545 scheduleUpdater(0); |
560 } | 546 } |
561 } | 547 } |
OLD | NEW |