|
4 | 4 | import android.content.Context; |
5 | 5 | import android.net.Uri; |
6 | 6 | import android.os.Environment; |
| 7 | + |
7 | 8 | import androidx.annotation.NonNull; |
8 | 9 | import androidx.annotation.Nullable; |
| 10 | + |
9 | 11 | import android.text.TextUtils; |
10 | 12 | import android.widget.Toast; |
11 | 13 |
|
| 14 | +import com.crashlytics.android.Crashlytics; |
12 | 15 | import com.fastaccess.App; |
13 | 16 | import com.fastaccess.BuildConfig; |
14 | 17 | import com.fastaccess.R; |
@@ -91,31 +94,36 @@ private static Retrofit provideRetrofit(boolean enterprise) { |
91 | 94 | } |
92 | 95 |
|
93 | 96 | public static void downloadFile(@NonNull Context context, @NonNull String url) { |
94 | | - if (InputHelper.isEmpty(url)) return; |
95 | | - boolean isEnterprise = LinkParserHelper.isEnterprise(url); |
96 | | - Uri uri = Uri.parse(url); |
97 | | - DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE); |
98 | | - DownloadManager.Request request = new DownloadManager.Request(uri); |
99 | | - String authToken = isEnterprise ? PrefGetter.getEnterpriseToken() : PrefGetter.getToken(); |
100 | | - if (!TextUtils.isEmpty(authToken)) { |
101 | | - request.addRequestHeader("Authorization", authToken.startsWith("Basic") ? authToken : "token " + authToken); |
102 | | - } |
103 | | - File direct = new File(Environment.getExternalStorageDirectory() + File.separator + context.getString(R.string.app_name)); |
104 | | - if (!direct.isDirectory() || !direct.exists()) { |
105 | | - boolean isCreated = direct.mkdirs(); |
106 | | - if (!isCreated) { |
107 | | - Toast.makeText(App.getInstance(), "Unable to create directory to download file", Toast.LENGTH_SHORT).show(); |
108 | | - return; |
| 97 | + try { |
| 98 | + if (InputHelper.isEmpty(url)) return; |
| 99 | + boolean isEnterprise = LinkParserHelper.isEnterprise(url); |
| 100 | + Uri uri = Uri.parse(url); |
| 101 | + DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE); |
| 102 | + DownloadManager.Request request = new DownloadManager.Request(uri); |
| 103 | + String authToken = isEnterprise ? PrefGetter.getEnterpriseToken() : PrefGetter.getToken(); |
| 104 | + if (!TextUtils.isEmpty(authToken)) { |
| 105 | + request.addRequestHeader("Authorization", authToken.startsWith("Basic") ? authToken : "token " + authToken); |
109 | 106 | } |
110 | | - } |
111 | | - String fileName = new File(url).getName(); |
112 | | - request.setDestinationInExternalPublicDir(context.getString(R.string.app_name), fileName); |
113 | | - request.setTitle(fileName); |
114 | | - request.setDescription(context.getString(R.string.downloading_file)); |
115 | | - request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE | DownloadManager.Request.NETWORK_WIFI); |
116 | | - request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); |
117 | | - if (downloadManager != null) { |
118 | | - downloadManager.enqueue(request); |
| 107 | + File direct = |
| 108 | + new File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS) + File.separator + context.getString(R.string.app_name)); |
| 109 | + if (!direct.isDirectory() || !direct.exists()) { |
| 110 | + boolean isCreated = direct.mkdirs(); |
| 111 | + if (!isCreated) { |
| 112 | + Toast.makeText(App.getInstance(), "Unable to create directory to download file", Toast.LENGTH_SHORT).show(); |
| 113 | + return; |
| 114 | + } |
| 115 | + } |
| 116 | + String fileName = new File(url).getName(); |
| 117 | + request.setDestinationInExternalPublicDir(context.getString(R.string.app_name), fileName); |
| 118 | + request.setTitle(fileName); |
| 119 | + request.setDescription(context.getString(R.string.downloading_file)); |
| 120 | + request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE | DownloadManager.Request.NETWORK_WIFI); |
| 121 | + request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); |
| 122 | + if (downloadManager != null) { |
| 123 | + downloadManager.enqueue(request); |
| 124 | + } |
| 125 | + } catch (Exception e) { |
| 126 | + Crashlytics.logException(e); |
119 | 127 | } |
120 | 128 | } |
121 | 129 |
|
|
0 commit comments