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
30import javax.annotation.Nullable;
31
32/**
33 * A class to help with calling DownloadManager methods.
34 *
35 * Mostly, the problem here is that most methods from DownloadManager may throw SQL exceptions if
36 * they can't open the database on disk. We want to avoid crashing in these cases but can't do
37 * much more, so this class insulates the callers from these. SQLiteException also inherit from
38 * RuntimeException so they are unchecked :(
39 * While we're at it, we also insulate callers from the cases where DownloadManager is disabled,
40 * and getSystemService returns null.
41 */
42public class DownloadManagerWrapper {
43    private final static String TAG = DownloadManagerWrapper.class.getSimpleName();
44    private final DownloadManager mDownloadManager;
45
46    public DownloadManagerWrapper(final Context context) {
47        this((DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE));
48    }
49
50    private DownloadManagerWrapper(final DownloadManager downloadManager) {
51        mDownloadManager = downloadManager;
52    }
53
54    public void remove(final long... ids) {
55        try {
56            if (null != mDownloadManager) {
57                mDownloadManager.remove(ids);
58            }
59        } catch (IllegalArgumentException e) {
60            // This is expected to happen on boot when the device is encrypted.
61        } catch (SQLiteException e) {
62            // We couldn't remove the file from DownloadManager. Apparently, the database can't
63            // be opened. It may be a problem with file system corruption. In any case, there is
64            // not much we can do apart from avoiding crashing.
65            Log.e(TAG, "Can't remove files with ID " + ids + " from download manager", 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 (IllegalArgumentException e) {
75            // This is expected to happen on boot when the device is encrypted.
76        } catch (SQLiteException e) {
77            Log.e(TAG, "Can't open downloaded file with ID " + fileId, e);
78        }
79        // We come here if mDownloadManager is null or if an exception was thrown.
80        throw new FileNotFoundException();
81    }
82
83    @Nullable
84    public Cursor query(final Query query) {
85        try {
86            if (null != mDownloadManager) {
87                return mDownloadManager.query(query);
88            }
89        } catch (IllegalArgumentException e) {
90            // This is expected to happen on boot when the device is encrypted.
91        } catch (SQLiteException e) {
92            Log.e(TAG, "Can't query the download manager", e);
93        }
94        // We come here if mDownloadManager is null or if an exception was thrown.
95        return null;
96    }
97
98    public long enqueue(final Request request) {
99        try {
100            if (null != mDownloadManager) {
101                return mDownloadManager.enqueue(request);
102            }
103        } catch (IllegalArgumentException e) {
104            // This is expected to happen on boot when the device is encrypted.
105        } catch (SQLiteException e) {
106            Log.e(TAG, "Can't enqueue a request with the download manager", e);
107        }
108        return 0;
109    }
110}
111