OLD | NEW |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 * Copyright (C) 2006-present eyeo GmbH | 3 * Copyright (C) 2006-present 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 25 matching lines...) Expand all Loading... |
36 import java.util.HashMap; | 36 import java.util.HashMap; |
37 import java.util.List; | 37 import java.util.List; |
38 import java.util.Map; | 38 import java.util.Map; |
39 import java.util.Set; | 39 import java.util.Set; |
40 import java.util.TreeSet; | 40 import java.util.TreeSet; |
41 import java.util.concurrent.LinkedBlockingQueue; | 41 import java.util.concurrent.LinkedBlockingQueue; |
42 import java.util.concurrent.TimeUnit; | 42 import java.util.concurrent.TimeUnit; |
43 import java.util.concurrent.locks.ReentrantLock; | 43 import java.util.concurrent.locks.ReentrantLock; |
44 | 44 |
45 import org.adblockplus.adblockplussbrowser.R; | 45 import org.adblockplus.adblockplussbrowser.R; |
| 46 import org.adblockplus.sbrowser.contentblocker.util.ConnectivityUtils; |
46 import org.adblockplus.sbrowser.contentblocker.util.SharedPrefsUtils; | 47 import org.adblockplus.sbrowser.contentblocker.util.SharedPrefsUtils; |
47 import org.adblockplus.sbrowser.contentblocker.util.SubscriptionUtils; | 48 import org.adblockplus.sbrowser.contentblocker.util.SubscriptionUtils; |
48 | 49 |
49 import android.content.Context; | 50 import android.content.Context; |
50 import android.content.Intent; | 51 import android.content.Intent; |
51 import android.content.pm.PackageInfo; | 52 import android.content.pm.PackageInfo; |
52 import android.content.pm.PackageManager; | 53 import android.content.pm.PackageManager; |
53 import android.content.pm.ResolveInfo; | 54 import android.content.pm.ResolveInfo; |
54 import android.net.ConnectivityManager; | 55 import android.net.ConnectivityManager; |
55 import android.net.NetworkInfo; | 56 import android.net.NetworkInfo; |
56 import android.net.Uri; | 57 import android.net.Uri; |
57 import android.os.Handler; | 58 import android.os.Handler; |
58 import android.os.Looper; | 59 import android.os.Looper; |
59 import android.text.TextUtils; | 60 import android.text.TextUtils; |
60 import android.text.format.DateUtils; | 61 import android.text.format.DateUtils; |
61 import android.util.Log; | 62 import android.util.Log; |
| 63 import android.widget.Toast; |
62 | 64 |
63 public final class Engine | 65 public final class Engine |
64 { | 66 { |
65 private static final String TAG = Engine.class.getSimpleName(); | 67 private static final String TAG = Engine.class.getSimpleName(); |
66 | 68 |
67 public static final String USER_FILTERS_TITLE = "__filters"; | 69 public static final String USER_FILTERS_TITLE = "__filters"; |
68 public static final String USER_EXCEPTIONS_TITLE = "__exceptions"; | 70 public static final String USER_EXCEPTIONS_TITLE = "__exceptions"; |
69 | 71 |
70 public static final String SBROWSER_APP_ID = "com.sec.android.app.sbrowser"; | 72 public static final String SBROWSER_APP_ID = "com.sec.android.app.sbrowser"; |
71 public static final String EASYLIST_URL = "https://easylist-downloads.adblockp
lus.org/easylist.txt"; | 73 public static final String EASYLIST_URL = "https://easylist-downloads.adblockp
lus.org/easylist.txt"; |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
189 public void run() | 191 public void run() |
190 { | 192 { |
191 final Intent intent = new Intent(); | 193 final Intent intent = new Intent(); |
192 intent.setAction(ACTION_UPDATE); | 194 intent.setAction(ACTION_UPDATE); |
193 intent.setData(Uri.parse("package:" + Engine.this.serviceContext.getPack
ageName())); | 195 intent.setData(Uri.parse("package:" + Engine.this.serviceContext.getPack
ageName())); |
194 Engine.this.serviceContext.sendBroadcast(intent); | 196 Engine.this.serviceContext.sendBroadcast(intent); |
195 } | 197 } |
196 }); | 198 }); |
197 } | 199 } |
198 | 200 |
199 boolean canUseInternet() | 201 boolean canUseInternet(final boolean allowMetered) |
200 { | 202 { |
201 final ConnectivityManager connManager = (ConnectivityManager) this.serviceCo
ntext | 203 // allow a metered connection to update default subscriptions at the first r
un. |
202 .getSystemService(Context.CONNECTIVITY_SERVICE); | 204 // See https://issues.adblockplus.org/ticket/5237 |
203 final NetworkInfo current = connManager.getActiveNetworkInfo(); | 205 return ConnectivityUtils.canUseInternet(serviceContext, allowMetered || wasF
irstRun()); |
204 if (current == null) | 206 } |
| 207 |
| 208 public void forceUpdateSubscriptions(final boolean allowMetered) |
| 209 { |
| 210 try |
205 { | 211 { |
206 return false; | 212 subscriptions.checkForUpdates(true, allowMetered); |
| 213 Toast.makeText(serviceContext, serviceContext.getText(R.string.updating_su
bscriptions), Toast.LENGTH_LONG).show(); |
207 } | 214 } |
208 | 215 catch (IOException e) |
209 if (wasFirstRun()) | |
210 { | 216 { |
211 return true; | 217 Log.e(TAG, "Failed checking for updates", e); |
212 } | 218 } |
213 | |
214 final boolean wifiOnly = "1".equals(SharedPrefsUtils.getString( | |
215 this.serviceContext, R.string.key_automatic_updates , "1")); | |
216 | |
217 if (wifiOnly) | |
218 { | |
219 if (current.isConnected() && !current.isRoaming()) | |
220 { | |
221 switch (current.getType()) | |
222 { | |
223 case ConnectivityManager.TYPE_BLUETOOTH: | |
224 case ConnectivityManager.TYPE_ETHERNET: | |
225 case ConnectivityManager.TYPE_WIFI: | |
226 case ConnectivityManager.TYPE_WIMAX: | |
227 return true; | |
228 default: | |
229 return false; | |
230 } | |
231 } | |
232 return false; | |
233 } | |
234 return current.isConnected(); | |
235 } | 219 } |
236 | 220 |
237 public List<SubscriptionInfo> getListedSubscriptions() | 221 public List<SubscriptionInfo> getListedSubscriptions() |
238 { | 222 { |
239 return this.subscriptions.getSubscriptions(this); | 223 return this.subscriptions.getSubscriptions(this); |
240 } | 224 } |
241 | 225 |
242 public void changeSubscriptionState(final String id, final boolean enabled) | 226 public void changeSubscriptionState(final String id, final boolean enabled) |
243 { | 227 { |
244 if (this.subscriptionUpdateCallback != null) | 228 if (this.subscriptionUpdateCallback != null) |
(...skipping 437 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
682 Log.d(TAG, "Unhandled type: " + event.getType()); | 666 Log.d(TAG, "Unhandled type: " + event.getType()); |
683 break; | 667 break; |
684 } | 668 } |
685 } | 669 } |
686 | 670 |
687 final long currentTime = System.currentTimeMillis(); | 671 final long currentTime = System.currentTimeMillis(); |
688 if (currentTime > nextUpdateCheck) | 672 if (currentTime > nextUpdateCheck) |
689 { | 673 { |
690 nextUpdateCheck = currentTime + UPDATE_CHECK_INTERVAL; | 674 nextUpdateCheck = currentTime + UPDATE_CHECK_INTERVAL; |
691 | 675 |
692 this.engine.subscriptions.checkForUpdates(); | 676 this.engine.subscriptions.checkForUpdates(false, false); |
693 } | 677 } |
694 | 678 |
695 if (currentTime > this.engine.nextUpdateBroadcast) | 679 if (currentTime > this.engine.nextUpdateBroadcast) |
696 { | 680 { |
697 this.engine.nextUpdateBroadcast = Long.MAX_VALUE; | 681 this.engine.nextUpdateBroadcast = Long.MAX_VALUE; |
698 Log.d(TAG, "Sending update broadcast"); | 682 Log.d(TAG, "Sending update broadcast"); |
699 this.engine.writeFileAndSendUpdateBroadcast(); | 683 this.engine.writeFileAndSendUpdateBroadcast(); |
700 } | 684 } |
701 } | 685 } |
702 finally | 686 finally |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
769 this.id = id; | 753 this.id = id; |
770 this.responseCode = responseCode; | 754 this.responseCode = responseCode; |
771 this.response = response; | 755 this.response = response; |
772 if (headers != null) | 756 if (headers != null) |
773 { | 757 { |
774 this.headers.putAll(headers); | 758 this.headers.putAll(headers); |
775 } | 759 } |
776 } | 760 } |
777 } | 761 } |
778 | 762 |
779 public void enqueueDownload(final Subscription sub, final boolean forced) thro
ws IOException | 763 public void enqueueDownload(final Subscription sub, final boolean forced, |
| 764 final boolean allowMetered) throws IOException |
780 { | 765 { |
781 if (sub.getURL() != null && sub.shouldUpdate(forced)) | 766 if (sub.getURL() != null && sub.shouldUpdate(forced)) |
782 { | 767 { |
783 final HashMap<String, String> headers = new HashMap<>(); | 768 final HashMap<String, String> headers = new HashMap<>(); |
784 if (sub.isMetaDataValid() && sub.isFiltersValid()) | 769 if (sub.isMetaDataValid() && sub.isFiltersValid()) |
785 { | 770 { |
786 final String lastModified = sub.getMeta(Subscription.KEY_HTTP_LAST_MODIF
IED); | 771 final String lastModified = sub.getMeta(Subscription.KEY_HTTP_LAST_MODIF
IED); |
787 if (!TextUtils.isEmpty(lastModified)) | 772 if (!TextUtils.isEmpty(lastModified)) |
788 { | 773 { |
789 headers.put("If-Modified-Since", lastModified); | 774 headers.put("If-Modified-Since", lastModified); |
790 } | 775 } |
791 final String etag = sub.getMeta(Subscription.KEY_HTTP_ETAG); | 776 final String etag = sub.getMeta(Subscription.KEY_HTTP_ETAG); |
792 if (!TextUtils.isEmpty(etag)) | 777 if (!TextUtils.isEmpty(etag)) |
793 { | 778 { |
794 headers.put("If-None-Match", etag); | 779 headers.put("If-None-Match", etag); |
795 } | 780 } |
796 } | 781 } |
797 Log.d(TAG, headers.toString()); | 782 Log.d(TAG, headers.toString()); |
798 this.downloader.enqueueDownload(this.createDownloadURL(sub), sub.getId(),
headers); | 783 this.downloader.enqueueDownload(this.createDownloadURL(sub), sub.getId(),
headers, allowMetered); |
799 } | 784 } |
800 } | 785 } |
801 | 786 |
802 public void connectivityChanged() | 787 public void connectivityChanged() |
803 { | 788 { |
804 this.downloader.connectivityChanged(); | 789 this.downloader.connectivityChanged(); |
805 } | 790 } |
806 | 791 |
807 public interface SubscriptionUpdateCallback | 792 public interface SubscriptionUpdateCallback |
808 { | 793 { |
809 void subscriptionUpdateRequested(boolean enabled); | 794 void subscriptionUpdateRequested(boolean enabled); |
810 void subscriptionUpdatedApplied(); | 795 void subscriptionUpdatedApplied(); |
811 } | 796 } |
812 | 797 |
813 public interface SubscriptionAddedCallback | 798 public interface SubscriptionAddedCallback |
814 { | 799 { |
815 void subscriptionAdded(); | 800 void subscriptionAdded(); |
816 } | 801 } |
817 } | 802 } |
OLD | NEW |