| 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 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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.ConnectivityUtils; |
| 47 import org.adblockplus.sbrowser.contentblocker.util.SharedPrefsUtils; | 47 import org.adblockplus.sbrowser.contentblocker.util.SharedPrefsUtils; |
| 48 import org.adblockplus.sbrowser.contentblocker.util.SubscriptionUtils; | 48 import org.adblockplus.sbrowser.contentblocker.util.SubscriptionUtils; |
| 49 | 49 |
| 50 import android.content.Context; | 50 import android.content.Context; |
| 51 import android.content.Intent; | 51 import android.content.Intent; |
| 52 import android.content.pm.PackageInfo; | 52 import android.content.pm.PackageInfo; |
| 53 import android.content.pm.PackageManager; | 53 import android.content.pm.PackageManager; |
| 54 import android.content.pm.ResolveInfo; | 54 import android.content.pm.ResolveInfo; |
| 55 import android.net.ConnectivityManager; | |
| 56 import android.net.NetworkInfo; | |
| 57 import android.net.Uri; | 55 import android.net.Uri; |
| 58 import android.os.Handler; | 56 import android.os.Handler; |
| 59 import android.os.Looper; | 57 import android.os.Looper; |
| 60 import android.text.TextUtils; | 58 import android.text.TextUtils; |
| 61 import android.text.format.DateUtils; | 59 import android.text.format.DateUtils; |
| 62 import android.util.Log; | 60 import android.util.Log; |
| 63 import android.widget.Toast; | 61 import android.widget.Toast; |
| 64 | 62 |
| 65 public final class Engine | 63 public final class Engine |
| 66 { | 64 { |
| (...skipping 20 matching lines...) Expand all Loading... |
| 87 | 85 |
| 88 private final ReentrantLock accessLock = new ReentrantLock(); | 86 private final ReentrantLock accessLock = new ReentrantLock(); |
| 89 private DefaultSubscriptions defaultSubscriptions; | 87 private DefaultSubscriptions defaultSubscriptions; |
| 90 private Subscriptions subscriptions; | 88 private Subscriptions subscriptions; |
| 91 private JSONPrefs jsonPrefs; | 89 private JSONPrefs jsonPrefs; |
| 92 private AppInfo appInfo; | 90 private AppInfo appInfo; |
| 93 private final LinkedBlockingQueue<EngineEvent> engineEvents = new LinkedBlocki
ngQueue<>(); | 91 private final LinkedBlockingQueue<EngineEvent> engineEvents = new LinkedBlocki
ngQueue<>(); |
| 94 private Thread handlerThread; | 92 private Thread handlerThread; |
| 95 private Downloader downloader; | 93 private Downloader downloader; |
| 96 private SubscriptionUpdateCallback subscriptionUpdateCallback; | 94 private SubscriptionUpdateCallback subscriptionUpdateCallback; |
| 97 private final Context serviceContext; | 95 private final Context context; |
| 98 private boolean wasFirstRun = false; | 96 private boolean wasFirstRun = false; |
| 99 private long nextUpdateBroadcast = Long.MAX_VALUE; | 97 private long nextUpdateBroadcast = Long.MAX_VALUE; |
| 100 | 98 |
| 101 private Engine(final Context context) | 99 private Engine(final Context context) |
| 102 { | 100 { |
| 103 this.serviceContext = context; | 101 this.context = context; |
| 104 } | 102 } |
| 105 | 103 |
| 106 public String getPrefsDefault(final String key) | 104 public String getPrefsDefault(final String key) |
| 107 { | 105 { |
| 108 return this.jsonPrefs.getDefaults(key); | 106 return this.jsonPrefs.getDefaults(key); |
| 109 } | 107 } |
| 110 | 108 |
| 111 DefaultSubscriptionInfo getDefaultSubscriptionInfo(final Subscription sub) | 109 DefaultSubscriptionInfo getDefaultSubscriptionInfo(final Subscription sub) |
| 112 { | 110 { |
| 113 return this.defaultSubscriptions.getForUrl(sub.getURL()); | 111 return this.defaultSubscriptions.getForUrl(sub.getURL()); |
| 114 } | 112 } |
| 115 | 113 |
| 116 void lock() | 114 void lock() |
| 117 { | 115 { |
| 118 this.accessLock.lock(); | 116 this.accessLock.lock(); |
| 119 } | 117 } |
| 120 | 118 |
| 121 void unlock() | 119 void unlock() |
| 122 { | 120 { |
| 123 this.accessLock.unlock(); | 121 this.accessLock.unlock(); |
| 124 } | 122 } |
| 125 | 123 |
| 126 public static boolean openSBrowserSettings(final Context activityContext) | 124 public static boolean openSBrowserSettings(final Context context) |
| 127 { | 125 { |
| 128 final Intent intent = new Intent(ACTION_OPEN_SETTINGS); | 126 final Intent intent = new Intent(ACTION_OPEN_SETTINGS); |
| 129 final List<ResolveInfo> list = activityContext.getPackageManager() | 127 final List<ResolveInfo> list = context.getPackageManager() |
| 130 .queryIntentActivities(intent, 0); | 128 .queryIntentActivities(intent, 0); |
| 131 if (list.size() > 0) | 129 if (list.size() > 0) |
| 132 { | 130 { |
| 133 activityContext.startActivity(intent); | 131 context.startActivity(intent); |
| 134 } | 132 } |
| 135 return list.size() > 0; | 133 return list.size() > 0; |
| 136 } | 134 } |
| 137 | 135 |
| 138 public static boolean hasCompatibleSBrowserInstalled(final Context activityCon
text) | 136 public static boolean hasCompatibleSBrowserInstalled(final Context context) |
| 139 { | 137 { |
| 140 try | 138 try |
| 141 { | 139 { |
| 142 return activityContext.getPackageManager() | 140 return context.getPackageManager() |
| 143 .queryIntentActivities(new Intent(ACTION_OPEN_SETTINGS), 0).size() > 0
; | 141 .queryIntentActivities(new Intent(ACTION_OPEN_SETTINGS), 0).size() > 0
; |
| 144 } | 142 } |
| 145 catch (final Throwable t) | 143 catch (final Throwable t) |
| 146 { | 144 { |
| 147 return false; | 145 return false; |
| 148 } | 146 } |
| 149 } | 147 } |
| 150 | 148 |
| 151 /** | 149 /** |
| 152 * Starting with Samsung Internet 5.0, the way to enable ad blocking has chang
ed. As a result, we | 150 * Starting with Samsung Internet 5.0, the way to enable ad blocking has chang
ed. As a result, we |
| 153 * need to check for the version of Samsung Internet and apply text changes to
the first run slide. | 151 * need to check for the version of Samsung Internet and apply text changes to
the first run slide. |
| 154 * | 152 * |
| 155 * @param activityContext | 153 * @param context |
| 156 * @return a boolean that indicates, if the user has Samsung Internet version
5.x | 154 * @return a boolean that indicates, if the user has Samsung Internet version
5.x |
| 157 */ | 155 */ |
| 158 public static boolean hasSamsungInternetVersion5OrNewer(final Context activity
Context) | 156 public static boolean hasSamsungInternetVersion5OrNewer(final Context context) |
| 159 { | 157 { |
| 160 try | 158 try |
| 161 { | 159 { |
| 162 PackageInfo packageInfo = activityContext.getPackageManager().getPackageIn
fo(SBROWSER_APP_ID, NO_FLAG); | 160 PackageInfo packageInfo = context.getPackageManager().getPackageInfo(SBROW
SER_APP_ID, NO_FLAG); |
| 163 return packageInfo.versionCode >= OLDEST_SAMSUNG_INTERNET_5_VERSIONCODE; | 161 return packageInfo.versionCode >= OLDEST_SAMSUNG_INTERNET_5_VERSIONCODE; |
| 164 } | 162 } |
| 165 catch (PackageManager.NameNotFoundException e) | 163 catch (PackageManager.NameNotFoundException e) |
| 166 { | 164 { |
| 167 // Should never happen, as checkAAStatusAndProceed() should not be called
if the user | 165 // Should never happen, as checkAAStatusAndProceed() should not be called
if the user |
| 168 // has no compatible SBrowser installed. Nevertheless we have to handle th
e Exception. | 166 // has no compatible SBrowser installed. Nevertheless we have to handle th
e Exception. |
| 169 Log.d(TAG, "No compatible Samsung Browser found.", e); | 167 Log.d(TAG, "No compatible Samsung Browser found.", e); |
| 170 return false; | 168 return false; |
| 171 } | 169 } |
| 172 } | 170 } |
| (...skipping 12 matching lines...) Expand all Loading... |
| 185 { | 183 { |
| 186 createAndWriteFile(); | 184 createAndWriteFile(); |
| 187 | 185 |
| 188 runOnUiThread(new Runnable() | 186 runOnUiThread(new Runnable() |
| 189 { | 187 { |
| 190 @Override | 188 @Override |
| 191 public void run() | 189 public void run() |
| 192 { | 190 { |
| 193 final Intent intent = new Intent(); | 191 final Intent intent = new Intent(); |
| 194 intent.setAction(ACTION_UPDATE); | 192 intent.setAction(ACTION_UPDATE); |
| 195 intent.setData(Uri.parse("package:" + Engine.this.serviceContext.getPack
ageName())); | 193 intent.setData(Uri.parse("package:" + Engine.this.context.getPackageName
())); |
| 196 Engine.this.serviceContext.sendBroadcast(intent); | 194 Engine.this.context.sendBroadcast(intent); |
| 197 } | 195 } |
| 198 }); | 196 }); |
| 199 } | 197 } |
| 200 | 198 |
| 201 boolean canUseInternet(final boolean allowMetered) | 199 boolean canUseInternet(final boolean allowMetered) |
| 202 { | 200 { |
| 203 // allow a metered connection to update default subscriptions at the first r
un. | 201 // allow a metered connection to update default subscriptions at the first r
un. |
| 204 // See https://issues.adblockplus.org/ticket/5237 | 202 // See https://issues.adblockplus.org/ticket/5237 |
| 205 return ConnectivityUtils.canUseInternet(serviceContext, allowMetered || wasF
irstRun()); | 203 return ConnectivityUtils.canUseInternet(context, allowMetered || wasFirstRun
()); |
| 206 } | 204 } |
| 207 | 205 |
| 208 public void forceUpdateSubscriptions(final boolean allowMetered) | 206 public void forceUpdateSubscriptions(final boolean allowMetered) |
| 209 { | 207 { |
| 210 try | 208 try |
| 211 { | 209 { |
| 212 subscriptions.checkForUpdates(true, allowMetered); | 210 subscriptions.checkForUpdates(true, allowMetered); |
| 213 Toast.makeText(serviceContext, serviceContext.getText(R.string.updating_su
bscriptions), Toast.LENGTH_LONG).show(); | 211 Toast.makeText(context, context.getText(R.string.updating_subscriptions),
Toast.LENGTH_LONG).show(); |
| 214 } | 212 } |
| 215 catch (IOException e) | 213 catch (IOException e) |
| 216 { | 214 { |
| 217 Log.e(TAG, "Failed checking for updates", e); | 215 Log.e(TAG, "Failed checking for updates", e); |
| 218 } | 216 } |
| 219 } | 217 } |
| 220 | 218 |
| 221 public List<SubscriptionInfo> getListedSubscriptions() | 219 public List<SubscriptionInfo> getListedSubscriptions() |
| 222 { | 220 { |
| 223 return this.subscriptions.getSubscriptions(this); | 221 return this.subscriptions.getSubscriptions(this); |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 262 this.engineEvents.add(new DownloadFinishedEvent(id, responseCode, response,
headers)); | 260 this.engineEvents.add(new DownloadFinishedEvent(id, responseCode, response,
headers)); |
| 263 } | 261 } |
| 264 | 262 |
| 265 private void createAndWriteFile() | 263 private void createAndWriteFile() |
| 266 { | 264 { |
| 267 this.lock(); | 265 this.lock(); |
| 268 try | 266 try |
| 269 { | 267 { |
| 270 Log.d(TAG, "Writing filters..."); | 268 Log.d(TAG, "Writing filters..."); |
| 271 final File filterFile = this.subscriptions.createAndWriteFile(); | 269 final File filterFile = this.subscriptions.createAndWriteFile(); |
| 272 writeWhitelistedWebsites(this.serviceContext, filterFile); | 270 writeWhitelistedWebsites(this.context, filterFile); |
| 273 | 271 |
| 274 SharedPrefsUtils.putString( | 272 SharedPrefsUtils.putString( |
| 275 this.serviceContext, R.string.key_cached_filter_path, filterFile.getAb
solutePath()); | 273 this.context, R.string.key_cached_filter_path, filterFile.getAbsoluteP
ath()); |
| 276 | 274 |
| 277 Log.d(TAG, "Cleaning up cache..."); | 275 Log.d(TAG, "Cleaning up cache..."); |
| 278 final File dummyFile = getDummyFilterFile(this.serviceContext); | 276 final File dummyFile = getDummyFilterFile(this.context); |
| 279 final File[] cacheDirFiles = getFilterCacheDir(this.serviceContext).listFi
les(); | 277 final File[] cacheDirFiles = getFilterCacheDir(this.context).listFiles(); |
| 280 if (cacheDirFiles != null) | 278 if (cacheDirFiles != null) |
| 281 { | 279 { |
| 282 for (final File file : cacheDirFiles) | 280 for (final File file : cacheDirFiles) |
| 283 { | 281 { |
| 284 if (!file.equals(dummyFile) && !file.equals(filterFile)) | 282 if (!file.equals(dummyFile) && !file.equals(filterFile)) |
| 285 { | 283 { |
| 286 Log.d(TAG, "Deleting file:" + file); | 284 Log.d(TAG, "Deleting file:" + file); |
| 287 file.delete(); | 285 file.delete(); |
| 288 } | 286 } |
| 289 } | 287 } |
| (...skipping 503 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 793 { | 791 { |
| 794 void subscriptionUpdateRequested(boolean enabled); | 792 void subscriptionUpdateRequested(boolean enabled); |
| 795 void subscriptionUpdatedApplied(); | 793 void subscriptionUpdatedApplied(); |
| 796 } | 794 } |
| 797 | 795 |
| 798 public interface SubscriptionAddedCallback | 796 public interface SubscriptionAddedCallback |
| 799 { | 797 { |
| 800 void subscriptionAdded(); | 798 void subscriptionAdded(); |
| 801 } | 799 } |
| 802 } | 800 } |
| OLD | NEW |