| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.chrome.browser.download; | 5 package org.chromium.chrome.browser.download; |
| 6 | 6 |
| 7 import android.app.DownloadManager; | 7 import android.app.DownloadManager; |
| 8 import android.content.Context; | 8 import android.content.Context; |
| 9 import android.database.Cursor; | 9 import android.database.Cursor; |
| 10 import android.net.Uri; |
| 10 import android.os.AsyncTask; | 11 import android.os.AsyncTask; |
| 12 import android.os.Build; |
| 13 |
| 14 import org.chromium.base.Log; |
| 15 |
| 16 import java.lang.reflect.InvocationTargetException; |
| 17 import java.lang.reflect.Method; |
| 11 | 18 |
| 12 /** | 19 /** |
| 13 * A wrapper for Android DownloadManager to provide utility functions. | 20 * A wrapper for Android DownloadManager to provide utility functions. |
| 14 */ | 21 */ |
| 15 public class DownloadManagerDelegate { | 22 public class DownloadManagerDelegate { |
| 23 private static final String TAG = "DownloadDelegate"; |
| 16 protected final Context mContext; | 24 protected final Context mContext; |
| 17 | 25 |
| 18 public DownloadManagerDelegate(Context context) { | 26 public DownloadManagerDelegate(Context context) { |
| 19 mContext = context; | 27 mContext = context; |
| 20 } | 28 } |
| 21 | 29 |
| 22 /** | 30 /** |
| 23 * @see android.app.DownloadManager#addCompletedDownload(String, String, boo
lean, String, | 31 * @see android.app.DownloadManager#addCompletedDownload(String, String, boo
lean, String, |
| 24 * String, long, boolean) | 32 * String, long, boolean) |
| 25 */ | 33 */ |
| 26 protected long addCompletedDownload(String fileName, String description, Str
ing mimeType, | 34 protected long addCompletedDownload(String fileName, String description, Str
ing mimeType, |
| 27 String path, long length, String originalUrl, String referer) { | 35 String path, long length, String originalUrl, String referer) { |
| 28 DownloadManager manager = | 36 DownloadManager manager = |
| 29 (DownloadManager) mContext.getSystemService(Context.DOWNLOAD_SER
VICE); | 37 (DownloadManager) mContext.getSystemService(Context.DOWNLOAD_SER
VICE); |
| 30 String newMimeType = | 38 String newMimeType = |
| 31 ChromeDownloadDelegate.remapGenericMimeType(mimeType, originalUr
l, fileName); | 39 ChromeDownloadDelegate.remapGenericMimeType(mimeType, originalUr
l, fileName); |
| 40 if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) { |
| 41 Class<?> c = manager.getClass(); |
| 42 try { |
| 43 Class[] args = {String.class, String.class, boolean.class, Strin
g.class, |
| 44 String.class, long.class, boolean.class, Uri.class, Uri.
class}; |
| 45 Method method = c.getMethod("addCompletedDownload", args); |
| 46 Uri originalUri = Uri.parse(originalUrl); |
| 47 Uri refererUri = referer == null ? Uri.EMPTY : Uri.parse(referer
); |
| 48 return (Long) method.invoke(manager, fileName, description, true
, newMimeType, path, |
| 49 length, false, originalUri, refererUri); |
| 50 } catch (SecurityException e) { |
| 51 Log.e(TAG, "Cannot access the needed method."); |
| 52 } catch (NoSuchMethodException e) { |
| 53 Log.e(TAG, "Cannot find the needed method."); |
| 54 } catch (InvocationTargetException e) { |
| 55 Log.e(TAG, "Error calling the needed method."); |
| 56 } catch (IllegalAccessException e) { |
| 57 Log.e(TAG, "Error accessing the needed method."); |
| 58 } |
| 59 } |
| 32 return manager.addCompletedDownload(fileName, description, true, newMime
Type, path, length, | 60 return manager.addCompletedDownload(fileName, description, true, newMime
Type, path, length, |
| 33 false); | 61 false); |
| 34 } | 62 } |
| 35 | 63 |
| 36 /** | 64 /** |
| 37 * Interface for returning the query result when it completes. | 65 * Interface for returning the query result when it completes. |
| 38 */ | 66 */ |
| 39 public interface DownloadQueryCallback { | 67 public interface DownloadQueryCallback { |
| 40 /** | 68 /** |
| 41 * Callback function to return query result. | 69 * Callback function to return query result. |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 136 return new DownloadQueryResult(mDownloadItem, downloadStatus, totalT
ime, | 164 return new DownloadQueryResult(mDownloadItem, downloadStatus, totalT
ime, |
| 137 bytesDownloaded, canResolve, failureReason); | 165 bytesDownloaded, canResolve, failureReason); |
| 138 } | 166 } |
| 139 | 167 |
| 140 @Override | 168 @Override |
| 141 protected void onPostExecute(DownloadQueryResult result) { | 169 protected void onPostExecute(DownloadQueryResult result) { |
| 142 mCallback.onQueryCompleted(result, mShowNotifications); | 170 mCallback.onQueryCompleted(result, mShowNotifications); |
| 143 } | 171 } |
| 144 } | 172 } |
| 145 } | 173 } |
| OLD | NEW |