blob: 4c7c829bd9fce314b6cfad7734b30609267abab7 [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) {
245
246 if (DOWNLOAD_PATH == requestCode) {
247 if (resultCode == Activity.RESULT_OK && intent != null) {
248 downloadPath = intent.getStringExtra("result_dir_sel");
249 if (downloadPath != null) {
250 String rawEmulatedStorageTarget = System.getenv(ENV_EMULATED_STORAGE_TARGET);
251 if (!TextUtils.isEmpty(rawEmulatedStorageTarget)) {
252 if (downloadPath.startsWith("/storage/sdcard0"))
253 downloadPath = downloadPath.replace("/storage/sdcard0",
254 "/storage/emulated/0");
255 if (downloadPath.startsWith("/storage/emulated/legacy"))
256 downloadPath = downloadPath.replace("/storage/emulated/legacy",
257 "/storage/emulated/0");
258 }
259 downloadPathForUser = DownloadHandler.getDownloadPathForUser(
260 DownloadSettings.this, downloadPath);
261 setDownloadPathForUserText(downloadPathForUser);
262 }
263 }
264 }
265 }
266
kaiyizf1a66762013-09-16 16:59:43 +0800267 // Add for carrier feature - download to related folders by mimetype.
268 private static String chooseFolderFromMimeType(String path, String mimeType) {
269 String destinationFolder = null;
270 if (!path.contains(Environment.DIRECTORY_DOWNLOADS) || null == mimeType)
271 return path;
272 if (mimeType.startsWith("audio"))
273 destinationFolder = Environment.DIRECTORY_MUSIC;
274 else if (mimeType.startsWith("video"))
275 destinationFolder = Environment.DIRECTORY_MOVIES;
276 else if (mimeType.startsWith("image"))
277 destinationFolder = Environment.DIRECTORY_PICTURES;
278 if (null != destinationFolder)
279 path = path.replace(Environment.DIRECTORY_DOWNLOADS, destinationFolder);
280 return path;
281 }
282
luxiaol62677b02013-07-22 07:54:49 +0800283 /**
284 * show download path for user
285 *
286 * @param downloadPath the download path user can see
287 */
288 private void setDownloadPathForUserText(String downloadPathForUser) {
289 downloadPathET.setText(downloadPathForUser);
290 }
291
292 /**
293 * get the filename from user select the download path
294 *
295 * @return String the filename from user selected
296 */
297 private String getFilenameBaseFromUserEnter() {
298 return downloadFilenameET.getText().toString();
299 }
300
301 /**
302 * set the download file size for user to be known
303 */
304 private void setDownloadFileSizeText() {
305 String sizeText;
306 if (contentLength <= 0) {
307 sizeText = getString(R.string.unknow_length);
308 } else {
309 sizeText = getDownloadFileSize();
310 }
311 downloadEstimateSize.setText(sizeText);
312
313 }
314
315 /**
316 * set the time which downloaded this file will be estimately use;
317 */
318 private void setDownloadFileTimeText() {
319 String neededTimeText;
320 if (contentLength <= 0) {
321 neededTimeText = getString(R.string.unknow_length);
322 } else {
323 neededTimeText = getNeededTime() + getString(R.string.time_min);
324 }
325 downloadEstimateTime.setText(neededTimeText);
326 }
327
328 /**
329 * count the download file's size and format the values
330 *
331 * @return String the format values
332 */
333 private String getDownloadFileSize() {
334 String currentSizeText = "";
335 if (contentLength > 0) {
336 currentSizeText = Formatter.formatFileSize(DownloadSettings.this, contentLength);
337 }
338 return currentSizeText;
339 }
340
341 /**
342 * get the time download this file will be use,and format this time values
343 *
344 * @return long the valses of time which download this file will be use
345 */
346 private long getNeededTime() {
347 long timeNeeded = contentLength / downloadRate;
348 if (timeNeeded < 1) {
349 timeNeeded = 1;
350 }
351 Log.e(LOGTAG, "TimeNeeded:" + timeNeeded + "min");
352 // return the time like 5 min, not 5 s;
353 return timeNeeded;
354 }
355}