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