blob: d67d6d60099e9057e6ae78b154026bdb08b9d164 [file] [log] [blame]
luxiaol62677b02013-07-22 07:54:49 +08001/*
Axesh R. Ajmera7cfd7a72014-04-01 16:03:42 -07002 * Copyright (c) 2013,2014, The Linux Foundation. All rights reserved.
luxiaol62677b02013-07-22 07:54:49 +08003 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above
10 * copyright notice, this list of conditions and the following
11 * disclaimer in the documentation and/or other materials provided
12 * with the distribution.
13 * * Neither the name of The Linux Foundation nor the names of its
14 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
Bijan Amirzada41242f22014-03-21 12:12:18 -070030package com.android.browser;
luxiaol62677b02013-07-22 07:54:49 +080031
32import java.io.File;
33
34import android.app.Activity;
35import android.content.Intent;
36import java.lang.Thread;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080037
Bijan Amirzada41242f22014-03-21 12:12:18 -070038import com.android.browser.R;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080039
luxiaol62677b02013-07-22 07:54:49 +080040import android.net.Uri;
41import android.os.Bundle;
kaiyizf1a66762013-09-16 16:59:43 +080042import android.os.Environment;
luxiaol62677b02013-07-22 07:54:49 +080043import android.text.format.*;
44import android.util.Log;
45import android.view.View;
46import android.view.View.OnClickListener;
47import android.widget.Button;
48import android.widget.EditText;
49import android.widget.TextView;
50import android.view.Window;
51import android.widget.Toast;
52import android.text.TextUtils;
53
Axesh R. Ajmera7cfd7a72014-04-01 16:03:42 -070054import com.android.browser.reflect.ReflectHelper;
55
luxiaol62677b02013-07-22 07:54:49 +080056public class DownloadSettings extends Activity {
57
58 private EditText downloadFilenameET;
59 private EditText downloadPathET;
60 private TextView downloadEstimateSize;
61 private TextView downloadEstimateTime;
62 private Button downloadStart;
63 private Button downloadCancel;
64 private String url;
65 private String userAgent;
66 private String contentDisposition;
67 private String mimetype;
68 private String referer;
69 private String filenameBase;
70 private String filename;
71 private String filenameExtension;
72 private boolean privateBrowsing;
73 private long contentLength;
74 private String downloadPath;
75 private String downloadPathForUser;
76 private static final int downloadRate = (1024 * 100 * 60);// Download Rate
77 // 100KB/s
78 private final static String LOGTAG = "DownloadSettings";
79 private final static int DOWNLOAD_PATH = 0;
80 private boolean isDownloadStarted = false;
81
82 private static final String ENV_EMULATED_STORAGE_TARGET = "EMULATED_STORAGE_TARGET";
Axesh R. Ajmera7cfd7a72014-04-01 16:03:42 -070083 private static final String APK_TYPE="apk";
84 private static final String OCTET_STREAM = "application/octet-stream";
luxiaol62677b02013-07-22 07:54:49 +080085
86 protected void onCreate(Bundle savedInstanceState) {
87 super.onCreate(savedInstanceState);
88 // initial the DownloadSettings view
89 requestWindowFeature(Window.FEATURE_NO_TITLE);
90 setContentView(R.layout.download_settings);
91 downloadFilenameET = (EditText) findViewById(R.id.download_filename_edit);
92 downloadPathET = (EditText) findViewById(R.id.download_filepath_selected);
93 downloadEstimateSize = (TextView) findViewById(R.id.download_estimate_size_content);
94 downloadEstimateTime = (TextView) findViewById(R.id.download_estimate_time_content);
95 downloadStart = (Button) findViewById(R.id.download_start);
96 downloadCancel = (Button) findViewById(R.id.download_cancle);
97 downloadPathET.setOnClickListener(downloadPathListener);
98 downloadStart.setOnClickListener(downloadStartListener);
99 downloadCancel.setOnClickListener(downloadCancelListener);
100
101 // get the bundle from Intent
102 Intent intent = getIntent();
103 Bundle fileInfo = intent.getExtras();
104 url = fileInfo.getString("url");
105 userAgent = fileInfo.getString("userAgent");
106 contentDisposition = fileInfo.getString("contentDisposition");
107 mimetype = fileInfo.getString("mimetype");
108 referer = fileInfo.getString("referer");
109 contentLength = fileInfo.getLong("contentLength");
110 privateBrowsing = fileInfo.getBoolean("privateBrowsing");
111 filename = fileInfo.getString("filename");
112
113 // download filenamebase's length is depended on filenameLength's values
114 // if filenamebase.length >= flienameLength, destroy the last string!
115
116 filenameBase = DownloadHandler.getFilenameBase(filename);
117 if (filenameBase.length() >= (BrowserUtils.FILENAME_MAX_LENGTH)) {
118 filenameBase = filenameBase.substring(0, BrowserUtils.FILENAME_MAX_LENGTH);
119 }
120
121 // warring when user enter more over letters into the EditText
122 BrowserUtils.maxLengthFilter(DownloadSettings.this, downloadFilenameET,
123 BrowserUtils.FILENAME_MAX_LENGTH);
124
125 downloadFilenameET.setText(filenameBase);
Axesh R. Ajmera7cfd7a72014-04-01 16:03:42 -0700126
127 String filenameExtension = DownloadHandler.getFilenameExtension(filename);
128
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700129 // introspect for octet stream mimetype what type of file extension it has
130 // and reassign mimetype
131 if (mimetype == null || mimetype.isEmpty() || mimetype.equals(OCTET_STREAM)) {
Axesh R. Ajmera7cfd7a72014-04-01 16:03:42 -0700132
133 String updatedFileName = filenameBase + "." + filenameExtension;
134 Object[] params = {updatedFileName};
135 Class[] type = new Class[] {String.class};
Bijan Amirzada58383e72014-04-01 14:45:22 -0700136 mimetype = (String) ReflectHelper.invokeMethod("android.media.MediaFile",
Axesh R. Ajmera7cfd7a72014-04-01 16:03:42 -0700137 "getMimeTypeForFile", type, params);
138 }
139
140 //Add special check for .apk files with octet-stream mimetype
Axesh R. Ajmera77258982014-04-02 14:39:09 -0700141 if (filenameExtension.equals(APK_TYPE) && mimetype != null && mimetype.equals(OCTET_STREAM)) {
Axesh R. Ajmera7cfd7a72014-04-01 16:03:42 -0700142 mimetype = "application/vnd.android.package-archive";
143 }
144
kaiyizf1a66762013-09-16 16:59:43 +0800145 downloadPath = chooseFolderFromMimeType(BrowserSettings.getInstance().getDownloadPath(),
146 mimetype);
luxiaol62677b02013-07-22 07:54:49 +0800147 downloadPathForUser = DownloadHandler.getDownloadPathForUser(DownloadSettings.this,
148 downloadPath);
149 setDownloadPathForUserText(downloadPathForUser);
150 setDownloadFileSizeText();
151 setDownloadFileTimeText();
luxiaol62677b02013-07-22 07:54:49 +0800152 }
153
154 private OnClickListener downloadPathListener = new OnClickListener() {
155
156 @Override
157 public void onClick(View v) {
158
159 // start filemanager for getting download path
160 try {
161 Intent downloadPathIntent = new Intent("com.android.fileexplorer.action.DIR_SEL");
162 DownloadSettings.this.startActivityForResult(downloadPathIntent, DOWNLOAD_PATH);
163 } catch (Exception e) {
164 String err_msg = getString(R.string.activity_not_found,
165 "com.android.fileexplorer.action.DIR_SEL");
166 Toast.makeText(DownloadSettings.this, err_msg, Toast.LENGTH_LONG).show();
167 }
168
169 }
170 };
171
172 private OnClickListener downloadStartListener = new OnClickListener() {
173
174 @Override
175 public void onClick(View v) {
176 filenameBase = getFilenameBaseFromUserEnter();
177 // check the filename user enter is null or not
178 if (filenameBase.length() <= 0) {
179 DownloadHandler.showFilenameEmptyDialog(DownloadSettings.this);
180 return;
181 }
182
183 filenameExtension = DownloadHandler.getFilenameExtension(filename);
184 filename = filenameBase + "." + filenameExtension;
185
186 // check the storage status
187 if (!DownloadHandler.isStorageStatusOK(DownloadSettings.this, filename, downloadPath)) {
188 return;
189 }
190
191 // check the storage memory enough or not
192 try {
193 DownloadHandler.setAppointedFolder(downloadPath);
194 } catch (Exception e) {
195 DownloadHandler.showNoEnoughMemoryDialog(DownloadSettings.this);
196 return;
197 }
kaiyize6a27d02013-08-22 15:08:19 +0800198 boolean isNoEnoughMemory = DownloadHandler.manageNoEnoughMemory(contentLength,
199 downloadPath);
luxiaol62677b02013-07-22 07:54:49 +0800200 if (isNoEnoughMemory) {
kaiyize6a27d02013-08-22 15:08:19 +0800201 DownloadHandler.showNoEnoughMemoryDialog(DownloadSettings.this);
luxiaol62677b02013-07-22 07:54:49 +0800202 return;
203 }
204
205 // check the download file is exist or not
206 String fullFilename = downloadPath + "/" + filename;
207 if (mimetype != null && new File(fullFilename).exists()) {
208 DownloadHandler.fileExistQueryDialog(DownloadSettings.this);
209 return;
210 }
211
212 // staring downloading
213 DownloadHandler.startingDownload(DownloadSettings.this,
214 url, userAgent, contentDisposition,
215 mimetype, referer, privateBrowsing, contentLength,
216 Uri.encode(filename), downloadPath);
217 isDownloadStarted = true;
218 }
219 };
220
221 private OnClickListener downloadCancelListener = new OnClickListener() {
luxiaol62677b02013-07-22 07:54:49 +0800222 @Override
223 public void onClick(View v) {
224 finish();
225 }
226 };
227
228 protected void onDestroy() {
229 super.onDestroy();
230 }
231
232 protected void onPause() {
233 super.onPause();
234 if (isDownloadStarted) {
235 finish();
236 }
237 }
238
239 protected void onResume() {
240 super.onResume();
241 }
242
243 @Override
244 protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
luxiaol62677b02013-07-22 07:54:49 +0800245 if (DOWNLOAD_PATH == requestCode) {
Axesh R. Ajmerab7fca882014-12-26 14:36:35 -0800246 if (resultCode != Activity.RESULT_CANCELED && intent != null) {
luxiaol62677b02013-07-22 07:54:49 +0800247 downloadPath = intent.getStringExtra("result_dir_sel");
Vivek Sekhar76074ac2014-06-27 16:33:13 -0700248 // Fallback logic to stock browser
249 if (downloadPath == null) {
250 Uri uri = intent.getData();
251 if(uri != null)
252 downloadPath = uri.getPath();
253 }
luxiaol62677b02013-07-22 07:54:49 +0800254 if (downloadPath != null) {
255 String rawEmulatedStorageTarget = System.getenv(ENV_EMULATED_STORAGE_TARGET);
256 if (!TextUtils.isEmpty(rawEmulatedStorageTarget)) {
257 if (downloadPath.startsWith("/storage/sdcard0"))
258 downloadPath = downloadPath.replace("/storage/sdcard0",
259 "/storage/emulated/0");
260 if (downloadPath.startsWith("/storage/emulated/legacy"))
261 downloadPath = downloadPath.replace("/storage/emulated/legacy",
262 "/storage/emulated/0");
263 }
264 downloadPathForUser = DownloadHandler.getDownloadPathForUser(
265 DownloadSettings.this, downloadPath);
266 setDownloadPathForUserText(downloadPathForUser);
267 }
268 }
269 }
270 }
271
kaiyizf1a66762013-09-16 16:59:43 +0800272 // Add for carrier feature - download to related folders by mimetype.
273 private static String chooseFolderFromMimeType(String path, String mimeType) {
274 String destinationFolder = null;
275 if (!path.contains(Environment.DIRECTORY_DOWNLOADS) || null == mimeType)
276 return path;
277 if (mimeType.startsWith("audio"))
278 destinationFolder = Environment.DIRECTORY_MUSIC;
279 else if (mimeType.startsWith("video"))
280 destinationFolder = Environment.DIRECTORY_MOVIES;
281 else if (mimeType.startsWith("image"))
282 destinationFolder = Environment.DIRECTORY_PICTURES;
283 if (null != destinationFolder)
284 path = path.replace(Environment.DIRECTORY_DOWNLOADS, destinationFolder);
285 return path;
286 }
287
luxiaol62677b02013-07-22 07:54:49 +0800288 /**
289 * show download path for user
290 *
291 * @param downloadPath the download path user can see
292 */
293 private void setDownloadPathForUserText(String downloadPathForUser) {
294 downloadPathET.setText(downloadPathForUser);
295 }
296
297 /**
298 * get the filename from user select the download path
299 *
300 * @return String the filename from user selected
301 */
302 private String getFilenameBaseFromUserEnter() {
303 return downloadFilenameET.getText().toString();
304 }
305
306 /**
307 * set the download file size for user to be known
308 */
309 private void setDownloadFileSizeText() {
310 String sizeText;
311 if (contentLength <= 0) {
312 sizeText = getString(R.string.unknow_length);
313 } else {
314 sizeText = getDownloadFileSize();
315 }
316 downloadEstimateSize.setText(sizeText);
317
318 }
319
320 /**
321 * set the time which downloaded this file will be estimately use;
322 */
323 private void setDownloadFileTimeText() {
324 String neededTimeText;
325 if (contentLength <= 0) {
326 neededTimeText = getString(R.string.unknow_length);
327 } else {
328 neededTimeText = getNeededTime() + getString(R.string.time_min);
329 }
330 downloadEstimateTime.setText(neededTimeText);
331 }
332
333 /**
334 * count the download file's size and format the values
335 *
336 * @return String the format values
337 */
338 private String getDownloadFileSize() {
339 String currentSizeText = "";
340 if (contentLength > 0) {
341 currentSizeText = Formatter.formatFileSize(DownloadSettings.this, contentLength);
342 }
343 return currentSizeText;
344 }
345
346 /**
347 * get the time download this file will be use,and format this time values
348 *
349 * @return long the valses of time which download this file will be use
350 */
351 private long getNeededTime() {
352 long timeNeeded = contentLength / downloadRate;
353 if (timeNeeded < 1) {
354 timeNeeded = 1;
355 }
356 Log.e(LOGTAG, "TimeNeeded:" + timeNeeded + "min");
357 // return the time like 5 min, not 5 s;
358 return timeNeeded;
359 }
360}