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 import 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  */
42 public class DownloadManagerWrapper {
43     private final static String TAG = DownloadManagerWrapper.class.getSimpleName();
44     private final DownloadManager mDownloadManager;
45 
DownloadManagerWrapper(final Context context)46     public DownloadManagerWrapper(final Context context) {
47         this((DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE));
48     }
49 
DownloadManagerWrapper(final DownloadManager downloadManager)50     private DownloadManagerWrapper(final DownloadManager downloadManager) {
51         mDownloadManager = downloadManager;
52     }
53 
remove(final long... ids)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 
openDownloadedFile(final long fileId)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
query(final Query query)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 
enqueue(final Request request)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