Alter download flow to request permissions before anything else.

Change-Id: I6ccbdbcc39a631035b4f8a8d6edae0f5a4dc8e36
Ticket-Id: CYNGNOS-2266
Ticket-Id: FEIJ-368
diff --git a/src/com/android/browser/DownloadSettings.java b/src/com/android/browser/DownloadSettings.java
index aa528d2..ee60210 100644
--- a/src/com/android/browser/DownloadSettings.java
+++ b/src/com/android/browser/DownloadSettings.java
@@ -212,62 +212,70 @@
     };
 
     private OnClickListener downloadStartListener = new OnClickListener() {
-
         @Override
         public void onClick(View v) {
-            filenameBase = getFilenameBaseFromUserEnter();
-            // check the filename user enter is null or not
-            if (TextUtils.isEmpty(filenameBase) || TextUtils.isEmpty(downloadPath)) {
-                DownloadHandler.showFilenameEmptyDialog(DownloadSettings.this);
-                return;
-            }
-
-            filenameExtension = DownloadHandler.getFilenameExtension(filename);
-            filename = filenameBase + "." + filenameExtension;
-
-            // check the storage status
-            if (!DownloadHandler.isStorageStatusOK(DownloadSettings.this, filename, downloadPath)) {
-                return;
-            }
-
-            // check the storage memory enough or not
-            try {
-                DownloadHandler.setAppointedFolder(downloadPath);
-            } catch (Exception e) {
-                DownloadHandler.showNoEnoughMemoryDialog(DownloadSettings.this);
-                return;
-            }
-            boolean isNoEnoughMemory = DownloadHandler.manageNoEnoughMemory(contentLength,
-                    downloadPath);
-            if (isNoEnoughMemory) {
-                DownloadHandler.showNoEnoughMemoryDialog(DownloadSettings.this);
-                return;
-            }
-
-            // check the download file is exist or not
-            String fullFilename = downloadPath + "/" + filename;
-            if (mimetype != null && new File(fullFilename).exists()) {
-                DownloadHandler.fileExistQueryDialog(DownloadSettings.this);
-                return;
-            }
-
-            // check for permission
-            if (!hasPermission(permission.WRITE_EXTERNAL_STORAGE)) {
-                requestPermissions(new String[] {permission.WRITE_EXTERNAL_STORAGE},
-                    ++nextRequestCode);
-            } else {
-                download();
-            }
+            onHandleDownload();
         }
     };
 
+    private void onHandleDownload() {
+
+        filenameBase = getFilenameBaseFromUserEnter();
+
+        // check the filename user enter is null or not
+        if (TextUtils.isEmpty(filenameBase) || TextUtils.isEmpty(downloadPath)) {
+            DownloadHandler.showFilenameEmptyDialog(DownloadSettings.this);
+            return;
+        }
+
+        // check for permission
+        if (!hasPermission(permission.WRITE_EXTERNAL_STORAGE)) {
+            requestPermissions(new String[] {permission.WRITE_EXTERNAL_STORAGE},
+                ++nextRequestCode);
+            return;
+        }
+
+        filenameExtension = DownloadHandler.getFilenameExtension(filename);
+        filename = filenameBase + "." + filenameExtension;
+
+        // check the storage status
+        if (!DownloadHandler.isStorageStatusOK(DownloadSettings.this, filename, downloadPath)) {
+            return;
+        }
+
+        // check the storage memory enough or not
+        try {
+            DownloadHandler.setAppointedFolder(downloadPath);
+        } catch (Exception e) {
+            DownloadHandler.showNoEnoughMemoryDialog(DownloadSettings.this);
+            return;
+        }
+        boolean isNoEnoughMemory = DownloadHandler.manageNoEnoughMemory(contentLength,
+                downloadPath);
+        if (isNoEnoughMemory) {
+            DownloadHandler.showNoEnoughMemoryDialog(DownloadSettings.this);
+            return;
+        }
+
+        // check the download file is exist or not
+        String fullFilename = downloadPath + "/" + filename;
+        if (mimetype != null && new File(fullFilename).exists()) {
+            DownloadHandler.fileExistQueryDialog(DownloadSettings.this);
+            return;
+        }
+
+        download();
+
+    }
+
+
     @Override
     public void onRequestPermissionsResult(int requestCode,
         String[] permissions, int[] grantResults) {
         if (nextRequestCode == requestCode) {
             if (grantResults.length > 0
                 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
-                download();
+                onHandleDownload();
             } else {
                 finish();
             }