DownloadManagerWrapper.java revision 9967f0a1d027cc4df972174a6be8b26e227d9de5
1/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * 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, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
16
17package com.android.inputmethod.dictionarypack;
18
19import android.app.DownloadManager;
20import android.app.DownloadManager.Query;
21import android.app.DownloadManager.Request;
22import android.content.Context;
23import android.database.Cursor;
24import android.database.sqlite.SQLiteException;
25import android.os.ParcelFileDescriptor;
26import android.util.Log;
27
28import java.io.FileNotFoundException;
29
30/**
31 * A class to help with calling DownloadManager methods.
32 *
33 * Mostly, the problem here is that most methods from DownloadManager may throw SQL exceptions if
34 * they can't open the database on disk. We want to avoid crashing in these cases but can't do
35 * much more, so this class insulates the callers from these. SQLiteException also inherit from
36 * RuntimeException so they are unchecked :(
37 * While we're at it, we also insulate callers from the cases where DownloadManager is disabled,
38 * and getSystemService returns null.
39 */
40public class DownloadManagerWrapper {
41    private final static String TAG = DownloadManagerWrapper.class.getSimpleName();
42    private final DownloadManager mDownloadManager;
43
44    public DownloadManagerWrapper(final Context context) {
45        this((DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE));
46    }
47
48    private DownloadManagerWrapper(final DownloadManager downloadManager) {
49        mDownloadManager = downloadManager;
50    }
51
52    public void remove(final long... ids) {
53        try {
54            if (null != mDownloadManager) {
55                mDownloadManager.remove(ids);
56            }
57        } catch (SQLiteException e) {
58            // We couldn't remove the file from DownloadManager. Apparently, the database can't
59            // be opened. It may be a problem with file system corruption. In any case, there is
60            // not much we can do apart from avoiding crashing.
61            Log.e(TAG, "Can't remove files with ID " + ids + " from download manager", e);
62        } catch (IllegalArgumentException e) {
63            // Not sure how this can happen, but it could be another case where the provider
64            // is disabled. Or it could be a bug in older versions of the framework.
65            Log.e(TAG, "Can't find the content URL for DownloadManager?", e);
66        }
67    }
68
69    public ParcelFileDescriptor openDownloadedFile(final long fileId) throws FileNotFoundException {
70        try {
71            if (null != mDownloadManager) {
72                return mDownloadManager.openDownloadedFile(fileId);
73            }
74        } catch (SQLiteException e) {
75            Log.e(TAG, "Can't open downloaded file with ID " + fileId, e);
76        } catch (IllegalArgumentException e) {
77            Log.e(TAG, "Can't find the content URL for DownloadManager?", e);
78        }
79        // We come here if mDownloadManager is null or if an exception was thrown.
80        throw new FileNotFoundException();
81    }
82
83    public Cursor query(final Query query) {
84        try {
85            if (null != mDownloadManager) {
86                return mDownloadManager.query(query);
87            }
88        } catch (SQLiteException e) {
89            Log.e(TAG, "Can't query the download manager", e);
90        } catch (IllegalArgumentException e) {
91            Log.e(TAG, "Can't find the content URL for DownloadManager?", e);
92        }
93        // We come here if mDownloadManager is null or if an exception was thrown.
94        return null;
95    }
96
97    public long enqueue(final Request request) {
98        try {
99            if (null != mDownloadManager) {
100                return mDownloadManager.enqueue(request);
101            }
102        } catch (SQLiteException e) {
103            Log.e(TAG, "Can't enqueue a request with the download manager", e);
104        } catch (IllegalArgumentException e) {
105            Log.e(TAG, "Can't find the content URL for DownloadManager?", e);
106        }
107        return 0;
108    }
109}
110