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