summaryrefslogtreecommitdiffstats
path: root/chrome/android/java/src/org/chromium/chrome/browser/download/DownloadManagerService.java
blob: b82177b1657263939e1ac376d952d5de08b8a229 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package org.chromium.chrome.browser.download;

import android.app.DownloadManager;
import android.content.ActivityNotFoundException;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Environment;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.util.LongSparseArray;
import android.util.Pair;

import org.chromium.base.Log;
import org.chromium.base.ThreadUtils;
import org.chromium.base.VisibleForTesting;
import org.chromium.base.annotations.SuppressFBWarnings;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.IntentHandler;
import org.chromium.chrome.browser.externalnav.ExternalNavigationDelegateImpl;
import org.chromium.content.browser.DownloadController;
import org.chromium.content.browser.DownloadInfo;
import org.chromium.ui.widget.Toast;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * Chrome implementation of the DownloadNotificationService interface. This class is responsible for
 * keeping track of which downloads are in progress. It generates updates for progress of downloads
 * and handles cleaning up of interrupted progress notifications.
 */
public class DownloadManagerService extends BroadcastReceiver implements
        DownloadController.DownloadNotificationService {
    private static final String TAG = "cr.DownloadService";
    private static final String DOWNLOAD_NOTIFICATION_IDS = "DownloadNotificationIds";
    private static final String DOWNLOAD_DIRECTORY = "Download";
    protected static final String PENDING_OMA_DOWNLOADS = "PendingOMADownloads";
    private static final String UNKNOWN_MIME_TYPE = "application/unknown";
    private static final long UPDATE_DELAY_MILLIS = 1000;
    private static final long INVALID_DOWNLOAD_ID = -1L;
    private static final int UNKNOWN_DOWNLOAD_STATUS = -1;
    // Set will be more expensive to initialize, so use an ArrayList here.
    private static final List<String> MIME_TYPES_TO_OPEN = new ArrayList<String>(Arrays.asList(
            OMADownloadHandler.OMA_DOWNLOAD_DESCRIPTOR_MIME,
            "application/pdf",
            "application/x-x509-ca-cert",
            "application/x-x509-user-cert",
            "application/x-x509-server-cert",
            "application/x-pkcs12",
            "application/application/x-pem-file",
            "application/pkix-cert",
            "application/x-wifi-config"));

    private static DownloadManagerService sDownloadManagerService;

    private final SharedPreferences mSharedPrefs;
    private final ConcurrentHashMap<Integer, DownloadProgress> mDownloadProgressMap =
            new ConcurrentHashMap<Integer, DownloadProgress>(4, 0.75f, 2);

    private final DownloadNotifier mDownloadNotifier;
    // Delay between UI updates.
    private final long mUpdateDelayInMillis;

    // Flag to track if we need to post a task to update download notifications.
    private final AtomicBoolean mIsUIUpdateScheduled;
    private final Handler mHandler;
    private final Context mContext;

    private final LongSparseArray<DownloadInfo> mPendingDownloads =
            new LongSparseArray<DownloadInfo>();
    private OMADownloadHandler mOMADownloadHandler;
    private DownloadSnackbarController mDownloadSnackbarController;

    /**
     * Enum representing status of a download.
     */
    private enum DownloadStatus {
        IN_PROGRESS,
        COMPLETE,
        FAILED
    }

    /**
     * Class representing progress of a download.
     */
    private static class DownloadProgress {
        final long mStartTimeInMillis;
        volatile DownloadInfo mDownloadInfo;
        volatile DownloadStatus mDownloadStatus;

        DownloadProgress(long startTimeInMillis, DownloadInfo downloadInfo,
                DownloadStatus downloadStatus) {
            mStartTimeInMillis = startTimeInMillis;
            mDownloadInfo = downloadInfo;
            mDownloadStatus = downloadStatus;
        }
    }

    /**
     * Class representing an OMA download entry to be stored in SharedPrefs.
     */
    @VisibleForTesting
    protected static class OMAEntry {
        final long mDownloadId;
        final String mInstallNotifyURI;

        OMAEntry(long downloadId, String installNotifyURI) {
            mDownloadId = downloadId;
            mInstallNotifyURI = installNotifyURI;
        }

        /**
         * Parse OMA entry from the SharedPrefs String
         * TODO(qinmin): use a file instead of SharedPrefs to store the OMA entry.
         *
         * @param entry String contains the OMA information.
         * @return an OMAEntry object.
         */
        @VisibleForTesting
        static OMAEntry parseOMAEntry(String entry) {
            int index = entry.indexOf(",");
            long downloadId = Long.parseLong(entry.substring(0, index));
            return new OMAEntry(downloadId, entry.substring(index + 1));
        }

        /**
         * Generates a string for an OMA entry to be inserted into the SharedPrefs.
         * TODO(qinmin): use a file instead of SharedPrefs to store the OMA entry.
         *
         * @return a String representing the download entry.
         */
        String generateSharedPrefsString() {
            return String.valueOf(mDownloadId) + "," + mInstallNotifyURI;
        }
    }

    /**
     * Creates DownloadManagerService.
     */
    @SuppressFBWarnings("LI_LAZY_INIT") // Findbugs doesn't see this is only UI thread.
    public static DownloadManagerService getDownloadManagerService(final Context context) {
        ThreadUtils.assertOnUiThread();
        assert context == context.getApplicationContext();
        if (sDownloadManagerService == null) {
            sDownloadManagerService = new DownloadManagerService(context,
                    new SystemDownloadNotifier(context),  new Handler(), UPDATE_DELAY_MILLIS);
        }
        return sDownloadManagerService;
    }

    /**
     * For tests only: sets the DownloadManagerService.
     * @param service An instance of DownloadManagerService.
     * @return Null or a currently set instance of DownloadManagerService.
     */
    @VisibleForTesting
    public static DownloadManagerService setDownloadManagerService(DownloadManagerService service) {
        ThreadUtils.assertOnUiThread();
        DownloadManagerService prev = sDownloadManagerService;
        sDownloadManagerService = service;
        return prev;
    }

    @VisibleForTesting
    protected DownloadManagerService(Context context,
            DownloadNotifier downloadNotifier,
            Handler handler,
            long updateDelayInMillis) {
        mContext = context;
        mSharedPrefs = PreferenceManager.getDefaultSharedPreferences(context
                .getApplicationContext());
        mDownloadNotifier = downloadNotifier;
        mUpdateDelayInMillis = updateDelayInMillis;
        mHandler = handler;
        mIsUIUpdateScheduled = new AtomicBoolean(false);
        mOMADownloadHandler = new OMADownloadHandler(context);
        mDownloadSnackbarController = new DownloadSnackbarController(context);
    }

    @Override
    public void onDownloadCompleted(final DownloadInfo downloadInfo) {
        DownloadStatus status = DownloadStatus.COMPLETE;
        if (!downloadInfo.isSuccessful() || downloadInfo.getContentLength() == 0) {
            status = DownloadStatus.FAILED;
        }
        updateDownloadProgress(downloadInfo, status);
        scheduleUpdateIfNeeded();
    }

    @Override
    public void onDownloadUpdated(final DownloadInfo downloadInfo) {
        updateDownloadProgress(downloadInfo, DownloadStatus.IN_PROGRESS);
        scheduleUpdateIfNeeded();
    }

    /**
     * Clear any pending notifications for incomplete downloads by reading them from shared prefs.
     * When Clank is restarted it clears any old notifications for incomplete downloads.
     */
    public void clearPendingDownloadNotifications() {
        if (mSharedPrefs.contains(DOWNLOAD_NOTIFICATION_IDS)) {
            Set<String> downloadIds = getStoredDownloadInfo(DOWNLOAD_NOTIFICATION_IDS);
            for (String id : downloadIds) {
                int notificationId = parseNotificationId(id);
                if (notificationId > 0) {
                    mDownloadNotifier.cancelNotification(notificationId);
                    Log.w(TAG, "Download failed: Cleared download id:" + id);
                }
            }
            mSharedPrefs.edit().remove(DOWNLOAD_NOTIFICATION_IDS).apply();
        }
        if (mSharedPrefs.contains(PENDING_OMA_DOWNLOADS)) {
            Set<String> omaDownloads = getStoredDownloadInfo(PENDING_OMA_DOWNLOADS);
            for (String omaDownload : omaDownloads) {
                OMAEntry entry = OMAEntry.parseOMAEntry(omaDownload);
                clearPendingOMADownload(entry.mDownloadId, entry.mInstallNotifyURI);
            }
        }
    }

    /**
     * Async task to clear the pending OMA download from SharedPrefs and inform
     * the OMADownloadHandler about download status.
     */
    private class ClearPendingOMADownloadTask extends
            AsyncTask<Void, Void, Pair<Integer, Boolean>> {
        private DownloadInfo mDownloadInfo;
        private final long mDownloadId;
        private final String mInstallNotifyURI;
        private int mFailureReason;

        public ClearPendingOMADownloadTask(long downloadId, String installNotifyURI) {
            mDownloadId = downloadId;
            mInstallNotifyURI = installNotifyURI;
            mDownloadInfo = mPendingDownloads.get(downloadId);
        }

        @Override
        public Pair<Integer, Boolean> doInBackground(Void...voids) {
            DownloadManager manager =
                    (DownloadManager) mContext.getSystemService(Context.DOWNLOAD_SERVICE);
            Cursor c = manager.query(new DownloadManager.Query().setFilterById(mDownloadId));
            int statusIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
            int reasonIndex = c.getColumnIndex(DownloadManager.COLUMN_REASON);
            int filenameIndex = c.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME);
            int status = DownloadManager.STATUS_FAILED;
            Boolean canResolve = false;
            if (c.moveToNext()) {
                status = c.getInt(statusIndex);
                String path = c.getString(filenameIndex);
                String fileName = TextUtils.isEmpty(path) ? null : new File(path).getName();
                if (mDownloadInfo == null) {
                    // Chrome has been killed, reconstruct a DownloadInfo.
                    mDownloadInfo = new DownloadInfo.Builder()
                            .setFileName(fileName)
                            .setDownloadId((int) mDownloadId)
                            .setDescription(c.getString(
                                    c.getColumnIndex(DownloadManager.COLUMN_DESCRIPTION)))
                            .setMimeType(c.getString(
                                    c.getColumnIndex(DownloadManager.COLUMN_MEDIA_TYPE)))
                            .setContentLength(Long.parseLong(c.getString(
                                    c.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES))))
                            .build();
                }
                if (status == DownloadManager.STATUS_SUCCESSFUL) {
                    mDownloadInfo = DownloadInfo.Builder.fromDownloadInfo(mDownloadInfo)
                            .setFileName(fileName)
                            .build();
                    canResolve = canResolveDownloadItem(mContext, mDownloadId);
                } else if (status == DownloadManager.STATUS_FAILED) {
                    mFailureReason = c.getInt(reasonIndex);
                    manager.remove(mDownloadId);
                }
            }
            c.close();
            return Pair.create(status, canResolve);
        }

        @Override
        protected void onPostExecute(Pair<Integer, Boolean> result) {
            if (result.first == DownloadManager.STATUS_SUCCESSFUL) {
                mOMADownloadHandler.onDownloadCompleted(mDownloadInfo, mInstallNotifyURI);
                removeOMADownloadFromSharedPrefs(mDownloadId);
                mDownloadSnackbarController.onDownloadSucceeded(
                        mDownloadInfo, mDownloadId, result.second);
            } else if (result.first == DownloadManager.STATUS_FAILED) {
                mOMADownloadHandler.onDownloadFailed(
                        mDownloadInfo, mFailureReason, mInstallNotifyURI);
                removeOMADownloadFromSharedPrefs(mDownloadId);
                String fileName = mDownloadInfo.getFileName();
                onDownloadFailed(fileName, mFailureReason);
            }
        }
    }

    /**
     * Clear pending OMA downloads for a particular download ID.
     *
     * @param downloadId Download identifier.
     * @param info Information about the download.
     * @param installNotifyURI URI to notify after installation.
     */
    private void clearPendingOMADownload(long downloadId, String installNotifyURI) {
        ClearPendingOMADownloadTask task = new ClearPendingOMADownloadTask(
                downloadId, installNotifyURI);
        task.execute();
    }

    /**
     * Parse the notification ID from a String object.
     *
     * @param id String containing the notification ID.
     * @return notification ID.
     */
    private static int parseNotificationId(String id) {
        try {
            return Integer.parseInt(id);
        } catch (NumberFormatException nfe) {
            Log.w(TAG, "Exception while parsing download id:" + id);
            return -1;
        }
    }

    /**
     * Broadcast that a download was successful.
     * @param downloadInfo info about the download.
     */
    protected void broadcastDownloadSuccessful(DownloadInfo downloadInfo) {}

    /**
     * Gets download information from SharedPrefs.
     * @param type Type of the information to retrieve.
     * @return download information saved to the SharedPrefs for the given type.
     */
    @VisibleForTesting
    protected Set<String> getStoredDownloadInfo(String type) {
        return new HashSet<String>(mSharedPrefs.getStringSet(
                type, new HashSet<String>()));
    }

    /**
     * Removes a donwload Id from SharedPrefs.
     * @param downloadId ID to be removed.
     */
    private void removeDownloadIdFromSharedPrefs(int downloadId) {
        Set<String> downloadIds = getStoredDownloadInfo(DOWNLOAD_NOTIFICATION_IDS);
        String id = Integer.toString(downloadId);
        if (downloadIds.contains(id)) {
            downloadIds.remove(id);
            storeDownloadInfo(DOWNLOAD_NOTIFICATION_IDS, downloadIds);
        }
    }

    /**
     * Add download ID to SharedPrefs.
     * @param downloadId ID to be stored.
     */
    private void addDownloadIdToSharedPrefs(int downloadId) {
        Set<String> downloadIds = getStoredDownloadInfo(DOWNLOAD_NOTIFICATION_IDS);
        downloadIds.add(Integer.toString(downloadId));
        storeDownloadInfo(DOWNLOAD_NOTIFICATION_IDS, downloadIds);
    }

    /**
     * Add OMA download info to SharedPrefs.
     * @param omaInfo OMA download information to save.
     */
    @VisibleForTesting
    protected void addOMADownloadToSharedPrefs(String omaInfo) {
        Set<String> omaDownloads = getStoredDownloadInfo(PENDING_OMA_DOWNLOADS);
        omaDownloads.add(omaInfo);
        storeDownloadInfo(PENDING_OMA_DOWNLOADS, omaDownloads);
    }

    /**
     * Remove OMA download info from SharedPrefs.
     * @param downloadId ID to be removed.
     */
    private void removeOMADownloadFromSharedPrefs(long downloadId) {
        Set<String> omaDownloads = getStoredDownloadInfo(PENDING_OMA_DOWNLOADS);
        for (String omaDownload : omaDownloads) {
            OMAEntry entry = OMAEntry.parseOMAEntry(omaDownload);
            if (entry.mDownloadId == downloadId) {
                omaDownloads.remove(omaDownload);
                storeDownloadInfo(PENDING_OMA_DOWNLOADS, omaDownloads);
                return;
            }
        }
    }

    /**
     * Check if a download ID is in OMA SharedPrefs.
     * @param downloadId Download identifier to check.
     * @param true if it is in the SharedPrefs, or false otherwise.
     */
    private boolean isDownloadIdInOMASharedPrefs(long downloadId) {
        Set<String> omaDownloads = getStoredDownloadInfo(PENDING_OMA_DOWNLOADS);
        for (String omaDownload : omaDownloads) {
            OMAEntry entry = OMAEntry.parseOMAEntry(omaDownload);
            if (entry.mDownloadId == downloadId) {
                return true;
            }
        }
        return false;
    }

    /**
     * Stores download information to shared preferences. The information can be
     * either pending download IDs, or pending OMA downloads.
     *
     * @param type Type of the information.
     * @param downloadInfo Information to be saved.
     */
    private void storeDownloadInfo(String type, Set<String> downloadInfo) {
        SharedPreferences.Editor editor = mSharedPrefs.edit();
        if (downloadInfo.isEmpty()) {
            editor.remove(type);
        } else {
            editor.putStringSet(type, downloadInfo);
        }
        editor.apply();
    }

    /**
     * Updates notifications for all current downloads. Should not be called from UI thread.
     *
     * @return A map that maps all download info to their corresponding download IDs in the
     *         download manager and whether the download can be resolved. If a download fails,
     *         its download ID is INVALID_DOWNLOAD_ID and the launching intent is null.
     */
    private Map<DownloadInfo, Pair<Long, Boolean>> updateAllNotifications() {
        assert !ThreadUtils.runningOnUiThread();
        Map<DownloadInfo, Pair<Long, Boolean>> completionMap =
                new HashMap<DownloadInfo, Pair<Long, Boolean>>();
        for (DownloadProgress progress : mDownloadProgressMap.values()) {
            if (progress != null) {
                switch (progress.mDownloadStatus) {
                    case COMPLETE:
                        removeProgressNotificationForDownload(progress.mDownloadInfo
                                .getDownloadId());
                        long downloadId = addCompletedDownload(progress.mDownloadInfo);
                        if (downloadId == INVALID_DOWNLOAD_ID) {
                            completionMap.put(
                                    progress.mDownloadInfo,
                                    Pair.create(INVALID_DOWNLOAD_ID, false));
                            mDownloadNotifier.notifyDownloadFailed(progress.mDownloadInfo);
                        } else {
                            boolean canResolve = canResolveDownloadItem(mContext, downloadId);
                            completionMap.put(
                                    progress.mDownloadInfo, Pair.create(downloadId, canResolve));
                            mDownloadNotifier.notifyDownloadSuccessful(
                                    progress.mDownloadInfo,
                                    getLaunchIntentFromDownloadId(mContext, downloadId));
                            broadcastDownloadSuccessful(progress.mDownloadInfo);
                        }
                        break;
                    case FAILED:
                        removeProgressNotificationForDownload(progress.mDownloadInfo
                                .getDownloadId());
                        mDownloadNotifier.notifyDownloadFailed(progress.mDownloadInfo);
                        completionMap.put(
                                progress.mDownloadInfo, Pair.create(INVALID_DOWNLOAD_ID, false));
                        Log.w(TAG, "Download failed: " + progress.mDownloadInfo.getFilePath());
                        break;
                    case IN_PROGRESS:
                        mDownloadNotifier.notifyDownloadProgress(progress.mDownloadInfo,
                                progress.mStartTimeInMillis);
                }
            }
        }
        return completionMap;
    }

    /**
     * Add a completed download into DownloadManager.
     *
     * @param downloadInfo Information of the downloaded file.
     * @return download ID if the download is added to the DownloadManager, or
     *         INVALID_DOWNLOAD_ID otherwise.
     */
    protected long addCompletedDownload(DownloadInfo downloadInfo) {
        String mimeType = downloadInfo.getMimeType();
        if (TextUtils.isEmpty(mimeType)) mimeType = UNKNOWN_MIME_TYPE;
        String description = downloadInfo.getDescription();
        if (TextUtils.isEmpty(description)) description = downloadInfo.getFileName();
        DownloadManager manager =
                (DownloadManager) mContext.getSystemService(Context.DOWNLOAD_SERVICE);
        long downloadId = INVALID_DOWNLOAD_ID;
        try {
            downloadId = manager.addCompletedDownload(
                    downloadInfo.getFileName(), description, true, mimeType,
                    downloadInfo.getFilePath(), downloadInfo.getContentLength(), false);
        } catch (IllegalArgumentException e) {
            Log.w(TAG, "Failed to add the download item to DownloadManager: ", e);
            if (downloadInfo.getFilePath() != null) {
                File file = new File(downloadInfo.getFilePath());
                if (!file.delete()) {
                    Log.w(TAG, "Failed to remove the unsucessful download");
                }
            }
            return INVALID_DOWNLOAD_ID;
        }
        return downloadId;
    }

    /**
     * Handle auto opennable files after download completes.
     *
     * @param info Information of the downloaded file.
     * @param downloadId Download identifier issued by the android DownloadManager.
     */
    private void handleAutoOpenAfterDownload(DownloadInfo info, long downloadId) {
        if (OMADownloadHandler.OMA_DOWNLOAD_DESCRIPTOR_MIME.equalsIgnoreCase(info.getMimeType())) {
            mOMADownloadHandler.handleOMADownload(info, downloadId);
            return;
        }
        openDownloadedContent(downloadId);
    }

    /**
     * Schedule an update if there is no update scheduled.
     */
    private void scheduleUpdateIfNeeded() {
        if (mIsUIUpdateScheduled.compareAndSet(false, true)) {
            Runnable updateTask = new Runnable() {
                @Override
                public void run() {
                    new AsyncTask<Void, Void, Map<DownloadInfo, Pair<Long, Boolean>>>() {
                        @Override
                        public Map<DownloadInfo, Pair<Long, Boolean>> doInBackground(
                                Void... params) {
                            return updateAllNotifications();
                        }

                        @Override
                        protected void onPostExecute(
                                Map<DownloadInfo, Pair<Long, Boolean>> result) {
                            for (Map.Entry<DownloadInfo, Pair<Long, Boolean>> entry :
                                    result.entrySet()) {
                                DownloadInfo info = entry.getKey();
                                long downloadId = entry.getValue().first;
                                if (downloadId == INVALID_DOWNLOAD_ID) {
                                    // TODO(qinmin): get the failure message from native.
                                    onDownloadFailed(info.getFileName(),
                                            DownloadManager.ERROR_UNKNOWN);
                                } else {
                                    boolean canResolve = entry.getValue().second;
                                    if (canResolve && shouldOpenAfterDownload(info)) {
                                        handleAutoOpenAfterDownload(info, downloadId);
                                    } else {
                                        mDownloadSnackbarController.onDownloadSucceeded(
                                                info, downloadId, canResolve);
                                    }
                                }
                            }
                        }
                    }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
                    mIsUIUpdateScheduled.set(false);
                }
            };
            mHandler.postDelayed(updateTask, mUpdateDelayInMillis);
        }
    }

    /**
     * Cancel the progress notification of download and clear any cached information about this
     * download.
     *
     * @param downloadId Download Identifier.
     */
    void removeProgressNotificationForDownload(int downloadId) {
        mDownloadProgressMap.remove(downloadId);
        mDownloadNotifier.cancelNotification(downloadId);
        removeDownloadIdFromSharedPrefs(downloadId);
    }

    /**
     * Updates the progress of a download.
     *
     * @param downloadInfo Information about the download.
     * @param status Status of the download.
     */
    private void updateDownloadProgress(DownloadInfo downloadInfo, DownloadStatus status) {
        assert downloadInfo.hasDownloadId();
        int downloadId = downloadInfo.getDownloadId();
        DownloadProgress progress = mDownloadProgressMap.get(downloadId);
        if (progress == null) {
            progress = new DownloadProgress(System.currentTimeMillis(), downloadInfo,
                    status);
            if (status == DownloadStatus.IN_PROGRESS) {
                // A new in-progress download, add an entry to shared prefs to make sure
                // to clear the notification.
                addDownloadIdToSharedPrefs(downloadId);
            }
            mDownloadProgressMap.putIfAbsent(downloadId, progress);
        } else {
            progress.mDownloadStatus = status;
            progress.mDownloadInfo = downloadInfo;
        }
    }

    /**
     * Sets the download handler for OMA downloads, for testing purpose.
     *
     * @param omaDownloadHandler Download handler for OMA contents.
     */
    @VisibleForTesting
    protected void setOMADownloadHandler(OMADownloadHandler omaDownloadHandler) {
        mOMADownloadHandler = omaDownloadHandler;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (!DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) return;
        long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID,
                INVALID_DOWNLOAD_ID);
        if (downloadId == INVALID_DOWNLOAD_ID) return;
        boolean isPendingOMADownload = mOMADownloadHandler.isPendingOMADownload(downloadId);
        boolean isInOMASharedPrefs = isDownloadIdInOMASharedPrefs(downloadId);
        if (isPendingOMADownload || isInOMASharedPrefs) {
            clearPendingOMADownload(downloadId, null);
            mPendingDownloads.remove(downloadId);
            return;
        }
        DownloadInfo info = mPendingDownloads.get(downloadId);
        if (info != null) {
            DownloadCompletionTask task = new DownloadCompletionTask(info, downloadId);
            task.execute();
            mPendingDownloads.remove(downloadId);
            if (mPendingDownloads.size() == 0) {
                mContext.unregisterReceiver(this);
            }
        }
    }

    /**
     * Async task to handle completed downloads.
     */
    private class DownloadCompletionTask extends AsyncTask<Void, Void, Pair<Integer, Boolean>> {
        private final long mDownloadId;
        private final DownloadInfo mDownloadInfo;
        private int mFailureReason;

        public DownloadCompletionTask(DownloadInfo info, long downloadId) {
            mDownloadInfo = info;
            mDownloadId = downloadId;
        }

        @Override
        public Pair<Integer, Boolean> doInBackground(Void...voids) {
            final DownloadManager manager =
                    (DownloadManager) mContext.getSystemService(Context.DOWNLOAD_SERVICE);
            Cursor c = manager.query(new DownloadManager.Query().setFilterById(mDownloadId));
            int status = UNKNOWN_DOWNLOAD_STATUS;
            boolean canResolve = false;
            if (c.moveToNext()) {
                status = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));
                if (status == DownloadManager.STATUS_SUCCESSFUL) {
                    canResolve = canResolveDownloadItem(mContext, mDownloadId);
                } else if (status == DownloadManager.STATUS_FAILED) {
                    mFailureReason = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_REASON));
                }
            }
            c.close();
            return Pair.create(status, canResolve);
        }

        @Override
        protected void onPostExecute(Pair<Integer, Boolean> result) {
            switch (result.first) {
                case DownloadManager.STATUS_SUCCESSFUL:
                    if (shouldOpenAfterDownload(mDownloadInfo) && result.second) {
                        handleAutoOpenAfterDownload(mDownloadInfo, mDownloadId);
                    } else {
                        mDownloadSnackbarController.onDownloadSucceeded(
                                mDownloadInfo, mDownloadId, result.second);
                    }
                    break;
                case DownloadManager.STATUS_FAILED:
                    onDownloadFailed(mDownloadInfo.getFileName(), mFailureReason);
                    break;
                default:
                    break;
            }
        }
    }

    /**
     * Sends the download request to Android download manager. If |notifyCompleted| is true,
     * a notification will be sent to the user once download is complete and the downloaded
     * content will be saved to the public directory on external storage. Otherwise, the
     * download will be saved in the app directory and user will not get any notifications
     * after download completion.
     *
     * @param info Download information about the download.
     * @param notifyCompleted Whether to notify the user after Downloadmanager completes the
     *                        download.
     */
    public void enqueueDownloadManagerRequest(final DownloadInfo info, boolean notifyCompleted) {
        EnqueueDownloadRequestTask task = new EnqueueDownloadRequestTask(info);
        task.execute(notifyCompleted);
    }

    /**
     * Async task to enqueue a download request into DownloadManager.
     */
    private class EnqueueDownloadRequestTask extends
            AsyncTask<Boolean, Void, Boolean> {
        private long mDownloadId;
        private DownloadInfo mDownloadInfo;
        private int mFailureReason;

        public EnqueueDownloadRequestTask(DownloadInfo downloadInfo) {
            mDownloadInfo = downloadInfo;
        }

        @Override
        public Boolean doInBackground(Boolean... booleans) {
            boolean notifyCompleted = booleans[0];
            Uri uri = Uri.parse(mDownloadInfo.getUrl());
            DownloadManager.Request request;
            try {
                request = new DownloadManager.Request(uri);
            } catch (IllegalArgumentException e) {
                Log.e(TAG, "Cannot download non http or https scheme");
                // Use ERROR_UNHANDLED_HTTP_CODE so that it will be treated as
                // a server error.
                mFailureReason = DownloadManager.ERROR_UNHANDLED_HTTP_CODE;
                return false;
            }

            request.setMimeType(mDownloadInfo.getMimeType());
            try {
                if (notifyCompleted) {
                    // Set downloaded file destination to /sdcard/Download or, should it be
                    // set to one of several Environment.DIRECTORY* dirs depending on mimetype?
                    request.setDestinationInExternalPublicDir(
                            Environment.DIRECTORY_DOWNLOADS, mDownloadInfo.getFileName());
                } else {
                    File dir = new File(mContext.getExternalFilesDir(null), DOWNLOAD_DIRECTORY);
                    if (dir.mkdir() || dir.isDirectory()) {
                        File file = new File(dir, mDownloadInfo.getFileName());
                        request.setDestinationUri(Uri.fromFile(file));
                    } else {
                        Log.e(TAG, "Cannot create download directory");
                        mFailureReason = DownloadManager.ERROR_FILE_ERROR;
                        return false;
                    }
                }
            } catch (IllegalStateException e) {
                Log.e(TAG, "Cannot create download directory");
                mFailureReason = DownloadManager.ERROR_FILE_ERROR;
                return false;
            }

            if (notifyCompleted) {
                // Let this downloaded file be scanned by MediaScanner - so that it can
                // show up in Gallery app, for example.
                request.allowScanningByMediaScanner();
                request.setNotificationVisibility(
                        DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
            } else {
                request.setNotificationVisibility(
                        DownloadManager.Request.VISIBILITY_VISIBLE);
            }
            String description = mDownloadInfo.getDescription();
            if (TextUtils.isEmpty(description)) {
                description = mDownloadInfo.getFileName();
            }
            request.setDescription(description);
            request.addRequestHeader("Cookie", mDownloadInfo.getCookie());
            request.addRequestHeader("Referer", mDownloadInfo.getReferer());
            request.addRequestHeader("User-Agent", mDownloadInfo.getUserAgent());

            DownloadManager manager =
                    (DownloadManager) mContext.getSystemService(Context.DOWNLOAD_SERVICE);
            try {
                mDownloadId = manager.enqueue(request);
            } catch (IllegalArgumentException e) {
                // See crbug.com/143499 for more details.
                Log.e(TAG, "Download failed: " + e);
                mFailureReason = DownloadManager.ERROR_UNKNOWN;
                return false;
            } catch (RuntimeException e) {
                // See crbug.com/490442 for more details.
                Log.e(TAG, "Failed to create target file on the external storage: " + e);
                mFailureReason = DownloadManager.ERROR_FILE_ERROR;
                return false;
            }
            return true;
        }

        @Override
        protected void onPostExecute(Boolean result) {
            boolean isPendingOMADownload =
                    mOMADownloadHandler.isPendingOMADownload(mDownloadInfo.getDownloadId());
            if (!result) {
                onDownloadFailed(mDownloadInfo.getFileName(), mFailureReason);
                if (isPendingOMADownload) {
                    mOMADownloadHandler.onDownloadFailed(
                            mDownloadInfo, DownloadManager.ERROR_UNKNOWN, null);
                }
                return;
            }
            Toast.makeText(mContext, R.string.download_pending, Toast.LENGTH_SHORT).show();
            if (isPendingOMADownload) {
                // A new downloadId is generated, needs to update the OMADownloadHandler
                // about this.
                mDownloadInfo = mOMADownloadHandler.updateDownloadInfo(
                        mDownloadInfo, mDownloadId);
                // TODO(qinmin): use a file instead of shared prefs to save the
                // OMA information in case chrome is killed. This will allow us to
                // save more information like cookies and user agent.
                String notifyUri = mOMADownloadHandler.getInstallNotifyInfo(mDownloadId);
                if (!TextUtils.isEmpty(notifyUri)) {
                    OMAEntry entry = new OMAEntry(mDownloadId, notifyUri);
                    addOMADownloadToSharedPrefs(entry.generateSharedPrefsString());
                }
            }
            if (mPendingDownloads.size() == 0) {
                mContext.registerReceiver(DownloadManagerService.this,
                        new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
            }
            mPendingDownloads.put(mDownloadId, mDownloadInfo);
        }
    }

    /**
     * Determines if the download should be immediately opened after
     * downloading.
     *
     * @param downloadInfo Information about the download.
     * @return true if the downloaded content should be opened, or false otherwise.
     */
    @VisibleForTesting
    static boolean shouldOpenAfterDownload(DownloadInfo downloadInfo) {
        String type = downloadInfo.getMimeType();
        return downloadInfo.hasUserGesture() && !isAttachment(downloadInfo.getContentDisposition())
                && MIME_TYPES_TO_OPEN.contains(type);
    }

    /**
     * Returns true if the download meant to be treated as an attachment.
     *
     * @param contentDisposition Content disposition of the download.
     * @return true if the downloaded is an attachment, or false otherwise.
     */
    public static boolean isAttachment(String contentDisposition) {
        return contentDisposition != null
                && contentDisposition.regionMatches(true, 0, "attachment", 0, 10);
    }

    /**
     * Launch the best activity for the given intent. If a default activity is provided,
     * choose the default one. Otherwise, return the Intent picker if there are more than one
     * capable activities. If the intent is pdf type, return the platform pdf viewer if
     * it is available so user don't need to choose it from Intent picker.
     *
     * @param context Context of the app.
     * @param intent Intent to open.
     * @param allowSelfOpen Whether chrome itself is allowed to open the intent.
     * @return true if an Intent is launched, or false otherwise.
     */
    public static boolean openIntent(Context context, Intent intent, boolean allowSelfOpen) {
        boolean activityResolved = ExternalNavigationDelegateImpl.resolveIntent(
                context, intent, allowSelfOpen);
        if (activityResolved) {
            try {
                context.startActivity(intent);
                return true;
            } catch (ActivityNotFoundException ex) {
                Log.d(TAG, "activity not found for " + intent.getType()
                        + " over " + intent.getData().getScheme(), ex);
            }
        }
        return false;
    }

    /**
     * Return the intent to launch for a given download item.
     *
     * @param context Context of the app.
     * @param downloadId ID of the download item in DownloadManager.
     * @return the intent to launch for the given download item.
     */
    private static Intent getLaunchIntentFromDownloadId(Context context, long downloadId) {
        assert !ThreadUtils.runningOnUiThread();
        DownloadManager manager =
                (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
        Uri uri = manager.getUriForDownloadedFile(downloadId);
        if (uri == null) return null;
        Intent launchIntent = new Intent(Intent.ACTION_VIEW);
        launchIntent.setDataAndType(uri, manager.getMimeTypeForDownloadedFile(downloadId));
        launchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                | Intent.FLAG_GRANT_READ_URI_PERMISSION);
        Uri referrer = new Uri.Builder().scheme(
                IntentHandler.ANDROID_APP_REFERRER_SCHEME).authority(
                        context.getPackageName()).build();
        launchIntent.putExtra(Intent.EXTRA_REFERRER, referrer);
        return launchIntent;
    }

    /**
     * Return whether a download item can be resolved to any activity.
     *
     * @param context Context of the app.
     * @param downloadId ID of the download item in DownloadManager.
     * @return true if the download item can be resolved, or false otherwise.
     */
    private static boolean canResolveDownloadItem(Context context, long downloadId) {
        assert !ThreadUtils.runningOnUiThread();
        Intent intent = getLaunchIntentFromDownloadId(context, downloadId);
        return (intent == null) ? false : ExternalNavigationDelegateImpl.resolveIntent(
                context, intent, true);
    }

    /**
     * Launch the intent for a given download item.
     *
     * @param downloadId ID of the download item in DownloadManager.
     */
    protected void openDownloadedContent(final long downloadId) {
        new AsyncTask<Void, Void, Intent>() {
            @Override
            public Intent doInBackground(Void... params) {
                return getLaunchIntentFromDownloadId(mContext, downloadId);
            }

            @Override
            protected void onPostExecute(Intent intent) {
                if (intent != null) {
                    openIntent(mContext, intent, true);
                }
            }
        }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
    }

    /**
     * Called when a download fails.
     *
     * @param fileName Name of the download file.
     * @param reason Reason of failure reported by android DownloadManager
     */
    protected void onDownloadFailed(String fileName, int reason) {
        String reasonString = mContext.getString(
                R.string.download_failed_reason_unknown_error, fileName);
        switch (reason) {
            case DownloadManager.ERROR_FILE_ALREADY_EXISTS:
                reasonString = mContext.getString(
                        R.string.download_failed_reason_file_already_exists, fileName);
                break;
            case DownloadManager.ERROR_FILE_ERROR:
                reasonString = mContext.getString(
                        R.string.download_failed_reason_file_system_error, fileName);
                break;
            case DownloadManager.ERROR_INSUFFICIENT_SPACE:
                reasonString = mContext.getString(
                        R.string.download_failed_reason_insufficient_space, fileName);
                break;
            case DownloadManager.ERROR_CANNOT_RESUME:
            case DownloadManager.ERROR_HTTP_DATA_ERROR:
                reasonString = mContext.getString(
                        R.string.download_failed_reason_network_failures, fileName);
                break;
            case DownloadManager.ERROR_TOO_MANY_REDIRECTS:
            case DownloadManager.ERROR_UNHANDLED_HTTP_CODE:
                reasonString = mContext.getString(
                        R.string.download_failed_reason_server_issues, fileName);
                break;
            case DownloadManager.ERROR_DEVICE_NOT_FOUND:
                reasonString = mContext.getString(
                        R.string.download_failed_reason_storage_not_found, fileName);
                break;
            case DownloadManager.ERROR_UNKNOWN:
            default:
                break;
        }
        mDownloadSnackbarController.onDownloadFailed(
                reasonString, reason == DownloadManager.ERROR_FILE_ALREADY_EXISTS);
    }

    /**
     * Set the DownloadSnackbarController for testing purpose.
     */
    @VisibleForTesting
    protected void setDownloadSnackbarController(
            DownloadSnackbarController downloadSnackbarController) {
        mDownloadSnackbarController = downloadSnackbarController;
    }

    /**
     * Open the Activity which shows a list of all downloads.
     * @param context Application context
     */
    protected static void openDownloadsPage(Context context) {
        Intent pageView = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
        pageView.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        try {
            context.startActivity(pageView);
        } catch (ActivityNotFoundException e) {
            Log.e(TAG, "Cannot find Downloads app", e);
        }
    }
}