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 
17 package com.android.inputmethod.dictionarypack;
18 
19 import android.app.DownloadManager;
20 import android.app.DownloadManager.Query;
21 import android.app.DownloadManager.Request;
22 import android.content.Context;
23 import android.database.Cursor;
24 import android.database.sqlite.SQLiteException;
25 import android.os.ParcelFileDescriptor;
26 import android.util.Log;
27 
28 import 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  */
40 public class DownloadManagerWrapper {
41     private final static String TAG = DownloadManagerWrapper.class.getSimpleName();
42     private final DownloadManager mDownloadManager;
43 
DownloadManagerWrapper(final Context context)44     public DownloadManagerWrapper(final Context context) {
45         this((DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE));
46     }
47 
DownloadManagerWrapper(final DownloadManager downloadManager)48     private DownloadManagerWrapper(final DownloadManager downloadManager) {
49         mDownloadManager = downloadManager;
50     }
51 
remove(final long... ids)52     public void remove(final long... ids) {
53         try {
54             if (null != mDownloadManager) {
55                 mDownloadManager.remove(ids);
56             }
57         } catch (IllegalArgumentException e) {
58             // This is expected to happen on boot when the device is encrypted.
59         } catch (SQLiteException e) {
60             // We couldn't remove the file from DownloadManager. Apparently, the database can't
61             // be opened. It may be a problem with file system corruption. In any case, there is
62             // not much we can do apart from avoiding crashing.
63             Log.e(TAG, "Can't remove files with ID " + ids + " from download manager", e);
64         }
65     }
66 
openDownloadedFile(final long fileId)67     public ParcelFileDescriptor openDownloadedFile(final long fileId) throws FileNotFoundException {
68         try {
69             if (null != mDownloadManager) {
70                 return mDownloadManager.openDownloadedFile(fileId);
71             }
72         } catch (IllegalArgumentException e) {
73             // This is expected to happen on boot when the device is encrypted.
74         } catch (SQLiteException e) {
75             Log.e(TAG, "Can't open downloaded file with ID " + fileId, e);
76         }
77         // We come here if mDownloadManager is null or if an exception was thrown.
78         throw new FileNotFoundException();
79     }
80 
query(final Query query)81     public Cursor query(final Query query) {
82         try {
83             if (null != mDownloadManager) {
84                 return mDownloadManager.query(query);
85             }
86         } catch (IllegalArgumentException e) {
87             // This is expected to happen on boot when the device is encrypted.
88         } catch (SQLiteException e) {
89             Log.e(TAG, "Can't query the download manager", e);
90         }
91         // We come here if mDownloadManager is null or if an exception was thrown.
92         return null;
93     }
94 
enqueue(final Request request)95     public long enqueue(final Request request) {
96         try {
97             if (null != mDownloadManager) {
98                 return mDownloadManager.enqueue(request);
99             }
100         } catch (IllegalArgumentException e) {
101             // This is expected to happen on boot when the device is encrypted.
102         } catch (SQLiteException e) {
103             Log.e(TAG, "Can't enqueue a request with the download manager", e);
104         }
105         return 0;
106     }
107 }
108