Remove singleton StorageManager.

Now DownloadService creates and owns the lifecycle of its own
StorageManager instance.

Change-Id: I8f6bedc02f1dbe610a8e6a25d55383a12716d344
diff --git a/src/com/android/providers/downloads/DownloadInfo.java b/src/com/android/providers/downloads/DownloadInfo.java
index 5172b69..2ea7d84 100644
--- a/src/com/android/providers/downloads/DownloadInfo.java
+++ b/src/com/android/providers/downloads/DownloadInfo.java
@@ -54,8 +54,9 @@
             mCursor = cursor;
         }
 
-        public DownloadInfo newDownloadInfo(Context context, SystemFacade systemFacade) {
-            DownloadInfo info = new DownloadInfo(context, systemFacade);
+        public DownloadInfo newDownloadInfo(Context context, SystemFacade systemFacade,
+                StorageManager storageManager) {
+            DownloadInfo info = new DownloadInfo(context, systemFacade, storageManager);
             updateFromDatabase(info);
             readRequestHeaders(info);
             return info;
@@ -229,12 +230,15 @@
     public int mFuzz;
 
     private List<Pair<String, String>> mRequestHeaders = new ArrayList<Pair<String, String>>();
-    private SystemFacade mSystemFacade;
-    private Context mContext;
 
-    private DownloadInfo(Context context, SystemFacade systemFacade) {
+    private final Context mContext;
+    private final SystemFacade mSystemFacade;
+    private final StorageManager mStorageManager;
+
+    private DownloadInfo(Context context, SystemFacade systemFacade, StorageManager storageManager) {
         mContext = context;
         mSystemFacade = systemFacade;
+        mStorageManager = storageManager;
         mFuzz = Helpers.sRandom.nextInt(1001);
     }
 
@@ -572,7 +576,7 @@
 
     void startDownloadThread() {
         DownloadThread downloader = new DownloadThread(mContext, mSystemFacade, this,
-                StorageManager.getInstance(mContext));
+                mStorageManager);
         mSystemFacade.startThread(downloader);
     }
 
diff --git a/src/com/android/providers/downloads/DownloadProvider.java b/src/com/android/providers/downloads/DownloadProvider.java
index c554e41..7af8173 100644
--- a/src/com/android/providers/downloads/DownloadProvider.java
+++ b/src/com/android/providers/downloads/DownloadProvider.java
@@ -446,7 +446,7 @@
         // saves us by getting some initialization code in DownloadService out of the way.
         Context context = getContext();
         context.startService(new Intent(context, DownloadService.class));
-        mDownloadsDataDir = StorageManager.getInstance(getContext()).getDownloadDataDirectory();
+        mDownloadsDataDir = StorageManager.getDownloadDataDirectory(getContext());
         return true;
     }
 
diff --git a/src/com/android/providers/downloads/DownloadService.java b/src/com/android/providers/downloads/DownloadService.java
index b97346b..e0fe4c5 100644
--- a/src/com/android/providers/downloads/DownloadService.java
+++ b/src/com/android/providers/downloads/DownloadService.java
@@ -225,7 +225,7 @@
         mNotifier = new DownloadNotifier(this);
         mNotifier.cancelAll();
 
-        mStorageManager = StorageManager.getInstance(getApplicationContext());
+        mStorageManager = new StorageManager(this);
         updateFromProvider();
     }
 
@@ -435,7 +435,7 @@
      * download if appropriate.
      */
     private DownloadInfo insertDownloadLocked(DownloadInfo.Reader reader, long now) {
-        DownloadInfo info = reader.newDownloadInfo(this, mSystemFacade);
+        DownloadInfo info = reader.newDownloadInfo(this, mSystemFacade, mStorageManager);
         mDownloads.put(info.mId, info);
 
         if (Constants.LOGVV) {
diff --git a/src/com/android/providers/downloads/StorageManager.java b/src/com/android/providers/downloads/StorageManager.java
index 915d141..8ca1730 100644
--- a/src/com/android/providers/downloads/StorageManager.java
+++ b/src/com/android/providers/downloads/StorageManager.java
@@ -71,12 +71,6 @@
      */
     private final File mDownloadDataDir;
 
-    /** the Singleton instance of this class.
-     * TODO: once DownloadService is refactored into a long-living object, there is no need
-     * for this Singleton'ing.
-     */
-    private static StorageManager sSingleton = null;
-
     /** how often do we need to perform checks on space to make sure space is available */
     private static final int FREQUENCY_OF_CHECKS_ON_SPACE_AVAILABILITY = 1024 * 1024; // 1MB
     private int mBytesDownloadedSinceLastCheckOnSpace = 0;
@@ -84,19 +78,9 @@
     /** misc members */
     private final Context mContext;
 
-    /**
-     * maintains Singleton instance of this class
-     */
-    synchronized static StorageManager getInstance(Context context) {
-        if (sSingleton == null) {
-            sSingleton = new StorageManager(context);
-        }
-        return sSingleton;
-    }
-
-    private StorageManager(Context context) { // constructor is private
+    public StorageManager(Context context) {
         mContext = context;
-        mDownloadDataDir = context.getCacheDir();
+        mDownloadDataDir = getDownloadDataDirectory(context);
         mExternalStorageDir = Environment.getExternalStorageDirectory();
         mSystemCacheDir = Environment.getDownloadCacheDirectory();
         startThreadToCleanupDatabaseAndPurgeFileSystem();
@@ -308,6 +292,10 @@
         return mDownloadDataDir;
     }
 
+    public static File getDownloadDataDirectory(Context context) {
+        return context.getCacheDir();
+    }
+
     /**
      * Deletes purgeable files from the cache partition. This also deletes
      * the matching database entries. Files are deleted in LRU order until