Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: adblockplussbrowser/src/main/java/org/adblockplus/sbrowser/contentblocker/engine/Engine.java

Issue 29760569: Issue 6238 - Download/store notifications.json (Closed)
Patch Set: Created May 15, 2018, 9:57 a.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: adblockplussbrowser/src/main/java/org/adblockplus/sbrowser/contentblocker/engine/Engine.java
===================================================================
--- a/adblockplussbrowser/src/main/java/org/adblockplus/sbrowser/contentblocker/engine/Engine.java
+++ b/adblockplussbrowser/src/main/java/org/adblockplus/sbrowser/contentblocker/engine/Engine.java
@@ -42,24 +42,28 @@ import java.util.concurrent.LinkedBlocki
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;
import org.adblockplus.adblockplussbrowser.R;
import org.adblockplus.sbrowser.contentblocker.util.ConnectivityUtils;
import org.adblockplus.sbrowser.contentblocker.util.SharedPrefsUtils;
import org.adblockplus.sbrowser.contentblocker.util.SubscriptionUtils;
+import android.app.job.JobInfo;
+import android.app.job.JobScheduler;
+import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
+import android.os.PersistableBundle;
import android.text.TextUtils;
import android.text.format.DateUtils;
import android.util.Log;
import android.widget.Toast;
public final class Engine
{
private static final String TAG = Engine.class.getSimpleName();
@@ -87,22 +91,25 @@ public final class Engine
private Subscriptions subscriptions;
private JSONPrefs jsonPrefs;
private AppInfo appInfo;
private final LinkedBlockingQueue<EngineEvent> engineEvents = new LinkedBlockingQueue<>();
private Thread handlerThread;
private Downloader downloader;
private SubscriptionUpdateCallback subscriptionUpdateCallback;
private final Context context;
+ private ComponentName componentName;
private boolean wasFirstRun = false;
private long nextUpdateBroadcast = Long.MAX_VALUE;
+ private int jobId = 0;
private Engine(final Context context)
{
this.context = context;
+ this.componentName = new ComponentName(context, DownloadJobService.class);
}
public String getPrefsDefault(final String key)
{
return this.jsonPrefs.getDefaults(key);
}
DefaultSubscriptionInfo getDefaultSubscriptionInfo(final Subscription sub)
@@ -420,16 +427,22 @@ public final class Engine
final Subscription exceptions = engine.subscriptions.add(Subscription
.create(engine.getPrefsDefault(SUBSCRIPTIONS_EXCEPTIONSURL))
.parseLines(readLines(exceptionsTxt)));
exceptions.putMeta(Subscription.KEY_UPDATE_TIMESTAMP, "0");
exceptions.setEnabled(true);
}
Log.d(TAG, "Added and enabled bundled exceptionslist");
+ // The Notification should be download regularly.
+ // See https://issues.adblockplus.org/ticket/6238
+ final Subscription notification = engine.subscriptions.add(Subscription
+ .create(Notification.NOTIFICATION_URL));
+ notification.setEnabled(true);
+
int additional = 0;
for (final Subscription sub : engine.defaultSubscriptions.createSubscriptions())
{
if (!engine.subscriptions.hasSubscription(sub.getId()))
{
additional++;
engine.subscriptions.add(sub);
}
@@ -650,18 +663,26 @@ public final class Engine
engine.subscriptions.changeSubscriptionState(cese.id, cese.enabled);
break;
}
case DOWNLOAD_FINISHED:
{
final DownloadFinishedEvent dfe = (DownloadFinishedEvent) event;
Log.d(TAG, "Download finished for '" + dfe.id + "' with response code "
+ dfe.responseCode);
- this.engine.subscriptions.updateSubscription(dfe.id, dfe.responseCode,
- dfe.response, dfe.headers);
+ if (SubscriptionUtils.isNotificationSubscription(dfe.id))
+ {
+ Notification.update(engine.context, dfe.responseCode, dfe.response,
+ engine.subscriptions.getNotificationDataFile());
+ }
+ else
+ {
+ this.engine.subscriptions.updateSubscription(dfe.id, dfe.responseCode,
+ dfe.response, dfe.headers);
+ }
break;
}
default:
Log.d(TAG, "Unhandled type: " + event.getType());
break;
}
}
@@ -755,34 +776,64 @@ public final class Engine
this.headers.putAll(headers);
}
}
}
public void enqueueDownload(final Subscription sub, final boolean forced,
final boolean allowMetered) throws IOException
{
- if (sub.getURL() != null && sub.shouldUpdate(forced))
+ // For now we want to use JobScheduler only for the Notification download.
+ // See https://issues.adblockplus.org/ticket/6238.
+ if (SubscriptionUtils.isNotificationSubscription(sub.getId()))
{
- final HashMap<String, String> headers = new HashMap<>();
- if (sub.isMetaDataValid() && sub.isFiltersValid())
+ if (Notification.shouldUpdate(context))
{
- final String lastModified = sub.getMeta(Subscription.KEY_HTTP_LAST_MODIFIED);
- if (!TextUtils.isEmpty(lastModified))
+ scheduleJob(this.createDownloadURL(sub), sub.getId(), allowMetered);
+ }
+ }
+ else
+ {
+ if (sub.getURL() != null && sub.shouldUpdate(forced))
+ {
+ final HashMap<String, String> headers = new HashMap<>();
+ if (sub.isMetaDataValid() && sub.isFiltersValid())
{
- headers.put("If-Modified-Since", lastModified);
+ final String lastModified = sub.getMeta(Subscription.KEY_HTTP_LAST_MODIFIED);
+ if (!TextUtils.isEmpty(lastModified))
+ {
+ headers.put("If-Modified-Since", lastModified);
+ }
+ final String etag = sub.getMeta(Subscription.KEY_HTTP_ETAG);
+ if (!TextUtils.isEmpty(etag))
+ {
+ headers.put("If-None-Match", etag);
+ }
}
- final String etag = sub.getMeta(Subscription.KEY_HTTP_ETAG);
- if (!TextUtils.isEmpty(etag))
- {
- headers.put("If-None-Match", etag);
- }
+ this.downloader.enqueueDownload(this.createDownloadURL(sub), sub.getId(), headers, allowMetered);
}
- Log.d(TAG, headers.toString());
- this.downloader.enqueueDownload(this.createDownloadURL(sub), sub.getId(), headers, allowMetered);
+ }
+ }
+
+ private void scheduleJob(final URL url, final String id, final boolean allowMetered)
+ {
+ final JobInfo.Builder builder = new JobInfo.Builder(jobId++, componentName);
+ builder.setRequiredNetworkType(allowMetered
+ ? JobInfo.NETWORK_TYPE_UNMETERED
+ : JobInfo.NETWORK_TYPE_ANY);
+
+ final PersistableBundle extras = new PersistableBundle();
+ extras.putString(Notification.KEY_EXTRA_ID, id);
+ extras.putString(Notification.KEY_EXTRA_URL, url.toString());
+ builder.setExtras(extras);
+
+ final JobScheduler scheduler = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
+ if (scheduler != null)
+ {
+ scheduler.schedule(builder.build());
}
}
public void connectivityChanged()
{
this.downloader.connectivityChanged();
}

Powered by Google App Engine
This is Rietveld