diff options
author | LuK1337 <priv.luk@gmail.com> | 2015-12-17 21:32:46 +0800 |
---|---|---|
committer | Umair Khan <omerjerk@gmail.com> | 2016-01-04 14:25:36 +0530 |
commit | 852bcd16b075d74f3c24373d8321f9b193200c2e (patch) | |
tree | 6f0a4c80f38caf2d22003d0757c24ca29969a0b6 /src/com/android/browser/DownloadHandler.java | |
parent | ad614da6fd8a6c85ac98bd27d92e2801098e2bf6 (diff) | |
download | packages_apps_Browser-852bcd16b075d74f3c24373d8321f9b193200c2e.zip packages_apps_Browser-852bcd16b075d74f3c24373d8321f9b193200c2e.tar.gz packages_apps_Browser-852bcd16b075d74f3c24373d8321f9b193200c2e.tar.bz2 |
Browser: runtime permissions
Change-Id: I19caa970421898196c005aa9ad7cc651cd574c0a
Signed-off-by: jrizzoli <joey@cyanogenmoditalia.it>
Diffstat (limited to 'src/com/android/browser/DownloadHandler.java')
-rwxr-xr-x | src/com/android/browser/DownloadHandler.java | 64 |
1 files changed, 63 insertions, 1 deletions
diff --git a/src/com/android/browser/DownloadHandler.java b/src/com/android/browser/DownloadHandler.java index 5fc5b79..37ab027 100755 --- a/src/com/android/browser/DownloadHandler.java +++ b/src/com/android/browser/DownloadHandler.java @@ -28,6 +28,8 @@ import android.content.pm.ResolveInfo; import android.net.Uri; import android.net.WebAddress; import android.os.Environment; +import android.support.v4.app.ActivityCompat; +import android.support.v4.content.ContextCompat; import android.text.TextUtils; import android.util.Log; import android.webkit.CookieManager; @@ -44,6 +46,46 @@ public class DownloadHandler { private static final String LOGTAG = "DLHandler"; + //Singleton to hold the information about any pending downloads + private static PendingDownloadBundle pendingDownloadBundle; + + private static class PendingDownloadBundle { + String url; + String userAgent; + String contentDisposition; + String mimetype; + String referer; + boolean privateBrowsing; + + public static PendingDownloadBundle create(String url, String userAgent, + String contentDisposition, String mimetype, String referer, + boolean privateBrowsing) { + PendingDownloadBundle pdb = new PendingDownloadBundle(); + pdb.url = url; + pdb.userAgent = userAgent; + pdb.contentDisposition = contentDisposition; + pdb.mimetype = mimetype; + pdb.referer = referer; + pdb.privateBrowsing = privateBrowsing; + return pdb; + } + } + + /** + * Check if there is any pending download and start the download automatically in case + * there is one. + * @param activity Activity requesting the download. + */ + public static void checkPendingDownloads(Activity activity) { + if (pendingDownloadBundle != null) { + onDownloadStartNoStream(activity, pendingDownloadBundle.url, + pendingDownloadBundle.userAgent, pendingDownloadBundle.contentDisposition, + pendingDownloadBundle.mimetype, pendingDownloadBundle.referer, + pendingDownloadBundle.privateBrowsing); + pendingDownloadBundle = null; + } + } + /** * Notify the host application a download should be done, or that * the data should be streamed if a streaming viewer is available. @@ -130,7 +172,7 @@ public class DownloadHandler { /** * Notify the host application a download should be done, even if there - * is a streaming viewer available for thise type. + * is a streaming viewer available for this 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. @@ -143,6 +185,26 @@ public class DownloadHandler { String url, String userAgent, String contentDisposition, String mimetype, String referer, boolean privateBrowsing) { + // Check permissions first when download will be start. + int permissionCheck = ContextCompat.checkSelfPermission(activity, + android.Manifest.permission.WRITE_EXTERNAL_STORAGE); + if (permissionCheck == PackageManager.PERMISSION_GRANTED) { + onDownloadNoStreamImpl(activity, url, userAgent, contentDisposition, + mimetype, referer, privateBrowsing); + } else { + pendingDownloadBundle = PendingDownloadBundle.create(url, userAgent, + contentDisposition, mimetype, referer, privateBrowsing); + // Permission not granted, request it from the user + ActivityCompat.requestPermissions(activity, + new String[] {android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, + BrowserActivity.PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE); + } + } + + private static void onDownloadNoStreamImpl(Activity activity, + String url, String userAgent, String contentDisposition, + String mimetype, String referer, boolean privateBrowsing) { + String filename = URLUtil.guessFileName(url, contentDisposition, mimetype); |