Bug 1291360 - Add a JobCreator class for Downloadable-Content r?sebastian draft
authorKrishna <k.krish@yahoo.com>
Fri, 19 Aug 2016 00:57:26 +0530
changeset 402731 30def74aff86126840d6f2794aaf28e5dfb35c30
parent 402703 5c348a4e266b6b67c45e6574ff77f2264e9a7ab7
child 528756 015cda9636ee5984f6085abd7282aaa844e2f2ff
push id26749
push userk.krish@yahoo.com
push dateThu, 18 Aug 2016 19:30:40 +0000
reviewerssebastian
bugs1291360
milestone51.0a1
Bug 1291360 - Add a JobCreator class for Downloadable-Content r?sebastian MozReview-Commit-ID: 5WXnLTXfQoo
mobile/android/base/java/org/mozilla/gecko/GeckoApplication.java
mobile/android/base/java/org/mozilla/gecko/dlc/DownloadContentJobCreator.java
mobile/android/base/java/org/mozilla/gecko/dlc/DownloadContentService.java
mobile/android/base/java/org/mozilla/gecko/dlc/catalog/DownloadContentCatalog.java
mobile/android/base/moz.build
--- a/mobile/android/base/java/org/mozilla/gecko/GeckoApplication.java
+++ b/mobile/android/base/java/org/mozilla/gecko/GeckoApplication.java
@@ -6,22 +6,24 @@ package org.mozilla.gecko;
 
 import android.app.Application;
 import android.content.Context;
 import android.content.SharedPreferences;
 import android.content.res.Configuration;
 import android.os.SystemClock;
 import android.util.Log;
 
+import com.evernote.android.job.JobManager;
 import com.squareup.leakcanary.LeakCanary;
 import com.squareup.leakcanary.RefWatcher;
 
 import org.mozilla.gecko.db.BrowserContract;
 import org.mozilla.gecko.db.BrowserDB;
 import org.mozilla.gecko.db.LocalBrowserDB;
+import org.mozilla.gecko.dlc.DownloadContentJobCreator;
 import org.mozilla.gecko.dlc.DownloadContentService;
 import org.mozilla.gecko.home.HomePanelsManager;
 import org.mozilla.gecko.lwt.LightweightTheme;
 import org.mozilla.gecko.mdns.MulticastDNSManager;
 import org.mozilla.gecko.notifications.NotificationHelper;
 import org.mozilla.gecko.util.Clipboard;
 import org.mozilla.gecko.util.HardwareUtils;
 import org.mozilla.gecko.util.ThreadUtils;
@@ -177,16 +179,18 @@ public class GeckoApplication extends Ap
                 // send operations to the ContentProvider, which does
                 // its own thing.
                 return new LocalBrowserDB(profileName);
             }
         });
 
         GeckoService.register();
 
+        JobManager.create(this).addJobCreator(new DownloadContentJobCreator(getContext()));
+
         super.onCreate();
     }
 
     public void onDelayedStartup() {
         if (AppConstants.MOZ_ANDROID_GCM) {
             // TODO: only run in main process.
             ThreadUtils.postToBackgroundThread(new Runnable() {
                 @Override
new file mode 100644
--- /dev/null
+++ b/mobile/android/base/java/org/mozilla/gecko/dlc/DownloadContentJobCreator.java
@@ -0,0 +1,33 @@
+/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+package org.mozilla.gecko.dlc;
+
+import android.content.Context;
+import com.evernote.android.job.Job;
+import com.evernote.android.job.JobCreator;
+import org.mozilla.gecko.dlc.catalog.DownloadContentCatalog;
+
+/**
+ * Using android-job scheduler to schedule the downloadable contents
+ * DownloadContentJobCreator is used to create separate jobs for each action.
+ */
+public class DownloadContentJobCreator implements JobCreator {
+    Context context;
+
+    private DownloadContentCatalog catalog;
+
+    public DownloadContentJobCreator() { }
+
+    public DownloadContentJobCreator(Context context) {
+        this.context = context;
+    }
+
+    @Override
+    public Job create(String tag) {
+        catalog = DownloadContentCatalog.getInstance(context);
+        return null;
+    }
+}
\ No newline at end of file
--- a/mobile/android/base/java/org/mozilla/gecko/dlc/DownloadContentService.java
+++ b/mobile/android/base/java/org/mozilla/gecko/dlc/DownloadContentService.java
@@ -84,17 +84,17 @@ public class DownloadContentService exte
     public DownloadContentService() {
         super(LOGTAG);
     }
 
     @Override
     public void onCreate() {
         super.onCreate();
 
-        catalog = new DownloadContentCatalog(this);
+        catalog = DownloadContentCatalog.getInstance(this);
     }
 
     protected void onHandleIntent(Intent intent) {
         if (!AppConstants.MOZ_ANDROID_DOWNLOAD_CONTENT_SERVICE) {
             Log.w(LOGTAG, "Download content is not enabled. Stop.");
             return;
         }
 
--- a/mobile/android/base/java/org/mozilla/gecko/dlc/catalog/DownloadContentCatalog.java
+++ b/mobile/android/base/java/org/mozilla/gecko/dlc/catalog/DownloadContentCatalog.java
@@ -25,30 +25,38 @@ import java.util.List;
 
 /**
  * Catalog of downloadable content (DLC).
  *
  * Changing elements returned by the catalog should be guarded by the catalog instance to guarantee visibility when
  * persisting changes.
  */
 public class DownloadContentCatalog {
+    private static DownloadContentCatalog instance;
     private static final String LOGTAG = "GeckoDLCCatalog";
     private static final String FILE_NAME = "download_content_catalog";
 
     private static final String JSON_KEY_CONTENT = "content";
 
     private static final int MAX_FAILURES_UNTIL_PERMANENTLY_FAILED = 10;
 
     private final AtomicFile file; // Guarded by 'file'
 
     private ArrayMap<String, DownloadContent> content; // Guarded by 'this'
     private boolean hasLoadedCatalog; // Guarded by 'this
     private boolean hasCatalogChanged; // Guarded by 'this'
 
-    public DownloadContentCatalog(Context context) {
+    public static synchronized DownloadContentCatalog getInstance(Context context) {
+        if (instance == null) {
+            instance = new DownloadContentCatalog(context);
+        }
+        return instance;
+    }
+
+    private DownloadContentCatalog(Context context) {
         this(new AtomicFile(new File(context.getApplicationInfo().dataDir, FILE_NAME)));
 
         startLoadFromDisk();
     }
 
     // For injecting mocked AtomicFile objects during test
     protected DownloadContentCatalog(AtomicFile file) {
         this.content = new ArrayMap<>();
--- a/mobile/android/base/moz.build
+++ b/mobile/android/base/moz.build
@@ -273,16 +273,17 @@ gbjar.sources += ['java/org/mozilla/geck
     'distribution/ReferrerDescriptor.java',
     'distribution/ReferrerReceiver.java',
     'dlc/BaseAction.java',
     'dlc/catalog/DownloadContent.java',
     'dlc/catalog/DownloadContentBootstrap.java',
     'dlc/catalog/DownloadContentBuilder.java',
     'dlc/catalog/DownloadContentCatalog.java',
     'dlc/DownloadAction.java',
+    'dlc/DownloadContentJobCreator.java',
     'dlc/DownloadContentService.java',
     'dlc/StudyAction.java',
     'dlc/SyncAction.java',
     'dlc/VerifyAction.java',
     'DoorHangerPopup.java',
     'DownloadsIntegration.java',
     'DynamicToolbar.java',
     'EditBookmarkDialog.java',