summaryrefslogtreecommitdiffstats
path: root/src/com/android/browser/DownloadHandler.java
blob: 6db529a29323746dd7ecc092ed382a0ad32e3234 (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
/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.browser;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.DownloadManager;
import android.app.DownloadManager.Request;
import android.content.ActivityNotFoundException;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.StatFs;
import android.os.storage.StorageManager;
import android.text.TextUtils;
import android.util.Log;
import org.codeaurora.swe.CookieManager;
import org.w3c.dom.Text;

import android.webkit.URLUtil;
import android.widget.Toast;

import com.android.browser.R;
import com.android.browser.mdm.DownloadDirRestriction;
import com.android.browser.platformsupport.WebAddress;
import com.android.browser.reflect.ReflectHelper;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import java.io.File;
/**
 * Handle download requests
 */
public class DownloadHandler {

    private static final boolean LOGD_ENABLED =
            com.android.browser.Browser.LOGD_ENABLED;

    private static final String LOGTAG = "DLHandler";
    private static String mInternalStorage;
    private static String mExternalStorage;
    private final static String INVALID_PATH = "/storage";

    public static void startingDownload(Activity activity,
            String url, String userAgent, String contentDisposition,
            String mimetype, String referer, String auth,
            boolean privateBrowsing, long contentLength,
            String filename, String downloadPath) {
        // java.net.URI is a lot stricter than KURL so we have to encode some
        // extra characters. Fix for b 2538060 and b 1634719
        WebAddress webAddress;
        try {
            webAddress = new WebAddress(url);
            webAddress.setPath(encodePath(webAddress.getPath()));
        } catch (Exception e) {
            // This only happens for very bad urls, we want to chatch the
            // exception here
            Log.e(LOGTAG, "Exception trying to parse url:" + url);
            return;
        }

        String addressString = webAddress.toString();
        Uri uri = Uri.parse(addressString);
        final DownloadManager.Request request;
        try {
            request = new DownloadManager.Request(uri);
        } catch (IllegalArgumentException e) {
            Toast.makeText(activity, R.string.cannot_download, Toast.LENGTH_SHORT).show();
            return;
        }
        request.setMimeType(mimetype);
        // set downloaded file destination to /sdcard/Download.
        // or, should it be set to one of several Environment.DIRECTORY* dirs
        // depending on mimetype?
        try {
            setDestinationDir(downloadPath, filename, request);
        } catch (Exception e) {
            showNoEnoughMemoryDialog(activity);
            return;
        }
        // let this downloaded file be scanned by MediaScanner - so that it can
        // show up in Gallery app, for example.
        request.allowScanningByMediaScanner();
        request.setDescription(webAddress.getHost());
        // XXX: Have to use the old url since the cookies were stored using the
        // old percent-encoded url.

        String cookies = CookieManager.getInstance().getCookie(url, privateBrowsing);
        request.addRequestHeader("cookie", cookies);
        request.addRequestHeader("User-Agent", userAgent);
        request.addRequestHeader("Referer", referer);
        request.setVisibleInDownloadsUi(!privateBrowsing);
        if (!TextUtils.isEmpty(auth)) {
            request.addRequestHeader("Authorization", auth);
        }
        request.setNotificationVisibility(
                DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
        final DownloadManager manager = (DownloadManager) activity
                .getSystemService(Context.DOWNLOAD_SERVICE);
        new Thread("Browser download") {
            public void run() {
                try {
                    manager.enqueue(request);
                } catch (Exception e) {
                     Log.w("DLHandler", "Could not enqueue the download", e);
                }
            }
        }.start();
        showStartDownloadToast(activity, privateBrowsing);
    }

    private static boolean isAudioFileType(int fileType){
        Object[] params  = {Integer.valueOf(fileType)};
        Class[] type = new Class[] {int.class};
        Boolean result = (Boolean) ReflectHelper.invokeMethod("android.media.MediaFile",
                               "isAudioFileType", type, params);
        return result;
    }

    private static boolean isVideoFileType(int fileType){
        Object[] params  = {Integer.valueOf(fileType)};
        Class[] type = new Class[] {int.class};
        Boolean result = (Boolean) ReflectHelper.invokeMethod("android.media.MediaFile",
                             "isVideoFileType", type, params);
        return result;
    }

    /**
     * Notify the host application a download should be done, or that
     * the data should be streamed if a streaming viewer is available.
     * @param activity Activity requesting the download.
     * @param url The full url to the content that should be downloaded
     * @param userAgent User agent of the downloading application.
     * @param contentDisposition Content-disposition http header, if present.
     * @param mimetype The mimetype of the content reported by the server
     * @param referer The referer associated with the downloaded url
     * @param privateBrowsing If the request is coming from a private browsing tab.
     */
    public static boolean onDownloadStart(final Activity activity, final String url,
            final String userAgent, final String contentDisposition, final String mimetype,
            final String referer, final String auth, final boolean privateBrowsing,
            final long contentLength) {
        // if we're dealing wih A/V content that's not explicitly marked
        //     for download, check if it's streamable.
        if (contentDisposition == null
                || !contentDisposition.regionMatches(
                        true, 0, "attachment", 0, 10)) {
            // Add for Carrier Feature - When open an audio/video link, prompt a dialog
            // to let the user choose play or download operation.
            Uri uri = Uri.parse(url);
            String scheme = uri.getScheme();
            Log.v(LOGTAG, "scheme:" + scheme + ", mimetype:" + mimetype);
            // Some mimetype for audio/video files is not started with "audio" or "video",
            // such as ogg audio file with mimetype "application/ogg". So we also check
            // file type by MediaFile.isAudioFileType() and MediaFile.isVideoFileType().
            // For those file types other than audio or video, download it immediately.
            Object[] params = {mimetype};
            Class[] type = new Class[] {String.class};
            Integer result = (Integer) ReflectHelper.invokeMethod("android.media.MediaFile",
                                           "getFileTypeForMimeType", type, params);
            int fileType = result.intValue();
            if ("http".equalsIgnoreCase(scheme) &&
                    (mimetype.startsWith("audio/") ||
                        mimetype.startsWith("video/") ||
                            isAudioFileType(fileType) ||
                                isVideoFileType(fileType))) {
                new AlertDialog.Builder(activity)
                .setTitle(R.string.application_name)
                .setIcon(R.drawable.default_video_poster)
                .setMessage(R.string.http_video_msg)
                .setPositiveButton(R.string.video_save, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        onDownloadStartNoStream(activity, url, userAgent, contentDisposition,
                                mimetype, referer, auth, privateBrowsing, contentLength);
                    }
                 })
                .setNegativeButton(R.string.video_play, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent(Intent.ACTION_VIEW);
                        intent.setDataAndType(Uri.parse(url), mimetype);
                        try {
                            String trimmedcontentDisposition = trimContentDisposition(contentDisposition);
                            String title = URLUtil.guessFileName(url, trimmedcontentDisposition, mimetype);
                            intent.putExtra(Intent.EXTRA_TITLE, title);
                            activity.startActivity(intent);
                        } catch (ActivityNotFoundException ex) {
                            Log.w(LOGTAG, "When http stream play, activity not found for "
                                    + mimetype + " over " + Uri.parse(url).getScheme(), ex);
                        }
                    }
                }).show();

                return true;
            }
            // query the package manager to see if there's a registered handler
            //     that matches.
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.parse(url), mimetype);
            ResolveInfo info = activity.getPackageManager().resolveActivity(intent,
                    PackageManager.MATCH_DEFAULT_ONLY);
            if (info != null) {
                ComponentName myName = activity.getComponentName();
                // If we resolved to ourselves, we don't want to attempt to
                // load the url only to try and download it again.
                if (!myName.getPackageName().equals(
                        info.activityInfo.packageName)
                        || !myName.getClassName().equals(
                                info.activityInfo.name)) {
                    // someone (other than us) knows how to handle this mime
                    // type with this scheme, don't download.
                    try {
                        activity.startActivity(intent);
                        return false;
                    } catch (ActivityNotFoundException ex) {
                        if (LOGD_ENABLED) {
                            Log.d(LOGTAG, "activity not found for " + mimetype
                                    + " over " + Uri.parse(url).getScheme(),
                                    ex);
                        }
                        // Best behavior is to fall back to a download in this
                        // case
                    }
                }
            }
        }
        onDownloadStartNoStream(activity, url, userAgent, contentDisposition,
                mimetype, referer, auth, privateBrowsing, contentLength);
        return false;
    }

    // This is to work around the fact that java.net.URI throws Exceptions
    // instead of just encoding URL's properly
    // Helper method for onDownloadStartNoStream
    private static String encodePath(String path) {
        char[] chars = path.toCharArray();

        boolean needed = false;
        for (char c : chars) {
            if (c == '[' || c == ']' || c == '|') {
                needed = true;
                break;
            }
        }
        if (needed == false) {
            return path;
        }

        StringBuilder sb = new StringBuilder("");
        for (char c : chars) {
            if (c == '[' || c == ']' || c == '|') {
                sb.append('%');
                sb.append(Integer.toHexString(c));
            } else {
                sb.append(c);
            }
        }

        return sb.toString();
    }

    /**
     * Notify the host application a download should be done, even if there
     * is a streaming viewer available for thise type.
     * @param activity Activity requesting the download.
     * @param url The full url to the content that should be downloaded
     * @param userAgent User agent of the downloading application.
     * @param contentDisposition Content-disposition http header, if present.
     * @param mimetype The mimetype of the content reported by the server
     * @param referer The referer associated with the downloaded url
     * @param privateBrowsing If the request is coming from a private browsing tab.
     */
    /* package */static void onDownloadStartNoStream(Activity activity,
            String url, String userAgent, String contentDisposition,
            String mimetype, String referer, String auth,
            boolean privateBrowsing, long contentLength) {

        contentDisposition = trimContentDisposition(contentDisposition);

        String filename = URLUtil.guessFileName(url,
                contentDisposition, mimetype);

        // Check to see if we have an SDCard
        String status = Environment.getExternalStorageState();
        if (!status.equals(Environment.MEDIA_MOUNTED)) {
            int title;
            String msg;

            // Check to see if the SDCard is busy, same as the music app
            if (status.equals(Environment.MEDIA_SHARED)) {
                msg = activity.getString(R.string.download_sdcard_busy_dlg_msg);
                title = R.string.download_sdcard_busy_dlg_title;
            } else {
                msg = activity.getString(R.string.download_no_sdcard_dlg_msg, filename);
                title = R.string.download_no_sdcard_dlg_title;
            }

            new AlertDialog.Builder(activity)
                .setTitle(title)
                .setIconAttribute(android.R.attr.alertDialogIcon)
                .setMessage(msg)
                .setPositiveButton(R.string.ok, null)
                .show();
            return;
        }

        if (mimetype == null) {
            // We must have long pressed on a link or image to download it. We
            // are not sure of the mimetype in this case, so do a head request
            new FetchUrlMimeType(activity, url, userAgent, referer, auth,
                    privateBrowsing, filename).start();
        } else {
            if (DownloadDirRestriction.getInstance().downloadsAllowed()) {
                startDownloadSettings(activity, url, userAgent, contentDisposition, mimetype, referer,
                        auth, privateBrowsing, contentLength, filename);
            }
            else {
                Toast.makeText(activity, R.string.managed_by_your_administrator, Toast.LENGTH_SHORT)
                .show();
            }
        }

    }

    static String trimContentDisposition(String contentDisposition) {
        final Pattern CONTENT_DISPOSITION_PATTERN =
            Pattern.compile("filename\\s*=\\s*(\"?)([^\";]*)\\1\\s*",
                Pattern.CASE_INSENSITIVE);

        if (contentDisposition != null) {

            try {
                Matcher m = CONTENT_DISPOSITION_PATTERN.matcher(contentDisposition);
                if (m.find()) {
                    contentDisposition = "attachment; filename="+m.group(2);
                }

                return contentDisposition;
            } catch (IllegalStateException ex) {
                // This function is defined as returning null when it can't parse the header
            }
        }
        return null;
    }

    public static void initStorageDefaultPath(Context context, String downloadPath) {
        mExternalStorage = getExternalStorageDirectory(context, downloadPath);
        if (isPhoneStorageSupported()) {
            mInternalStorage = Environment.getExternalStorageDirectory().getPath();
        } else {
            mInternalStorage = null;
        }
    }

    public static void startDownloadSettings(Activity activity,
            String url, String userAgent, String contentDisposition,
            String mimetype, String referer, String auth,
            boolean privateBrowsing, long contentLength,
            String filename) {
        Bundle fileInfo = new Bundle();
        fileInfo.putString("url", url);
        fileInfo.putString("userAgent", userAgent);
        fileInfo.putString("contentDisposition", contentDisposition);
        fileInfo.putString("mimetype", mimetype);
        fileInfo.putString("referer", referer);
        fileInfo.putString("authorization", auth);
        fileInfo.putLong("contentLength", contentLength);
        fileInfo.putBoolean("privateBrowsing", privateBrowsing);
        fileInfo.putString("filename", filename);
        Intent intent = new Intent("android.intent.action.BROWSERDOWNLOAD");

        // Since there could be multiple browsers capable of handling
        //  the same intent we assure that the same package handles it
        intent.setPackage(activity.getPackageName());

        intent.putExtras(fileInfo);
        activity.startActivity(intent);
    }

    public static void setAppointedFolder(String downloadPath) {
        File file = new File(downloadPath);
        if (file.exists()) {
            if (!file.isDirectory()) {
                throw new IllegalStateException(file.getAbsolutePath() +
                        " already exists and is not a directory");
            }
        } else {
            if (!file.mkdir()) {
                throw new IllegalStateException("Unable to create directory: " +
                        file.getAbsolutePath());
            }
        }
    }

    private static void setDestinationDir(String downloadPath, String filename, Request request) {
        File file = new File(downloadPath);
        if (file.exists()) {
            if (!file.isDirectory()) {
                throw new IllegalStateException(file.getAbsolutePath() +
                        " already exists and is not a directory");
            }
        } else {
            if (!file.mkdir()) {
                throw new IllegalStateException("Unable to create directory: " +
                        file.getAbsolutePath());
            }
        }
        setDestinationFromBase(file, filename, request);
    }

    private static void setDestinationFromBase(File file, String filename, Request request) {
        if (filename == null) {
            throw new NullPointerException("filename cannot be null");
        }
        request.setDestinationUri(Uri.withAppendedPath(Uri.fromFile(file), filename));
    }

    public static void fileExistQueryDialog(Activity activity) {
        new AlertDialog.Builder(activity)
                .setTitle(R.string.download_file_exist)
                .setIcon(android.R.drawable.ic_dialog_info)
                .setMessage(R.string.download_file_exist_msg)
                // if yes, delete existed file and start new download thread
                .setPositiveButton(R.string.ok, null)
                // if no, do nothing at all
                .show();
    }

    public static long getAvailableMemory(String root) {
        StatFs stat = new StatFs(root);
        final long LEFT10MByte = 2560;
        long blockSize = stat.getBlockSize();
        long availableBlocks = stat.getAvailableBlocks() - LEFT10MByte;
        return availableBlocks * blockSize;
    }

    public static void showNoEnoughMemoryDialog(Activity mContext) {
        new AlertDialog.Builder(mContext)
                .setTitle(R.string.download_no_enough_memory)
                .setIconAttribute(android.R.attr.alertDialogIcon)
                .setMessage(R.string.download_no_enough_memory)
                .setPositiveButton(R.string.ok, null)
                .show();
    }

    public static boolean manageNoEnoughMemory(long contentLength, String root) {
        long mAvailableBytes = getAvailableMemory(root);
        if (mAvailableBytes > 0) {
            if (contentLength > mAvailableBytes) {
                return true;
            }
        } else {
            return true;
        }
        return false;
    }

    public static void showStartDownloadToast(Activity activity,
            boolean privateBrowsing) {
        if (!privateBrowsing) {
            Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            activity.startActivity(intent);
        } else {
            activity.finish();
        }
        Toast.makeText(activity, R.string.download_pending, Toast.LENGTH_SHORT)
                .show();
    }

    /**
     * wheather the storage status OK for download file
     *
     * @param activity
     * @param filename the download file's name
     * @param downloadPath the download file's path will be in
     * @return boolean true is ok,and false is not
     */
    public static boolean isStorageStatusOK(Activity activity, String filename, String downloadPath) {
        if (downloadPath.equals(INVALID_PATH)) {
            new AlertDialog.Builder(activity)
                    .setTitle(R.string.path_wrong)
                    .setIcon(android.R.drawable.ic_dialog_alert)
                    .setMessage(R.string.invalid_path)
                    .setPositiveButton(R.string.ok, null)
                    .show();
            return false;
        }

        // assure that internal storage is initialized before
        // comparing it with download path
        if (mInternalStorage == null) {
            initStorageDefaultPath(activity, downloadPath);
        }

        if (!(isPhoneStorageSupported() && downloadPath.contains(mInternalStorage))) {
            String status = getExternalStorageState(activity, downloadPath);
            if (!status.equals(Environment.MEDIA_MOUNTED)) {
                int title;
                String msg;

                // Check to see if the SDCard is busy, same as the music app
                if (status.equals(Environment.MEDIA_SHARED)) {
                    msg = activity.getString(R.string.download_sdcard_busy_dlg_msg);
                    title = R.string.download_sdcard_busy_dlg_title;
                } else {
                    msg = activity.getString(R.string.download_no_sdcard_dlg_msg, filename);
                    title = R.string.download_no_sdcard_dlg_title;
                }

                new AlertDialog.Builder(activity)
                        .setTitle(title)
                        .setIcon(android.R.drawable.ic_dialog_alert)
                        .setMessage(msg)
                        .setPositiveButton(R.string.ok, null)
                        .show();
                return false;
            }
        } else {
            String status = Environment.getExternalStorageState();
            if (!status.equals(Environment.MEDIA_MOUNTED)) {
                int mTitle = R.string.download_path_unavailable_dlg_title;
                String mMsg = activity.getString(R.string.download_path_unavailable_dlg_msg);
                new AlertDialog.Builder(activity)
                        .setTitle(mTitle)
                        .setIcon(android.R.drawable.ic_dialog_alert)
                        .setMessage(mMsg)
                        .setPositiveButton(R.string.ok, null)
                        .show();
                return false;
            }
        }
        return true;
    }

    /**
     * wheather support Phone Storage
     *
     * @return boolean true support Phone Storage ,false will be not
     */
    public static boolean isPhoneStorageSupported() {
        return true;
    }

    /**
     * show Dialog to warn filename is null
     *
     * @param activity
     */
    public static void showFilenameEmptyDialog(Activity activity) {
        new AlertDialog.Builder(activity)
                .setTitle(R.string.filename_empty_title)
                .setIcon(android.R.drawable.ic_dialog_alert)
                .setMessage(R.string.filename_empty_msg)
                .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                    }
                })
                .show();
    }

    /**
     * get the filename except the suffix and dot
     *
     * @return String the filename except suffix and dot
     */
    public static String getFilenameBase(String filename) {
        int dotindex = filename.lastIndexOf('.');
        if (dotindex != -1) {
            return filename.substring(0, dotindex);
        } else {
            return "";
        }
    }

    /**
     * get the filename's extension from filename
     *
     * @param filename the download filename, may be the user entered
     * @return String the filename's extension
     */
    public static String getFilenameExtension(String filename) {
        int dotindex = filename.lastIndexOf('.');
        if (dotindex != -1) {
            return filename.substring(dotindex + 1);
        } else {
            return "";
        }
    }

    public static String getDefaultDownloadPath(Context context, String downloadPath) {
        String defaultDownloadPath;

        String defaultStorage;
        if (isPhoneStorageSupported()) {
            defaultStorage = Environment.getExternalStorageDirectory().getPath();
        } else {
            defaultStorage = getExternalStorageDirectory(context, downloadPath);
        }

        defaultDownloadPath = defaultStorage + DownloadDirRestriction.getInstance().getDownloadDirectory();
        Log.e(LOGTAG, "defaultStorage directory is : " + defaultDownloadPath);
        return defaultDownloadPath;
    }

    /**
     * translate the directory name into a name which is easy to know for user
     *
     * @param activity
     * @param downloadPath
     * @return String
     */
    public static String getDownloadPathForUser(Activity activity, String downloadPath) {
        if (downloadPath == null) {
            return downloadPath;
        }
        final String phoneStorageDir;
        final String sdCardDir = getExternalStorageDirectory(activity, downloadPath);
        if (isPhoneStorageSupported()) {
            phoneStorageDir = Environment.getExternalStorageDirectory().getPath();
        } else {
            phoneStorageDir = null;
        }

        if (sdCardDir != null && downloadPath.startsWith(sdCardDir)) {
            String sdCardLabel = activity.getResources().getString(
                    R.string.download_path_sd_card_label);
            downloadPath = downloadPath.replace(sdCardDir, sdCardLabel);
        } else if ((phoneStorageDir != null) && downloadPath.startsWith(phoneStorageDir)) {
            String phoneStorageLabel = activity.getResources().getString(
                    R.string.download_path_phone_storage_label);
            downloadPath = downloadPath.replace(phoneStorageDir, phoneStorageLabel);
        }
        return downloadPath;
    }

    private static boolean isRemovable(Object obj) {
        return (Boolean) ReflectHelper.invokeMethod(obj,
                "isRemovable", null, null);
    }

    private static boolean allowMassStorage(Object obj) {
        return (Boolean) ReflectHelper.invokeMethod(obj,
                "allowMassStorage", null, null);
    }

    private static String getPath(Object obj) {
        return (String) ReflectHelper.invokeMethod(obj,
                "getPath", null, null);
    }

    private static String getExternalStorageDirectory(Context context, String downloadPath) {
        String sd = null;
        StorageManager mStorageManager = (StorageManager) context
                .getSystemService(Context.STORAGE_SERVICE);
        Object[] volumes = (Object[]) ReflectHelper.invokeMethod(
                                 mStorageManager, "getVolumeList", null, null);
        for (int i = 0; i < volumes.length; i++) {
            if (isRemovable(volumes[i])
                && ((allowMassStorage(volumes[i]) && downloadPath == null)
                    || (downloadPath != null
                        && downloadPath.startsWith(getPath(volumes[i]))))) {
                sd = getPath(volumes[i]);
                break;
            }
        }
        return sd;
    }

    private static String getExternalStorageState(Context context, String downloadPath) {
        StorageManager mStorageManager = (StorageManager) context
                .getSystemService(Context.STORAGE_SERVICE);
        String path = getExternalStorageDirectory(context, downloadPath);
        Object[] params  = {path};
        Class[] type = new Class[] {String.class};
        return (String) ReflectHelper.invokeMethod(mStorageManager,
                "getVolumeState", type, params);
    }
}