1 /* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.providers.downloads; 18 19 import android.content.res.Resources; 20 import android.util.Log; 21 22 import java.util.ArrayList; 23 import java.util.HashMap; 24 import java.util.Iterator; 25 import java.util.LinkedHashMap; 26 27 public class DownloadHandler { 28 29 private static final String TAG = "DownloadHandler"; 30 private final LinkedHashMap<Long, DownloadInfo> mDownloadsQueue = 31 new LinkedHashMap<Long, DownloadInfo>(); 32 private final HashMap<Long, DownloadInfo> mDownloadsInProgress = 33 new HashMap<Long, DownloadInfo>(); 34 private static final DownloadHandler mDownloadHandler = new DownloadHandler(); 35 private final int mMaxConcurrentDownloadsAllowed = Resources.getSystem().getInteger( 36 com.android.internal.R.integer.config_MaxConcurrentDownloadsAllowed); 37 38 static DownloadHandler getInstance() { 39 return mDownloadHandler; 40 } 41 42 synchronized void enqueueDownload(DownloadInfo info) { 43 if (!mDownloadsQueue.containsKey(info.mId)) { 44 if (Constants.LOGV) { 45 Log.i(TAG, "enqueued download. id: " + info.mId + ", uri: " + info.mUri); 46 } 47 mDownloadsQueue.put(info.mId, info); 48 startDownloadThread(); 49 } 50 } 51 52 private synchronized void startDownloadThread() { 53 Iterator<Long> keys = mDownloadsQueue.keySet().iterator(); 54 ArrayList<Long> ids = new ArrayList<Long>(); 55 while (mDownloadsInProgress.size() < mMaxConcurrentDownloadsAllowed && keys.hasNext()) { 56 Long id = keys.next(); 57 DownloadInfo info = mDownloadsQueue.get(id); 58 info.startDownloadThread(); 59 ids.add(id); 60 mDownloadsInProgress.put(id, mDownloadsQueue.get(id)); 61 if (Constants.LOGV) { 62 Log.i(TAG, "started download for : " + id); 63 } 64 } 65 for (Long id : ids) { 66 mDownloadsQueue.remove(id); 67 } 68 } 69 70 synchronized boolean hasDownloadInQueue(long id) { 71 return mDownloadsQueue.containsKey(id) || mDownloadsInProgress.containsKey(id); 72 } 73 74 synchronized void dequeueDownload(long mId) { 75 mDownloadsInProgress.remove(mId); 76 startDownloadThread(); 77 if (mDownloadsInProgress.size() == 0 && mDownloadsQueue.size() == 0) { 78 notifyAll(); 79 } 80 } 81 82 // right now this is only used by tests. but there is no reason why it can't be used 83 // by any module using DownloadManager (TODO add API to DownloadManager.java) 84 public synchronized void WaitUntilDownloadsTerminate() throws InterruptedException { 85 if (mDownloadsInProgress.size() == 0 && mDownloadsQueue.size() == 0) { 86 if (Constants.LOGVV) { 87 Log.i(TAG, "nothing to wait on"); 88 } 89 return; 90 } 91 if (Constants.LOGVV) { 92 for (DownloadInfo info : mDownloadsInProgress.values()) { 93 Log.i(TAG, "** progress: " + info.mId + ", " + info.mUri); 94 } 95 for (DownloadInfo info : mDownloadsQueue.values()) { 96 Log.i(TAG, "** in Q: " + info.mId + ", " + info.mUri); 97 } 98 } 99 if (Constants.LOGVV) { 100 Log.i(TAG, "waiting for 5 sec"); 101 } 102 // wait upto 5 sec 103 wait(5 * 1000); 104 } 105 } 106